2 * Copyright (c) 2000-2015 Apple 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@
28 /* @(#)hfs_vfsutils.c 4.0
30 * (c) 1997-2002 Apple Inc. All Rights Reserved
32 * hfs_vfsutils.c -- Routines that go between the HFS layer and the VFS.
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/malloc.h>
40 #include <sys/mount.h>
44 #include <sys/unistd.h>
45 #include <sys/utfconv.h>
46 #include <sys/kauth.h>
47 #include <sys/fcntl.h>
48 #include <sys/fsctl.h>
49 #include <sys/mount.h>
50 #include <sys/sysctl.h>
51 #include <kern/clock.h>
53 #include <miscfs/specfs/specdev.h>
54 #include <libkern/OSAtomic.h>
55 #include <IOKit/IOLib.h>
57 /* for parsing boot-args */
58 #include <pexpert/pexpert.h>
59 #include <kern/kalloc.h>
61 #include "hfs_iokit.h"
63 #include "hfs_catalog.h"
65 #include "hfs_mount.h"
66 #include "hfs_endian.h"
67 #include "hfs_cnode.h"
68 #include "hfs_fsctl.h"
69 #include "hfs_cprotect.h"
71 #include "FileMgrInternal.h"
72 #include "BTreesInternal.h"
73 #include "HFSUnicodeWrappers.h"
75 /* Enable/disable debugging code for live volume resizing, defined in hfs_resize.c */
76 extern int hfs_resize_debug
;
78 static void ReleaseMetaFileVNode(struct vnode
*vp
);
79 static int hfs_late_journal_init(struct hfsmount
*hfsmp
, HFSPlusVolumeHeader
*vhp
, void *_args
);
81 static u_int32_t
hfs_hotfile_freeblocks(struct hfsmount
*);
82 static void hfs_thaw_locked(struct hfsmount
*hfsmp
);
84 #define HFS_MOUNT_DEBUG 1
87 //*******************************************************************************
88 // Note: Finder information in the HFS/HFS+ metadata are considered opaque and
89 // hence are not in the right byte order on little endian machines. It is
90 // the responsibility of the finder and other clients to swap the data.
91 //*******************************************************************************
93 //*******************************************************************************
94 // Routine: hfs_MountHFSVolume
97 //*******************************************************************************
98 unsigned char hfs_catname
[] = "Catalog B-tree";
99 unsigned char hfs_extname
[] = "Extents B-tree";
100 unsigned char hfs_vbmname
[] = "Volume Bitmap";
101 unsigned char hfs_attrname
[] = "Attribute B-tree";
102 unsigned char hfs_startupname
[] = "Startup File";
105 OSErr
hfs_MountHFSVolume(struct hfsmount
*hfsmp
, HFSMasterDirectoryBlock
*mdb
,
106 __unused
struct proc
*p
)
108 ExtendedVCB
*vcb
= HFSTOVCB(hfsmp
);
111 struct cat_desc cndesc
;
112 struct cat_attr cnattr
;
113 struct cat_fork fork
;
114 int newvnode_flags
= 0;
116 /* Block size must be a multiple of 512 */
117 if (SWAP_BE32(mdb
->drAlBlkSiz
) == 0 ||
118 (SWAP_BE32(mdb
->drAlBlkSiz
) & 0x01FF) != 0)
121 /* don't mount a writeable volume if its dirty, it must be cleaned by fsck_hfs */
122 if (((hfsmp
->hfs_flags
& HFS_READ_ONLY
) == 0) &&
123 ((SWAP_BE16(mdb
->drAtrb
) & kHFSVolumeUnmountedMask
) == 0)) {
126 hfsmp
->hfs_flags
|= HFS_STANDARD
;
128 * The MDB seems OK: transfer info from it into VCB
129 * Note - the VCB starts out clear (all zeros)
132 vcb
->vcbSigWord
= SWAP_BE16 (mdb
->drSigWord
);
133 vcb
->hfs_itime
= to_bsd_time(LocalToUTC(SWAP_BE32(mdb
->drCrDate
)));
134 vcb
->localCreateDate
= SWAP_BE32 (mdb
->drCrDate
);
135 vcb
->vcbLsMod
= to_bsd_time(LocalToUTC(SWAP_BE32(mdb
->drLsMod
)));
136 vcb
->vcbAtrb
= SWAP_BE16 (mdb
->drAtrb
);
137 vcb
->vcbNmFls
= SWAP_BE16 (mdb
->drNmFls
);
138 vcb
->vcbVBMSt
= SWAP_BE16 (mdb
->drVBMSt
);
139 vcb
->nextAllocation
= SWAP_BE16 (mdb
->drAllocPtr
);
140 vcb
->totalBlocks
= SWAP_BE16 (mdb
->drNmAlBlks
);
141 vcb
->allocLimit
= vcb
->totalBlocks
;
142 vcb
->blockSize
= SWAP_BE32 (mdb
->drAlBlkSiz
);
143 vcb
->vcbClpSiz
= SWAP_BE32 (mdb
->drClpSiz
);
144 vcb
->vcbAlBlSt
= SWAP_BE16 (mdb
->drAlBlSt
);
145 vcb
->vcbNxtCNID
= SWAP_BE32 (mdb
->drNxtCNID
);
146 vcb
->freeBlocks
= SWAP_BE16 (mdb
->drFreeBks
);
147 vcb
->vcbVolBkUp
= to_bsd_time(LocalToUTC(SWAP_BE32(mdb
->drVolBkUp
)));
148 vcb
->vcbWrCnt
= SWAP_BE32 (mdb
->drWrCnt
);
149 vcb
->vcbNmRtDirs
= SWAP_BE16 (mdb
->drNmRtDirs
);
150 vcb
->vcbFilCnt
= SWAP_BE32 (mdb
->drFilCnt
);
151 vcb
->vcbDirCnt
= SWAP_BE32 (mdb
->drDirCnt
);
152 bcopy(mdb
->drFndrInfo
, vcb
->vcbFndrInfo
, sizeof(vcb
->vcbFndrInfo
));
153 if ((hfsmp
->hfs_flags
& HFS_READ_ONLY
) == 0)
154 vcb
->vcbWrCnt
++; /* Compensate for write of MDB on last flush */
156 /* convert hfs encoded name into UTF-8 string */
157 error
= hfs_to_utf8(vcb
, mdb
->drVN
, NAME_MAX
, &utf8chars
, vcb
->vcbVN
);
159 * When an HFS name cannot be encoded with the current
160 * volume encoding we use MacRoman as a fallback.
162 if (error
|| (utf8chars
== 0)) {
163 error
= mac_roman_to_utf8(mdb
->drVN
, NAME_MAX
, &utf8chars
, vcb
->vcbVN
);
164 /* If we fail to encode to UTF8 from Mac Roman, the name is bad. Deny the mount */
170 hfsmp
->hfs_logBlockSize
= BestBlockSizeFit(vcb
->blockSize
, MAXBSIZE
, hfsmp
->hfs_logical_block_size
);
171 vcb
->vcbVBMIOSize
= kHFSBlockSize
;
173 /* Generate the partition-based AVH location */
174 hfsmp
->hfs_partition_avh_sector
= HFS_ALT_SECTOR(hfsmp
->hfs_logical_block_size
,
175 hfsmp
->hfs_logical_block_count
);
177 /* HFS standard is read-only, so just stuff the FS location in here, too */
178 hfsmp
->hfs_fs_avh_sector
= hfsmp
->hfs_partition_avh_sector
;
180 bzero(&cndesc
, sizeof(cndesc
));
181 cndesc
.cd_parentcnid
= kHFSRootParentID
;
182 cndesc
.cd_flags
|= CD_ISMETA
;
183 bzero(&cnattr
, sizeof(cnattr
));
184 cnattr
.ca_linkcount
= 1;
185 cnattr
.ca_mode
= S_IFREG
;
186 bzero(&fork
, sizeof(fork
));
189 * Set up Extents B-tree vnode
191 cndesc
.cd_nameptr
= hfs_extname
;
192 cndesc
.cd_namelen
= strlen((char *)hfs_extname
);
193 cndesc
.cd_cnid
= cnattr
.ca_fileid
= kHFSExtentsFileID
;
194 fork
.cf_size
= SWAP_BE32(mdb
->drXTFlSize
);
195 fork
.cf_blocks
= fork
.cf_size
/ vcb
->blockSize
;
196 fork
.cf_clump
= SWAP_BE32(mdb
->drXTClpSiz
);
198 fork
.cf_extents
[0].startBlock
= SWAP_BE16(mdb
->drXTExtRec
[0].startBlock
);
199 fork
.cf_extents
[0].blockCount
= SWAP_BE16(mdb
->drXTExtRec
[0].blockCount
);
200 fork
.cf_extents
[1].startBlock
= SWAP_BE16(mdb
->drXTExtRec
[1].startBlock
);
201 fork
.cf_extents
[1].blockCount
= SWAP_BE16(mdb
->drXTExtRec
[1].blockCount
);
202 fork
.cf_extents
[2].startBlock
= SWAP_BE16(mdb
->drXTExtRec
[2].startBlock
);
203 fork
.cf_extents
[2].blockCount
= SWAP_BE16(mdb
->drXTExtRec
[2].blockCount
);
204 cnattr
.ca_blocks
= fork
.cf_blocks
;
206 error
= hfs_getnewvnode(hfsmp
, NULL
, NULL
, &cndesc
, 0, &cnattr
, &fork
,
207 &hfsmp
->hfs_extents_vp
, &newvnode_flags
);
209 if (HFS_MOUNT_DEBUG
) {
210 printf("hfs_mounthfs (std): error creating Ext Vnode (%d) \n", error
);
214 error
= MacToVFSError(BTOpenPath(VTOF(hfsmp
->hfs_extents_vp
),
215 (KeyCompareProcPtr
)CompareExtentKeys
));
217 if (HFS_MOUNT_DEBUG
) {
218 printf("hfs_mounthfs (std): error opening Ext Vnode (%d) \n", error
);
220 hfs_unlock(VTOC(hfsmp
->hfs_extents_vp
));
223 hfsmp
->hfs_extents_cp
= VTOC(hfsmp
->hfs_extents_vp
);
226 * Set up Catalog B-tree vnode...
228 cndesc
.cd_nameptr
= hfs_catname
;
229 cndesc
.cd_namelen
= strlen((char *)hfs_catname
);
230 cndesc
.cd_cnid
= cnattr
.ca_fileid
= kHFSCatalogFileID
;
231 fork
.cf_size
= SWAP_BE32(mdb
->drCTFlSize
);
232 fork
.cf_blocks
= fork
.cf_size
/ vcb
->blockSize
;
233 fork
.cf_clump
= SWAP_BE32(mdb
->drCTClpSiz
);
235 fork
.cf_extents
[0].startBlock
= SWAP_BE16(mdb
->drCTExtRec
[0].startBlock
);
236 fork
.cf_extents
[0].blockCount
= SWAP_BE16(mdb
->drCTExtRec
[0].blockCount
);
237 fork
.cf_extents
[1].startBlock
= SWAP_BE16(mdb
->drCTExtRec
[1].startBlock
);
238 fork
.cf_extents
[1].blockCount
= SWAP_BE16(mdb
->drCTExtRec
[1].blockCount
);
239 fork
.cf_extents
[2].startBlock
= SWAP_BE16(mdb
->drCTExtRec
[2].startBlock
);
240 fork
.cf_extents
[2].blockCount
= SWAP_BE16(mdb
->drCTExtRec
[2].blockCount
);
241 cnattr
.ca_blocks
= fork
.cf_blocks
;
243 error
= hfs_getnewvnode(hfsmp
, NULL
, NULL
, &cndesc
, 0, &cnattr
, &fork
,
244 &hfsmp
->hfs_catalog_vp
, &newvnode_flags
);
246 if (HFS_MOUNT_DEBUG
) {
247 printf("hfs_mounthfs (std): error creating catalog Vnode (%d) \n", error
);
249 hfs_unlock(VTOC(hfsmp
->hfs_extents_vp
));
252 error
= MacToVFSError(BTOpenPath(VTOF(hfsmp
->hfs_catalog_vp
),
253 (KeyCompareProcPtr
)CompareCatalogKeys
));
255 if (HFS_MOUNT_DEBUG
) {
256 printf("hfs_mounthfs (std): error opening catalog Vnode (%d) \n", error
);
258 hfs_unlock(VTOC(hfsmp
->hfs_catalog_vp
));
259 hfs_unlock(VTOC(hfsmp
->hfs_extents_vp
));
262 hfsmp
->hfs_catalog_cp
= VTOC(hfsmp
->hfs_catalog_vp
);
265 * Set up dummy Allocation file vnode (used only for locking bitmap)
267 cndesc
.cd_nameptr
= hfs_vbmname
;
268 cndesc
.cd_namelen
= strlen((char *)hfs_vbmname
);
269 cndesc
.cd_cnid
= cnattr
.ca_fileid
= kHFSAllocationFileID
;
270 bzero(&fork
, sizeof(fork
));
271 cnattr
.ca_blocks
= 0;
273 error
= hfs_getnewvnode(hfsmp
, NULL
, NULL
, &cndesc
, 0, &cnattr
, &fork
,
274 &hfsmp
->hfs_allocation_vp
, &newvnode_flags
);
276 if (HFS_MOUNT_DEBUG
) {
277 printf("hfs_mounthfs (std): error creating bitmap Vnode (%d) \n", error
);
279 hfs_unlock(VTOC(hfsmp
->hfs_catalog_vp
));
280 hfs_unlock(VTOC(hfsmp
->hfs_extents_vp
));
283 hfsmp
->hfs_allocation_cp
= VTOC(hfsmp
->hfs_allocation_vp
);
285 /* mark the volume dirty (clear clean unmount bit) */
286 vcb
->vcbAtrb
&= ~kHFSVolumeUnmountedMask
;
288 if (error
== noErr
) {
289 error
= cat_idlookup(hfsmp
, kHFSRootFolderID
, 0, 0, NULL
, NULL
, NULL
);
290 if (HFS_MOUNT_DEBUG
) {
291 printf("hfs_mounthfs (std): error looking up root folder (%d) \n", error
);
295 if (error
== noErr
) {
296 /* If the disk isn't write protected.. */
297 if ( !(vcb
->vcbAtrb
& kHFSVolumeHardwareLockMask
)) {
298 MarkVCBDirty (vcb
); // mark VCB dirty so it will be written
303 * all done with system files so we can unlock now...
305 hfs_unlock(VTOC(hfsmp
->hfs_allocation_vp
));
306 hfs_unlock(VTOC(hfsmp
->hfs_catalog_vp
));
307 hfs_unlock(VTOC(hfsmp
->hfs_extents_vp
));
309 if (error
== noErr
) {
310 /* If successful, then we can just return once we've unlocked the cnodes */
314 //-- Release any resources allocated so far before exiting with an error:
316 hfsUnmount(hfsmp
, NULL
);
323 //*******************************************************************************
325 // Sanity check Volume Header Block:
326 // Input argument *vhp is a pointer to a HFSPlusVolumeHeader block that has
327 // not been endian-swapped and represents the on-disk contents of this sector.
328 // This routine will not change the endianness of vhp block.
330 //*******************************************************************************
331 OSErr
hfs_ValidateHFSPlusVolumeHeader(struct hfsmount
*hfsmp
, HFSPlusVolumeHeader
*vhp
)
334 u_int16_t hfs_version
;
337 signature
= SWAP_BE16(vhp
->signature
);
338 hfs_version
= SWAP_BE16(vhp
->version
);
340 if (signature
== kHFSPlusSigWord
) {
341 if (hfs_version
!= kHFSPlusVersion
) {
342 printf("hfs_ValidateHFSPlusVolumeHeader: invalid HFS+ version: %x\n", hfs_version
);
345 } else if (signature
== kHFSXSigWord
) {
346 if (hfs_version
!= kHFSXVersion
) {
347 printf("hfs_ValidateHFSPlusVolumeHeader: invalid HFSX version: %x\n", hfs_version
);
351 /* Removed printf for invalid HFS+ signature because it gives
352 * false error for UFS root volume
354 if (HFS_MOUNT_DEBUG
) {
355 printf("hfs_ValidateHFSPlusVolumeHeader: unknown Volume Signature : %x\n", signature
);
360 /* Block size must be at least 512 and a power of 2 */
361 blockSize
= SWAP_BE32(vhp
->blockSize
);
362 if (blockSize
< 512 || !powerof2(blockSize
)) {
363 if (HFS_MOUNT_DEBUG
) {
364 printf("hfs_ValidateHFSPlusVolumeHeader: invalid blocksize (%d) \n", blockSize
);
369 if (blockSize
< hfsmp
->hfs_logical_block_size
) {
370 if (HFS_MOUNT_DEBUG
) {
371 printf("hfs_ValidateHFSPlusVolumeHeader: invalid physical blocksize (%d), hfs_logical_blocksize (%d) \n",
372 blockSize
, hfsmp
->hfs_logical_block_size
);
379 //*******************************************************************************
380 // Routine: hfs_MountHFSPlusVolume
383 //*******************************************************************************
385 OSErr
hfs_MountHFSPlusVolume(struct hfsmount
*hfsmp
, HFSPlusVolumeHeader
*vhp
,
386 off_t embeddedOffset
, u_int64_t disksize
, __unused
struct proc
*p
, void *args
, kauth_cred_t cred
)
388 register ExtendedVCB
*vcb
;
389 struct cat_desc cndesc
;
390 struct cat_attr cnattr
;
391 struct cat_fork cfork
;
393 daddr64_t spare_sectors
;
394 struct BTreeInfoRec btinfo
;
396 u_int16_t hfs_version
;
397 int newvnode_flags
= 0;
400 char converted_volname
[256];
401 size_t volname_length
= 0;
402 size_t conv_volname_length
= 0;
404 signature
= SWAP_BE16(vhp
->signature
);
405 hfs_version
= SWAP_BE16(vhp
->version
);
407 retval
= hfs_ValidateHFSPlusVolumeHeader(hfsmp
, vhp
);
411 if (signature
== kHFSXSigWord
) {
412 /* The in-memory signature is always 'H+'. */
413 signature
= kHFSPlusSigWord
;
414 hfsmp
->hfs_flags
|= HFS_X
;
417 blockSize
= SWAP_BE32(vhp
->blockSize
);
418 /* don't mount a writable volume if its dirty, it must be cleaned by fsck_hfs */
419 if ((hfsmp
->hfs_flags
& HFS_READ_ONLY
) == 0 && hfsmp
->jnl
== NULL
&&
420 (SWAP_BE32(vhp
->attributes
) & kHFSVolumeUnmountedMask
) == 0) {
421 if (HFS_MOUNT_DEBUG
) {
422 printf("hfs_mounthfsplus: cannot mount dirty non-journaled volumes\n");
427 /* Make sure we can live with the physical block size. */
428 if ((disksize
& (hfsmp
->hfs_logical_block_size
- 1)) ||
429 (embeddedOffset
& (hfsmp
->hfs_logical_block_size
- 1))) {
430 if (HFS_MOUNT_DEBUG
) {
431 printf("hfs_mounthfsplus: hfs_logical_blocksize (%d) \n",
432 hfsmp
->hfs_logical_block_size
);
438 * If allocation block size is less than the physical block size,
439 * same data could be cached in two places and leads to corruption.
441 * HFS Plus reserves one allocation block for the Volume Header.
442 * If the physical size is larger, then when we read the volume header,
443 * we will also end up reading in the next allocation block(s).
444 * If those other allocation block(s) is/are modified, and then the volume
445 * header is modified, the write of the volume header's buffer will write
446 * out the old contents of the other allocation blocks.
448 * We assume that the physical block size is same as logical block size.
449 * The physical block size value is used to round down the offsets for
450 * reading and writing the primary and alternate volume headers.
452 * The same logic to ensure good hfs_physical_block_size is also in
453 * hfs_mountfs so that hfs_mountfs, hfs_MountHFSPlusVolume and
454 * later are doing the I/Os using same block size.
456 if (blockSize
< hfsmp
->hfs_physical_block_size
) {
457 hfsmp
->hfs_physical_block_size
= hfsmp
->hfs_logical_block_size
;
458 hfsmp
->hfs_log_per_phys
= 1;
462 * The VolumeHeader seems OK: transfer info from it into VCB
463 * Note - the VCB starts out clear (all zeros)
465 vcb
= HFSTOVCB(hfsmp
);
467 vcb
->vcbSigWord
= signature
;
468 vcb
->vcbJinfoBlock
= SWAP_BE32(vhp
->journalInfoBlock
);
469 vcb
->vcbLsMod
= to_bsd_time(SWAP_BE32(vhp
->modifyDate
));
470 vcb
->vcbAtrb
= SWAP_BE32(vhp
->attributes
);
471 vcb
->vcbClpSiz
= SWAP_BE32(vhp
->rsrcClumpSize
);
472 vcb
->vcbNxtCNID
= SWAP_BE32(vhp
->nextCatalogID
);
473 vcb
->vcbVolBkUp
= to_bsd_time(SWAP_BE32(vhp
->backupDate
));
474 vcb
->vcbWrCnt
= SWAP_BE32(vhp
->writeCount
);
475 vcb
->vcbFilCnt
= SWAP_BE32(vhp
->fileCount
);
476 vcb
->vcbDirCnt
= SWAP_BE32(vhp
->folderCount
);
478 /* copy 32 bytes of Finder info */
479 bcopy(vhp
->finderInfo
, vcb
->vcbFndrInfo
, sizeof(vhp
->finderInfo
));
481 vcb
->vcbAlBlSt
= 0; /* hfs+ allocation blocks start at first block of volume */
482 if ((hfsmp
->hfs_flags
& HFS_READ_ONLY
) == 0)
483 vcb
->vcbWrCnt
++; /* compensate for write of Volume Header on last flush */
485 /* Now fill in the Extended VCB info */
486 vcb
->nextAllocation
= SWAP_BE32(vhp
->nextAllocation
);
487 vcb
->totalBlocks
= SWAP_BE32(vhp
->totalBlocks
);
488 vcb
->allocLimit
= vcb
->totalBlocks
;
489 vcb
->freeBlocks
= SWAP_BE32(vhp
->freeBlocks
);
490 vcb
->blockSize
= blockSize
;
491 vcb
->encodingsBitmap
= SWAP_BE64(vhp
->encodingsBitmap
);
492 vcb
->localCreateDate
= SWAP_BE32(vhp
->createDate
);
494 vcb
->hfsPlusIOPosOffset
= embeddedOffset
;
496 /* Default to no free block reserve */
497 vcb
->reserveBlocks
= 0;
500 * Update the logical block size in the mount struct
501 * (currently set up from the wrapper MDB) using the
502 * new blocksize value:
504 hfsmp
->hfs_logBlockSize
= BestBlockSizeFit(vcb
->blockSize
, MAXBSIZE
, hfsmp
->hfs_logical_block_size
);
505 vcb
->vcbVBMIOSize
= min(vcb
->blockSize
, MAXPHYSIO
);
508 * Validate and initialize the location of the alternate volume header.
510 * Note that there may be spare sectors beyond the end of the filesystem that still
511 * belong to our partition.
514 spare_sectors
= hfsmp
->hfs_logical_block_count
-
515 (((daddr64_t
)vcb
->totalBlocks
* blockSize
) /
516 hfsmp
->hfs_logical_block_size
);
519 * Differentiate between "innocuous" spare sectors and the more unusual
522 * *** Innocuous spare sectors exist if:
524 * A) the number of bytes assigned to the partition (by multiplying logical
525 * block size * logical block count) is greater than the filesystem size
526 * (by multiplying allocation block count and allocation block size)
530 * B) the remainder is less than the size of a full allocation block's worth of bytes.
532 * This handles the normal case where there may be a few extra sectors, but the two
533 * are fundamentally in sync.
535 * *** Degenerate spare sectors exist if:
536 * A) The number of bytes assigned to the partition (by multiplying logical
537 * block size * logical block count) is greater than the filesystem size
538 * (by multiplying allocation block count and block size).
542 * B) the remainder is greater than a full allocation's block worth of bytes.
543 * In this case, a smaller file system exists in a larger partition.
544 * This can happen in various ways, including when volume is resized but the
545 * partition is yet to be resized. Under this condition, we have to assume that
546 * a partition management software may resize the partition to match
547 * the file system size in the future. Therefore we should update
548 * alternate volume header at two locations on the disk,
549 * a. 1024 bytes before end of the partition
550 * b. 1024 bytes before end of the file system
553 if (spare_sectors
> (daddr64_t
)(blockSize
/ hfsmp
->hfs_logical_block_size
)) {
555 * Handle the degenerate case above. FS < partition size.
556 * AVH located at 1024 bytes from the end of the partition
558 hfsmp
->hfs_partition_avh_sector
= (hfsmp
->hfsPlusIOPosOffset
/ hfsmp
->hfs_logical_block_size
) +
559 HFS_ALT_SECTOR(hfsmp
->hfs_logical_block_size
, hfsmp
->hfs_logical_block_count
);
561 /* AVH located at 1024 bytes from the end of the filesystem */
562 hfsmp
->hfs_fs_avh_sector
= (hfsmp
->hfsPlusIOPosOffset
/ hfsmp
->hfs_logical_block_size
) +
563 HFS_ALT_SECTOR(hfsmp
->hfs_logical_block_size
,
564 (((daddr64_t
)vcb
->totalBlocks
* blockSize
) / hfsmp
->hfs_logical_block_size
));
567 /* Innocuous spare sectors; Partition & FS notion are in sync */
568 hfsmp
->hfs_partition_avh_sector
= (hfsmp
->hfsPlusIOPosOffset
/ hfsmp
->hfs_logical_block_size
) +
569 HFS_ALT_SECTOR(hfsmp
->hfs_logical_block_size
, hfsmp
->hfs_logical_block_count
);
571 hfsmp
->hfs_fs_avh_sector
= hfsmp
->hfs_partition_avh_sector
;
573 if (hfs_resize_debug
) {
574 printf ("hfs_MountHFSPlusVolume: partition_avh_sector=%qu, fs_avh_sector=%qu\n",
575 hfsmp
->hfs_partition_avh_sector
, hfsmp
->hfs_fs_avh_sector
);
578 bzero(&cndesc
, sizeof(cndesc
));
579 cndesc
.cd_parentcnid
= kHFSRootParentID
;
580 cndesc
.cd_flags
|= CD_ISMETA
;
581 bzero(&cnattr
, sizeof(cnattr
));
582 cnattr
.ca_linkcount
= 1;
583 cnattr
.ca_mode
= S_IFREG
;
586 * Set up Extents B-tree vnode
588 cndesc
.cd_nameptr
= hfs_extname
;
589 cndesc
.cd_namelen
= strlen((char *)hfs_extname
);
590 cndesc
.cd_cnid
= cnattr
.ca_fileid
= kHFSExtentsFileID
;
592 cfork
.cf_size
= SWAP_BE64 (vhp
->extentsFile
.logicalSize
);
593 cfork
.cf_new_size
= 0;
594 cfork
.cf_clump
= SWAP_BE32 (vhp
->extentsFile
.clumpSize
);
595 cfork
.cf_blocks
= SWAP_BE32 (vhp
->extentsFile
.totalBlocks
);
596 cfork
.cf_vblocks
= 0;
597 cnattr
.ca_blocks
= cfork
.cf_blocks
;
598 for (i
= 0; i
< kHFSPlusExtentDensity
; i
++) {
599 cfork
.cf_extents
[i
].startBlock
=
600 SWAP_BE32 (vhp
->extentsFile
.extents
[i
].startBlock
);
601 cfork
.cf_extents
[i
].blockCount
=
602 SWAP_BE32 (vhp
->extentsFile
.extents
[i
].blockCount
);
604 retval
= hfs_getnewvnode(hfsmp
, NULL
, NULL
, &cndesc
, 0, &cnattr
, &cfork
,
605 &hfsmp
->hfs_extents_vp
, &newvnode_flags
);
608 if (HFS_MOUNT_DEBUG
) {
609 printf("hfs_mounthfsplus: hfs_getnewvnode returned (%d) getting extentoverflow BT\n", retval
);
614 hfsmp
->hfs_extents_cp
= VTOC(hfsmp
->hfs_extents_vp
);
616 retval
= MacToVFSError(BTOpenPath(VTOF(hfsmp
->hfs_extents_vp
),
617 (KeyCompareProcPtr
) CompareExtentKeysPlus
));
619 hfs_unlock(hfsmp
->hfs_extents_cp
);
623 if (HFS_MOUNT_DEBUG
) {
624 printf("hfs_mounthfsplus: BTOpenPath returned (%d) getting extentoverflow BT\n", retval
);
629 * Set up Catalog B-tree vnode
631 cndesc
.cd_nameptr
= hfs_catname
;
632 cndesc
.cd_namelen
= strlen((char *)hfs_catname
);
633 cndesc
.cd_cnid
= cnattr
.ca_fileid
= kHFSCatalogFileID
;
635 cfork
.cf_size
= SWAP_BE64 (vhp
->catalogFile
.logicalSize
);
636 cfork
.cf_clump
= SWAP_BE32 (vhp
->catalogFile
.clumpSize
);
637 cfork
.cf_blocks
= SWAP_BE32 (vhp
->catalogFile
.totalBlocks
);
638 cfork
.cf_vblocks
= 0;
639 cnattr
.ca_blocks
= cfork
.cf_blocks
;
640 for (i
= 0; i
< kHFSPlusExtentDensity
; i
++) {
641 cfork
.cf_extents
[i
].startBlock
=
642 SWAP_BE32 (vhp
->catalogFile
.extents
[i
].startBlock
);
643 cfork
.cf_extents
[i
].blockCount
=
644 SWAP_BE32 (vhp
->catalogFile
.extents
[i
].blockCount
);
646 retval
= hfs_getnewvnode(hfsmp
, NULL
, NULL
, &cndesc
, 0, &cnattr
, &cfork
,
647 &hfsmp
->hfs_catalog_vp
, &newvnode_flags
);
649 if (HFS_MOUNT_DEBUG
) {
650 printf("hfs_mounthfsplus: hfs_getnewvnode returned (%d) getting catalog BT\n", retval
);
654 hfsmp
->hfs_catalog_cp
= VTOC(hfsmp
->hfs_catalog_vp
);
656 retval
= MacToVFSError(BTOpenPath(VTOF(hfsmp
->hfs_catalog_vp
),
657 (KeyCompareProcPtr
) CompareExtendedCatalogKeys
));
660 if (HFS_MOUNT_DEBUG
) {
661 printf("hfs_mounthfsplus: BTOpenPath returned (%d) getting catalog BT\n", retval
);
663 hfs_unlock(hfsmp
->hfs_catalog_cp
);
666 if ((hfsmp
->hfs_flags
& HFS_X
) &&
667 BTGetInformation(VTOF(hfsmp
->hfs_catalog_vp
), 0, &btinfo
) == 0) {
668 if (btinfo
.keyCompareType
== kHFSBinaryCompare
) {
669 hfsmp
->hfs_flags
|= HFS_CASE_SENSITIVE
;
670 /* Install a case-sensitive key compare */
671 (void) BTOpenPath(VTOF(hfsmp
->hfs_catalog_vp
),
672 (KeyCompareProcPtr
)cat_binarykeycompare
);
676 hfs_unlock(hfsmp
->hfs_catalog_cp
);
679 * Set up Allocation file vnode
681 cndesc
.cd_nameptr
= hfs_vbmname
;
682 cndesc
.cd_namelen
= strlen((char *)hfs_vbmname
);
683 cndesc
.cd_cnid
= cnattr
.ca_fileid
= kHFSAllocationFileID
;
685 cfork
.cf_size
= SWAP_BE64 (vhp
->allocationFile
.logicalSize
);
686 cfork
.cf_clump
= SWAP_BE32 (vhp
->allocationFile
.clumpSize
);
687 cfork
.cf_blocks
= SWAP_BE32 (vhp
->allocationFile
.totalBlocks
);
688 cfork
.cf_vblocks
= 0;
689 cnattr
.ca_blocks
= cfork
.cf_blocks
;
690 for (i
= 0; i
< kHFSPlusExtentDensity
; i
++) {
691 cfork
.cf_extents
[i
].startBlock
=
692 SWAP_BE32 (vhp
->allocationFile
.extents
[i
].startBlock
);
693 cfork
.cf_extents
[i
].blockCount
=
694 SWAP_BE32 (vhp
->allocationFile
.extents
[i
].blockCount
);
696 retval
= hfs_getnewvnode(hfsmp
, NULL
, NULL
, &cndesc
, 0, &cnattr
, &cfork
,
697 &hfsmp
->hfs_allocation_vp
, &newvnode_flags
);
699 if (HFS_MOUNT_DEBUG
) {
700 printf("hfs_mounthfsplus: hfs_getnewvnode returned (%d) getting bitmap\n", retval
);
704 hfsmp
->hfs_allocation_cp
= VTOC(hfsmp
->hfs_allocation_vp
);
705 hfs_unlock(hfsmp
->hfs_allocation_cp
);
708 * Set up Attribute B-tree vnode
710 if (vhp
->attributesFile
.totalBlocks
!= 0) {
711 cndesc
.cd_nameptr
= hfs_attrname
;
712 cndesc
.cd_namelen
= strlen((char *)hfs_attrname
);
713 cndesc
.cd_cnid
= cnattr
.ca_fileid
= kHFSAttributesFileID
;
715 cfork
.cf_size
= SWAP_BE64 (vhp
->attributesFile
.logicalSize
);
716 cfork
.cf_clump
= SWAP_BE32 (vhp
->attributesFile
.clumpSize
);
717 cfork
.cf_blocks
= SWAP_BE32 (vhp
->attributesFile
.totalBlocks
);
718 cfork
.cf_vblocks
= 0;
719 cnattr
.ca_blocks
= cfork
.cf_blocks
;
720 for (i
= 0; i
< kHFSPlusExtentDensity
; i
++) {
721 cfork
.cf_extents
[i
].startBlock
=
722 SWAP_BE32 (vhp
->attributesFile
.extents
[i
].startBlock
);
723 cfork
.cf_extents
[i
].blockCount
=
724 SWAP_BE32 (vhp
->attributesFile
.extents
[i
].blockCount
);
726 retval
= hfs_getnewvnode(hfsmp
, NULL
, NULL
, &cndesc
, 0, &cnattr
, &cfork
,
727 &hfsmp
->hfs_attribute_vp
, &newvnode_flags
);
729 if (HFS_MOUNT_DEBUG
) {
730 printf("hfs_mounthfsplus: hfs_getnewvnode returned (%d) getting EA BT\n", retval
);
734 hfsmp
->hfs_attribute_cp
= VTOC(hfsmp
->hfs_attribute_vp
);
735 retval
= MacToVFSError(BTOpenPath(VTOF(hfsmp
->hfs_attribute_vp
),
736 (KeyCompareProcPtr
) hfs_attrkeycompare
));
737 hfs_unlock(hfsmp
->hfs_attribute_cp
);
739 if (HFS_MOUNT_DEBUG
) {
740 printf("hfs_mounthfsplus: BTOpenPath returned (%d) getting EA BT\n", retval
);
745 /* Initialize vnode for virtual attribute data file that spans the
746 * entire file system space for performing I/O to attribute btree
747 * We hold iocount on the attrdata vnode for the entire duration
748 * of mount (similar to btree vnodes)
750 retval
= init_attrdata_vnode(hfsmp
);
752 if (HFS_MOUNT_DEBUG
) {
753 printf("hfs_mounthfsplus: init_attrdata_vnode returned (%d) for virtual EA file\n", retval
);
760 * Set up Startup file vnode
762 if (vhp
->startupFile
.totalBlocks
!= 0) {
763 cndesc
.cd_nameptr
= hfs_startupname
;
764 cndesc
.cd_namelen
= strlen((char *)hfs_startupname
);
765 cndesc
.cd_cnid
= cnattr
.ca_fileid
= kHFSStartupFileID
;
767 cfork
.cf_size
= SWAP_BE64 (vhp
->startupFile
.logicalSize
);
768 cfork
.cf_clump
= SWAP_BE32 (vhp
->startupFile
.clumpSize
);
769 cfork
.cf_blocks
= SWAP_BE32 (vhp
->startupFile
.totalBlocks
);
770 cfork
.cf_vblocks
= 0;
771 cnattr
.ca_blocks
= cfork
.cf_blocks
;
772 for (i
= 0; i
< kHFSPlusExtentDensity
; i
++) {
773 cfork
.cf_extents
[i
].startBlock
=
774 SWAP_BE32 (vhp
->startupFile
.extents
[i
].startBlock
);
775 cfork
.cf_extents
[i
].blockCount
=
776 SWAP_BE32 (vhp
->startupFile
.extents
[i
].blockCount
);
778 retval
= hfs_getnewvnode(hfsmp
, NULL
, NULL
, &cndesc
, 0, &cnattr
, &cfork
,
779 &hfsmp
->hfs_startup_vp
, &newvnode_flags
);
781 if (HFS_MOUNT_DEBUG
) {
782 printf("hfs_mounthfsplus: hfs_getnewvnode returned (%d) getting startup file\n", retval
);
786 hfsmp
->hfs_startup_cp
= VTOC(hfsmp
->hfs_startup_vp
);
787 hfs_unlock(hfsmp
->hfs_startup_cp
);
791 * Pick up volume name and create date
793 * Acquiring the volume name should not manipulate the bitmap, only the catalog
794 * btree and possibly the extents overflow b-tree.
796 retval
= cat_idlookup(hfsmp
, kHFSRootFolderID
, 0, 0, &cndesc
, &cnattr
, NULL
);
798 if (HFS_MOUNT_DEBUG
) {
799 printf("hfs_mounthfsplus: cat_idlookup returned (%d) getting rootfolder \n", retval
);
803 vcb
->hfs_itime
= cnattr
.ca_itime
;
804 vcb
->volumeNameEncodingHint
= cndesc
.cd_encoding
;
805 bcopy(cndesc
.cd_nameptr
, vcb
->vcbVN
, min(255, cndesc
.cd_namelen
));
806 volname_length
= strlen ((const char*)vcb
->vcbVN
);
807 cat_releasedesc(&cndesc
);
809 /* Send the volume name down to CoreStorage if necessary */
810 retval
= utf8_normalizestr(vcb
->vcbVN
, volname_length
, (u_int8_t
*)converted_volname
, &conv_volname_length
, 256, UTF_PRECOMPOSED
);
812 (void) VNOP_IOCTL (hfsmp
->hfs_devvp
, _DKIOCCSSETLVNAME
, converted_volname
, 0, vfs_context_current());
815 /* reset retval == 0. we don't care about errors in volname conversion */
820 * We now always initiate a full bitmap scan even if the volume is read-only because this is
821 * our only shot to do I/Os of dramaticallly different sizes than what the buffer cache ordinarily
822 * expects. TRIMs will not be delivered to the underlying media if the volume is not
825 thread_t allocator_scanner
;
828 /* Take the HFS mount mutex and wait on scan_var */
829 hfs_lock_mount (hfsmp
);
831 kernel_thread_start ((thread_continue_t
) hfs_scan_blocks
, hfsmp
, &allocator_scanner
);
833 /* Wait until it registers that it's got the appropriate locks (or that it is finished) */
834 while ((hfsmp
->scan_var
& (HFS_ALLOCATOR_SCAN_INFLIGHT
|HFS_ALLOCATOR_SCAN_COMPLETED
)) == 0) {
835 msleep (&hfsmp
->scan_var
, &hfsmp
->hfs_mutex
, PINOD
, "hfs_scan_blocks", 0);
838 hfs_unlock_mount(hfsmp
);
840 thread_deallocate (allocator_scanner
);
842 /* mark the volume dirty (clear clean unmount bit) */
843 vcb
->vcbAtrb
&= ~kHFSVolumeUnmountedMask
;
844 if (hfsmp
->jnl
&& (hfsmp
->hfs_flags
& HFS_READ_ONLY
) == 0) {
845 hfs_flushvolumeheader(hfsmp
, HFS_FVH_WAIT
);
848 /* kHFSHasFolderCount is only supported/updated on HFSX volumes */
849 if ((hfsmp
->hfs_flags
& HFS_X
) != 0) {
850 hfsmp
->hfs_flags
|= HFS_FOLDERCOUNT
;
854 // Check if we need to do late journal initialization. This only
855 // happens if a previous version of MacOS X (or 9) touched the disk.
856 // In that case hfs_late_journal_init() will go re-locate the journal
857 // and journal_info_block files and validate that they're still kosher.
859 if ( (vcb
->vcbAtrb
& kHFSVolumeJournaledMask
)
860 && (SWAP_BE32(vhp
->lastMountedVersion
) != kHFSJMountVersion
)
861 && (hfsmp
->jnl
== NULL
)) {
863 retval
= hfs_late_journal_init(hfsmp
, vhp
, args
);
865 if (retval
== EROFS
) {
866 // EROFS is a special error code that means the volume has an external
867 // journal which we couldn't find. in that case we do not want to
868 // rewrite the volume header - we'll just refuse to mount the volume.
869 if (HFS_MOUNT_DEBUG
) {
870 printf("hfs_mounthfsplus: hfs_late_journal_init returned (%d), maybe an external jnl?\n", retval
);
878 // if the journal failed to open, then set the lastMountedVersion
879 // to be "FSK!" which fsck_hfs will see and force the fsck instead
880 // of just bailing out because the volume is journaled.
881 if (!(hfsmp
->hfs_flags
& HFS_READ_ONLY
)) {
882 HFSPlusVolumeHeader
*jvhp
;
883 daddr64_t mdb_offset
;
884 struct buf
*bp
= NULL
;
886 hfsmp
->hfs_flags
|= HFS_NEED_JNL_RESET
;
888 mdb_offset
= (daddr64_t
)((embeddedOffset
/ blockSize
) + HFS_PRI_SECTOR(blockSize
));
891 retval
= (int)buf_meta_bread(hfsmp
->hfs_devvp
,
892 HFS_PHYSBLK_ROUNDDOWN(mdb_offset
, hfsmp
->hfs_log_per_phys
),
893 hfsmp
->hfs_physical_block_size
, cred
, &bp
);
895 jvhp
= (HFSPlusVolumeHeader
*)(buf_dataptr(bp
) + HFS_PRI_OFFSET(hfsmp
->hfs_physical_block_size
));
897 if (SWAP_BE16(jvhp
->signature
) == kHFSPlusSigWord
|| SWAP_BE16(jvhp
->signature
) == kHFSXSigWord
) {
898 printf ("hfs(3): Journal replay fail. Writing lastMountVersion as FSK!\n");
899 jvhp
->lastMountedVersion
= SWAP_BE32(kFSKMountVersion
);
907 // clear this so the error exit path won't try to use it
912 if (HFS_MOUNT_DEBUG
) {
913 printf("hfs_mounthfsplus: hfs_late_journal_init returned (%d)\n", retval
);
917 } else if (hfsmp
->jnl
) {
918 vfs_setflags(hfsmp
->hfs_mp
, (u_int64_t
)((unsigned int)MNT_JOURNALED
));
920 } else if (hfsmp
->jnl
|| ((vcb
->vcbAtrb
& kHFSVolumeJournaledMask
) && (hfsmp
->hfs_flags
& HFS_READ_ONLY
))) {
921 struct cat_attr jinfo_attr
, jnl_attr
;
923 if (hfsmp
->hfs_flags
& HFS_READ_ONLY
) {
924 vcb
->vcbAtrb
&= ~kHFSVolumeJournaledMask
;
927 // if we're here we need to fill in the fileid's for the
928 // journal and journal_info_block.
929 hfsmp
->hfs_jnlinfoblkid
= GetFileInfo(vcb
, kRootDirID
, ".journal_info_block", &jinfo_attr
, NULL
);
930 hfsmp
->hfs_jnlfileid
= GetFileInfo(vcb
, kRootDirID
, ".journal", &jnl_attr
, NULL
);
931 if (hfsmp
->hfs_jnlinfoblkid
== 0 || hfsmp
->hfs_jnlfileid
== 0) {
932 printf("hfs: danger! couldn't find the file-id's for the journal or journal_info_block\n");
933 printf("hfs: jnlfileid %d, jnlinfoblkid %d\n", hfsmp
->hfs_jnlfileid
, hfsmp
->hfs_jnlinfoblkid
);
936 if (hfsmp
->hfs_flags
& HFS_READ_ONLY
) {
937 vcb
->vcbAtrb
|= kHFSVolumeJournaledMask
;
940 if (hfsmp
->jnl
== NULL
) {
941 vfs_clearflags(hfsmp
->hfs_mp
, (u_int64_t
)((unsigned int)MNT_JOURNALED
));
945 if ( !(vcb
->vcbAtrb
& kHFSVolumeHardwareLockMask
) ) // if the disk is not write protected
947 MarkVCBDirty( vcb
); // mark VCB dirty so it will be written
950 if (hfsmp
->hfs_flags
& HFS_CS_METADATA_PIN
) {
951 hfs_pin_fs_metadata(hfsmp
);
954 * Distinguish 3 potential cases involving content protection:
955 * 1. mount point bit set; vcbAtrb does not support it. Fail.
956 * 2. mount point bit set; vcbattrb supports it. we're good.
957 * 3. mount point bit not set; vcbatrb supports it, turn bit on, then good.
959 if (vfs_flags(hfsmp
->hfs_mp
) & MNT_CPROTECT
) {
960 /* Does the mount point support it ? */
961 if ((vcb
->vcbAtrb
& kHFSContentProtectionMask
) == 0) {
968 /* not requested in the mount point. Is it in FS? */
969 if (vcb
->vcbAtrb
& kHFSContentProtectionMask
) {
971 vfs_setflags (hfsmp
->hfs_mp
, MNT_CPROTECT
);
975 /* At this point, if the mount point flag is set, we can enable it. */
976 if (vfs_flags(hfsmp
->hfs_mp
) & MNT_CPROTECT
) {
977 /* Cases 2+3 above */
979 /* Get the EAs as needed. */
981 struct cp_root_xattr
*xattr
= NULL
;
982 xattr
= hfs_malloc(sizeof(*xattr
));
984 /* go get the EA to get the version information */
985 cperr
= cp_getrootxattr (hfsmp
, xattr
);
987 * If there was no EA there, then write one out.
988 * Assuming EA is not present on the root means
989 * this is an erase install or a very old FS
993 /* Have to run a valid CP version. */
994 if (!cp_is_supported_version(xattr
->major_version
)) {
998 else if (cperr
== ENOATTR
) {
999 printf("No root EA set, creating new EA with new version: %d\n", CP_CURRENT_VERS
);
1000 bzero(xattr
, sizeof(struct cp_root_xattr
));
1001 xattr
->major_version
= CP_CURRENT_VERS
;
1002 xattr
->minor_version
= CP_MINOR_VERS
;
1003 cperr
= cp_setrootxattr (hfsmp
, xattr
);
1007 hfs_free(xattr
, sizeof(*xattr
));
1012 /* If we got here, then the CP version is valid. Set it in the mount point */
1013 hfsmp
->hfs_running_cp_major_vers
= xattr
->major_version
;
1014 printf("Running with CP root xattr: %d.%d\n", xattr
->major_version
, xattr
->minor_version
);
1015 hfsmp
->cproot_flags
= xattr
->flags
;
1016 hfsmp
->cp_crypto_generation
= ISSET(xattr
->flags
, CP_ROOT_CRYPTOG1
) ? 1 : 0;
1017 #if HFS_CONFIG_KEY_ROLL
1018 hfsmp
->hfs_auto_roll_min_key_os_version
= xattr
->auto_roll_min_version
;
1019 hfsmp
->hfs_auto_roll_max_key_os_version
= xattr
->auto_roll_max_version
;
1022 hfs_free(xattr
, sizeof(*xattr
));
1025 * Acquire the boot-arg for the AKS default key; if invalid, obtain from the device tree.
1026 * Ensure that the boot-arg's value is valid for FILES (not directories),
1027 * since only files are actually protected for now.
1030 PE_parse_boot_argn("aks_default_class", &hfsmp
->default_cp_class
, sizeof(hfsmp
->default_cp_class
));
1032 if (cp_is_valid_class(0, hfsmp
->default_cp_class
) == 0) {
1033 PE_get_default("kern.default_cp_class", &hfsmp
->default_cp_class
, sizeof(hfsmp
->default_cp_class
));
1038 PE_parse_boot_argn("aks_verbose", &hfsmp
->hfs_cp_verbose
, sizeof(hfsmp
->hfs_cp_verbose
));
1042 if (cp_is_valid_class(0, hfsmp
->default_cp_class
) == 0) {
1043 hfsmp
->default_cp_class
= PROTECTION_CLASS_C
;
1047 /* If CONFIG_PROTECT not built, ignore CP */
1048 vfs_clearflags(hfsmp
->hfs_mp
, MNT_CPROTECT
);
1053 * Establish a metadata allocation zone.
1055 hfs_metadatazone_init(hfsmp
, false);
1058 * Make any metadata zone adjustments.
1060 if (hfsmp
->hfs_flags
& HFS_METADATA_ZONE
) {
1061 /* Keep the roving allocator out of the metadata zone. */
1062 if (vcb
->nextAllocation
>= hfsmp
->hfs_metazone_start
&&
1063 vcb
->nextAllocation
<= hfsmp
->hfs_metazone_end
) {
1064 HFS_UPDATE_NEXT_ALLOCATION(hfsmp
, hfsmp
->hfs_metazone_end
+ 1);
1067 if (vcb
->nextAllocation
<= 1) {
1068 vcb
->nextAllocation
= hfsmp
->hfs_min_alloc_start
;
1071 vcb
->sparseAllocation
= hfsmp
->hfs_min_alloc_start
;
1073 /* Setup private/hidden directories for hardlinks. */
1074 hfs_privatedir_init(hfsmp
, FILE_HARDLINKS
);
1075 hfs_privatedir_init(hfsmp
, DIR_HARDLINKS
);
1077 if ((hfsmp
->hfs_flags
& HFS_READ_ONLY
) == 0)
1078 hfs_remove_orphans(hfsmp
);
1080 /* See if we need to erase unused Catalog nodes due to <rdar://problem/6947811>. */
1081 if ((hfsmp
->hfs_flags
& HFS_READ_ONLY
) == 0)
1083 retval
= hfs_erase_unused_nodes(hfsmp
);
1085 if (HFS_MOUNT_DEBUG
) {
1086 printf("hfs_mounthfsplus: hfs_erase_unused_nodes returned (%d) for %s \n", retval
, hfsmp
->vcbVN
);
1094 * Allow hot file clustering if conditions allow.
1096 if ((hfsmp
->hfs_flags
& HFS_METADATA_ZONE
) && !(hfsmp
->hfs_flags
& HFS_READ_ONLY
) &&
1097 ((hfsmp
->hfs_flags
& HFS_SSD
) == 0 || (hfsmp
->hfs_flags
& HFS_CS_HOTFILE_PIN
))) {
1099 // Wait until the bitmap scan completes before we initializes the
1100 // hotfile area so that we do not run into any issues with the
1101 // bitmap being read while hotfiles is initializing itself. On
1102 // some older/slower machines, without this interlock, the bitmap
1103 // would sometimes get corrupted at boot time.
1105 hfs_lock_mount(hfsmp
);
1106 while(!(hfsmp
->scan_var
& HFS_ALLOCATOR_SCAN_COMPLETED
)) {
1107 (void) msleep (&hfsmp
->scan_var
, &hfsmp
->hfs_mutex
, PINOD
, "hfs_hotfile_bitmap_interlock", 0);
1109 hfs_unlock_mount(hfsmp
);
1112 * Note: at this point we are not allowed to fail the
1113 * mount operation because the HotFile init code
1114 * in hfs_recording_init() will lookup vnodes with
1115 * VNOP_LOOKUP() which hangs vnodes off the mount
1116 * (and if we were to fail, VFS is not prepared to
1117 * clean that up at this point. Since HotFiles are
1118 * optional, this is not a big deal.
1120 (void) hfs_recording_init(hfsmp
);
1123 /* Force ACLs on HFS+ file systems. */
1124 vfs_setextendedsecurity(HFSTOVFS(hfsmp
));
1126 /* Enable extent-based extended attributes by default */
1127 hfsmp
->hfs_flags
|= HFS_XATTR_EXTENTS
;
1133 * A fatal error occurred and the volume cannot be mounted, so
1134 * release any resources that we acquired...
1136 hfsUnmount(hfsmp
, NULL
);
1138 if (HFS_MOUNT_DEBUG
) {
1139 printf("hfs_mounthfsplus: encountered error (%d)\n", retval
);
1145 _pin_metafile(struct hfsmount
*hfsmp
, vnode_t vp
)
1149 err
= hfs_lock(VTOC(vp
), HFS_SHARED_LOCK
, HFS_LOCK_DEFAULT
);
1151 err
= hfs_pin_vnode(hfsmp
, vp
, HFS_PIN_IT
, NULL
);
1152 hfs_unlock(VTOC(vp
));
1159 hfs_pin_fs_metadata(struct hfsmount
*hfsmp
)
1164 vcb
= HFSTOVCB(hfsmp
);
1166 err
= _pin_metafile(hfsmp
, hfsmp
->hfs_extents_vp
);
1168 printf("hfs: failed to pin extents overflow file %d\n", err
);
1170 err
= _pin_metafile(hfsmp
, hfsmp
->hfs_catalog_vp
);
1172 printf("hfs: failed to pin catalog file %d\n", err
);
1174 err
= _pin_metafile(hfsmp
, hfsmp
->hfs_allocation_vp
);
1176 printf("hfs: failed to pin bitmap file %d\n", err
);
1178 err
= _pin_metafile(hfsmp
, hfsmp
->hfs_attribute_vp
);
1180 printf("hfs: failed to pin extended attr file %d\n", err
);
1183 hfs_pin_block_range(hfsmp
, HFS_PIN_IT
, 0, 1);
1184 hfs_pin_block_range(hfsmp
, HFS_PIN_IT
, vcb
->totalBlocks
-1, 1);
1186 if (vfs_flags(hfsmp
->hfs_mp
) & MNT_JOURNALED
) {
1187 // and hey, if we've got a journal, let's pin that too!
1188 hfs_pin_block_range(hfsmp
, HFS_PIN_IT
, hfsmp
->jnl_start
, howmany(hfsmp
->jnl_size
, vcb
->blockSize
));
1193 * ReleaseMetaFileVNode
1197 static void ReleaseMetaFileVNode(struct vnode
*vp
)
1199 struct filefork
*fp
;
1201 if (vp
&& (fp
= VTOF(vp
))) {
1202 if (fp
->fcbBTCBPtr
!= NULL
) {
1203 (void)hfs_lock(VTOC(vp
), HFS_EXCLUSIVE_LOCK
, HFS_LOCK_DEFAULT
);
1204 (void) BTClosePath(fp
);
1205 hfs_unlock(VTOC(vp
));
1208 /* release the node even if BTClosePath fails */
1215 /*************************************************************
1217 * Unmounts a hfs volume.
1218 * At this point vflush() has been called (to dump all non-metadata files)
1220 *************************************************************/
1223 hfsUnmount( register struct hfsmount
*hfsmp
, __unused
struct proc
*p
)
1225 /* Get rid of our attribute data vnode (if any). This is done
1226 * after the vflush() during mount, so we don't need to worry
1229 if (hfsmp
->hfs_attrdata_vp
) {
1230 ReleaseMetaFileVNode(hfsmp
->hfs_attrdata_vp
);
1231 hfsmp
->hfs_attrdata_vp
= NULLVP
;
1234 if (hfsmp
->hfs_startup_vp
) {
1235 ReleaseMetaFileVNode(hfsmp
->hfs_startup_vp
);
1236 hfsmp
->hfs_startup_cp
= NULL
;
1237 hfsmp
->hfs_startup_vp
= NULL
;
1240 if (hfsmp
->hfs_attribute_vp
) {
1241 ReleaseMetaFileVNode(hfsmp
->hfs_attribute_vp
);
1242 hfsmp
->hfs_attribute_cp
= NULL
;
1243 hfsmp
->hfs_attribute_vp
= NULL
;
1246 if (hfsmp
->hfs_catalog_vp
) {
1247 ReleaseMetaFileVNode(hfsmp
->hfs_catalog_vp
);
1248 hfsmp
->hfs_catalog_cp
= NULL
;
1249 hfsmp
->hfs_catalog_vp
= NULL
;
1252 if (hfsmp
->hfs_extents_vp
) {
1253 ReleaseMetaFileVNode(hfsmp
->hfs_extents_vp
);
1254 hfsmp
->hfs_extents_cp
= NULL
;
1255 hfsmp
->hfs_extents_vp
= NULL
;
1258 if (hfsmp
->hfs_allocation_vp
) {
1259 ReleaseMetaFileVNode(hfsmp
->hfs_allocation_vp
);
1260 hfsmp
->hfs_allocation_cp
= NULL
;
1261 hfsmp
->hfs_allocation_vp
= NULL
;
1269 * Test if fork has overflow extents.
1272 * non-zero - overflow extents exist
1273 * zero - overflow extents do not exist
1275 bool overflow_extents(struct filefork
*fp
)
1280 // If the vnode pointer is NULL then we're being called
1281 // from hfs_remove_orphans() with a faked-up filefork
1282 // and therefore it has to be an HFS+ volume. Otherwise
1283 // we check through the volume header to see what type
1284 // of volume we're on.
1288 if (FTOV(fp
) && VTOVCB(FTOV(fp
))->vcbSigWord
== kHFSSigWord
) {
1289 if (fp
->ff_extents
[2].blockCount
== 0)
1292 blocks
= fp
->ff_extents
[0].blockCount
+
1293 fp
->ff_extents
[1].blockCount
+
1294 fp
->ff_extents
[2].blockCount
;
1296 return fp
->ff_blocks
> blocks
;
1300 if (fp
->ff_extents
[7].blockCount
== 0)
1303 blocks
= fp
->ff_extents
[0].blockCount
+
1304 fp
->ff_extents
[1].blockCount
+
1305 fp
->ff_extents
[2].blockCount
+
1306 fp
->ff_extents
[3].blockCount
+
1307 fp
->ff_extents
[4].blockCount
+
1308 fp
->ff_extents
[5].blockCount
+
1309 fp
->ff_extents
[6].blockCount
+
1310 fp
->ff_extents
[7].blockCount
;
1312 return fp
->ff_blocks
> blocks
;
1315 static __attribute__((pure
))
1316 boolean_t
hfs_is_frozen(struct hfsmount
*hfsmp
)
1318 return (hfsmp
->hfs_freeze_state
== HFS_FROZEN
1319 || (hfsmp
->hfs_freeze_state
== HFS_FREEZING
1320 && current_thread() != hfsmp
->hfs_freezing_thread
));
1324 * Lock the HFS global journal lock
1327 hfs_lock_global (struct hfsmount
*hfsmp
, enum hfs_locktype locktype
)
1329 thread_t thread
= current_thread();
1331 if (hfsmp
->hfs_global_lockowner
== thread
) {
1332 panic ("hfs_lock_global: locking against myself!");
1336 * This check isn't really necessary but this stops us taking
1337 * the mount lock in most cases. The essential check is below.
1339 if (hfs_is_frozen(hfsmp
)) {
1341 * Unfortunately, there is no easy way of getting a notification
1342 * for when a process is exiting and it's possible for the exiting
1343 * process to get blocked somewhere else. To catch this, we
1344 * periodically monitor the frozen process here and thaw if
1345 * we spot that it's exiting.
1348 hfs_lock_mount(hfsmp
);
1350 struct timespec ts
= { 0, 500 * NSEC_PER_MSEC
};
1352 while (hfs_is_frozen(hfsmp
)) {
1353 if (hfsmp
->hfs_freeze_state
== HFS_FROZEN
1354 && proc_exiting(hfsmp
->hfs_freezing_proc
)) {
1355 hfs_thaw_locked(hfsmp
);
1359 msleep(&hfsmp
->hfs_freeze_state
, &hfsmp
->hfs_mutex
,
1360 PWAIT
, "hfs_lock_global (frozen)", &ts
);
1362 hfs_unlock_mount(hfsmp
);
1365 /* HFS_SHARED_LOCK */
1366 if (locktype
== HFS_SHARED_LOCK
) {
1367 lck_rw_lock_shared (&hfsmp
->hfs_global_lock
);
1368 hfsmp
->hfs_global_lockowner
= HFS_SHARED_OWNER
;
1370 /* HFS_EXCLUSIVE_LOCK */
1372 lck_rw_lock_exclusive (&hfsmp
->hfs_global_lock
);
1373 hfsmp
->hfs_global_lockowner
= thread
;
1377 * We have to check if we're frozen again because of the time
1378 * between when we checked and when we took the global lock.
1380 if (hfs_is_frozen(hfsmp
)) {
1381 hfs_unlock_global(hfsmp
);
1390 * Unlock the HFS global journal lock
1393 hfs_unlock_global (struct hfsmount
*hfsmp
)
1395 thread_t thread
= current_thread();
1397 /* HFS_LOCK_EXCLUSIVE */
1398 if (hfsmp
->hfs_global_lockowner
== thread
) {
1399 hfsmp
->hfs_global_lockowner
= NULL
;
1400 lck_rw_unlock_exclusive (&hfsmp
->hfs_global_lock
);
1402 /* HFS_LOCK_SHARED */
1404 lck_rw_unlock_shared (&hfsmp
->hfs_global_lock
);
1409 * Lock the HFS mount lock
1411 * Note: this is a mutex, not a rw lock!
1414 void hfs_lock_mount (struct hfsmount
*hfsmp
) {
1415 lck_mtx_lock (&(hfsmp
->hfs_mutex
));
1419 * Unlock the HFS mount lock
1421 * Note: this is a mutex, not a rw lock!
1424 void hfs_unlock_mount (struct hfsmount
*hfsmp
) {
1425 lck_mtx_unlock (&(hfsmp
->hfs_mutex
));
1429 * Lock HFS system file(s).
1431 * This function accepts a @flags parameter which indicates which
1432 * system file locks are required. The value it returns should be
1433 * used in a subsequent call to hfs_systemfile_unlock. The caller
1434 * should treat this value as opaque; it may or may not have a
1435 * relation to the @flags field that is passed in. The *only*
1436 * guarantee that we make is that a value of zero means that no locks
1437 * were taken and that there is no need to call hfs_systemfile_unlock
1438 * (although it is harmless to do so). Recursion is supported but
1439 * care must still be taken to ensure correct lock ordering. Note
1440 * that requests for certain locks may cause other locks to also be
1441 * taken, including locks that are not possible to ask for via the
1445 hfs_systemfile_lock(struct hfsmount
*hfsmp
, int flags
, enum hfs_locktype locktype
)
1448 * Locking order is Catalog file, Attributes file, Startup file, Bitmap file, Extents file
1450 if (flags
& SFL_CATALOG
) {
1451 if (hfsmp
->hfs_catalog_cp
1452 && hfsmp
->hfs_catalog_cp
->c_lockowner
!= current_thread()) {
1453 #ifdef HFS_CHECK_LOCK_ORDER
1454 if (hfsmp
->hfs_attribute_cp
&& hfsmp
->hfs_attribute_cp
->c_lockowner
== current_thread()) {
1455 panic("hfs_systemfile_lock: bad lock order (Attributes before Catalog)");
1457 if (hfsmp
->hfs_startup_cp
&& hfsmp
->hfs_startup_cp
->c_lockowner
== current_thread()) {
1458 panic("hfs_systemfile_lock: bad lock order (Startup before Catalog)");
1460 if (hfsmp
-> hfs_extents_cp
&& hfsmp
->hfs_extents_cp
->c_lockowner
== current_thread()) {
1461 panic("hfs_systemfile_lock: bad lock order (Extents before Catalog)");
1463 #endif /* HFS_CHECK_LOCK_ORDER */
1465 (void) hfs_lock(hfsmp
->hfs_catalog_cp
, locktype
, HFS_LOCK_DEFAULT
);
1467 * When the catalog file has overflow extents then
1468 * also acquire the extents b-tree lock if its not
1469 * already requested.
1471 if (((flags
& SFL_EXTENTS
) == 0) &&
1472 (hfsmp
->hfs_catalog_vp
!= NULL
) &&
1473 (overflow_extents(VTOF(hfsmp
->hfs_catalog_vp
)))) {
1474 flags
|= SFL_EXTENTS
;
1477 flags
&= ~SFL_CATALOG
;
1481 if (flags
& SFL_ATTRIBUTE
) {
1482 if (hfsmp
->hfs_attribute_cp
1483 && hfsmp
->hfs_attribute_cp
->c_lockowner
!= current_thread()) {
1484 #ifdef HFS_CHECK_LOCK_ORDER
1485 if (hfsmp
->hfs_startup_cp
&& hfsmp
->hfs_startup_cp
->c_lockowner
== current_thread()) {
1486 panic("hfs_systemfile_lock: bad lock order (Startup before Attributes)");
1488 if (hfsmp
->hfs_extents_cp
&& hfsmp
->hfs_extents_cp
->c_lockowner
== current_thread()) {
1489 panic("hfs_systemfile_lock: bad lock order (Extents before Attributes)");
1491 #endif /* HFS_CHECK_LOCK_ORDER */
1493 (void) hfs_lock(hfsmp
->hfs_attribute_cp
, locktype
, HFS_LOCK_DEFAULT
);
1495 * When the attribute file has overflow extents then
1496 * also acquire the extents b-tree lock if its not
1497 * already requested.
1499 if (((flags
& SFL_EXTENTS
) == 0) &&
1500 (hfsmp
->hfs_attribute_vp
!= NULL
) &&
1501 (overflow_extents(VTOF(hfsmp
->hfs_attribute_vp
)))) {
1502 flags
|= SFL_EXTENTS
;
1505 flags
&= ~SFL_ATTRIBUTE
;
1509 if (flags
& SFL_STARTUP
) {
1510 if (hfsmp
->hfs_startup_cp
1511 && hfsmp
->hfs_startup_cp
->c_lockowner
!= current_thread()) {
1512 #ifdef HFS_CHECK_LOCK_ORDER
1513 if (hfsmp
-> hfs_extents_cp
&& hfsmp
->hfs_extents_cp
->c_lockowner
== current_thread()) {
1514 panic("hfs_systemfile_lock: bad lock order (Extents before Startup)");
1516 #endif /* HFS_CHECK_LOCK_ORDER */
1518 (void) hfs_lock(hfsmp
->hfs_startup_cp
, locktype
, HFS_LOCK_DEFAULT
);
1520 * When the startup file has overflow extents then
1521 * also acquire the extents b-tree lock if its not
1522 * already requested.
1524 if (((flags
& SFL_EXTENTS
) == 0) &&
1525 (hfsmp
->hfs_startup_vp
!= NULL
) &&
1526 (overflow_extents(VTOF(hfsmp
->hfs_startup_vp
)))) {
1527 flags
|= SFL_EXTENTS
;
1530 flags
&= ~SFL_STARTUP
;
1535 * To prevent locks being taken in the wrong order, the extent lock
1536 * gets a bitmap lock as well.
1538 if (flags
& (SFL_BITMAP
| SFL_EXTENTS
)) {
1539 if (hfsmp
->hfs_allocation_cp
) {
1540 (void) hfs_lock(hfsmp
->hfs_allocation_cp
, HFS_EXCLUSIVE_LOCK
, HFS_LOCK_DEFAULT
);
1542 * The bitmap lock is also grabbed when only extent lock
1543 * was requested. Set the bitmap lock bit in the lock
1544 * flags which callers will use during unlock.
1546 flags
|= SFL_BITMAP
;
1548 flags
&= ~SFL_BITMAP
;
1552 if (flags
& SFL_EXTENTS
) {
1554 * Since the extents btree lock is recursive we always
1555 * need exclusive access.
1557 if (hfsmp
->hfs_extents_cp
) {
1558 (void) hfs_lock(hfsmp
->hfs_extents_cp
, HFS_EXCLUSIVE_LOCK
, HFS_LOCK_DEFAULT
);
1560 if (vfs_isswapmount(hfsmp
->hfs_mp
)) {
1562 * because we may need this lock on the pageout path (if a swapfile allocation
1563 * spills into the extents overflow tree), we will grant the holder of this
1564 * lock the privilege of dipping into the reserve free pool in order to prevent
1565 * a deadlock from occurring if we need those pageouts to complete before we
1566 * will make any new pages available on the free list... the deadlock can occur
1567 * if this thread needs to allocate memory while this lock is held
1569 if (set_vm_privilege(TRUE
) == FALSE
) {
1571 * indicate that we need to drop vm_privilege
1574 flags
|= SFL_VM_PRIV
;
1578 flags
&= ~SFL_EXTENTS
;
1586 * unlock HFS system file(s).
1589 hfs_systemfile_unlock(struct hfsmount
*hfsmp
, int flags
)
1595 u_int32_t lastfsync
;
1596 int numOfLockedBuffs
;
1598 if (hfsmp
->jnl
== NULL
) {
1600 lastfsync
= tv
.tv_sec
;
1602 if (flags
& SFL_STARTUP
&& hfsmp
->hfs_startup_cp
) {
1603 hfs_unlock(hfsmp
->hfs_startup_cp
);
1605 if (flags
& SFL_ATTRIBUTE
&& hfsmp
->hfs_attribute_cp
) {
1606 if (hfsmp
->jnl
== NULL
) {
1607 BTGetLastSync((FCB
*)VTOF(hfsmp
->hfs_attribute_vp
), &lastfsync
);
1608 numOfLockedBuffs
= count_lock_queue();
1609 if ((numOfLockedBuffs
> kMaxLockedMetaBuffers
) ||
1610 ((numOfLockedBuffs
> 1) && ((tv
.tv_sec
- lastfsync
) >
1611 kMaxSecsForFsync
))) {
1612 hfs_btsync(hfsmp
->hfs_attribute_vp
, HFS_SYNCTRANS
);
1615 hfs_unlock(hfsmp
->hfs_attribute_cp
);
1617 if (flags
& SFL_CATALOG
&& hfsmp
->hfs_catalog_cp
) {
1618 if (hfsmp
->jnl
== NULL
) {
1619 BTGetLastSync((FCB
*)VTOF(hfsmp
->hfs_catalog_vp
), &lastfsync
);
1620 numOfLockedBuffs
= count_lock_queue();
1621 if ((numOfLockedBuffs
> kMaxLockedMetaBuffers
) ||
1622 ((numOfLockedBuffs
> 1) && ((tv
.tv_sec
- lastfsync
) >
1623 kMaxSecsForFsync
))) {
1624 hfs_btsync(hfsmp
->hfs_catalog_vp
, HFS_SYNCTRANS
);
1627 hfs_unlock(hfsmp
->hfs_catalog_cp
);
1629 if (flags
& SFL_BITMAP
&& hfsmp
->hfs_allocation_cp
) {
1630 hfs_unlock(hfsmp
->hfs_allocation_cp
);
1632 if (flags
& SFL_EXTENTS
&& hfsmp
->hfs_extents_cp
) {
1633 if (hfsmp
->jnl
== NULL
) {
1634 BTGetLastSync((FCB
*)VTOF(hfsmp
->hfs_extents_vp
), &lastfsync
);
1635 numOfLockedBuffs
= count_lock_queue();
1636 if ((numOfLockedBuffs
> kMaxLockedMetaBuffers
) ||
1637 ((numOfLockedBuffs
> 1) && ((tv
.tv_sec
- lastfsync
) >
1638 kMaxSecsForFsync
))) {
1639 hfs_btsync(hfsmp
->hfs_extents_vp
, HFS_SYNCTRANS
);
1642 hfs_unlock(hfsmp
->hfs_extents_cp
);
1644 if (flags
& SFL_VM_PRIV
) {
1646 * revoke the vm_privilege we granted this thread
1647 * now that we have unlocked the overflow extents
1649 set_vm_privilege(FALSE
);
1658 * Check to see if a vnode is locked in the current context
1659 * This is to be used for debugging purposes only!!
1662 void RequireFileLock(FileReference vp
, int shareable
)
1666 /* The extents btree and allocation bitmap are always exclusive. */
1667 if (VTOC(vp
)->c_fileid
== kHFSExtentsFileID
||
1668 VTOC(vp
)->c_fileid
== kHFSAllocationFileID
) {
1672 locked
= VTOC(vp
)->c_lockowner
== current_thread();
1674 if (!locked
&& !shareable
) {
1675 switch (VTOC(vp
)->c_fileid
) {
1676 case kHFSExtentsFileID
:
1677 panic("hfs: extents btree not locked! v: 0x%08X\n #\n", (u_int
)vp
);
1679 case kHFSCatalogFileID
:
1680 panic("hfs: catalog btree not locked! v: 0x%08X\n #\n", (u_int
)vp
);
1682 case kHFSAllocationFileID
:
1683 /* The allocation file can hide behind the jornal lock. */
1684 if (VTOHFS(vp
)->jnl
== NULL
)
1685 panic("hfs: allocation file not locked! v: 0x%08X\n #\n", (u_int
)vp
);
1687 case kHFSStartupFileID
:
1688 panic("hfs: startup file not locked! v: 0x%08X\n #\n", (u_int
)vp
);
1689 case kHFSAttributesFileID
:
1690 panic("hfs: attributes btree not locked! v: 0x%08X\n #\n", (u_int
)vp
);
1699 * There are three ways to qualify for ownership rights on an object:
1701 * 1. (a) Your UID matches the cnode's UID.
1702 * (b) The object in question is owned by "unknown"
1703 * 2. (a) Permissions on the filesystem are being ignored and
1704 * your UID matches the replacement UID.
1705 * (b) Permissions on the filesystem are being ignored and
1706 * the replacement UID is "unknown".
1711 hfs_owner_rights(struct hfsmount
*hfsmp
, uid_t cnode_uid
, kauth_cred_t cred
,
1712 __unused
struct proc
*p
, int invokesuperuserstatus
)
1714 if ((kauth_cred_getuid(cred
) == cnode_uid
) || /* [1a] */
1715 (cnode_uid
== UNKNOWNUID
) || /* [1b] */
1716 ((((unsigned int)vfs_flags(HFSTOVFS(hfsmp
))) & MNT_UNKNOWNPERMISSIONS
) && /* [2] */
1717 ((kauth_cred_getuid(cred
) == hfsmp
->hfs_uid
) || /* [2a] */
1718 (hfsmp
->hfs_uid
== UNKNOWNUID
))) || /* [2b] */
1719 (invokesuperuserstatus
&& (suser(cred
, 0) == 0))) { /* [3] */
1727 u_int32_t
BestBlockSizeFit(u_int32_t allocationBlockSize
,
1728 u_int32_t blockSizeLimit
,
1729 u_int32_t baseMultiple
) {
1731 Compute the optimal (largest) block size (no larger than allocationBlockSize) that is less than the
1732 specified limit but still an even multiple of the baseMultiple.
1734 int baseBlockCount
, blockCount
;
1735 u_int32_t trialBlockSize
;
1737 if (allocationBlockSize
% baseMultiple
!= 0) {
1739 Whoops: the allocation blocks aren't even multiples of the specified base:
1740 no amount of dividing them into even parts will be a multiple, either then!
1742 return 512; /* Hope for the best */
1745 /* Try the obvious winner first, to prevent 12K allocation blocks, for instance,
1746 from being handled as two 6K logical blocks instead of 3 4K logical blocks.
1747 Even though the former (the result of the loop below) is the larger allocation
1748 block size, the latter is more efficient: */
1749 if (allocationBlockSize
% PAGE_SIZE
== 0) return PAGE_SIZE
;
1751 /* No clear winner exists: pick the largest even fraction <= MAXBSIZE: */
1752 baseBlockCount
= allocationBlockSize
/ baseMultiple
; /* Now guaranteed to be an even multiple */
1754 for (blockCount
= baseBlockCount
; blockCount
> 0; --blockCount
) {
1755 trialBlockSize
= blockCount
* baseMultiple
;
1756 if (allocationBlockSize
% trialBlockSize
== 0) { /* An even multiple? */
1757 if ((trialBlockSize
<= blockSizeLimit
) &&
1758 (trialBlockSize
% baseMultiple
== 0)) {
1759 return trialBlockSize
;
1764 /* Note: we should never get here, since blockCount = 1 should always work,
1765 but this is nice and safe and makes the compiler happy, too ... */
1771 GetFileInfo(ExtendedVCB
*vcb
, __unused u_int32_t dirid
, const char *name
,
1772 struct cat_attr
*fattr
, struct cat_fork
*forkinfo
)
1774 struct hfsmount
* hfsmp
;
1775 struct cat_desc jdesc
;
1779 if (vcb
->vcbSigWord
!= kHFSPlusSigWord
)
1782 hfsmp
= VCBTOHFS(vcb
);
1784 memset(&jdesc
, 0, sizeof(struct cat_desc
));
1785 jdesc
.cd_parentcnid
= kRootDirID
;
1786 jdesc
.cd_nameptr
= (const u_int8_t
*)name
;
1787 jdesc
.cd_namelen
= strlen(name
);
1789 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_SHARED_LOCK
);
1790 error
= cat_lookup(hfsmp
, &jdesc
, 0, 0, NULL
, fattr
, forkinfo
, NULL
);
1791 hfs_systemfile_unlock(hfsmp
, lockflags
);
1794 return (fattr
->ca_fileid
);
1795 } else if (hfsmp
->hfs_flags
& HFS_READ_ONLY
) {
1799 return (0); /* XXX what callers expect on an error */
1804 * On HFS Plus Volumes, there can be orphaned files or directories
1805 * These are files or directories that were unlinked while busy.
1806 * If the volume was not cleanly unmounted then some of these may
1807 * have persisted and need to be removed.
1810 hfs_remove_orphans(struct hfsmount
* hfsmp
)
1812 struct BTreeIterator
* iterator
= NULL
;
1813 struct FSBufferDescriptor btdata
;
1814 struct HFSPlusCatalogFile filerec
;
1815 struct HFSPlusCatalogKey
* keyp
;
1816 struct proc
*p
= current_proc();
1822 cat_cookie_t cookie
;
1825 bool started_tr
= false;
1828 int orphaned_files
= 0;
1829 int orphaned_dirs
= 0;
1831 bzero(&cookie
, sizeof(cookie
));
1833 if (hfsmp
->hfs_flags
& HFS_CLEANED_ORPHANS
)
1836 vcb
= HFSTOVCB(hfsmp
);
1837 fcb
= VTOF(hfsmp
->hfs_catalog_vp
);
1839 btdata
.bufferAddress
= &filerec
;
1840 btdata
.itemSize
= sizeof(filerec
);
1841 btdata
.itemCount
= 1;
1843 iterator
= hfs_mallocz(sizeof(*iterator
));
1845 /* Build a key to "temp" */
1846 keyp
= (HFSPlusCatalogKey
*)&iterator
->key
;
1847 keyp
->parentID
= hfsmp
->hfs_private_desc
[FILE_HARDLINKS
].cd_cnid
;
1848 keyp
->nodeName
.length
= 4; /* "temp" */
1849 keyp
->keyLength
= kHFSPlusCatalogKeyMinimumLength
+ keyp
->nodeName
.length
* 2;
1850 keyp
->nodeName
.unicode
[0] = 't';
1851 keyp
->nodeName
.unicode
[1] = 'e';
1852 keyp
->nodeName
.unicode
[2] = 'm';
1853 keyp
->nodeName
.unicode
[3] = 'p';
1856 * Position the iterator just before the first real temp file/dir.
1858 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_EXCLUSIVE_LOCK
);
1859 (void) BTSearchRecord(fcb
, iterator
, NULL
, NULL
, iterator
);
1860 hfs_systemfile_unlock(hfsmp
, lockflags
);
1862 /* Visit all the temp files/dirs in the HFS+ private directory. */
1864 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_EXCLUSIVE_LOCK
);
1865 result
= BTIterateRecord(fcb
, kBTreeNextRecord
, iterator
, &btdata
, NULL
);
1866 hfs_systemfile_unlock(hfsmp
, lockflags
);
1869 if (keyp
->parentID
!= hfsmp
->hfs_private_desc
[FILE_HARDLINKS
].cd_cnid
)
1872 (void) utf8_encodestr(keyp
->nodeName
.unicode
, keyp
->nodeName
.length
* 2,
1873 (u_int8_t
*)filename
, &namelen
, sizeof(filename
), 0, 0);
1875 (void) snprintf(tempname
, sizeof(tempname
), "%s%d",
1876 HFS_DELETE_PREFIX
, filerec
.fileID
);
1879 * Delete all files (and directories) named "tempxxx",
1880 * where xxx is the file's cnid in decimal.
1883 if (bcmp(tempname
, filename
, namelen
+ 1) != 0)
1886 struct filefork dfork
;
1887 struct filefork rfork
;
1891 bzero(&dfork
, sizeof(dfork
));
1892 bzero(&rfork
, sizeof(rfork
));
1893 bzero(&cnode
, sizeof(cnode
));
1895 if (hfs_start_transaction(hfsmp
) != 0) {
1896 printf("hfs_remove_orphans: failed to start transaction\n");
1902 * Reserve some space in the Catalog file.
1904 if (cat_preflight(hfsmp
, CAT_DELETE
, &cookie
, p
) != 0) {
1905 printf("hfs_remove_orphans: cat_preflight failed\n");
1910 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
| SFL_ATTRIBUTE
| SFL_EXTENTS
| SFL_BITMAP
, HFS_EXCLUSIVE_LOCK
);
1913 /* Build a fake cnode */
1914 cat_convertattr(hfsmp
, (CatalogRecord
*)&filerec
, &cnode
.c_attr
,
1915 &dfork
.ff_data
, &rfork
.ff_data
);
1916 cnode
.c_desc
.cd_parentcnid
= hfsmp
->hfs_private_desc
[FILE_HARDLINKS
].cd_cnid
;
1917 cnode
.c_desc
.cd_nameptr
= (const u_int8_t
*)filename
;
1918 cnode
.c_desc
.cd_namelen
= namelen
;
1919 cnode
.c_desc
.cd_cnid
= cnode
.c_attr
.ca_fileid
;
1920 cnode
.c_blocks
= dfork
.ff_blocks
+ rfork
.ff_blocks
;
1922 /* Position iterator at previous entry */
1923 if (BTIterateRecord(fcb
, kBTreePrevRecord
, iterator
,
1928 /* Truncate the file to zero (both forks) */
1929 if (dfork
.ff_blocks
> 0) {
1932 dfork
.ff_cp
= &cnode
;
1933 cnode
.c_datafork
= &dfork
;
1934 cnode
.c_rsrcfork
= NULL
;
1935 fsize
= (u_int64_t
)dfork
.ff_blocks
* (u_int64_t
)HFSTOVCB(hfsmp
)->blockSize
;
1937 if (fsize
> HFS_BIGFILE_SIZE
) {
1938 fsize
-= HFS_BIGFILE_SIZE
;
1943 if (TruncateFileC(vcb
, (FCB
*)&dfork
, fsize
, 1, 0,
1944 cnode
.c_attr
.ca_fileid
, false) != 0) {
1945 printf("hfs: error truncating data fork!\n");
1950 // if we're iteratively truncating this file down,
1951 // then end the transaction and start a new one so
1952 // that no one transaction gets too big.
1955 /* Drop system file locks before starting
1956 * another transaction to preserve lock order.
1958 hfs_systemfile_unlock(hfsmp
, lockflags
);
1960 hfs_end_transaction(hfsmp
);
1962 if (hfs_start_transaction(hfsmp
) != 0) {
1966 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
| SFL_ATTRIBUTE
| SFL_EXTENTS
| SFL_BITMAP
, HFS_EXCLUSIVE_LOCK
);
1972 if (rfork
.ff_blocks
> 0) {
1973 rfork
.ff_cp
= &cnode
;
1974 cnode
.c_datafork
= NULL
;
1975 cnode
.c_rsrcfork
= &rfork
;
1976 if (TruncateFileC(vcb
, (FCB
*)&rfork
, 0, 1, 1, cnode
.c_attr
.ca_fileid
, false) != 0) {
1977 printf("hfs: error truncating rsrc fork!\n");
1982 // Deal with extended attributes
1983 if (ISSET(cnode
.c_attr
.ca_recflags
, kHFSHasAttributesMask
)) {
1984 // hfs_removeallattr uses its own transactions
1985 hfs_systemfile_unlock(hfsmp
, lockflags
);
1987 hfs_end_transaction(hfsmp
);
1989 hfs_removeallattr(hfsmp
, cnode
.c_attr
.ca_fileid
, &started_tr
);
1992 if (hfs_start_transaction(hfsmp
) != 0) {
1993 printf("hfs_remove_orphans: failed to start transaction\n");
1999 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
| SFL_ATTRIBUTE
| SFL_EXTENTS
| SFL_BITMAP
, HFS_EXCLUSIVE_LOCK
);
2003 /* Remove the file or folder record from the Catalog */
2004 if (cat_delete(hfsmp
, &cnode
.c_desc
, &cnode
.c_attr
) != 0) {
2005 printf("hfs_remove_orphans: error deleting cat rec for id %d!\n", cnode
.c_desc
.cd_cnid
);
2006 hfs_systemfile_unlock(hfsmp
, lockflags
);
2008 hfs_volupdate(hfsmp
, VOL_UPDATE
, 0);
2012 mode
= cnode
.c_attr
.ca_mode
& S_IFMT
;
2014 if (mode
== S_IFDIR
) {
2021 /* Update parent and volume counts */
2022 hfsmp
->hfs_private_attr
[FILE_HARDLINKS
].ca_entries
--;
2023 if (mode
== S_IFDIR
) {
2024 DEC_FOLDERCOUNT(hfsmp
, hfsmp
->hfs_private_attr
[FILE_HARDLINKS
]);
2027 (void)cat_update(hfsmp
, &hfsmp
->hfs_private_desc
[FILE_HARDLINKS
],
2028 &hfsmp
->hfs_private_attr
[FILE_HARDLINKS
], NULL
, NULL
);
2030 /* Drop locks and end the transaction */
2031 hfs_systemfile_unlock(hfsmp
, lockflags
);
2032 cat_postflight(hfsmp
, &cookie
, p
);
2033 catlock
= catreserve
= 0;
2036 Now that Catalog is unlocked, update the volume info, making
2037 sure to differentiate between files and directories
2039 if (mode
== S_IFDIR
) {
2040 hfs_volupdate(hfsmp
, VOL_RMDIR
, 0);
2043 hfs_volupdate(hfsmp
, VOL_RMFILE
, 0);
2046 hfs_end_transaction(hfsmp
);
2052 if (orphaned_files
> 0 || orphaned_dirs
> 0)
2053 printf("hfs: Removed %d orphaned / unlinked files and %d directories \n", orphaned_files
, orphaned_dirs
);
2055 hfs_systemfile_unlock(hfsmp
, lockflags
);
2058 cat_postflight(hfsmp
, &cookie
, p
);
2061 hfs_end_transaction(hfsmp
);
2064 hfs_free(iterator
, sizeof(*iterator
));
2065 hfsmp
->hfs_flags
|= HFS_CLEANED_ORPHANS
;
2070 * This will return the correct logical block size for a given vnode.
2071 * For most files, it is the allocation block size, for meta data like
2072 * BTrees, this is kept as part of the BTree private nodeSize
2075 GetLogicalBlockSize(struct vnode
*vp
)
2077 u_int32_t logBlockSize
;
2079 hfs_assert(vp
!= NULL
);
2081 /* start with default */
2082 logBlockSize
= VTOHFS(vp
)->hfs_logBlockSize
;
2084 if (vnode_issystem(vp
)) {
2085 if (VTOF(vp
)->fcbBTCBPtr
!= NULL
) {
2086 BTreeInfoRec bTreeInfo
;
2089 * We do not lock the BTrees, because if we are getting block..then the tree
2090 * should be locked in the first place.
2091 * We just want the nodeSize wich will NEVER change..so even if the world
2092 * is changing..the nodeSize should remain the same. Which argues why lock
2093 * it in the first place??
2096 (void) BTGetInformation (VTOF(vp
), kBTreeInfoVersion
, &bTreeInfo
);
2098 logBlockSize
= bTreeInfo
.nodeSize
;
2100 } else if (VTOC(vp
)->c_fileid
== kHFSAllocationFileID
) {
2101 logBlockSize
= VTOVCB(vp
)->vcbVBMIOSize
;
2105 hfs_assert(logBlockSize
> 0);
2107 return logBlockSize
;
2111 static bool hfs_get_backing_free_blks(hfsmount_t
*hfsmp
, uint64_t *pfree_blks
)
2113 struct vfsstatfs
*vfsp
; /* 272 bytes */
2117 hfs_lock_mount(hfsmp
);
2119 vnode_t backing_vp
= hfsmp
->hfs_backingvp
;
2121 hfs_unlock_mount(hfsmp
);
2125 // usecount is not enough; we need iocount
2126 if (vnode_get(backing_vp
)) {
2127 hfs_unlock_mount(hfsmp
);
2132 uint32_t loanedblks
= hfsmp
->loanedBlocks
+ hfsmp
->lockedBlocks
;
2133 uint32_t bandblks
= hfsmp
->hfs_sparsebandblks
;
2134 uint64_t maxblks
= hfsmp
->hfs_backingfs_maxblocks
;
2136 hfs_unlock_mount(hfsmp
);
2138 mount_t backingfs_mp
= vnode_mount(backing_vp
);
2141 if ((now
.tv_sec
- hfsmp
->hfs_last_backingstatfs
) >= 1) {
2142 vfs_update_vfsstat(backingfs_mp
, vfs_context_kernel(), VFS_KERNEL_EVENT
);
2143 hfsmp
->hfs_last_backingstatfs
= now
.tv_sec
;
2146 if (!(vfsp
= vfs_statfs(backingfs_mp
))) {
2147 vnode_put(backing_vp
);
2151 vfreeblks
= vfsp
->f_bavail
;
2152 /* Normalize block count if needed. */
2153 if (vfsp
->f_bsize
!= hfsmp
->blockSize
)
2154 vfreeblks
= vfreeblks
* vfsp
->f_bsize
/ hfsmp
->blockSize
;
2155 if (vfreeblks
> bandblks
)
2156 vfreeblks
-= bandblks
;
2161 * Take into account any delayed allocations. It is not
2162 * certain what the original reason for the "2 *" is. Most
2163 * likely it is to allow for additional requirements in the
2164 * host file system and metadata required by disk images. The
2165 * number of loaned blocks is likely to be small and we will
2166 * stop using them as we get close to the limit.
2168 loanedblks
= 2 * loanedblks
;
2169 if (vfreeblks
> loanedblks
)
2170 vfreeblks
-= loanedblks
;
2175 vfreeblks
= MIN(vfreeblks
, maxblks
);
2177 vnode_put(backing_vp
);
2179 *pfree_blks
= vfreeblks
;
2186 hfs_free_cnids(struct hfsmount
* hfsmp
)
2188 return HFS_MAX_FILES
- hfsmp
->hfs_filecount
- hfsmp
->hfs_dircount
;
2192 hfs_freeblks(struct hfsmount
* hfsmp
, int wantreserve
)
2199 * We don't bother taking the mount lock
2200 * to look at these values since the values
2201 * themselves are each updated atomically
2202 * on aligned addresses.
2204 freeblks
= hfsmp
->freeBlocks
;
2205 rsrvblks
= hfsmp
->reserveBlocks
;
2206 loanblks
= hfsmp
->loanedBlocks
+ hfsmp
->lockedBlocks
;
2208 if (freeblks
> rsrvblks
)
2209 freeblks
-= rsrvblks
;
2213 if (freeblks
> loanblks
)
2214 freeblks
-= loanblks
;
2220 * When the underlying device is sparse, check the
2221 * available space on the backing store volume.
2224 if (hfs_get_backing_free_blks(hfsmp
, &vfreeblks
))
2225 freeblks
= MIN(freeblks
, vfreeblks
);
2226 #endif /* HFS_SPARSE_DEV */
2232 * Map HFS Common errors (negative) to BSD error codes (positive).
2233 * Positive errors (ie BSD errors) are passed through unchanged.
2235 short MacToVFSError(OSErr err
)
2240 /* BSD/VFS internal errnos */
2242 case HFS_ERESERVEDNAME
: /* -8 */
2247 case dskFulErr
: /* -34 */
2248 case btNoSpaceAvail
: /* -32733 */
2250 case fxOvFlErr
: /* -32750 */
2253 case btBadNode
: /* -32731 */
2256 case memFullErr
: /* -108 */
2257 return ENOMEM
; /* +12 */
2259 case cmExists
: /* -32718 */
2260 case btExists
: /* -32734 */
2261 return EEXIST
; /* +17 */
2263 case cmNotFound
: /* -32719 */
2264 case btNotFound
: /* -32735 */
2265 return ENOENT
; /* 28 */
2267 case cmNotEmpty
: /* -32717 */
2268 return ENOTEMPTY
; /* 66 */
2270 case cmFThdDirErr
: /* -32714 */
2271 return EISDIR
; /* 21 */
2273 case fxRangeErr
: /* -32751 */
2276 case bdNamErr
: /* -37 */
2277 return ENAMETOOLONG
; /* 63 */
2279 case paramErr
: /* -50 */
2280 case fileBoundsErr
: /* -1309 */
2281 return EINVAL
; /* +22 */
2283 case fsBTBadNodeSize
:
2287 return EIO
; /* +5 */
2293 * Find the current thread's directory hint for a given index.
2295 * Requires an exclusive lock on directory cnode.
2297 * Use detach if the cnode lock must be dropped while the hint is still active.
2300 hfs_getdirhint(struct cnode
*dcp
, int index
, int detach
)
2303 directoryhint_t
*hint
;
2304 boolean_t need_remove
, need_init
;
2305 const u_int8_t
* name
;
2310 * Look for an existing hint first. If not found, create a new one (when
2311 * the list is not full) or recycle the oldest hint. Since new hints are
2312 * always added to the head of the list, the last hint is always the
2315 TAILQ_FOREACH(hint
, &dcp
->c_hintlist
, dh_link
) {
2316 if (hint
->dh_index
== index
)
2319 if (hint
!= NULL
) { /* found an existing hint */
2322 } else { /* cannot find an existing hint */
2324 if (dcp
->c_dirhintcnt
< HFS_MAXDIRHINTS
) { /* we don't need recycling */
2325 /* Create a default directory hint */
2326 hint
= hfs_malloc(sizeof(directoryhint_t
));
2327 ++dcp
->c_dirhintcnt
;
2328 need_remove
= false;
2329 } else { /* recycle the last (i.e., the oldest) hint */
2330 hint
= TAILQ_LAST(&dcp
->c_hintlist
, hfs_hinthead
);
2331 if ((hint
->dh_desc
.cd_flags
& CD_HASBUF
) &&
2332 (name
= hint
->dh_desc
.cd_nameptr
)) {
2333 hint
->dh_desc
.cd_nameptr
= NULL
;
2334 hint
->dh_desc
.cd_namelen
= 0;
2335 hint
->dh_desc
.cd_flags
&= ~CD_HASBUF
;
2336 vfs_removename((const char *)name
);
2343 TAILQ_REMOVE(&dcp
->c_hintlist
, hint
, dh_link
);
2346 --dcp
->c_dirhintcnt
;
2348 TAILQ_INSERT_HEAD(&dcp
->c_hintlist
, hint
, dh_link
);
2351 hint
->dh_index
= index
;
2352 hint
->dh_desc
.cd_flags
= 0;
2353 hint
->dh_desc
.cd_encoding
= 0;
2354 hint
->dh_desc
.cd_namelen
= 0;
2355 hint
->dh_desc
.cd_nameptr
= NULL
;
2356 hint
->dh_desc
.cd_parentcnid
= dcp
->c_fileid
;
2357 hint
->dh_desc
.cd_hint
= dcp
->c_childhint
;
2358 hint
->dh_desc
.cd_cnid
= 0;
2360 hint
->dh_time
= tv
.tv_sec
;
2365 * Release a single directory hint.
2367 * Requires an exclusive lock on directory cnode.
2370 hfs_reldirhint(struct cnode
*dcp
, directoryhint_t
* relhint
)
2372 const u_int8_t
* name
;
2373 directoryhint_t
*hint
;
2375 /* Check if item is on list (could be detached) */
2376 TAILQ_FOREACH(hint
, &dcp
->c_hintlist
, dh_link
) {
2377 if (hint
== relhint
) {
2378 TAILQ_REMOVE(&dcp
->c_hintlist
, relhint
, dh_link
);
2379 --dcp
->c_dirhintcnt
;
2383 name
= relhint
->dh_desc
.cd_nameptr
;
2384 if ((relhint
->dh_desc
.cd_flags
& CD_HASBUF
) && (name
!= NULL
)) {
2385 relhint
->dh_desc
.cd_nameptr
= NULL
;
2386 relhint
->dh_desc
.cd_namelen
= 0;
2387 relhint
->dh_desc
.cd_flags
&= ~CD_HASBUF
;
2388 vfs_removename((const char *)name
);
2390 hfs_free(relhint
, sizeof(*relhint
));
2394 * Release directory hints for given directory
2396 * Requires an exclusive lock on directory cnode.
2399 hfs_reldirhints(struct cnode
*dcp
, int stale_hints_only
)
2402 directoryhint_t
*hint
, *prev
;
2403 const u_int8_t
* name
;
2405 if (stale_hints_only
)
2408 /* searching from the oldest to the newest, so we can stop early when releasing stale hints only */
2409 for (hint
= TAILQ_LAST(&dcp
->c_hintlist
, hfs_hinthead
); hint
!= NULL
; hint
= prev
) {
2410 if (stale_hints_only
&& (tv
.tv_sec
- hint
->dh_time
) < HFS_DIRHINT_TTL
)
2411 break; /* stop here if this entry is too new */
2412 name
= hint
->dh_desc
.cd_nameptr
;
2413 if ((hint
->dh_desc
.cd_flags
& CD_HASBUF
) && (name
!= NULL
)) {
2414 hint
->dh_desc
.cd_nameptr
= NULL
;
2415 hint
->dh_desc
.cd_namelen
= 0;
2416 hint
->dh_desc
.cd_flags
&= ~CD_HASBUF
;
2417 vfs_removename((const char *)name
);
2419 prev
= TAILQ_PREV(hint
, hfs_hinthead
, dh_link
); /* must save this pointer before calling FREE_ZONE on this node */
2420 TAILQ_REMOVE(&dcp
->c_hintlist
, hint
, dh_link
);
2421 hfs_free(hint
, sizeof(*hint
));
2422 --dcp
->c_dirhintcnt
;
2427 * Insert a detached directory hint back into the list of dirhints.
2429 * Requires an exclusive lock on directory cnode.
2432 hfs_insertdirhint(struct cnode
*dcp
, directoryhint_t
* hint
)
2434 directoryhint_t
*test
;
2436 TAILQ_FOREACH(test
, &dcp
->c_hintlist
, dh_link
) {
2438 panic("hfs_insertdirhint: hint %p already on list!", hint
);
2441 TAILQ_INSERT_HEAD(&dcp
->c_hintlist
, hint
, dh_link
);
2442 ++dcp
->c_dirhintcnt
;
2446 * Perform a case-insensitive compare of two UTF-8 filenames.
2448 * Returns 0 if the strings match.
2451 hfs_namecmp(const u_int8_t
*str1
, size_t len1
, const u_int8_t
*str2
, size_t len2
)
2453 u_int16_t
*ustr1
, *ustr2
;
2454 size_t ulen1
, ulen2
;
2461 maxbytes
= kHFSPlusMaxFileNameChars
<< 1;
2462 ustr1
= hfs_malloc(maxbytes
<< 1);
2463 ustr2
= ustr1
+ (maxbytes
>> 1);
2465 if (utf8_decodestr(str1
, len1
, ustr1
, &ulen1
, maxbytes
, ':', 0) != 0)
2467 if (utf8_decodestr(str2
, len2
, ustr2
, &ulen2
, maxbytes
, ':', 0) != 0)
2470 cmp
= FastUnicodeCompare(ustr1
, ulen1
>>1, ustr2
, ulen2
>>1);
2472 hfs_free(ustr1
, maxbytes
<< 1);
2476 typedef struct jopen_cb_info
{
2487 journal_open_cb(const char *bsd_dev_name
, const char *uuid_str
, void *arg
)
2489 jopen_cb_info
*ji
= (jopen_cb_info
*)arg
;
2493 strlcpy(&bsd_name
[0], "/dev/", sizeof(bsd_name
));
2494 strlcpy(&bsd_name
[5], bsd_dev_name
, sizeof(bsd_name
)-5);
2496 if ((error
= vnode_lookup(bsd_name
, VNODE_LOOKUP_NOFOLLOW
, &ji
->jvp
,
2497 vfs_context_kernel()))) {
2498 printf("hfs: journal open cb: error %d looking up device %s (dev uuid %s)\n", error
, bsd_name
, uuid_str
);
2499 return 1; // keep iterating
2502 struct vnop_open_args oargs
= {
2504 .a_mode
= FREAD
| FWRITE
,
2505 .a_context
= vfs_context_kernel(),
2508 if (spec_open(&oargs
)) {
2514 // if the journal is dirty and we didn't specify a desired
2515 // journal device uuid, then do not use the journal. but
2516 // if the journal is just invalid (e.g. it hasn't been
2517 // initialized) then just set the need_init flag.
2518 if (ji
->need_clean
&& ji
->desired_uuid
&& ji
->desired_uuid
[0] == '\0') {
2519 error
= journal_is_clean(ji
->jvp
, 0, ji
->jsize
,
2520 (void *)1, ji
->blksize
);
2521 if (error
== EBUSY
) {
2522 struct vnop_close_args cargs
= {
2524 .a_fflag
= FREAD
| FWRITE
,
2525 .a_context
= vfs_context_kernel()
2530 return 1; // keep iterating
2531 } else if (error
== EINVAL
) {
2536 if (ji
->desired_uuid
&& ji
->desired_uuid
[0] == '\0') {
2537 strlcpy(ji
->desired_uuid
, uuid_str
, 128);
2539 vnode_setmountedon(ji
->jvp
);
2540 return 0; // stop iterating
2544 open_journal_dev(mount_t mp
,
2545 const char *vol_device
,
2548 char *machine_serial_num
,
2553 int retry_counter
=0;
2558 ji
.desired_uuid
= uuid_str
;
2560 ji
.blksize
= blksize
;
2561 ji
.need_clean
= need_clean
;
2564 // if (uuid_str[0] == '\0') {
2565 // printf("hfs: open journal dev: %s: locating any available non-dirty external journal partition\n", vol_device);
2567 // printf("hfs: open journal dev: %s: trying to find the external journal partition w/uuid %s\n", vol_device, uuid_str);
2569 while (ji
.jvp
== NULL
&& retry_counter
++ < 4) {
2570 if (retry_counter
> 1) {
2572 printf("hfs: open_journal_dev: uuid %s not found. waiting 10sec.\n", uuid_str
);
2574 printf("hfs: open_journal_dev: no available external journal partition found. waiting 10sec.\n");
2576 delay_for_interval(10* 1000000, NSEC_PER_USEC
); // wait for ten seconds and then try again
2579 hfs_iterate_media_with_content(EXTJNL_CONTENT_TYPE_UUID
,
2580 journal_open_cb
, &ji
);
2583 if (ji
.jvp
== NULL
) {
2584 printf("hfs: volume: %s: did not find jnl device uuid: %s from machine serial number: %s\n",
2585 vol_device
, uuid_str
, machine_serial_num
);
2588 *need_init
= ji
.need_init
;
2593 void hfs_close_jvp(hfsmount_t
*hfsmp
)
2595 if (!hfsmp
|| !hfsmp
->jvp
|| hfsmp
->jvp
== hfsmp
->hfs_devvp
)
2598 vnode_clearmountedon(hfsmp
->jvp
);
2599 struct vnop_close_args cargs
= {
2601 .a_fflag
= FREAD
| FWRITE
,
2602 .a_context
= vfs_context_kernel()
2605 vnode_put(hfsmp
->jvp
);
2610 hfs_early_journal_init(struct hfsmount
*hfsmp
, HFSPlusVolumeHeader
*vhp
,
2611 void *_args
, off_t embeddedOffset
, daddr64_t mdb_offset
,
2612 HFSMasterDirectoryBlock
*mdbp
, kauth_cred_t cred
)
2614 JournalInfoBlock
*jibp
;
2615 struct buf
*jinfo_bp
, *bp
;
2616 int sectors_per_fsblock
, arg_flags
=0, arg_tbufsz
=0;
2617 int retval
, write_jibp
= 0;
2618 uint32_t blksize
= hfsmp
->hfs_logical_block_size
;
2619 struct vnode
*devvp
;
2620 struct hfs_mount_args
*args
= _args
;
2621 u_int32_t jib_flags
;
2622 u_int64_t jib_offset
;
2624 const char *dev_name
;
2626 devvp
= hfsmp
->hfs_devvp
;
2627 dev_name
= vnode_getname_printable(devvp
);
2629 if (args
!= NULL
&& (args
->flags
& HFSFSMNT_EXTENDED_ARGS
)) {
2630 arg_flags
= args
->journal_flags
;
2631 arg_tbufsz
= args
->journal_tbuffer_size
;
2634 sectors_per_fsblock
= SWAP_BE32(vhp
->blockSize
) / blksize
;
2637 retval
= (int)buf_meta_bread(devvp
,
2638 (daddr64_t
)((embeddedOffset
/blksize
) +
2639 ((u_int64_t
)SWAP_BE32(vhp
->journalInfoBlock
)*sectors_per_fsblock
)),
2640 hfsmp
->hfs_physical_block_size
, cred
, &jinfo_bp
);
2643 buf_brelse(jinfo_bp
);
2645 goto cleanup_dev_name
;
2648 jibp
= (JournalInfoBlock
*)buf_dataptr(jinfo_bp
);
2649 jib_flags
= SWAP_BE32(jibp
->flags
);
2650 jib_size
= SWAP_BE64(jibp
->size
);
2652 if (jib_flags
& kJIJournalInFSMask
) {
2653 hfsmp
->jvp
= hfsmp
->hfs_devvp
;
2654 jib_offset
= SWAP_BE64(jibp
->offset
);
2658 // if the volume was unmounted cleanly then we'll pick any
2659 // available external journal partition
2661 if (SWAP_BE32(vhp
->attributes
) & kHFSVolumeUnmountedMask
) {
2662 *((char *)&jibp
->ext_jnl_uuid
[0]) = '\0';
2665 hfsmp
->jvp
= open_journal_dev(hfsmp
->hfs_mp
,
2667 !(jib_flags
& kJIJournalNeedInitMask
),
2668 (char *)&jibp
->ext_jnl_uuid
[0],
2669 (char *)&jibp
->machine_serial_num
[0],
2671 hfsmp
->hfs_logical_block_size
,
2673 if (hfsmp
->jvp
== NULL
) {
2674 buf_brelse(jinfo_bp
);
2676 goto cleanup_dev_name
;
2678 if (hfs_get_platform_serial_number(&jibp
->machine_serial_num
[0], sizeof(jibp
->machine_serial_num
)) != KERN_SUCCESS
) {
2679 strlcpy(&jibp
->machine_serial_num
[0], "unknown-machine-uuid", sizeof(jibp
->machine_serial_num
));
2686 jib_flags
|= kJIJournalNeedInitMask
;
2690 // save this off for the hack-y check in hfs_remove()
2691 hfsmp
->jnl_start
= jib_offset
/ SWAP_BE32(vhp
->blockSize
);
2692 hfsmp
->jnl_size
= jib_size
;
2694 if ((hfsmp
->hfs_flags
& HFS_READ_ONLY
) && (vfs_flags(hfsmp
->hfs_mp
) & MNT_ROOTFS
) == 0) {
2695 // if the file system is read-only, check if the journal is empty.
2696 // if it is, then we can allow the mount. otherwise we have to
2698 retval
= journal_is_clean(hfsmp
->jvp
,
2699 jib_offset
+ embeddedOffset
,
2702 hfsmp
->hfs_logical_block_size
);
2706 buf_brelse(jinfo_bp
);
2709 const char *name
= vnode_getname_printable(devvp
);
2710 printf("hfs: early journal init: volume on %s is read-only and journal is dirty. Can not mount volume.\n",
2712 vnode_putname_printable(name
);
2715 goto cleanup_dev_name
;
2718 if (jib_flags
& kJIJournalNeedInitMask
) {
2719 printf("hfs: Initializing the journal (joffset 0x%llx sz 0x%llx)...\n",
2720 jib_offset
+ embeddedOffset
, jib_size
);
2721 hfsmp
->jnl
= journal_create(hfsmp
->jvp
,
2722 jib_offset
+ embeddedOffset
,
2728 hfs_sync_metadata
, hfsmp
->hfs_mp
,
2731 journal_trim_set_callback(hfsmp
->jnl
, hfs_trim_callback
, hfsmp
);
2733 // no need to start a transaction here... if this were to fail
2734 // we'd just re-init it on the next mount.
2735 jib_flags
&= ~kJIJournalNeedInitMask
;
2736 jibp
->flags
= SWAP_BE32(jib_flags
);
2737 buf_bwrite(jinfo_bp
);
2741 //printf("hfs: Opening the journal (joffset 0x%llx sz 0x%llx vhp_blksize %d)...\n",
2742 // jib_offset + embeddedOffset,
2743 // jib_size, SWAP_BE32(vhp->blockSize));
2745 hfsmp
->jnl
= journal_open(hfsmp
->jvp
,
2746 jib_offset
+ embeddedOffset
,
2752 hfs_sync_metadata
, hfsmp
->hfs_mp
,
2755 journal_trim_set_callback(hfsmp
->jnl
, hfs_trim_callback
, hfsmp
);
2758 buf_bwrite(jinfo_bp
);
2760 buf_brelse(jinfo_bp
);
2765 if (hfsmp
->jnl
&& mdbp
) {
2766 // reload the mdb because it could have changed
2767 // if the journal had to be replayed.
2768 if (mdb_offset
== 0) {
2769 mdb_offset
= (daddr64_t
)((embeddedOffset
/ blksize
) + HFS_PRI_SECTOR(blksize
));
2772 retval
= (int)buf_meta_bread(devvp
,
2773 HFS_PHYSBLK_ROUNDDOWN(mdb_offset
, hfsmp
->hfs_log_per_phys
),
2774 hfsmp
->hfs_physical_block_size
, cred
, &bp
);
2779 printf("hfs: failed to reload the mdb after opening the journal (retval %d)!\n",
2781 goto cleanup_dev_name
;
2783 bcopy((char *)buf_dataptr(bp
) + HFS_PRI_OFFSET(hfsmp
->hfs_physical_block_size
), mdbp
, 512);
2789 // if we expected the journal to be there and we couldn't
2790 // create it or open it then we have to bail out.
2791 if (hfsmp
->jnl
== NULL
) {
2792 printf("hfs: early jnl init: failed to open/create the journal (retval %d).\n", retval
);
2794 goto cleanup_dev_name
;
2800 vnode_putname_printable(dev_name
);
2806 // This function will go and re-locate the .journal_info_block and
2807 // the .journal files in case they moved (which can happen if you
2808 // run Norton SpeedDisk). If we fail to find either file we just
2809 // disable journaling for this volume and return. We turn off the
2810 // journaling bit in the vcb and assume it will get written to disk
2811 // later (if it doesn't on the next mount we'd do the same thing
2812 // again which is harmless). If we disable journaling we don't
2813 // return an error so that the volume is still mountable.
2815 // If the info we find for the .journal_info_block and .journal files
2816 // isn't what we had stored, we re-set our cached info and proceed
2817 // with opening the journal normally.
2820 hfs_late_journal_init(struct hfsmount
*hfsmp
, HFSPlusVolumeHeader
*vhp
, void *_args
)
2822 JournalInfoBlock
*jibp
;
2823 struct buf
*jinfo_bp
;
2824 int sectors_per_fsblock
, arg_flags
=0, arg_tbufsz
=0;
2825 int retval
, write_jibp
= 0, recreate_journal
= 0;
2826 struct vnode
*devvp
;
2827 struct cat_attr jib_attr
, jattr
;
2828 struct cat_fork jib_fork
, jfork
;
2831 struct hfs_mount_args
*args
= _args
;
2832 u_int32_t jib_flags
;
2833 u_int64_t jib_offset
;
2836 devvp
= hfsmp
->hfs_devvp
;
2837 vcb
= HFSTOVCB(hfsmp
);
2839 if (args
!= NULL
&& (args
->flags
& HFSFSMNT_EXTENDED_ARGS
)) {
2840 if (args
->journal_disable
) {
2844 arg_flags
= args
->journal_flags
;
2845 arg_tbufsz
= args
->journal_tbuffer_size
;
2848 fid
= GetFileInfo(vcb
, kRootDirID
, ".journal_info_block", &jib_attr
, &jib_fork
);
2849 if (fid
== 0 || jib_fork
.cf_extents
[0].startBlock
== 0 || jib_fork
.cf_size
== 0) {
2850 printf("hfs: can't find the .journal_info_block! disabling journaling (start: %d).\n",
2851 fid
? jib_fork
.cf_extents
[0].startBlock
: 0);
2852 vcb
->vcbAtrb
&= ~kHFSVolumeJournaledMask
;
2855 hfsmp
->hfs_jnlinfoblkid
= fid
;
2857 // make sure the journal_info_block begins where we think it should.
2858 if (SWAP_BE32(vhp
->journalInfoBlock
) != jib_fork
.cf_extents
[0].startBlock
) {
2859 printf("hfs: The journal_info_block moved (was: %d; is: %d). Fixing up\n",
2860 SWAP_BE32(vhp
->journalInfoBlock
), jib_fork
.cf_extents
[0].startBlock
);
2862 vcb
->vcbJinfoBlock
= jib_fork
.cf_extents
[0].startBlock
;
2863 vhp
->journalInfoBlock
= SWAP_BE32(jib_fork
.cf_extents
[0].startBlock
);
2864 recreate_journal
= 1;
2868 sectors_per_fsblock
= SWAP_BE32(vhp
->blockSize
) / hfsmp
->hfs_logical_block_size
;
2870 retval
= (int)buf_meta_bread(devvp
,
2871 (vcb
->hfsPlusIOPosOffset
/ hfsmp
->hfs_logical_block_size
+
2872 ((u_int64_t
)SWAP_BE32(vhp
->journalInfoBlock
)*sectors_per_fsblock
)),
2873 hfsmp
->hfs_physical_block_size
, NOCRED
, &jinfo_bp
);
2876 buf_brelse(jinfo_bp
);
2878 printf("hfs: can't read journal info block. disabling journaling.\n");
2879 vcb
->vcbAtrb
&= ~kHFSVolumeJournaledMask
;
2883 jibp
= (JournalInfoBlock
*)buf_dataptr(jinfo_bp
);
2884 jib_flags
= SWAP_BE32(jibp
->flags
);
2885 jib_offset
= SWAP_BE64(jibp
->offset
);
2886 jib_size
= SWAP_BE64(jibp
->size
);
2888 fid
= GetFileInfo(vcb
, kRootDirID
, ".journal", &jattr
, &jfork
);
2889 if (fid
== 0 || jfork
.cf_extents
[0].startBlock
== 0 || jfork
.cf_size
== 0) {
2890 printf("hfs: can't find the journal file! disabling journaling (start: %d)\n",
2891 fid
? jfork
.cf_extents
[0].startBlock
: 0);
2892 buf_brelse(jinfo_bp
);
2893 vcb
->vcbAtrb
&= ~kHFSVolumeJournaledMask
;
2896 hfsmp
->hfs_jnlfileid
= fid
;
2898 // make sure the journal file begins where we think it should.
2899 if ((jib_flags
& kJIJournalInFSMask
) && (jib_offset
/ (u_int64_t
)vcb
->blockSize
) != jfork
.cf_extents
[0].startBlock
) {
2900 printf("hfs: The journal file moved (was: %lld; is: %d). Fixing up\n",
2901 (jib_offset
/ (u_int64_t
)vcb
->blockSize
), jfork
.cf_extents
[0].startBlock
);
2903 jib_offset
= (u_int64_t
)jfork
.cf_extents
[0].startBlock
* (u_int64_t
)vcb
->blockSize
;
2905 recreate_journal
= 1;
2908 // check the size of the journal file.
2909 if (jib_size
!= (u_int64_t
)jfork
.cf_extents
[0].blockCount
*vcb
->blockSize
) {
2910 printf("hfs: The journal file changed size! (was %lld; is %lld). Fixing up.\n",
2911 jib_size
, (u_int64_t
)jfork
.cf_extents
[0].blockCount
*vcb
->blockSize
);
2913 jib_size
= (u_int64_t
)jfork
.cf_extents
[0].blockCount
* vcb
->blockSize
;
2915 recreate_journal
= 1;
2918 if (jib_flags
& kJIJournalInFSMask
) {
2919 hfsmp
->jvp
= hfsmp
->hfs_devvp
;
2920 jib_offset
+= (off_t
)vcb
->hfsPlusIOPosOffset
;
2922 const char *dev_name
;
2925 dev_name
= vnode_getname_printable(devvp
);
2927 // since the journal is empty, just use any available external journal
2928 *((char *)&jibp
->ext_jnl_uuid
[0]) = '\0';
2930 // this fills in the uuid of the device we actually get
2931 hfsmp
->jvp
= open_journal_dev(hfsmp
->hfs_mp
,
2933 !(jib_flags
& kJIJournalNeedInitMask
),
2934 (char *)&jibp
->ext_jnl_uuid
[0],
2935 (char *)&jibp
->machine_serial_num
[0],
2937 hfsmp
->hfs_logical_block_size
,
2939 if (hfsmp
->jvp
== NULL
) {
2940 buf_brelse(jinfo_bp
);
2941 vnode_putname_printable(dev_name
);
2944 if (hfs_get_platform_serial_number(&jibp
->machine_serial_num
[0], sizeof(jibp
->machine_serial_num
)) != KERN_SUCCESS
) {
2945 strlcpy(&jibp
->machine_serial_num
[0], "unknown-machine-serial-num", sizeof(jibp
->machine_serial_num
));
2949 recreate_journal
= 1;
2952 jib_flags
|= kJIJournalNeedInitMask
;
2954 vnode_putname_printable(dev_name
);
2957 // save this off for the hack-y check in hfs_remove()
2958 hfsmp
->jnl_start
= jib_offset
/ SWAP_BE32(vhp
->blockSize
);
2959 hfsmp
->jnl_size
= jib_size
;
2961 if ((hfsmp
->hfs_flags
& HFS_READ_ONLY
) && (vfs_flags(hfsmp
->hfs_mp
) & MNT_ROOTFS
) == 0) {
2962 // if the file system is read-only, check if the journal is empty.
2963 // if it is, then we can allow the mount. otherwise we have to
2965 retval
= journal_is_clean(hfsmp
->jvp
,
2969 hfsmp
->hfs_logical_block_size
);
2973 buf_brelse(jinfo_bp
);
2976 const char *name
= vnode_getname_printable(devvp
);
2977 printf("hfs: late journal init: volume on %s is read-only and journal is dirty. Can not mount volume.\n",
2979 vnode_putname_printable(name
);
2985 if ((jib_flags
& kJIJournalNeedInitMask
) || recreate_journal
) {
2986 printf("hfs: Initializing the journal (joffset 0x%llx sz 0x%llx)...\n",
2987 jib_offset
, jib_size
);
2988 hfsmp
->jnl
= journal_create(hfsmp
->jvp
,
2992 hfsmp
->hfs_logical_block_size
,
2995 hfs_sync_metadata
, hfsmp
->hfs_mp
,
2998 journal_trim_set_callback(hfsmp
->jnl
, hfs_trim_callback
, hfsmp
);
3000 // no need to start a transaction here... if this were to fail
3001 // we'd just re-init it on the next mount.
3002 jib_flags
&= ~kJIJournalNeedInitMask
;
3007 // if we weren't the last person to mount this volume
3008 // then we need to throw away the journal because it
3009 // is likely that someone else mucked with the disk.
3010 // if the journal is empty this is no big deal. if the
3011 // disk is dirty this prevents us from replaying the
3012 // journal over top of changes that someone else made.
3014 arg_flags
|= JOURNAL_RESET
;
3016 //printf("hfs: Opening the journal (joffset 0x%llx sz 0x%llx vhp_blksize %d)...\n",
3018 // jib_size, SWAP_BE32(vhp->blockSize));
3020 hfsmp
->jnl
= journal_open(hfsmp
->jvp
,
3024 hfsmp
->hfs_logical_block_size
,
3027 hfs_sync_metadata
, hfsmp
->hfs_mp
,
3030 journal_trim_set_callback(hfsmp
->jnl
, hfs_trim_callback
, hfsmp
);
3035 jibp
->flags
= SWAP_BE32(jib_flags
);
3036 jibp
->offset
= SWAP_BE64(jib_offset
);
3037 jibp
->size
= SWAP_BE64(jib_size
);
3039 buf_bwrite(jinfo_bp
);
3041 buf_brelse(jinfo_bp
);
3046 // if we expected the journal to be there and we couldn't
3047 // create it or open it then we have to bail out.
3048 if (hfsmp
->jnl
== NULL
) {
3049 printf("hfs: late jnl init: failed to open/create the journal (retval %d).\n", retval
);
3057 * Calculate the allocation zone for metadata.
3059 * This zone includes the following:
3060 * Allocation Bitmap file
3061 * Overflow Extents file
3064 * Clustered Hot files
3067 * METADATA ALLOCATION ZONE
3068 * ____________________________________________________________________________
3070 * | BM | JF | OEF | CATALOG |---> | HOT FILES |
3071 * |____|____|_____|_______________|______________________________|___________|
3073 * <------------------------------- N * 128 MB ------------------------------->
3076 #define GIGABYTE (u_int64_t)(1024*1024*1024)
3078 #define HOTBAND_MINIMUM_SIZE (10*1024*1024)
3079 #define HOTBAND_MAXIMUM_SIZE (512*1024*1024)
3081 /* Initialize the metadata zone.
3083 * If the size of the volume is less than the minimum size for
3084 * metadata zone, metadata zone is disabled.
3086 * If disable is true, disable metadata zone unconditionally.
3089 hfs_metadatazone_init(struct hfsmount
*hfsmp
, int disable
)
3097 int items
, really_do_it
=1;
3099 vcb
= HFSTOVCB(hfsmp
);
3100 fs_size
= (u_int64_t
)vcb
->blockSize
* (u_int64_t
)vcb
->allocLimit
;
3103 * For volumes less than 10 GB, don't bother.
3105 if (fs_size
< ((u_int64_t
)10 * GIGABYTE
)) {
3110 * Skip non-journaled volumes as well.
3112 if (hfsmp
->jnl
== NULL
) {
3116 /* If caller wants to disable metadata zone, do it */
3117 if (disable
== true) {
3122 * Start with space for the boot blocks and Volume Header.
3123 * 1536 = byte offset from start of volume to end of volume header:
3124 * 1024 bytes is the offset from the start of the volume to the
3125 * start of the volume header (defined by the volume format)
3126 * + 512 bytes (the size of the volume header).
3128 zonesize
= roundup(1536, hfsmp
->blockSize
);
3131 * Add the on-disk size of allocation bitmap.
3133 zonesize
+= hfsmp
->hfs_allocation_cp
->c_datafork
->ff_blocks
* hfsmp
->blockSize
;
3136 * Add space for the Journal Info Block and Journal (if they're in
3137 * this file system).
3139 if (hfsmp
->jnl
&& hfsmp
->jvp
== hfsmp
->hfs_devvp
) {
3140 zonesize
+= hfsmp
->blockSize
+ hfsmp
->jnl_size
;
3144 * Add the existing size of the Extents Overflow B-tree.
3145 * (It rarely grows, so don't bother reserving additional room for it.)
3147 zonesize
+= hfs_blk_to_bytes(hfsmp
->hfs_extents_cp
->c_datafork
->ff_blocks
, hfsmp
->blockSize
);
3150 * If there is an Attributes B-tree, leave room for 11 clumps worth.
3151 * newfs_hfs allocates one clump, and leaves a gap of 10 clumps.
3152 * When installing a full OS install onto a 20GB volume, we use
3153 * 7 to 8 clumps worth of space (depending on packages), so that leaves
3154 * us with another 3 or 4 clumps worth before we need another extent.
3156 if (hfsmp
->hfs_attribute_cp
) {
3157 zonesize
+= 11 * hfsmp
->hfs_attribute_cp
->c_datafork
->ff_clumpsize
;
3161 * Leave room for 11 clumps of the Catalog B-tree.
3162 * Again, newfs_hfs allocates one clump plus a gap of 10 clumps.
3163 * When installing a full OS install onto a 20GB volume, we use
3164 * 7 to 8 clumps worth of space (depending on packages), so that leaves
3165 * us with another 3 or 4 clumps worth before we need another extent.
3167 zonesize
+= 11 * hfsmp
->hfs_catalog_cp
->c_datafork
->ff_clumpsize
;
3170 * Add space for hot file region.
3172 * ...for now, use 5 MB per 1 GB (0.5 %)
3174 filesize
= (fs_size
/ 1024) * 5;
3175 if (filesize
> HOTBAND_MAXIMUM_SIZE
)
3176 filesize
= HOTBAND_MAXIMUM_SIZE
;
3177 else if (filesize
< HOTBAND_MINIMUM_SIZE
)
3178 filesize
= HOTBAND_MINIMUM_SIZE
;
3180 * Calculate user quota file requirements.
3182 if (hfsmp
->hfs_flags
& HFS_QUOTAS
) {
3183 items
= QF_USERS_PER_GB
* (fs_size
/ GIGABYTE
);
3184 if (items
< QF_MIN_USERS
)
3185 items
= QF_MIN_USERS
;
3186 else if (items
> QF_MAX_USERS
)
3187 items
= QF_MAX_USERS
;
3188 if (!powerof2(items
)) {
3196 filesize
+= (items
+ 1) * sizeof(struct dqblk
);
3198 * Calculate group quota file requirements.
3201 items
= QF_GROUPS_PER_GB
* (fs_size
/ GIGABYTE
);
3202 if (items
< QF_MIN_GROUPS
)
3203 items
= QF_MIN_GROUPS
;
3204 else if (items
> QF_MAX_GROUPS
)
3205 items
= QF_MAX_GROUPS
;
3206 if (!powerof2(items
)) {
3214 filesize
+= (items
+ 1) * sizeof(struct dqblk
);
3216 zonesize
+= filesize
;
3219 * Round up entire zone to a bitmap block's worth.
3220 * The extra space goes to the catalog file and hot file area.
3223 zonesize
= roundup(zonesize
, (u_int64_t
)vcb
->vcbVBMIOSize
* 8 * vcb
->blockSize
);
3224 hfsmp
->hfs_min_alloc_start
= zonesize
/ vcb
->blockSize
;
3226 * If doing the round up for hfs_min_alloc_start would push us past
3227 * allocLimit, then just reset it back to 0. Though using a value
3228 * bigger than allocLimit would not cause damage in the block allocator
3229 * code, this value could get stored in the volume header and make it out
3230 * to disk, making the volume header technically corrupt.
3232 if (hfsmp
->hfs_min_alloc_start
>= hfsmp
->allocLimit
) {
3233 hfsmp
->hfs_min_alloc_start
= 0;
3236 if (really_do_it
== 0) {
3237 /* If metadata zone needs to be disabled because the
3238 * volume was truncated, clear the bit and zero out
3239 * the values that are no longer needed.
3241 if (hfsmp
->hfs_flags
& HFS_METADATA_ZONE
) {
3242 /* Disable metadata zone */
3243 hfsmp
->hfs_flags
&= ~HFS_METADATA_ZONE
;
3245 /* Zero out mount point values that are not required */
3246 hfsmp
->hfs_catalog_maxblks
= 0;
3247 hfsmp
->hfs_hotfile_maxblks
= 0;
3248 hfsmp
->hfs_hotfile_start
= 0;
3249 hfsmp
->hfs_hotfile_end
= 0;
3250 hfsmp
->hfs_hotfile_freeblks
= 0;
3251 hfsmp
->hfs_metazone_start
= 0;
3252 hfsmp
->hfs_metazone_end
= 0;
3258 temp
= zonesize
- temp
; /* temp has extra space */
3259 filesize
+= temp
/ 3;
3260 hfsmp
->hfs_catalog_maxblks
+= (temp
- (temp
/ 3)) / vcb
->blockSize
;
3262 if (hfsmp
->hfs_flags
& HFS_CS_HOTFILE_PIN
) {
3263 hfsmp
->hfs_hotfile_maxblks
= (uint32_t) (hfsmp
->hfs_cs_hotfile_size
/ HFSTOVCB(hfsmp
)->blockSize
);
3265 hfsmp
->hfs_hotfile_maxblks
= filesize
/ vcb
->blockSize
;
3268 /* Convert to allocation blocks. */
3269 blk
= zonesize
/ vcb
->blockSize
;
3271 /* The default metadata zone location is at the start of volume. */
3272 hfsmp
->hfs_metazone_start
= 1;
3273 hfsmp
->hfs_metazone_end
= blk
- 1;
3275 /* The default hotfile area is at the end of the zone. */
3276 if (vfs_flags(HFSTOVFS(hfsmp
)) & MNT_ROOTFS
) {
3277 hfsmp
->hfs_hotfile_start
= blk
- (filesize
/ vcb
->blockSize
);
3278 hfsmp
->hfs_hotfile_end
= hfsmp
->hfs_metazone_end
;
3279 hfsmp
->hfs_hotfile_freeblks
= hfs_hotfile_freeblocks(hfsmp
);
3282 hfsmp
->hfs_hotfile_start
= 0;
3283 hfsmp
->hfs_hotfile_end
= 0;
3284 hfsmp
->hfs_hotfile_freeblks
= 0;
3287 printf("hfs:%s: metadata zone is %d to %d\n", hfsmp
->vcbVN
, hfsmp
->hfs_metazone_start
, hfsmp
->hfs_metazone_end
);
3288 printf("hfs:%s: hot file band is %d to %d\n", hfsmp
->vcbVN
, hfsmp
->hfs_hotfile_start
, hfsmp
->hfs_hotfile_end
);
3289 printf("hfs:%s: hot file band free blocks = %d\n", hfsmp
->vcbVN
, hfsmp
->hfs_hotfile_freeblks
);
3292 hfsmp
->hfs_flags
|= HFS_METADATA_ZONE
;
3297 hfs_hotfile_freeblocks(struct hfsmount
*hfsmp
)
3299 ExtendedVCB
*vcb
= HFSTOVCB(hfsmp
);
3303 if (hfsmp
->hfs_flags
& HFS_CS_HOTFILE_PIN
) {
3305 // This is only used at initialization time and on an ssd
3306 // we'll get the real info from the hotfile btree user
3312 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_BITMAP
, HFS_EXCLUSIVE_LOCK
);
3313 freeblocks
= MetaZoneFreeBlocks(vcb
);
3314 hfs_systemfile_unlock(hfsmp
, lockflags
);
3316 /* Minus Extents overflow file reserve. */
3317 if ((uint32_t)hfsmp
->hfs_overflow_maxblks
>= VTOF(hfsmp
->hfs_extents_vp
)->ff_blocks
) {
3318 freeblocks
-= hfsmp
->hfs_overflow_maxblks
- VTOF(hfsmp
->hfs_extents_vp
)->ff_blocks
;
3321 /* Minus catalog file reserve. */
3322 if ((uint32_t)hfsmp
->hfs_catalog_maxblks
>= VTOF(hfsmp
->hfs_catalog_vp
)->ff_blocks
) {
3323 freeblocks
-= hfsmp
->hfs_catalog_maxblks
- VTOF(hfsmp
->hfs_catalog_vp
)->ff_blocks
;
3329 // printf("hfs: hotfile_freeblocks: MIN(%d, %d) = %d\n", freeblocks, hfsmp->hfs_hotfile_maxblks, MIN(freeblocks, hfsmp->hfs_hotfile_maxblks));
3330 return MIN(freeblocks
, hfsmp
->hfs_hotfile_maxblks
);
3334 * Determine if a file is a "virtual" metadata file.
3335 * This includes journal and quota files.
3338 hfs_virtualmetafile(struct cnode
*cp
)
3340 const char * filename
;
3343 if (cp
->c_parentcnid
!= kHFSRootFolderID
)
3346 filename
= (const char *)cp
->c_desc
.cd_nameptr
;
3347 if (filename
== NULL
)
3350 if ((strncmp(filename
, ".journal", sizeof(".journal")) == 0) ||
3351 (strncmp(filename
, ".journal_info_block", sizeof(".journal_info_block")) == 0) ||
3352 (strncmp(filename
, ".quota.user", sizeof(".quota.user")) == 0) ||
3353 (strncmp(filename
, ".quota.group", sizeof(".quota.group")) == 0) ||
3354 (strncmp(filename
, ".hotfiles.btree", sizeof(".hotfiles.btree")) == 0))
3360 void hfs_syncer_lock(struct hfsmount
*hfsmp
)
3362 hfs_lock_mount(hfsmp
);
3365 void hfs_syncer_unlock(struct hfsmount
*hfsmp
)
3367 hfs_unlock_mount(hfsmp
);
3370 void hfs_syncer_wait(struct hfsmount
*hfsmp
, struct timespec
*ts
)
3372 msleep(&hfsmp
->hfs_syncer_thread
, &hfsmp
->hfs_mutex
, PWAIT
,
3373 "hfs_syncer_wait", ts
);
3376 void hfs_syncer_wakeup(struct hfsmount
*hfsmp
)
3378 wakeup(&hfsmp
->hfs_syncer_thread
);
3381 uint64_t hfs_usecs_to_deadline(uint64_t usecs
)
3384 clock_interval_to_deadline(usecs
, NSEC_PER_USEC
, &deadline
);
3389 // Fire off a timed callback to sync the disk if the
3390 // volume is on ejectable media.
3392 void hfs_sync_ejectable(struct hfsmount
*hfsmp
)
3394 // If we don't have a syncer or we get called by the syncer, just return
3395 if (!ISSET(hfsmp
->hfs_flags
, HFS_RUN_SYNCER
)
3396 || current_thread() == hfsmp
->hfs_syncer_thread
) {
3400 hfs_syncer_lock(hfsmp
);
3402 if (!timerisset(&hfsmp
->hfs_sync_req_oldest
))
3403 microuptime(&hfsmp
->hfs_sync_req_oldest
);
3405 /* If hfs_unmount is running, it will clear the HFS_RUN_SYNCER
3406 flag. Also, we don't want to queue again if there is a sync
3408 if (!ISSET(hfsmp
->hfs_flags
, HFS_RUN_SYNCER
)
3409 || hfsmp
->hfs_syncer_thread
) {
3410 hfs_syncer_unlock(hfsmp
);
3414 hfsmp
->hfs_syncer_thread
= (void *)1;
3416 hfs_syncer_unlock(hfsmp
);
3418 kernel_thread_start(hfs_syncer
, hfsmp
, &hfsmp
->hfs_syncer_thread
);
3419 thread_deallocate(hfsmp
->hfs_syncer_thread
);
3423 hfs_start_transaction(struct hfsmount
*hfsmp
)
3425 int ret
= 0, unlock_on_err
= 0;
3426 thread_t thread
= current_thread();
3428 #ifdef HFS_CHECK_LOCK_ORDER
3430 * You cannot start a transaction while holding a system
3431 * file lock. (unless the transaction is nested.)
3433 if (hfsmp
->jnl
&& journal_owner(hfsmp
->jnl
) != thread
) {
3434 if (hfsmp
->hfs_catalog_cp
&& hfsmp
->hfs_catalog_cp
->c_lockowner
== thread
) {
3435 panic("hfs_start_transaction: bad lock order (cat before jnl)\n");
3437 if (hfsmp
->hfs_attribute_cp
&& hfsmp
->hfs_attribute_cp
->c_lockowner
== thread
) {
3438 panic("hfs_start_transaction: bad lock order (attr before jnl)\n");
3440 if (hfsmp
->hfs_extents_cp
&& hfsmp
->hfs_extents_cp
->c_lockowner
== thread
) {
3441 panic("hfs_start_transaction: bad lock order (ext before jnl)\n");
3444 #endif /* HFS_CHECK_LOCK_ORDER */
3449 if (journal_owner(hfsmp
->jnl
) != thread
) {
3451 * The global lock should be held shared if journal is
3452 * active to prevent disabling. If we're not the owner
3453 * of the journal lock, verify that we're not already
3454 * holding the global lock exclusive before moving on.
3456 if (hfsmp
->hfs_global_lockowner
== thread
) {
3461 hfs_lock_global (hfsmp
, HFS_SHARED_LOCK
);
3463 // Things could have changed
3465 hfs_unlock_global(hfsmp
);
3469 OSAddAtomic(1, (SInt32
*)&hfsmp
->hfs_active_threads
);
3474 if (hfsmp
->hfs_global_lockowner
!= thread
) {
3475 hfs_lock_global(hfsmp
, HFS_EXCLUSIVE_LOCK
);
3477 // Things could have changed
3479 hfs_unlock_global(hfsmp
);
3483 OSAddAtomic(1, (SInt32
*)&hfsmp
->hfs_active_threads
);
3488 /* If a downgrade to read-only mount is in progress, no other
3489 * thread than the downgrade thread is allowed to modify
3492 if ((hfsmp
->hfs_flags
& HFS_RDONLY_DOWNGRADE
) &&
3493 hfsmp
->hfs_downgrading_thread
!= thread
) {
3499 ret
= journal_start_transaction(hfsmp
->jnl
);
3505 ++hfsmp
->hfs_transaction_nesting
;
3508 if (ret
!= 0 && unlock_on_err
) {
3509 hfs_unlock_global (hfsmp
);
3510 OSAddAtomic(-1, (SInt32
*)&hfsmp
->hfs_active_threads
);
3517 hfs_end_transaction(struct hfsmount
*hfsmp
)
3521 hfs_assert(!hfsmp
->jnl
|| journal_owner(hfsmp
->jnl
) == current_thread());
3522 hfs_assert(hfsmp
->hfs_transaction_nesting
> 0);
3524 if (hfsmp
->jnl
&& hfsmp
->hfs_transaction_nesting
== 1)
3525 hfs_flushvolumeheader(hfsmp
, HFS_FVH_FLUSH_IF_DIRTY
);
3527 bool need_unlock
= !--hfsmp
->hfs_transaction_nesting
;
3530 ret
= journal_end_transaction(hfsmp
->jnl
);
3536 OSAddAtomic(-1, (SInt32
*)&hfsmp
->hfs_active_threads
);
3537 hfs_unlock_global (hfsmp
);
3538 hfs_sync_ejectable(hfsmp
);
3546 hfs_journal_lock(struct hfsmount
*hfsmp
)
3548 /* Only peek at hfsmp->jnl while holding the global lock */
3549 hfs_lock_global (hfsmp
, HFS_SHARED_LOCK
);
3551 journal_lock(hfsmp
->jnl
);
3553 hfs_unlock_global (hfsmp
);
3557 hfs_journal_unlock(struct hfsmount
*hfsmp
)
3559 /* Only peek at hfsmp->jnl while holding the global lock */
3560 hfs_lock_global (hfsmp
, HFS_SHARED_LOCK
);
3562 journal_unlock(hfsmp
->jnl
);
3564 hfs_unlock_global (hfsmp
);
3568 * Flush the contents of the journal to the disk.
3570 * - HFS_FLUSH_JOURNAL
3571 * Wait to write in-memory journal to the disk consistently.
3572 * This means that the journal still contains uncommitted
3573 * transactions and the file system metadata blocks in
3574 * the journal transactions might be written asynchronously
3575 * to the disk. But there is no guarantee that they are
3576 * written to the disk before returning to the caller.
3577 * Note that this option is sufficient for file system
3578 * data integrity as it guarantees consistent journal
3579 * content on the disk.
3581 * - HFS_FLUSH_JOURNAL_META
3582 * Wait to write in-memory journal to the disk
3583 * consistently, and also wait to write all asynchronous
3584 * metadata blocks to its corresponding locations
3585 * consistently on the disk. This is overkill in normal
3586 * scenarios but is useful whenever the metadata blocks
3587 * are required to be consistent on-disk instead of
3588 * just the journalbeing consistent; like before live
3589 * verification and live volume resizing. The update of the
3590 * metadata doesn't include a barrier of track cache flush.
3593 * HFS_FLUSH_JOURNAL + force a track cache flush to media
3596 * Force a track cache flush to media.
3598 * - HFS_FLUSH_BARRIER
3599 * Barrier-only flush to ensure write order
3602 errno_t
hfs_flush(struct hfsmount
*hfsmp
, hfs_flush_mode_t mode
)
3606 dk_synchronize_t sync_req
= { .options
= DK_SYNCHRONIZE_OPTION_BARRIER
};
3609 case HFS_FLUSH_JOURNAL_META
:
3610 // wait for journal, metadata blocks and previous async flush to finish
3611 SET(options
, JOURNAL_WAIT_FOR_IO
);
3615 case HFS_FLUSH_JOURNAL
:
3616 case HFS_FLUSH_JOURNAL_BARRIER
:
3617 case HFS_FLUSH_FULL
:
3619 if (mode
== HFS_FLUSH_JOURNAL_BARRIER
&&
3620 !(hfsmp
->hfs_flags
& HFS_FEATURE_BARRIER
))
3621 mode
= HFS_FLUSH_FULL
;
3623 if (mode
== HFS_FLUSH_FULL
)
3624 SET(options
, JOURNAL_FLUSH_FULL
);
3626 /* Only peek at hfsmp->jnl while holding the global lock */
3627 hfs_lock_global (hfsmp
, HFS_SHARED_LOCK
);
3630 error
= journal_flush(hfsmp
->jnl
, options
);
3632 hfs_unlock_global (hfsmp
);
3635 * This may result in a double barrier as
3636 * journal_flush may have issued a barrier itself
3638 if (mode
== HFS_FLUSH_JOURNAL_BARRIER
)
3639 error
= VNOP_IOCTL(hfsmp
->hfs_devvp
,
3640 DKIOCSYNCHRONIZE
, (caddr_t
)&sync_req
,
3645 case HFS_FLUSH_CACHE
:
3647 sync_req
.options
= 0;
3651 case HFS_FLUSH_BARRIER
:
3652 // If barrier only flush doesn't support, fall back to use full flush.
3653 if (!(hfsmp
->hfs_flags
& HFS_FEATURE_BARRIER
))
3654 sync_req
.options
= 0;
3656 error
= VNOP_IOCTL(hfsmp
->hfs_devvp
, DKIOCSYNCHRONIZE
, (caddr_t
)&sync_req
,
3668 * hfs_erase_unused_nodes
3670 * Check wheter a volume may suffer from unused Catalog B-tree nodes that
3671 * are not zeroed (due to <rdar://problem/6947811>). If so, just write
3672 * zeroes to the unused nodes.
3674 * How do we detect when a volume needs this repair? We can't always be
3675 * certain. If a volume was created after a certain date, then it may have
3676 * been created with the faulty newfs_hfs. Since newfs_hfs only created one
3677 * clump, we can assume that if a Catalog B-tree is larger than its clump size,
3678 * that means that the entire first clump must have been written to, which means
3679 * there shouldn't be unused and unwritten nodes in that first clump, and this
3680 * repair is not needed.
3682 * We have defined a bit in the Volume Header's attributes to indicate when the
3683 * unused nodes have been repaired. A newer newfs_hfs will set this bit.
3684 * As will fsck_hfs when it repairs the unused nodes.
3686 int hfs_erase_unused_nodes(struct hfsmount
*hfsmp
)
3689 struct filefork
*catalog
;
3692 if (hfsmp
->vcbAtrb
& kHFSUnusedNodeFixMask
)
3694 /* This volume has already been checked and repaired. */
3698 if ((hfsmp
->localCreateDate
< kHFSUnusedNodesFixDate
))
3700 /* This volume is too old to have had the problem. */
3701 hfsmp
->vcbAtrb
|= kHFSUnusedNodeFixMask
;
3705 catalog
= hfsmp
->hfs_catalog_cp
->c_datafork
;
3706 if (catalog
->ff_size
> catalog
->ff_clumpsize
)
3708 /* The entire first clump must have been in use at some point. */
3709 hfsmp
->vcbAtrb
|= kHFSUnusedNodeFixMask
;
3714 * If we get here, we need to zero out those unused nodes.
3716 * We start a transaction and lock the catalog since we're going to be
3717 * making on-disk changes. But note that BTZeroUnusedNodes doens't actually
3718 * do its writing via the journal, because that would be too much I/O
3719 * to fit in a transaction, and it's a pain to break it up into multiple
3720 * transactions. (It behaves more like growing a B-tree would.)
3722 printf("hfs_erase_unused_nodes: updating volume %s.\n", hfsmp
->vcbVN
);
3723 result
= hfs_start_transaction(hfsmp
);
3726 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_EXCLUSIVE_LOCK
);
3727 result
= BTZeroUnusedNodes(catalog
);
3728 vnode_waitforwrites(hfsmp
->hfs_catalog_vp
, 0, 0, 0, "hfs_erase_unused_nodes");
3729 hfs_systemfile_unlock(hfsmp
, lockflags
);
3730 hfs_end_transaction(hfsmp
);
3732 hfsmp
->vcbAtrb
|= kHFSUnusedNodeFixMask
;
3733 printf("hfs_erase_unused_nodes: done updating volume %s.\n", hfsmp
->vcbVN
);
3741 check_for_dataless_file(struct vnode
*vp
, uint64_t op_type
)
3745 if (vp
== NULL
|| (VTOC(vp
)->c_bsdflags
& UF_COMPRESSED
) == 0 || VTOCMP(vp
) == NULL
|| decmpfs_cnode_cmp_type(VTOCMP(vp
)) != DATALESS_CMPFS_TYPE
) {
3746 // there's nothing to do, it's not dataless
3750 /* Swap files are special; ignore them */
3751 if (vnode_isswap(vp
)) {
3755 // printf("hfs: dataless: encountered a file with the dataless bit set! (vp %p)\n", vp);
3756 error
= resolve_nspace_item(vp
, op_type
| NAMESPACE_HANDLER_NSPACE_EVENT
);
3757 if (error
== EDEADLK
&& op_type
== NAMESPACE_HANDLER_WRITE_OP
) {
3760 if (error
== EAGAIN
) {
3761 printf("hfs: dataless: timed out waiting for namespace handler...\n");
3762 // XXXdbg - return the fabled ENOTPRESENT (i.e. EJUKEBOX)?
3764 } else if (error
== EINTR
) {
3765 // printf("hfs: dataless: got a signal while waiting for namespace handler...\n");
3768 } else if (VTOC(vp
)->c_bsdflags
& UF_COMPRESSED
) {
3770 // if we're here, the dataless bit is still set on the file
3771 // which means it didn't get handled. we return an error
3772 // but it's presently ignored by all callers of this function.
3774 // XXXdbg - EDATANOTPRESENT is what we really need...
3784 // NOTE: this function takes care of starting a transaction and
3785 // acquiring the systemfile lock so that it can call
3788 // NOTE: do NOT hold and cnode locks while calling this function
3789 // to avoid deadlocks (because we take a lock on the root
3793 hfs_generate_document_id(struct hfsmount
*hfsmp
, uint32_t *docid
)
3799 error
= hfs_vfs_root(HFSTOVFS(hfsmp
), &rvp
, vfs_context_kernel());
3805 if ((error
= hfs_lock(cp
, HFS_EXCLUSIVE_LOCK
, HFS_LOCK_DEFAULT
)) != 0) {
3808 struct FndrExtendedDirInfo
*extinfo
= (struct FndrExtendedDirInfo
*)((void *)((char *)&cp
->c_attr
.ca_finderinfo
+ 16));
3811 if ((error
= hfs_start_transaction(hfsmp
)) != 0) {
3814 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_EXCLUSIVE_LOCK
);
3816 if (extinfo
->document_id
== 0) {
3817 // initialize this to start at 3 (one greater than the root-dir id)
3818 extinfo
->document_id
= 3;
3821 *docid
= extinfo
->document_id
++;
3823 // mark the root cnode dirty
3824 cp
->c_flag
|= C_MODIFIED
;
3825 hfs_update(cp
->c_vp
, 0);
3827 hfs_systemfile_unlock (hfsmp
, lockflags
);
3828 (void) hfs_end_transaction(hfsmp
);
3830 (void) hfs_unlock(cp
);
3840 * Return information about number of file system allocation blocks
3841 * taken by metadata on a volume.
3843 * This function populates struct hfsinfo_metadata with allocation blocks
3844 * used by extents overflow btree, catalog btree, bitmap, attribute btree,
3845 * journal file, and sum of all of the above.
3848 hfs_getinfo_metadata_blocks(struct hfsmount
*hfsmp
, struct hfsinfo_metadata
*hinfo
)
3851 int ret_lockflags
= 0;
3853 /* Zero out the output buffer */
3854 bzero(hinfo
, sizeof(struct hfsinfo_metadata
));
3857 * Getting number of allocation blocks for all btrees
3858 * should be a quick operation, so we grab locks for
3859 * all of them at the same time
3861 lockflags
= SFL_CATALOG
| SFL_EXTENTS
| SFL_BITMAP
| SFL_ATTRIBUTE
;
3862 ret_lockflags
= hfs_systemfile_lock(hfsmp
, lockflags
, HFS_EXCLUSIVE_LOCK
);
3864 * Make sure that we were able to acquire all locks requested
3865 * to protect us against conditions like unmount in progress.
3867 if ((lockflags
& ret_lockflags
) != lockflags
) {
3868 /* Release any locks that were acquired */
3869 hfs_systemfile_unlock(hfsmp
, ret_lockflags
);
3873 /* Get information about all the btrees */
3874 hinfo
->extents
= hfsmp
->hfs_extents_cp
->c_datafork
->ff_blocks
;
3875 hinfo
->catalog
= hfsmp
->hfs_catalog_cp
->c_datafork
->ff_blocks
;
3876 hinfo
->allocation
= hfsmp
->hfs_allocation_cp
->c_datafork
->ff_blocks
;
3877 hinfo
->attribute
= hfsmp
->hfs_attribute_cp
->c_datafork
->ff_blocks
;
3879 /* Done with btrees, give up the locks */
3880 hfs_systemfile_unlock(hfsmp
, ret_lockflags
);
3882 /* Get information about journal file */
3883 hinfo
->journal
= howmany(hfsmp
->jnl_size
, hfsmp
->blockSize
);
3885 /* Calculate total number of metadata blocks */
3886 hinfo
->total
= hinfo
->extents
+ hinfo
->catalog
+
3887 hinfo
->allocation
+ hinfo
->attribute
+
3894 hfs_freezewrite_callback(struct vnode
*vp
, __unused
void *cargs
)
3896 vnode_waitforwrites(vp
, 0, 0, 0, "hfs freeze 8");
3901 int hfs_freeze(struct hfsmount
*hfsmp
)
3903 // First make sure some other process isn't freezing
3904 hfs_lock_mount(hfsmp
);
3905 while (hfsmp
->hfs_freeze_state
!= HFS_THAWED
) {
3906 if (msleep(&hfsmp
->hfs_freeze_state
, &hfsmp
->hfs_mutex
,
3907 PWAIT
| PCATCH
, "hfs freeze 1", NULL
) == EINTR
) {
3908 hfs_unlock_mount(hfsmp
);
3913 // Stop new syncers from starting
3914 hfsmp
->hfs_freeze_state
= HFS_WANT_TO_FREEZE
;
3916 // Now wait for all syncers to finish
3917 while (hfsmp
->hfs_syncers
) {
3918 if (msleep(&hfsmp
->hfs_freeze_state
, &hfsmp
->hfs_mutex
,
3919 PWAIT
| PCATCH
, "hfs freeze 2", NULL
) == EINTR
) {
3920 hfs_thaw_locked(hfsmp
);
3921 hfs_unlock_mount(hfsmp
);
3925 hfs_unlock_mount(hfsmp
);
3927 // flush things before we get started to try and prevent
3928 // dirty data from being paged out while we're frozen.
3929 // note: we can't do this once we're in the freezing state because
3930 // other threads will need to take the global lock
3931 vnode_iterate(hfsmp
->hfs_mp
, 0, hfs_freezewrite_callback
, NULL
);
3933 // Block everything in hfs_lock_global now
3934 hfs_lock_mount(hfsmp
);
3935 hfsmp
->hfs_freeze_state
= HFS_FREEZING
;
3936 hfsmp
->hfs_freezing_thread
= current_thread();
3937 hfs_unlock_mount(hfsmp
);
3939 /* Take the exclusive lock to flush out anything else that
3940 might have the global lock at the moment and also so we
3941 can flush the journal. */
3942 hfs_lock_global(hfsmp
, HFS_EXCLUSIVE_LOCK
);
3943 journal_flush(hfsmp
->jnl
, JOURNAL_WAIT_FOR_IO
);
3944 hfs_unlock_global(hfsmp
);
3946 // don't need to iterate on all vnodes, we just need to
3947 // wait for writes to the system files and the device vnode
3949 // Now that journal flush waits for all metadata blocks to
3950 // be written out, waiting for btree writes is probably no
3952 if (HFSTOVCB(hfsmp
)->extentsRefNum
)
3953 vnode_waitforwrites(HFSTOVCB(hfsmp
)->extentsRefNum
, 0, 0, 0, "hfs freeze 3");
3954 if (HFSTOVCB(hfsmp
)->catalogRefNum
)
3955 vnode_waitforwrites(HFSTOVCB(hfsmp
)->catalogRefNum
, 0, 0, 0, "hfs freeze 4");
3956 if (HFSTOVCB(hfsmp
)->allocationsRefNum
)
3957 vnode_waitforwrites(HFSTOVCB(hfsmp
)->allocationsRefNum
, 0, 0, 0, "hfs freeze 5");
3958 if (hfsmp
->hfs_attribute_vp
)
3959 vnode_waitforwrites(hfsmp
->hfs_attribute_vp
, 0, 0, 0, "hfs freeze 6");
3960 vnode_waitforwrites(hfsmp
->hfs_devvp
, 0, 0, 0, "hfs freeze 7");
3962 // We're done, mark frozen
3963 hfs_lock_mount(hfsmp
);
3964 hfsmp
->hfs_freeze_state
= HFS_FROZEN
;
3965 hfsmp
->hfs_freezing_proc
= current_proc();
3966 hfs_unlock_mount(hfsmp
);
3971 int hfs_thaw(struct hfsmount
*hfsmp
, const struct proc
*process
)
3973 hfs_lock_mount(hfsmp
);
3975 if (hfsmp
->hfs_freeze_state
!= HFS_FROZEN
) {
3976 hfs_unlock_mount(hfsmp
);
3979 if (process
&& hfsmp
->hfs_freezing_proc
!= process
) {
3980 hfs_unlock_mount(hfsmp
);
3984 hfs_thaw_locked(hfsmp
);
3986 hfs_unlock_mount(hfsmp
);
3991 static void hfs_thaw_locked(struct hfsmount
*hfsmp
)
3993 hfsmp
->hfs_freezing_proc
= NULL
;
3994 hfsmp
->hfs_freeze_state
= HFS_THAWED
;
3996 wakeup(&hfsmp
->hfs_freeze_state
);
3999 uintptr_t obfuscate_addr(void *addr
)
4001 vm_offset_t new_addr
;
4002 vm_kernel_addrperm_external((vm_offset_t
)addr
, &new_addr
);
4008 * Convert HFS encoded string into UTF-8
4010 * Unicode output is fully decomposed
4011 * '/' chars are converted to ':'
4014 hfs_to_utf8(ExtendedVCB
*vcb
, const Str31 hfs_str
, ByteCount maxDstLen
, ByteCount
*actualDstLen
, unsigned char* dstStr
)
4017 UniChar uniStr
[MAX_HFS_UNICODE_CHARS
];
4020 hfs_to_unicode_func_t hfs_get_unicode
= VCBTOHFS(vcb
)->hfs_get_unicode
;
4021 u_int8_t pascal_length
= 0;
4024 * Validate the length of the Pascal-style string before passing it
4025 * down to the decoding engine.
4027 pascal_length
= *((const u_int8_t
*)(hfs_str
));
4028 if (pascal_length
> 31) {
4029 /* invalid string; longer than 31 bytes */
4034 error
= hfs_get_unicode(hfs_str
, uniStr
, MAX_HFS_UNICODE_CHARS
, &uniCount
);
4040 error
= utf8_encodestr(uniStr
, uniCount
* sizeof(UniChar
), dstStr
, &utf8len
, maxDstLen
, ':', 0);
4041 if (error
== ENAMETOOLONG
)
4042 *actualDstLen
= utf8_encodelen(uniStr
, uniCount
* sizeof(UniChar
), ':', 0);
4044 *actualDstLen
= utf8len
;
4051 * Convert UTF-8 string into HFS encoding
4053 * ':' chars are converted to '/'
4054 * Assumes input represents fully decomposed Unicode
4057 utf8_to_hfs(ExtendedVCB
*vcb
, ByteCount srcLen
, const unsigned char* srcStr
, Str31 dstStr
/*, int retry*/)
4060 UniChar uniStr
[MAX_HFS_UNICODE_CHARS
];
4063 error
= utf8_decodestr(srcStr
, srcLen
, uniStr
, &ucslen
, sizeof(uniStr
), ':', 0);
4065 error
= unicode_to_hfs(vcb
, ucslen
, uniStr
, dstStr
, 1);
4071 * Convert Unicode string into HFS encoding
4073 * ':' chars are converted to '/'
4074 * Assumes input represents fully decomposed Unicode
4077 unicode_to_hfs(ExtendedVCB
*vcb
, ByteCount srcLen
, u_int16_t
* srcStr
, Str31 dstStr
, int retry
)
4080 unicode_to_hfs_func_t hfs_get_hfsname
= VCBTOHFS(vcb
)->hfs_get_hfsname
;
4082 error
= hfs_get_hfsname(srcStr
, srcLen
/sizeof(UniChar
), dstStr
);
4083 if (error
&& retry
) {
4084 error
= unicode_to_mac_roman(srcStr
, srcLen
/sizeof(UniChar
), dstStr
);
4089 #endif // CONFIG_HFS_STD
4091 static uint64_t hfs_allocated
__attribute__((aligned(8)));
4093 #if HFS_MALLOC_DEBUG
4095 #warning HFS_MALLOC_DEBUG is on
4097 #include <libkern/OSDebug.h>
4098 #include "hfs_alloc_trace.h"
4100 struct alloc_debug_header
{
4104 LIST_ENTRY(alloc_debug_header
) chain
;
4105 void *backtrace
[HFS_ALLOC_BACKTRACE_LEN
];
4109 HFS_ALLOC_MAGIC
= 0x68667361, // "hfsa"
4110 HFS_ALLOC_DEAD
= 0x68667364, // "hfsd"
4113 static LIST_HEAD(, alloc_debug_header
) hfs_alloc_list
;
4114 static lck_mtx_t
*hfs_alloc_mtx
;
4115 static int hfs_alloc_tracing
;
4116 static uint64_t hfs_alloc_sequence
;
4118 void hfs_alloc_trace_enable(void)
4120 if (hfs_alloc_tracing
)
4123 // Not thread-safe, but this is debug so who cares
4124 extern lck_grp_t
*hfs_mutex_group
;
4125 extern lck_attr_t
*hfs_lock_attr
;
4127 if (!hfs_alloc_mtx
) {
4128 hfs_alloc_mtx
= lck_mtx_alloc_init(hfs_mutex_group
, hfs_lock_attr
);
4129 LIST_INIT(&hfs_alloc_list
);
4132 // Using OSCompareAndSwap in lieu of a barrier
4133 OSCompareAndSwap(hfs_alloc_tracing
, true, &hfs_alloc_tracing
);
4136 void hfs_alloc_trace_disable(void)
4138 if (!hfs_alloc_tracing
)
4141 hfs_alloc_tracing
= false;
4143 lck_mtx_lock_spin(hfs_alloc_mtx
);
4145 struct alloc_debug_header
*hdr
;
4146 LIST_FOREACH(hdr
, &hfs_alloc_list
, chain
) {
4147 hdr
->chain
.le_prev
= NULL
;
4149 LIST_INIT(&hfs_alloc_list
);
4151 lck_mtx_unlock(hfs_alloc_mtx
);
4154 static int hfs_handle_alloc_tracing SYSCTL_HANDLER_ARGS
4156 int v
= hfs_alloc_tracing
;
4158 int err
= sysctl_handle_int(oidp
, &v
, 0, req
);
4159 if (err
|| req
->newptr
== USER_ADDR_NULL
|| v
== hfs_alloc_tracing
)
4163 hfs_alloc_trace_enable();
4165 hfs_alloc_trace_disable();
4170 HFS_SYSCTL(PROC
, _vfs_generic_hfs
, OID_AUTO
, alloc_tracing
,
4171 CTLTYPE_INT
| CTLFLAG_RW
| CTLFLAG_LOCKED
, NULL
, 0,
4172 hfs_handle_alloc_tracing
, "I", "Allocation tracing")
4174 static int hfs_handle_alloc_trace_info SYSCTL_HANDLER_ARGS
4176 if (!hfs_alloc_tracing
) {
4177 struct hfs_alloc_trace_info info
= {};
4178 return sysctl_handle_opaque(oidp
, &info
, sizeof(info
), req
);
4181 const int size
= 128 * 1024;
4182 struct hfs_alloc_trace_info
*info
= kalloc(size
);
4184 const int max_entries
= ((size
- sizeof(*info
))
4185 / sizeof(struct hfs_alloc_info_entry
));
4187 info
->entry_count
= 0;
4190 lck_mtx_lock_spin(hfs_alloc_mtx
);
4192 struct alloc_debug_header
*hdr
;
4193 LIST_FOREACH(hdr
, &hfs_alloc_list
, chain
) {
4194 if (info
->entry_count
== max_entries
) {
4199 vm_kernel_addrperm_external((vm_offset_t
)hdr
, &o
);
4200 info
->entries
[info
->entry_count
].ptr
= o
;
4201 info
->entries
[info
->entry_count
].size
= hdr
->size
;
4202 info
->entries
[info
->entry_count
].sequence
= hdr
->sequence
;
4203 for (int i
= 0; i
< HFS_ALLOC_BACKTRACE_LEN
; ++i
) {
4204 vm_kernel_unslide_or_perm_external((vm_offset_t
)hdr
->backtrace
[i
], &o
);
4205 info
->entries
[info
->entry_count
].backtrace
[i
] = o
;
4207 ++info
->entry_count
;
4210 lck_mtx_unlock(hfs_alloc_mtx
);
4212 int err
= sysctl_handle_opaque(oidp
, info
,
4213 sizeof(*info
) + info
->entry_count
4214 * sizeof(struct hfs_alloc_info_entry
),
4222 HFS_SYSCTL(PROC
, _vfs_generic_hfs
, OID_AUTO
, alloc_trace_info
,
4223 CTLTYPE_OPAQUE
| CTLFLAG_RD
| CTLFLAG_LOCKED
, NULL
, 0,
4224 hfs_handle_alloc_trace_info
, "-", "Allocation trace info")
4226 bool hfs_dump_allocations(void)
4231 lck_mtx_lock(hfs_alloc_mtx
);
4233 struct alloc_debug_header
*hdr
;
4234 LIST_FOREACH(hdr
, &hfs_alloc_list
, chain
) {
4236 vm_kernel_addrperm_external((vm_offset_t
)hdr
, &o
);
4237 printf(" -- 0x%lx:%llu <%u> --\n", o
, hdr
->sequence
, hdr
->size
);
4238 for (int j
= 0; j
< HFS_ALLOC_BACKTRACE_LEN
&& hdr
->backtrace
[j
]; ++j
) {
4239 vm_kernel_unslide_or_perm_external((vm_offset_t
)hdr
->backtrace
[j
], &o
);
4240 printf("0x%lx\n", o
);
4244 lck_mtx_unlock(hfs_alloc_mtx
);
4251 HFS_SYSCTL(QUAD
, _vfs_generic_hfs
, OID_AUTO
, allocated
,
4252 CTLFLAG_RD
| CTLFLAG_LOCKED
, &hfs_allocated
, "Memory allocated")
4254 // Any allocation >= PAGE_SIZE will be page aligned
4255 void *hfs_malloc(size_t size
)
4257 #if HFS_MALLOC_DEBUG
4258 hfs_assert(size
<= 0xffffffff);
4260 struct alloc_debug_header
*hdr
;
4264 if (size
>= PAGE_SIZE
)
4265 ptr
= IOMallocAligned(size
+ sizeof(*hdr
), PAGE_SIZE
);
4267 ptr
= kalloc(size
+ sizeof(*hdr
));
4271 hdr
->magic
= HFS_ALLOC_MAGIC
;
4274 if (hfs_alloc_tracing
) {
4275 OSBacktrace(hdr
->backtrace
, HFS_ALLOC_BACKTRACE_LEN
);
4276 lck_mtx_lock_spin(hfs_alloc_mtx
);
4277 LIST_INSERT_HEAD(&hfs_alloc_list
, hdr
, chain
);
4278 hdr
->sequence
= ++hfs_alloc_sequence
;
4279 lck_mtx_unlock(hfs_alloc_mtx
);
4281 hdr
->chain
.le_prev
= NULL
;
4284 if (size
>= PAGE_SIZE
)
4285 ptr
= IOMallocAligned(size
, PAGE_SIZE
);
4290 OSAddAtomic64(size
, &hfs_allocated
);
4295 void hfs_free(void *ptr
, size_t size
)
4300 OSAddAtomic64(-(int64_t)size
, &hfs_allocated
);
4302 #if HFS_MALLOC_DEBUG
4303 struct alloc_debug_header
*hdr
= ptr
+ size
;
4305 hfs_assert(hdr
->magic
== HFS_ALLOC_MAGIC
);
4306 hfs_assert(hdr
->size
== size
);
4308 hdr
->magic
= HFS_ALLOC_DEAD
;
4310 if (hdr
->chain
.le_prev
) {
4311 lck_mtx_lock_spin(hfs_alloc_mtx
);
4312 LIST_REMOVE(hdr
, chain
);
4313 lck_mtx_unlock(hfs_alloc_mtx
);
4316 if (size
>= PAGE_SIZE
)
4317 IOFreeAligned(ptr
, size
+ sizeof(*hdr
));
4319 kfree(ptr
, size
+ sizeof(*hdr
));
4321 if (size
>= PAGE_SIZE
)
4322 IOFreeAligned(ptr
, size
);
4328 void *hfs_mallocz(size_t size
)
4330 void *ptr
= hfs_malloc(size
);
4335 struct hfs_sysctl_chain
*sysctl_list
;
4337 void hfs_sysctl_register(void)
4339 struct hfs_sysctl_chain
*e
= sysctl_list
;
4341 sysctl_register_oid(e
->oid
);
4346 void hfs_sysctl_unregister(void)
4348 struct hfs_sysctl_chain
*e
= sysctl_list
;
4350 sysctl_unregister_oid(e
->oid
);
4355 void hfs_assert_fail(const char *file
, unsigned line
, const char *expr
)
4357 Assert(file
, line
, expr
);
4358 __builtin_unreachable();