2 * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 /* @(#)hfs_vfsutils.c 4.0
30 * (c) 1997-2002 Apple Computer, 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>
43 #include <sys/unistd.h>
44 #include <sys/utfconv.h>
45 #include <sys/kauth.h>
48 #include "hfs_catalog.h"
50 #include "hfs_mount.h"
51 #include "hfs_endian.h"
52 #include "hfs_cnode.h"
54 #include "hfscommon/headers/FileMgrInternal.h"
55 #include "hfscommon/headers/BTreesInternal.h"
56 #include "hfscommon/headers/HFSUnicodeWrappers.h"
59 extern int count_lock_queue(void);
62 static void ReleaseMetaFileVNode(struct vnode
*vp
);
63 static int hfs_late_journal_init(struct hfsmount
*hfsmp
, HFSPlusVolumeHeader
*vhp
, void *_args
);
65 static void hfs_metadatazone_init(struct hfsmount
*);
66 static u_int32_t
hfs_hotfile_freeblocks(struct hfsmount
*);
70 u_int32_t
GetLogicalBlockSize(struct vnode
*vp
);
72 extern int hfs_attrkeycompare(HFSPlusAttrKey
*searchKey
, HFSPlusAttrKey
*trialKey
);
75 //*******************************************************************************
76 // Note: Finder information in the HFS/HFS+ metadata are considered opaque and
77 // hence are not in the right byte order on little endian machines. It is
78 // the responsibility of the finder and other clients to swap the data.
79 //*******************************************************************************
81 //*******************************************************************************
82 // Routine: hfs_MountHFSVolume
85 //*******************************************************************************
86 char hfs_catname
[] = "Catalog B-tree";
87 char hfs_extname
[] = "Extents B-tree";
88 char hfs_vbmname
[] = "Volume Bitmap";
89 char hfs_attrname
[] = "Attribute B-tree";
91 char hfs_privdirname
[] =
92 "\xE2\x90\x80\xE2\x90\x80\xE2\x90\x80\xE2\x90\x80HFS+ Private Data";
95 OSErr
hfs_MountHFSVolume(struct hfsmount
*hfsmp
, HFSMasterDirectoryBlock
*mdb
,
98 ExtendedVCB
*vcb
= HFSTOVCB(hfsmp
);
101 struct cat_desc cndesc
;
102 struct cat_attr cnattr
;
103 struct cat_fork fork
;
105 /* Block size must be a multiple of 512 */
106 if (SWAP_BE32(mdb
->drAlBlkSiz
) == 0 ||
107 (SWAP_BE32(mdb
->drAlBlkSiz
) & 0x01FF) != 0)
110 /* don't mount a writeable volume if its dirty, it must be cleaned by fsck_hfs */
111 if (((hfsmp
->hfs_flags
& HFS_READ_ONLY
) == 0) &&
112 ((SWAP_BE16(mdb
->drAtrb
) & kHFSVolumeUnmountedMask
) == 0)) {
115 hfsmp
->hfs_flags
|= HFS_STANDARD
;
117 * The MDB seems OK: transfer info from it into VCB
118 * Note - the VCB starts out clear (all zeros)
121 vcb
->vcbSigWord
= SWAP_BE16 (mdb
->drSigWord
);
122 vcb
->vcbCrDate
= to_bsd_time(LocalToUTC(SWAP_BE32(mdb
->drCrDate
)));
123 vcb
->localCreateDate
= SWAP_BE32 (mdb
->drCrDate
);
124 vcb
->vcbLsMod
= to_bsd_time(LocalToUTC(SWAP_BE32(mdb
->drLsMod
)));
125 vcb
->vcbAtrb
= SWAP_BE16 (mdb
->drAtrb
);
126 vcb
->vcbNmFls
= SWAP_BE16 (mdb
->drNmFls
);
127 vcb
->vcbVBMSt
= SWAP_BE16 (mdb
->drVBMSt
);
128 vcb
->nextAllocation
= SWAP_BE16 (mdb
->drAllocPtr
);
129 vcb
->totalBlocks
= SWAP_BE16 (mdb
->drNmAlBlks
);
130 vcb
->blockSize
= SWAP_BE32 (mdb
->drAlBlkSiz
);
131 vcb
->vcbClpSiz
= SWAP_BE32 (mdb
->drClpSiz
);
132 vcb
->vcbAlBlSt
= SWAP_BE16 (mdb
->drAlBlSt
);
133 vcb
->vcbNxtCNID
= SWAP_BE32 (mdb
->drNxtCNID
);
134 vcb
->freeBlocks
= SWAP_BE16 (mdb
->drFreeBks
);
135 vcb
->vcbVolBkUp
= to_bsd_time(LocalToUTC(SWAP_BE32(mdb
->drVolBkUp
)));
136 vcb
->vcbWrCnt
= SWAP_BE32 (mdb
->drWrCnt
);
137 vcb
->vcbNmRtDirs
= SWAP_BE16 (mdb
->drNmRtDirs
);
138 vcb
->vcbFilCnt
= SWAP_BE32 (mdb
->drFilCnt
);
139 vcb
->vcbDirCnt
= SWAP_BE32 (mdb
->drDirCnt
);
140 bcopy(mdb
->drFndrInfo
, vcb
->vcbFndrInfo
, sizeof(vcb
->vcbFndrInfo
));
141 if ((hfsmp
->hfs_flags
& HFS_READ_ONLY
) == 0)
142 vcb
->vcbWrCnt
++; /* Compensate for write of MDB on last flush */
144 /* convert hfs encoded name into UTF-8 string */
145 error
= hfs_to_utf8(vcb
, mdb
->drVN
, NAME_MAX
, &utf8chars
, vcb
->vcbVN
);
147 * When an HFS name cannot be encoded with the current
148 * volume encoding we use MacRoman as a fallback.
150 if (error
|| (utf8chars
== 0))
151 (void) mac_roman_to_utf8(mdb
->drVN
, NAME_MAX
, &utf8chars
, vcb
->vcbVN
);
153 hfsmp
->hfs_logBlockSize
= BestBlockSizeFit(vcb
->blockSize
, MAXBSIZE
, hfsmp
->hfs_phys_block_size
);
154 vcb
->vcbVBMIOSize
= kHFSBlockSize
;
156 hfsmp
->hfs_alt_id_sector
= HFS_ALT_SECTOR(hfsmp
->hfs_phys_block_size
,
157 hfsmp
->hfs_phys_block_count
);
159 bzero(&cndesc
, sizeof(cndesc
));
160 cndesc
.cd_parentcnid
= kHFSRootParentID
;
161 cndesc
.cd_flags
|= CD_ISMETA
;
162 bzero(&cnattr
, sizeof(cnattr
));
164 cnattr
.ca_mode
= S_IFREG
;
165 bzero(&fork
, sizeof(fork
));
168 * Set up Extents B-tree vnode
170 cndesc
.cd_nameptr
= hfs_extname
;
171 cndesc
.cd_namelen
= strlen(hfs_extname
);
172 cndesc
.cd_cnid
= cnattr
.ca_fileid
= kHFSExtentsFileID
;
173 fork
.cf_size
= SWAP_BE32(mdb
->drXTFlSize
);
174 fork
.cf_blocks
= fork
.cf_size
/ vcb
->blockSize
;
175 fork
.cf_clump
= SWAP_BE32(mdb
->drXTClpSiz
);
177 fork
.cf_extents
[0].startBlock
= SWAP_BE16(mdb
->drXTExtRec
[0].startBlock
);
178 fork
.cf_extents
[0].blockCount
= SWAP_BE16(mdb
->drXTExtRec
[0].blockCount
);
179 fork
.cf_extents
[1].startBlock
= SWAP_BE16(mdb
->drXTExtRec
[1].startBlock
);
180 fork
.cf_extents
[1].blockCount
= SWAP_BE16(mdb
->drXTExtRec
[1].blockCount
);
181 fork
.cf_extents
[2].startBlock
= SWAP_BE16(mdb
->drXTExtRec
[2].startBlock
);
182 fork
.cf_extents
[2].blockCount
= SWAP_BE16(mdb
->drXTExtRec
[2].blockCount
);
183 cnattr
.ca_blocks
= fork
.cf_blocks
;
185 error
= hfs_getnewvnode(hfsmp
, NULL
, NULL
, &cndesc
, 0, &cnattr
, &fork
,
186 &hfsmp
->hfs_extents_vp
);
187 if (error
) goto MtVolErr
;
188 error
= MacToVFSError(BTOpenPath(VTOF(hfsmp
->hfs_extents_vp
),
189 (KeyCompareProcPtr
)CompareExtentKeys
));
191 hfs_unlock(VTOC(hfsmp
->hfs_extents_vp
));
196 * Set up Catalog B-tree vnode...
198 cndesc
.cd_nameptr
= hfs_catname
;
199 cndesc
.cd_namelen
= strlen(hfs_catname
);
200 cndesc
.cd_cnid
= cnattr
.ca_fileid
= kHFSCatalogFileID
;
201 fork
.cf_size
= SWAP_BE32(mdb
->drCTFlSize
);
202 fork
.cf_blocks
= fork
.cf_size
/ vcb
->blockSize
;
203 fork
.cf_clump
= SWAP_BE32(mdb
->drCTClpSiz
);
205 fork
.cf_extents
[0].startBlock
= SWAP_BE16(mdb
->drCTExtRec
[0].startBlock
);
206 fork
.cf_extents
[0].blockCount
= SWAP_BE16(mdb
->drCTExtRec
[0].blockCount
);
207 fork
.cf_extents
[1].startBlock
= SWAP_BE16(mdb
->drCTExtRec
[1].startBlock
);
208 fork
.cf_extents
[1].blockCount
= SWAP_BE16(mdb
->drCTExtRec
[1].blockCount
);
209 fork
.cf_extents
[2].startBlock
= SWAP_BE16(mdb
->drCTExtRec
[2].startBlock
);
210 fork
.cf_extents
[2].blockCount
= SWAP_BE16(mdb
->drCTExtRec
[2].blockCount
);
211 cnattr
.ca_blocks
= fork
.cf_blocks
;
213 error
= hfs_getnewvnode(hfsmp
, NULL
, NULL
, &cndesc
, 0, &cnattr
, &fork
,
214 &hfsmp
->hfs_catalog_vp
);
216 hfs_unlock(VTOC(hfsmp
->hfs_extents_vp
));
219 error
= MacToVFSError(BTOpenPath(VTOF(hfsmp
->hfs_catalog_vp
),
220 (KeyCompareProcPtr
)CompareCatalogKeys
));
222 hfs_unlock(VTOC(hfsmp
->hfs_catalog_vp
));
223 hfs_unlock(VTOC(hfsmp
->hfs_extents_vp
));
228 * Set up dummy Allocation file vnode (used only for locking bitmap)
230 cndesc
.cd_nameptr
= hfs_vbmname
;
231 cndesc
.cd_namelen
= strlen(hfs_vbmname
);
232 cndesc
.cd_cnid
= cnattr
.ca_fileid
= kHFSAllocationFileID
;
233 bzero(&fork
, sizeof(fork
));
234 cnattr
.ca_blocks
= 0;
236 error
= hfs_getnewvnode(hfsmp
, NULL
, NULL
, &cndesc
, 0, &cnattr
, &fork
,
237 &hfsmp
->hfs_allocation_vp
);
239 hfs_unlock(VTOC(hfsmp
->hfs_catalog_vp
));
240 hfs_unlock(VTOC(hfsmp
->hfs_extents_vp
));
244 /* mark the volume dirty (clear clean unmount bit) */
245 vcb
->vcbAtrb
&= ~kHFSVolumeUnmountedMask
;
248 * all done with system files so we can unlock now...
250 hfs_unlock(VTOC(hfsmp
->hfs_allocation_vp
));
251 hfs_unlock(VTOC(hfsmp
->hfs_catalog_vp
));
252 hfs_unlock(VTOC(hfsmp
->hfs_extents_vp
));
256 error
= cat_idlookup(hfsmp
, kHFSRootFolderID
, NULL
, NULL
, NULL
);
259 if ( error
== noErr
)
261 if ( !(vcb
->vcbAtrb
& kHFSVolumeHardwareLockMask
) ) // if the disk is not write protected
263 MarkVCBDirty( vcb
); // mark VCB dirty so it will be written
268 //-- Release any resources allocated so far before exiting with an error:
270 ReleaseMetaFileVNode(hfsmp
->hfs_catalog_vp
);
271 ReleaseMetaFileVNode(hfsmp
->hfs_extents_vp
);
277 //*******************************************************************************
278 // Routine: hfs_MountHFSPlusVolume
281 //*******************************************************************************
284 OSErr
hfs_MountHFSPlusVolume(struct hfsmount
*hfsmp
, HFSPlusVolumeHeader
*vhp
,
285 off_t embeddedOffset
, u_int64_t disksize
, struct proc
*p
, void *args
, kauth_cred_t cred
)
287 register ExtendedVCB
*vcb
;
288 struct cat_desc cndesc
;
289 struct cat_attr cnattr
;
290 struct cat_fork cfork
;
292 daddr64_t spare_sectors
;
293 struct BTreeInfoRec btinfo
;
299 signature
= SWAP_BE16(vhp
->signature
);
300 version
= SWAP_BE16(vhp
->version
);
302 if (signature
== kHFSPlusSigWord
) {
303 if (version
!= kHFSPlusVersion
) {
304 printf("hfs_mount: invalid HFS+ version: %d\n", version
);
307 } else if (signature
== kHFSXSigWord
) {
308 if (version
!= kHFSXVersion
) {
309 printf("hfs_mount: invalid HFSX version: %d\n", version
);
312 /* The in-memory signature is always 'H+'. */
313 signature
= kHFSPlusSigWord
;
314 hfsmp
->hfs_flags
|= HFS_X
;
316 /* Removed printf for invalid HFS+ signature because it gives
317 * false error for UFS root volume
322 /* Block size must be at least 512 and a power of 2 */
323 blockSize
= SWAP_BE32(vhp
->blockSize
);
324 if (blockSize
< 512 || !powerof2(blockSize
))
327 /* don't mount a writable volume if its dirty, it must be cleaned by fsck_hfs */
328 if ((hfsmp
->hfs_flags
& HFS_READ_ONLY
) == 0 && hfsmp
->jnl
== NULL
&&
329 (SWAP_BE32(vhp
->attributes
) & kHFSVolumeUnmountedMask
) == 0)
332 /* Make sure we can live with the physical block size. */
333 if ((disksize
& (hfsmp
->hfs_phys_block_size
- 1)) ||
334 (embeddedOffset
& (hfsmp
->hfs_phys_block_size
- 1)) ||
335 (blockSize
< hfsmp
->hfs_phys_block_size
)) {
339 * The VolumeHeader seems OK: transfer info from it into VCB
340 * Note - the VCB starts out clear (all zeros)
342 vcb
= HFSTOVCB(hfsmp
);
344 vcb
->vcbSigWord
= signature
;
345 vcb
->vcbJinfoBlock
= SWAP_BE32(vhp
->journalInfoBlock
);
346 vcb
->vcbLsMod
= to_bsd_time(SWAP_BE32(vhp
->modifyDate
));
347 vcb
->vcbAtrb
= SWAP_BE32(vhp
->attributes
);
348 vcb
->vcbClpSiz
= SWAP_BE32(vhp
->rsrcClumpSize
);
349 vcb
->vcbNxtCNID
= SWAP_BE32(vhp
->nextCatalogID
);
350 vcb
->vcbVolBkUp
= to_bsd_time(SWAP_BE32(vhp
->backupDate
));
351 vcb
->vcbWrCnt
= SWAP_BE32(vhp
->writeCount
);
352 vcb
->vcbFilCnt
= SWAP_BE32(vhp
->fileCount
);
353 vcb
->vcbDirCnt
= SWAP_BE32(vhp
->folderCount
);
355 /* copy 32 bytes of Finder info */
356 bcopy(vhp
->finderInfo
, vcb
->vcbFndrInfo
, sizeof(vhp
->finderInfo
));
358 vcb
->vcbAlBlSt
= 0; /* hfs+ allocation blocks start at first block of volume */
359 if ((hfsmp
->hfs_flags
& HFS_READ_ONLY
) == 0)
360 vcb
->vcbWrCnt
++; /* compensate for write of Volume Header on last flush */
362 /* Now fill in the Extended VCB info */
363 vcb
->nextAllocation
= SWAP_BE32(vhp
->nextAllocation
);
364 vcb
->totalBlocks
= SWAP_BE32(vhp
->totalBlocks
);
365 vcb
->freeBlocks
= SWAP_BE32(vhp
->freeBlocks
);
366 vcb
->blockSize
= blockSize
;
367 vcb
->encodingsBitmap
= SWAP_BE64(vhp
->encodingsBitmap
);
368 vcb
->localCreateDate
= SWAP_BE32(vhp
->createDate
);
370 vcb
->hfsPlusIOPosOffset
= embeddedOffset
;
372 /* Default to no free block reserve */
373 vcb
->reserveBlocks
= 0;
376 * Update the logical block size in the mount struct
377 * (currently set up from the wrapper MDB) using the
378 * new blocksize value:
380 hfsmp
->hfs_logBlockSize
= BestBlockSizeFit(vcb
->blockSize
, MAXBSIZE
, hfsmp
->hfs_phys_block_size
);
381 vcb
->vcbVBMIOSize
= min(vcb
->blockSize
, MAXPHYSIO
);
384 * Validate and initialize the location of the alternate volume header.
386 spare_sectors
= hfsmp
->hfs_phys_block_count
-
387 (((daddr64_t
)vcb
->totalBlocks
* blockSize
) /
388 hfsmp
->hfs_phys_block_size
);
390 if (spare_sectors
> (blockSize
/ hfsmp
->hfs_phys_block_size
)) {
391 hfsmp
->hfs_alt_id_sector
= 0; /* partition has grown! */
393 hfsmp
->hfs_alt_id_sector
= (hfsmp
->hfsPlusIOPosOffset
/ hfsmp
->hfs_phys_block_size
) +
394 HFS_ALT_SECTOR(hfsmp
->hfs_phys_block_size
,
395 hfsmp
->hfs_phys_block_count
);
398 bzero(&cndesc
, sizeof(cndesc
));
399 cndesc
.cd_parentcnid
= kHFSRootParentID
;
400 cndesc
.cd_flags
|= CD_ISMETA
;
401 bzero(&cnattr
, sizeof(cnattr
));
403 cnattr
.ca_mode
= S_IFREG
;
406 * Set up Extents B-tree vnode
408 cndesc
.cd_nameptr
= hfs_extname
;
409 cndesc
.cd_namelen
= strlen(hfs_extname
);
410 cndesc
.cd_cnid
= cnattr
.ca_fileid
= kHFSExtentsFileID
;
412 cfork
.cf_size
= SWAP_BE64 (vhp
->extentsFile
.logicalSize
);
413 cfork
.cf_clump
= SWAP_BE32 (vhp
->extentsFile
.clumpSize
);
414 cfork
.cf_blocks
= SWAP_BE32 (vhp
->extentsFile
.totalBlocks
);
415 cfork
.cf_vblocks
= 0;
416 cnattr
.ca_blocks
= cfork
.cf_blocks
;
417 for (i
= 0; i
< kHFSPlusExtentDensity
; i
++) {
418 cfork
.cf_extents
[i
].startBlock
=
419 SWAP_BE32 (vhp
->extentsFile
.extents
[i
].startBlock
);
420 cfork
.cf_extents
[i
].blockCount
=
421 SWAP_BE32 (vhp
->extentsFile
.extents
[i
].blockCount
);
423 retval
= hfs_getnewvnode(hfsmp
, NULL
, NULL
, &cndesc
, 0, &cnattr
, &cfork
,
424 &hfsmp
->hfs_extents_vp
);
426 if (retval
) goto ErrorExit
;
427 retval
= MacToVFSError(BTOpenPath(VTOF(hfsmp
->hfs_extents_vp
),
428 (KeyCompareProcPtr
) CompareExtentKeysPlus
));
430 hfs_unlock(VTOC(hfsmp
->hfs_extents_vp
));
435 * Set up Catalog B-tree vnode
437 cndesc
.cd_nameptr
= hfs_catname
;
438 cndesc
.cd_namelen
= strlen(hfs_catname
);
439 cndesc
.cd_cnid
= cnattr
.ca_fileid
= kHFSCatalogFileID
;
441 cfork
.cf_size
= SWAP_BE64 (vhp
->catalogFile
.logicalSize
);
442 cfork
.cf_clump
= SWAP_BE32 (vhp
->catalogFile
.clumpSize
);
443 cfork
.cf_blocks
= SWAP_BE32 (vhp
->catalogFile
.totalBlocks
);
444 cfork
.cf_vblocks
= 0;
445 cnattr
.ca_blocks
= cfork
.cf_blocks
;
446 for (i
= 0; i
< kHFSPlusExtentDensity
; i
++) {
447 cfork
.cf_extents
[i
].startBlock
=
448 SWAP_BE32 (vhp
->catalogFile
.extents
[i
].startBlock
);
449 cfork
.cf_extents
[i
].blockCount
=
450 SWAP_BE32 (vhp
->catalogFile
.extents
[i
].blockCount
);
452 retval
= hfs_getnewvnode(hfsmp
, NULL
, NULL
, &cndesc
, 0, &cnattr
, &cfork
,
453 &hfsmp
->hfs_catalog_vp
);
455 hfs_unlock(VTOC(hfsmp
->hfs_extents_vp
));
458 retval
= MacToVFSError(BTOpenPath(VTOF(hfsmp
->hfs_catalog_vp
),
459 (KeyCompareProcPtr
) CompareExtendedCatalogKeys
));
461 hfs_unlock(VTOC(hfsmp
->hfs_catalog_vp
));
462 hfs_unlock(VTOC(hfsmp
->hfs_extents_vp
));
465 if ((hfsmp
->hfs_flags
& HFS_X
) &&
466 BTGetInformation(VTOF(hfsmp
->hfs_catalog_vp
), 0, &btinfo
) == 0) {
467 if (btinfo
.keyCompareType
== kHFSBinaryCompare
) {
468 hfsmp
->hfs_flags
|= HFS_CASE_SENSITIVE
;
469 /* Install a case-sensitive key compare */
470 (void) BTOpenPath(VTOF(hfsmp
->hfs_catalog_vp
),
471 (KeyCompareProcPtr
)cat_binarykeycompare
);
476 * Set up Allocation file vnode
478 cndesc
.cd_nameptr
= hfs_vbmname
;
479 cndesc
.cd_namelen
= strlen(hfs_vbmname
);
480 cndesc
.cd_cnid
= cnattr
.ca_fileid
= kHFSAllocationFileID
;
482 cfork
.cf_size
= SWAP_BE64 (vhp
->allocationFile
.logicalSize
);
483 cfork
.cf_clump
= SWAP_BE32 (vhp
->allocationFile
.clumpSize
);
484 cfork
.cf_blocks
= SWAP_BE32 (vhp
->allocationFile
.totalBlocks
);
485 cfork
.cf_vblocks
= 0;
486 cnattr
.ca_blocks
= cfork
.cf_blocks
;
487 for (i
= 0; i
< kHFSPlusExtentDensity
; i
++) {
488 cfork
.cf_extents
[i
].startBlock
=
489 SWAP_BE32 (vhp
->allocationFile
.extents
[i
].startBlock
);
490 cfork
.cf_extents
[i
].blockCount
=
491 SWAP_BE32 (vhp
->allocationFile
.extents
[i
].blockCount
);
493 retval
= hfs_getnewvnode(hfsmp
, NULL
, NULL
, &cndesc
, 0, &cnattr
, &cfork
,
494 &hfsmp
->hfs_allocation_vp
);
496 hfs_unlock(VTOC(hfsmp
->hfs_catalog_vp
));
497 hfs_unlock(VTOC(hfsmp
->hfs_extents_vp
));
502 * Set up Attribute B-tree vnode
504 if (vhp
->attributesFile
.totalBlocks
!= 0) {
505 cndesc
.cd_nameptr
= hfs_attrname
;
506 cndesc
.cd_namelen
= strlen(hfs_attrname
);
507 cndesc
.cd_cnid
= cnattr
.ca_fileid
= kHFSAttributesFileID
;
509 cfork
.cf_size
= SWAP_BE64 (vhp
->attributesFile
.logicalSize
);
510 cfork
.cf_clump
= SWAP_BE32 (vhp
->attributesFile
.clumpSize
);
511 cfork
.cf_blocks
= SWAP_BE32 (vhp
->attributesFile
.totalBlocks
);
512 cfork
.cf_vblocks
= 0;
513 cnattr
.ca_blocks
= cfork
.cf_blocks
;
514 for (i
= 0; i
< kHFSPlusExtentDensity
; i
++) {
515 cfork
.cf_extents
[i
].startBlock
=
516 SWAP_BE32 (vhp
->attributesFile
.extents
[i
].startBlock
);
517 cfork
.cf_extents
[i
].blockCount
=
518 SWAP_BE32 (vhp
->attributesFile
.extents
[i
].blockCount
);
520 retval
= hfs_getnewvnode(hfsmp
, NULL
, NULL
, &cndesc
, 0, &cnattr
, &cfork
,
521 &hfsmp
->hfs_attribute_vp
);
523 hfs_unlock(VTOC(hfsmp
->hfs_allocation_vp
));
524 hfs_unlock(VTOC(hfsmp
->hfs_catalog_vp
));
525 hfs_unlock(VTOC(hfsmp
->hfs_extents_vp
));
528 retval
= MacToVFSError(BTOpenPath(VTOF(hfsmp
->hfs_attribute_vp
),
529 (KeyCompareProcPtr
) hfs_attrkeycompare
));
531 hfs_unlock(VTOC(hfsmp
->hfs_attribute_vp
));
532 hfs_unlock(VTOC(hfsmp
->hfs_allocation_vp
));
533 hfs_unlock(VTOC(hfsmp
->hfs_catalog_vp
));
534 hfs_unlock(VTOC(hfsmp
->hfs_extents_vp
));
540 /* Pick up volume name and create date */
541 retval
= cat_idlookup(hfsmp
, kHFSRootFolderID
, &cndesc
, &cnattr
, NULL
);
543 if (hfsmp
->hfs_attribute_vp
)
544 hfs_unlock(VTOC(hfsmp
->hfs_attribute_vp
));
545 hfs_unlock(VTOC(hfsmp
->hfs_allocation_vp
));
546 hfs_unlock(VTOC(hfsmp
->hfs_catalog_vp
));
547 hfs_unlock(VTOC(hfsmp
->hfs_extents_vp
));
550 vcb
->vcbCrDate
= cnattr
.ca_itime
;
551 vcb
->volumeNameEncodingHint
= cndesc
.cd_encoding
;
552 bcopy(cndesc
.cd_nameptr
, vcb
->vcbVN
, min(255, cndesc
.cd_namelen
));
553 cat_releasedesc(&cndesc
);
555 /* mark the volume dirty (clear clean unmount bit) */
556 vcb
->vcbAtrb
&= ~kHFSVolumeUnmountedMask
;
557 if (hfsmp
->jnl
&& (hfsmp
->hfs_flags
& HFS_READ_ONLY
) == 0) {
558 hfs_flushvolumeheader(hfsmp
, TRUE
, 0);
562 * all done with metadata files so we can unlock now...
564 if (hfsmp
->hfs_attribute_vp
)
565 hfs_unlock(VTOC(hfsmp
->hfs_attribute_vp
));
566 hfs_unlock(VTOC(hfsmp
->hfs_allocation_vp
));
567 hfs_unlock(VTOC(hfsmp
->hfs_catalog_vp
));
568 hfs_unlock(VTOC(hfsmp
->hfs_extents_vp
));
571 // Check if we need to do late journal initialization. This only
572 // happens if a previous version of MacOS X (or 9) touched the disk.
573 // In that case hfs_late_journal_init() will go re-locate the journal
574 // and journal_info_block files and validate that they're still kosher.
576 if ( (vcb
->vcbAtrb
& kHFSVolumeJournaledMask
)
577 && (SWAP_BE32(vhp
->lastMountedVersion
) != kHFSJMountVersion
)
578 && (hfsmp
->jnl
== NULL
)) {
580 retval
= hfs_late_journal_init(hfsmp
, vhp
, args
);
584 // if the journal failed to open, then set the lastMountedVersion
585 // to be "FSK!" which fsck_hfs will see and force the fsck instead
586 // of just bailing out because the volume is journaled.
587 if (!(hfsmp
->hfs_flags
& HFS_READ_ONLY
)) {
588 HFSPlusVolumeHeader
*jvhp
;
589 daddr64_t mdb_offset
;
590 struct buf
*bp
= NULL
;
592 hfsmp
->hfs_flags
|= HFS_NEED_JNL_RESET
;
594 mdb_offset
= (daddr64_t
)((embeddedOffset
/ blockSize
) + HFS_PRI_SECTOR(blockSize
));
596 retval
= (int)buf_meta_bread(hfsmp
->hfs_devvp
, mdb_offset
, blockSize
, cred
, &bp
);
598 jvhp
= (HFSPlusVolumeHeader
*)(buf_dataptr(bp
) + HFS_PRI_OFFSET(blockSize
));
600 if (SWAP_BE16(jvhp
->signature
) == kHFSPlusSigWord
|| SWAP_BE16(jvhp
->signature
) == kHFSXSigWord
) {
601 printf ("hfs(3): Journal replay fail. Writing lastMountVersion as FSK!\n");
602 jvhp
->lastMountedVersion
= SWAP_BE32(kFSKMountVersion
);
610 // clear this so the error exit path won't try to use it
617 } else if (hfsmp
->jnl
) {
618 vfs_setflags(hfsmp
->hfs_mp
, (uint64_t)((unsigned int)MNT_JOURNALED
));
620 } else if (hfsmp
->jnl
|| ((vcb
->vcbAtrb
& kHFSVolumeJournaledMask
) && (hfsmp
->hfs_flags
& HFS_READ_ONLY
))) {
621 struct cat_attr jinfo_attr
, jnl_attr
;
623 if (hfsmp
->hfs_flags
& HFS_READ_ONLY
) {
624 vcb
->vcbAtrb
&= ~kHFSVolumeJournaledMask
;
627 // if we're here we need to fill in the fileid's for the
628 // journal and journal_info_block.
629 hfsmp
->hfs_jnlinfoblkid
= GetFileInfo(vcb
, kRootDirID
, ".journal_info_block", &jinfo_attr
, NULL
);
630 hfsmp
->hfs_jnlfileid
= GetFileInfo(vcb
, kRootDirID
, ".journal", &jnl_attr
, NULL
);
631 if (hfsmp
->hfs_jnlinfoblkid
== 0 || hfsmp
->hfs_jnlfileid
== 0) {
632 printf("hfs: danger! couldn't find the file-id's for the journal or journal_info_block\n");
633 printf("hfs: jnlfileid %d, jnlinfoblkid %d\n", hfsmp
->hfs_jnlfileid
, hfsmp
->hfs_jnlinfoblkid
);
636 if (hfsmp
->hfs_flags
& HFS_READ_ONLY
) {
637 vcb
->vcbAtrb
|= kHFSVolumeJournaledMask
;
642 * Establish a metadata allocation zone.
644 hfs_metadatazone_init(hfsmp
);
647 * Make any metadata zone adjustments.
649 if (hfsmp
->hfs_flags
& HFS_METADATA_ZONE
) {
650 /* Keep the roving allocator out of the metadata zone. */
651 if (vcb
->nextAllocation
>= hfsmp
->hfs_metazone_start
&&
652 vcb
->nextAllocation
<= hfsmp
->hfs_metazone_end
) {
653 vcb
->nextAllocation
= hfsmp
->hfs_metazone_end
+ 1;
657 /* setup private/hidden directory for unlinked files */
658 FindMetaDataDirectory(vcb
);
659 if ((hfsmp
->hfs_flags
& HFS_READ_ONLY
) == 0)
660 hfs_remove_orphans(hfsmp
);
662 if ( !(vcb
->vcbAtrb
& kHFSVolumeHardwareLockMask
) ) // if the disk is not write protected
664 MarkVCBDirty( vcb
); // mark VCB dirty so it will be written
668 * Allow hot file clustering if conditions allow.
670 if ((hfsmp
->hfs_flags
& HFS_METADATA_ZONE
) &&
671 ((hfsmp
->hfs_flags
& HFS_READ_ONLY
) == 0)) {
672 (void) hfs_recording_init(hfsmp
);
675 hfs_checkextendedsecurity(hfsmp
);
681 * A fatal error occurred and the volume cannot be mounted
682 * release any resources that we aquired...
684 if (hfsmp
->hfs_attribute_vp
)
685 ReleaseMetaFileVNode(hfsmp
->hfs_attribute_vp
);
686 ReleaseMetaFileVNode(hfsmp
->hfs_allocation_vp
);
687 ReleaseMetaFileVNode(hfsmp
->hfs_catalog_vp
);
688 ReleaseMetaFileVNode(hfsmp
->hfs_extents_vp
);
695 * ReleaseMetaFileVNode
699 static void ReleaseMetaFileVNode(struct vnode
*vp
)
703 if (vp
&& (fp
= VTOF(vp
))) {
704 if (fp
->fcbBTCBPtr
!= NULL
) {
705 (void)hfs_lock(VTOC(vp
), HFS_EXCLUSIVE_LOCK
);
706 (void) BTClosePath(fp
);
707 hfs_unlock(VTOC(vp
));
710 /* release the node even if BTClosePath fails */
717 /*************************************************************
719 * Unmounts a hfs volume.
720 * At this point vflush() has been called (to dump all non-metadata files)
722 *************************************************************/
726 hfsUnmount( register struct hfsmount
*hfsmp
, struct proc
*p
)
728 if (hfsmp
->hfs_allocation_vp
)
729 ReleaseMetaFileVNode(hfsmp
->hfs_allocation_vp
);
731 if (hfsmp
->hfs_attribute_vp
)
732 ReleaseMetaFileVNode(hfsmp
->hfs_attribute_vp
);
734 ReleaseMetaFileVNode(hfsmp
->hfs_catalog_vp
);
735 ReleaseMetaFileVNode(hfsmp
->hfs_extents_vp
);
742 * Test if fork has overflow extents.
746 overflow_extents(struct filefork
*fp
)
751 // If the vnode pointer is NULL then we're being called
752 // from hfs_remove_orphans() with a faked-up filefork
753 // and therefore it has to be an HFS+ volume. Otherwise
754 // we check through the volume header to see what type
755 // of volume we're on.
757 if (FTOV(fp
) == NULL
|| VTOVCB(FTOV(fp
))->vcbSigWord
== kHFSPlusSigWord
) {
758 if (fp
->ff_extents
[7].blockCount
== 0)
761 blocks
= fp
->ff_extents
[0].blockCount
+
762 fp
->ff_extents
[1].blockCount
+
763 fp
->ff_extents
[2].blockCount
+
764 fp
->ff_extents
[3].blockCount
+
765 fp
->ff_extents
[4].blockCount
+
766 fp
->ff_extents
[5].blockCount
+
767 fp
->ff_extents
[6].blockCount
+
768 fp
->ff_extents
[7].blockCount
;
770 if (fp
->ff_extents
[2].blockCount
== 0)
773 blocks
= fp
->ff_extents
[0].blockCount
+
774 fp
->ff_extents
[1].blockCount
+
775 fp
->ff_extents
[2].blockCount
;
778 return (fp
->ff_blocks
> blocks
);
783 * Lock HFS system file(s).
787 hfs_systemfile_lock(struct hfsmount
*hfsmp
, int flags
, enum hfslocktype locktype
)
789 if (flags
& ~SFL_VALIDMASK
)
790 panic("hfs_systemfile_lock: invalid lock request (0x%x)", (unsigned long) flags
);
792 * Locking order is Catalog file, Attributes file, Bitmap file, Extents file
794 if (flags
& SFL_CATALOG
) {
795 (void) hfs_lock(VTOC(hfsmp
->hfs_catalog_vp
), locktype
);
797 * When the catalog file has overflow extents then
798 * also acquire the extents b-tree lock if its not
801 if ((flags
& SFL_EXTENTS
) == 0 &&
802 overflow_extents(VTOF(hfsmp
->hfs_catalog_vp
))) {
803 flags
|= SFL_EXTENTS
;
806 if (flags
& SFL_ATTRIBUTE
) {
807 if (hfsmp
->hfs_attribute_vp
) {
808 (void) hfs_lock(VTOC(hfsmp
->hfs_attribute_vp
), locktype
);
810 * When the attribute file has overflow extents then
811 * also acquire the extents b-tree lock if its not
814 if ((flags
& SFL_EXTENTS
) == 0 &&
815 overflow_extents(VTOF(hfsmp
->hfs_attribute_vp
))) {
816 flags
|= SFL_EXTENTS
;
819 flags
&= ~SFL_ATTRIBUTE
;
822 if (flags
& SFL_BITMAP
) {
824 * Since the only bitmap operations are clearing and
825 * setting bits we always need exclusive access. And
826 * when we have a journal, we can "hide" behind that
827 * lock since we can only change the bitmap from
828 * within a transaction.
831 flags
&= ~SFL_BITMAP
;
833 (void) hfs_lock(VTOC(hfsmp
->hfs_allocation_vp
), HFS_EXCLUSIVE_LOCK
);
836 if (flags
& SFL_EXTENTS
) {
838 * Since the extents btree lock is recursive we always
839 * need exclusive access.
841 (void) hfs_lock(VTOC(hfsmp
->hfs_extents_vp
), HFS_EXCLUSIVE_LOCK
);
847 * unlock HFS system file(s).
851 hfs_systemfile_unlock(struct hfsmount
*hfsmp
, int flags
)
855 int numOfLockedBuffs
;
858 lastfsync
= tv
.tv_sec
;
860 if (flags
& ~SFL_VALIDMASK
)
861 panic("hfs_systemfile_unlock: invalid lock request (0x%x)", (unsigned long) flags
);
863 if (flags
& SFL_ATTRIBUTE
&& hfsmp
->hfs_attribute_vp
) {
864 if (hfsmp
->jnl
== NULL
) {
865 BTGetLastSync((FCB
*)VTOF(hfsmp
->hfs_attribute_vp
), &lastfsync
);
866 numOfLockedBuffs
= count_lock_queue();
867 if ((numOfLockedBuffs
> kMaxLockedMetaBuffers
) ||
868 ((numOfLockedBuffs
> 1) && ((tv
.tv_sec
- lastfsync
) >
869 kMaxSecsForFsync
))) {
870 hfs_btsync(hfsmp
->hfs_attribute_vp
, HFS_SYNCTRANS
);
873 hfs_unlock(VTOC(hfsmp
->hfs_attribute_vp
));
875 if (flags
& SFL_CATALOG
) {
876 if (hfsmp
->jnl
== NULL
) {
877 BTGetLastSync((FCB
*)VTOF(hfsmp
->hfs_catalog_vp
), &lastfsync
);
878 numOfLockedBuffs
= count_lock_queue();
879 if ((numOfLockedBuffs
> kMaxLockedMetaBuffers
) ||
880 ((numOfLockedBuffs
> 1) && ((tv
.tv_sec
- lastfsync
) >
881 kMaxSecsForFsync
))) {
882 hfs_btsync(hfsmp
->hfs_catalog_vp
, HFS_SYNCTRANS
);
885 hfs_unlock(VTOC(hfsmp
->hfs_catalog_vp
));
887 if (flags
& SFL_BITMAP
) {
888 hfs_unlock(VTOC(hfsmp
->hfs_allocation_vp
));
890 if (flags
& SFL_EXTENTS
) {
891 if (hfsmp
->jnl
== NULL
) {
892 BTGetLastSync((FCB
*)VTOF(hfsmp
->hfs_extents_vp
), &lastfsync
);
893 numOfLockedBuffs
= count_lock_queue();
894 if ((numOfLockedBuffs
> kMaxLockedMetaBuffers
) ||
895 ((numOfLockedBuffs
> 1) && ((tv
.tv_sec
- lastfsync
) >
896 kMaxSecsForFsync
))) {
897 hfs_btsync(hfsmp
->hfs_extents_vp
, HFS_SYNCTRANS
);
900 hfs_unlock(VTOC(hfsmp
->hfs_extents_vp
));
908 * Check to see if a vnode is locked in the current context
909 * This is to be used for debugging purposes only!!
912 void RequireFileLock(FileReference vp
, int shareable
)
916 /* The extents btree and allocation bitmap are always exclusive. */
917 if (VTOC(vp
)->c_fileid
== kHFSExtentsFileID
||
918 VTOC(vp
)->c_fileid
== kHFSAllocationFileID
) {
922 locked
= VTOC(vp
)->c_lockowner
== (void *)current_thread();
924 if (!locked
&& !shareable
) {
925 switch (VTOC(vp
)->c_fileid
) {
926 case kHFSExtentsFileID
:
927 panic("extents btree not locked! v: 0x%08X\n #\n", (u_int
)vp
);
929 case kHFSCatalogFileID
:
930 panic("catalog btree not locked! v: 0x%08X\n #\n", (u_int
)vp
);
932 case kHFSAllocationFileID
:
933 /* The allocation file can hide behind the jornal lock. */
934 if (VTOHFS(vp
)->jnl
== NULL
)
935 panic("allocation file not locked! v: 0x%08X\n #\n", (u_int
)vp
);
937 case kHFSAttributesFileID
:
938 panic("attributes btree not locked! v: 0x%08X\n #\n", (u_int
)vp
);
947 * There are three ways to qualify for ownership rights on an object:
949 * 1. (a) Your UID matches the cnode's UID.
950 * (b) The object in question is owned by "unknown"
951 * 2. (a) Permissions on the filesystem are being ignored and
952 * your UID matches the replacement UID.
953 * (b) Permissions on the filesystem are being ignored and
954 * the replacement UID is "unknown".
959 hfs_owner_rights(struct hfsmount
*hfsmp
, uid_t cnode_uid
, kauth_cred_t cred
,
960 struct proc
*p
, int invokesuperuserstatus
)
962 if ((kauth_cred_getuid(cred
) == cnode_uid
) || /* [1a] */
963 (cnode_uid
== UNKNOWNUID
) || /* [1b] */
964 ((((unsigned int)vfs_flags(HFSTOVFS(hfsmp
))) & MNT_UNKNOWNPERMISSIONS
) && /* [2] */
965 ((kauth_cred_getuid(cred
) == hfsmp
->hfs_uid
) || /* [2a] */
966 (hfsmp
->hfs_uid
== UNKNOWNUID
))) || /* [2b] */
967 (invokesuperuserstatus
&& (suser(cred
, 0) == 0))) { /* [3] */
975 unsigned long BestBlockSizeFit(unsigned long allocationBlockSize
,
976 unsigned long blockSizeLimit
,
977 unsigned long baseMultiple
) {
979 Compute the optimal (largest) block size (no larger than allocationBlockSize) that is less than the
980 specified limit but still an even multiple of the baseMultiple.
982 int baseBlockCount
, blockCount
;
983 unsigned long trialBlockSize
;
985 if (allocationBlockSize
% baseMultiple
!= 0) {
987 Whoops: the allocation blocks aren't even multiples of the specified base:
988 no amount of dividing them into even parts will be a multiple, either then!
990 return 512; /* Hope for the best */
993 /* Try the obvious winner first, to prevent 12K allocation blocks, for instance,
994 from being handled as two 6K logical blocks instead of 3 4K logical blocks.
995 Even though the former (the result of the loop below) is the larger allocation
996 block size, the latter is more efficient: */
997 if (allocationBlockSize
% PAGE_SIZE
== 0) return PAGE_SIZE
;
999 /* No clear winner exists: pick the largest even fraction <= MAXBSIZE: */
1000 baseBlockCount
= allocationBlockSize
/ baseMultiple
; /* Now guaranteed to be an even multiple */
1002 for (blockCount
= baseBlockCount
; blockCount
> 0; --blockCount
) {
1003 trialBlockSize
= blockCount
* baseMultiple
;
1004 if (allocationBlockSize
% trialBlockSize
== 0) { /* An even multiple? */
1005 if ((trialBlockSize
<= blockSizeLimit
) &&
1006 (trialBlockSize
% baseMultiple
== 0)) {
1007 return trialBlockSize
;
1012 /* Note: we should never get here, since blockCount = 1 should always work,
1013 but this is nice and safe and makes the compiler happy, too ... */
1019 * To make the HFS Plus filesystem follow UFS unlink semantics, a remove
1020 * of an active vnode is translated to a move/rename so the file appears
1021 * deleted. The destination folder for these move/renames is setup here
1022 * and a reference to it is place in hfsmp->hfs_privdir_desc.
1026 FindMetaDataDirectory(ExtendedVCB
*vcb
)
1028 struct hfsmount
* hfsmp
;
1029 struct vnode
* dvp
= NULL
;
1030 struct cnode
* dcp
= NULL
;
1031 struct FndrDirInfo
* fndrinfo
;
1032 struct cat_desc out_desc
= {0};
1033 struct proc
*p
= current_proc();
1035 cat_cookie_t cookie
;
1039 if (vcb
->vcbSigWord
!= kHFSPlusSigWord
)
1042 hfsmp
= VCBTOHFS(vcb
);
1044 if (hfsmp
->hfs_privdir_desc
.cd_parentcnid
== 0) {
1045 hfsmp
->hfs_privdir_desc
.cd_parentcnid
= kRootDirID
;
1046 hfsmp
->hfs_privdir_desc
.cd_nameptr
= hfs_privdirname
;
1047 hfsmp
->hfs_privdir_desc
.cd_namelen
= strlen(hfs_privdirname
);
1048 hfsmp
->hfs_privdir_desc
.cd_flags
= CD_ISDIR
;
1051 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_SHARED_LOCK
);
1053 error
= cat_lookup(hfsmp
, &hfsmp
->hfs_privdir_desc
, 0, NULL
,
1054 &hfsmp
->hfs_privdir_attr
, NULL
, NULL
);
1056 hfs_systemfile_unlock(hfsmp
, lockflags
);
1059 hfsmp
->hfs_metadata_createdate
= hfsmp
->hfs_privdir_attr
.ca_itime
;
1060 hfsmp
->hfs_privdir_desc
.cd_cnid
= hfsmp
->hfs_privdir_attr
.ca_fileid
;
1062 * Clear the system immutable flag if set...
1064 if ((hfsmp
->hfs_privdir_attr
.ca_flags
& SF_IMMUTABLE
) &&
1065 (hfsmp
->hfs_flags
& HFS_READ_ONLY
) == 0) {
1066 hfsmp
->hfs_privdir_attr
.ca_flags
&= ~SF_IMMUTABLE
;
1068 if ((error
= hfs_start_transaction(hfsmp
)) != 0) {
1069 return (hfsmp
->hfs_privdir_attr
.ca_fileid
);
1072 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_SHARED_LOCK
);
1073 (void) cat_update(hfsmp
, &hfsmp
->hfs_privdir_desc
,
1074 &hfsmp
->hfs_privdir_attr
, NULL
, NULL
);
1075 hfs_systemfile_unlock(hfsmp
, lockflags
);
1077 hfs_end_transaction(hfsmp
);
1079 return (hfsmp
->hfs_privdir_attr
.ca_fileid
);
1081 } else if (hfsmp
->hfs_flags
& HFS_READ_ONLY
) {
1086 /* Setup the default attributes */
1087 bzero(&hfsmp
->hfs_privdir_attr
, sizeof(struct cat_attr
));
1088 hfsmp
->hfs_privdir_attr
.ca_mode
= S_IFDIR
;
1089 hfsmp
->hfs_privdir_attr
.ca_nlink
= 2;
1090 hfsmp
->hfs_privdir_attr
.ca_itime
= vcb
->vcbCrDate
;
1092 hfsmp
->hfs_privdir_attr
.ca_mtime
= tv
.tv_sec
;
1094 /* hidden and off the desktop view */
1095 fndrinfo
= (struct FndrDirInfo
*)&hfsmp
->hfs_privdir_attr
.ca_finderinfo
;
1096 fndrinfo
->frLocation
.v
= SWAP_BE16 (22460);
1097 fndrinfo
->frLocation
.h
= SWAP_BE16 (22460);
1098 fndrinfo
->frFlags
|= SWAP_BE16 (kIsInvisible
+ kNameLocked
);
1100 if ((error
= hfs_start_transaction(hfsmp
)) != 0) {
1103 /* Reserve some space in the Catalog file. */
1104 if (cat_preflight(hfsmp
, CAT_CREATE
, &cookie
, p
) != 0) {
1105 hfs_end_transaction(hfsmp
);
1110 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_EXCLUSIVE_LOCK
);
1112 error
= cat_create(hfsmp
, &hfsmp
->hfs_privdir_desc
,
1113 &hfsmp
->hfs_privdir_attr
, &out_desc
);
1115 hfs_systemfile_unlock(hfsmp
, lockflags
);
1117 cat_postflight(hfsmp
, &cookie
, p
);
1120 hfs_volupdate(hfsmp
, VOL_UPDATE
, 0);
1122 hfs_end_transaction(hfsmp
);
1127 hfsmp
->hfs_privdir_desc
.cd_hint
= out_desc
.cd_hint
;
1128 hfsmp
->hfs_privdir_desc
.cd_cnid
= out_desc
.cd_cnid
;
1129 hfsmp
->hfs_privdir_attr
.ca_fileid
= out_desc
.cd_cnid
;
1130 hfsmp
->hfs_metadata_createdate
= vcb
->vcbCrDate
;
1132 if (hfs_vget(hfsmp
, kRootDirID
, &dvp
, 0) == 0) {
1134 dcp
->c_childhint
= out_desc
.cd_hint
;
1137 dcp
->c_touch_chgtime
= TRUE
;
1138 dcp
->c_touch_modtime
= TRUE
;
1139 (void) hfs_update(dvp
, 0);
1143 hfs_volupdate(hfsmp
, VOL_MKDIR
, 1);
1144 hfs_end_transaction(hfsmp
);
1146 cat_releasedesc(&out_desc
);
1148 return (out_desc
.cd_cnid
);
1153 GetFileInfo(ExtendedVCB
*vcb
, u_int32_t dirid
, const char *name
,
1154 struct cat_attr
*fattr
, struct cat_fork
*forkinfo
)
1156 struct hfsmount
* hfsmp
;
1157 struct vnode
* dvp
= NULL
;
1158 struct cnode
* dcp
= NULL
;
1159 struct FndrDirInfo
* fndrinfo
;
1160 struct cat_desc jdesc
;
1164 if (vcb
->vcbSigWord
!= kHFSPlusSigWord
)
1167 hfsmp
= VCBTOHFS(vcb
);
1169 memset(&jdesc
, 0, sizeof(struct cat_desc
));
1170 jdesc
.cd_parentcnid
= kRootDirID
;
1171 jdesc
.cd_nameptr
= name
;
1172 jdesc
.cd_namelen
= strlen(name
);
1174 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_SHARED_LOCK
);
1175 error
= cat_lookup(hfsmp
, &jdesc
, 0, NULL
, fattr
, forkinfo
, NULL
);
1176 hfs_systemfile_unlock(hfsmp
, lockflags
);
1179 return (fattr
->ca_fileid
);
1180 } else if (hfsmp
->hfs_flags
& HFS_READ_ONLY
) {
1184 return (0); /* XXX what callers expect on an error */
1189 * On HFS Plus Volume, there can be orphaned files. These
1190 * are files that were unlinked while busy. If the volume
1191 * was not cleanly unmounted then some of these files may
1192 * have persisted and need to be removed.
1196 hfs_remove_orphans(struct hfsmount
* hfsmp
)
1198 struct BTreeIterator
* iterator
= NULL
;
1199 struct FSBufferDescriptor btdata
;
1200 struct HFSPlusCatalogFile filerec
;
1201 struct HFSPlusCatalogKey
* keyp
;
1202 struct proc
*p
= current_proc();
1208 cat_cookie_t cookie
;
1214 int orphanedlinks
= 0;
1216 bzero(&cookie
, sizeof(cookie
));
1218 if (hfsmp
->hfs_flags
& HFS_CLEANED_ORPHANS
)
1221 vcb
= HFSTOVCB(hfsmp
);
1222 fcb
= VTOF(hfsmp
->hfs_catalog_vp
);
1224 btdata
.bufferAddress
= &filerec
;
1225 btdata
.itemSize
= sizeof(filerec
);
1226 btdata
.itemCount
= 1;
1228 MALLOC(iterator
, struct BTreeIterator
*, sizeof(*iterator
), M_TEMP
, M_WAITOK
);
1229 bzero(iterator
, sizeof(*iterator
));
1231 /* Build a key to "temp" */
1232 keyp
= (HFSPlusCatalogKey
*)&iterator
->key
;
1233 keyp
->parentID
= hfsmp
->hfs_privdir_desc
.cd_cnid
;
1234 keyp
->nodeName
.length
= 4; /* "temp" */
1235 keyp
->keyLength
= kHFSPlusCatalogKeyMinimumLength
+ keyp
->nodeName
.length
* 2;
1236 keyp
->nodeName
.unicode
[0] = 't';
1237 keyp
->nodeName
.unicode
[1] = 'e';
1238 keyp
->nodeName
.unicode
[2] = 'm';
1239 keyp
->nodeName
.unicode
[3] = 'p';
1242 * Position the iterator just before the first real temp file.
1244 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_EXCLUSIVE_LOCK
);
1245 (void) BTSearchRecord(fcb
, iterator
, NULL
, NULL
, iterator
);
1246 hfs_systemfile_unlock(hfsmp
, lockflags
);
1248 /* Visit all the temp files in the HFS+ private directory. */
1250 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_EXCLUSIVE_LOCK
);
1251 result
= BTIterateRecord(fcb
, kBTreeNextRecord
, iterator
, &btdata
, NULL
);
1252 hfs_systemfile_unlock(hfsmp
, lockflags
);
1255 if (keyp
->parentID
!= hfsmp
->hfs_privdir_desc
.cd_cnid
)
1257 if (filerec
.recordType
!= kHFSPlusFileRecord
)
1260 (void) utf8_encodestr(keyp
->nodeName
.unicode
, keyp
->nodeName
.length
* 2,
1261 filename
, &namelen
, sizeof(filename
), 0, 0);
1263 (void) sprintf(tempname
, "%s%d", HFS_DELETE_PREFIX
, filerec
.fileID
);
1266 * Delete all files named "tempxxx", where
1267 * xxx is the file's cnid in decimal.
1270 if (bcmp(tempname
, filename
, namelen
) == 0) {
1271 struct filefork dfork
;
1272 struct filefork rfork
;
1275 bzero(&dfork
, sizeof(dfork
));
1276 bzero(&rfork
, sizeof(rfork
));
1277 bzero(&cnode
, sizeof(cnode
));
1279 if (hfs_start_transaction(hfsmp
) != 0) {
1280 printf("hfs_remove_orphans: failed to start transaction\n");
1286 * Reserve some space in the Catalog file.
1288 if (cat_preflight(hfsmp
, CAT_DELETE
, &cookie
, p
) != 0) {
1289 printf("hfs_remove_orphans: cat_preflight failed\n");
1294 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
| SFL_ATTRIBUTE
| SFL_EXTENTS
| SFL_BITMAP
, HFS_EXCLUSIVE_LOCK
);
1297 /* Build a fake cnode */
1298 cat_convertattr(hfsmp
, (CatalogRecord
*)&filerec
, &cnode
.c_attr
,
1299 &dfork
.ff_data
, &rfork
.ff_data
);
1300 cnode
.c_desc
.cd_parentcnid
= hfsmp
->hfs_privdir_desc
.cd_cnid
;
1301 cnode
.c_desc
.cd_nameptr
= filename
;
1302 cnode
.c_desc
.cd_namelen
= namelen
;
1303 cnode
.c_desc
.cd_cnid
= cnode
.c_attr
.ca_fileid
;
1304 cnode
.c_blocks
= dfork
.ff_blocks
+ rfork
.ff_blocks
;
1306 /* Position iterator at previous entry */
1307 if (BTIterateRecord(fcb
, kBTreePrevRecord
, iterator
,
1312 /* Truncate the file to zero (both forks) */
1313 if (dfork
.ff_blocks
> 0) {
1316 dfork
.ff_cp
= &cnode
;
1317 cnode
.c_datafork
= &dfork
;
1318 cnode
.c_rsrcfork
= NULL
;
1319 fsize
= (u_int64_t
)dfork
.ff_blocks
* (u_int64_t
)HFSTOVCB(hfsmp
)->blockSize
;
1321 if (fsize
> HFS_BIGFILE_SIZE
&& overflow_extents(&dfork
)) {
1322 fsize
-= HFS_BIGFILE_SIZE
;
1327 if (TruncateFileC(vcb
, (FCB
*)&dfork
, fsize
, false) != 0) {
1328 printf("error truncting data fork!\n");
1333 // if we're iteratively truncating this file down,
1334 // then end the transaction and start a new one so
1335 // that no one transaction gets too big.
1337 if (fsize
> 0 && started_tr
) {
1338 hfs_end_transaction(hfsmp
);
1339 if (hfs_start_transaction(hfsmp
) != 0) {
1347 if (rfork
.ff_blocks
> 0) {
1348 rfork
.ff_cp
= &cnode
;
1349 cnode
.c_datafork
= NULL
;
1350 cnode
.c_rsrcfork
= &rfork
;
1351 if (TruncateFileC(vcb
, (FCB
*)&rfork
, 0, false) != 0) {
1352 printf("error truncting rsrc fork!\n");
1357 /* Remove the file record from the Catalog */
1358 if (cat_delete(hfsmp
, &cnode
.c_desc
, &cnode
.c_attr
) != 0) {
1359 printf("hfs_remove_oprhans: error deleting cat rec for id %d!\n", cnode
.c_desc
.cd_cnid
);
1360 hfs_volupdate(hfsmp
, VOL_UPDATE
, 0);
1365 /* Delete any attributes, ignore errors */
1366 (void) hfs_removeallattr(hfsmp
, cnode
.c_fileid
);
1368 /* Update parent and volume counts */
1369 hfsmp
->hfs_privdir_attr
.ca_entries
--;
1370 (void)cat_update(hfsmp
, &hfsmp
->hfs_privdir_desc
,
1371 &hfsmp
->hfs_privdir_attr
, NULL
, NULL
);
1372 hfs_volupdate(hfsmp
, VOL_RMFILE
, 0);
1374 /* Drop locks and end the transaction */
1375 hfs_systemfile_unlock(hfsmp
, lockflags
);
1376 cat_postflight(hfsmp
, &cookie
, p
);
1377 catlock
= catreserve
= 0;
1379 hfs_end_transaction(hfsmp
);
1385 if (orphanedlinks
> 0)
1386 printf("HFS: Removed %d orphaned unlinked files\n", orphanedlinks
);
1389 hfs_systemfile_unlock(hfsmp
, lockflags
);
1392 cat_postflight(hfsmp
, &cookie
, p
);
1395 hfs_end_transaction(hfsmp
);
1398 FREE(iterator
, M_TEMP
);
1399 hfsmp
->hfs_flags
|= HFS_CLEANED_ORPHANS
;
1404 * This will return the correct logical block size for a given vnode.
1405 * For most files, it is the allocation block size, for meta data like
1406 * BTrees, this is kept as part of the BTree private nodeSize
1409 GetLogicalBlockSize(struct vnode
*vp
)
1411 u_int32_t logBlockSize
;
1413 DBG_ASSERT(vp
!= NULL
);
1415 /* start with default */
1416 logBlockSize
= VTOHFS(vp
)->hfs_logBlockSize
;
1418 if (vnode_issystem(vp
)) {
1419 if (VTOF(vp
)->fcbBTCBPtr
!= NULL
) {
1420 BTreeInfoRec bTreeInfo
;
1423 * We do not lock the BTrees, because if we are getting block..then the tree
1424 * should be locked in the first place.
1425 * We just want the nodeSize wich will NEVER change..so even if the world
1426 * is changing..the nodeSize should remain the same. Which argues why lock
1427 * it in the first place??
1430 (void) BTGetInformation (VTOF(vp
), kBTreeInfoVersion
, &bTreeInfo
);
1432 logBlockSize
= bTreeInfo
.nodeSize
;
1434 } else if (VTOC(vp
)->c_fileid
== kHFSAllocationFileID
) {
1435 logBlockSize
= VTOVCB(vp
)->vcbVBMIOSize
;
1439 DBG_ASSERT(logBlockSize
> 0);
1441 return logBlockSize
;
1446 hfs_freeblks(struct hfsmount
* hfsmp
, int wantreserve
)
1448 ExtendedVCB
*vcb
= HFSTOVCB(hfsmp
);
1451 HFS_MOUNT_LOCK(hfsmp
, TRUE
);
1452 freeblks
= vcb
->freeBlocks
;
1454 if (freeblks
> vcb
->reserveBlocks
)
1455 freeblks
-= vcb
->reserveBlocks
;
1459 if (freeblks
> vcb
->loanedBlocks
)
1460 freeblks
-= vcb
->loanedBlocks
;
1463 HFS_MOUNT_UNLOCK(hfsmp
, TRUE
);
1465 #ifdef HFS_SPARSE_DEV
1467 * When the underlying device is sparse, check the
1468 * available space on the backing store volume.
1470 if ((hfsmp
->hfs_flags
& HFS_HAS_SPARSE_DEVICE
) && hfsmp
->hfs_backingfs_rootvp
) {
1471 struct vfsstatfs
*vfsp
; /* 272 bytes */
1472 u_int32_t vfreeblks
;
1473 u_int32_t loanedblks
;
1474 struct mount
* backingfs_mp
;
1476 backingfs_mp
= vnode_mount(hfsmp
->hfs_backingfs_rootvp
);
1478 if (vfsp
= vfs_statfs(backingfs_mp
)) {
1479 HFS_MOUNT_LOCK(hfsmp
, TRUE
);
1480 vfreeblks
= (u_int32_t
)vfsp
->f_bavail
;
1481 /* Normalize block count if needed. */
1482 if (vfsp
->f_bsize
!= vcb
->blockSize
) {
1483 vfreeblks
= ((u_int64_t
)vfreeblks
* (u_int64_t
)(vfsp
->f_bsize
)) / vcb
->blockSize
;
1485 if (vfreeblks
> hfsmp
->hfs_sparsebandblks
)
1486 vfreeblks
-= hfsmp
->hfs_sparsebandblks
;
1490 /* Take into account any delayed allocations. */
1491 loanedblks
= 2 * vcb
->loanedBlocks
;
1492 if (vfreeblks
> loanedblks
)
1493 vfreeblks
-= loanedblks
;
1497 freeblks
= MIN(vfreeblks
, freeblks
);
1498 HFS_MOUNT_UNLOCK(hfsmp
, TRUE
);
1501 #endif /* HFS_SPARSE_DEV */
1507 * Map HFS Common errors (negative) to BSD error codes (positive).
1508 * Positive errors (ie BSD errors) are passed through unchanged.
1510 short MacToVFSError(OSErr err
)
1516 case dskFulErr
: /* -34 */
1517 case btNoSpaceAvail
: /* -32733 */
1519 case fxOvFlErr
: /* -32750 */
1522 case btBadNode
: /* -32731 */
1525 case memFullErr
: /* -108 */
1526 return ENOMEM
; /* +12 */
1528 case cmExists
: /* -32718 */
1529 case btExists
: /* -32734 */
1530 return EEXIST
; /* +17 */
1532 case cmNotFound
: /* -32719 */
1533 case btNotFound
: /* -32735 */
1534 return ENOENT
; /* 28 */
1536 case cmNotEmpty
: /* -32717 */
1537 return ENOTEMPTY
; /* 66 */
1539 case cmFThdDirErr
: /* -32714 */
1540 return EISDIR
; /* 21 */
1542 case fxRangeErr
: /* -32751 */
1545 case bdNamErr
: /* -37 */
1546 return ENAMETOOLONG
; /* 63 */
1548 case paramErr
: /* -50 */
1549 case fileBoundsErr
: /* -1309 */
1550 return EINVAL
; /* +22 */
1552 case fsBTBadNodeSize
:
1556 return EIO
; /* +5 */
1562 * Find the current thread's directory hint for a given index.
1564 * Requires an exclusive lock on directory cnode.
1568 hfs_getdirhint(struct cnode
*dcp
, int index
)
1571 directoryhint_t
*hint
;
1572 boolean_t need_remove
, need_init
;
1578 * Look for an existing hint first. If not found, create a new one (when
1579 * the list is not full) or recycle the oldest hint. Since new hints are
1580 * always added to the head of the list, the last hint is always the
1583 TAILQ_FOREACH(hint
, &dcp
->c_hintlist
, dh_link
) {
1584 if (hint
->dh_index
== index
)
1587 if (hint
!= NULL
) { /* found an existing hint */
1590 } else { /* cannot find an existing hint */
1592 if (dcp
->c_dirhintcnt
< HFS_MAXDIRHINTS
) { /* we don't need recycling */
1593 /* Create a default directory hint */
1594 MALLOC_ZONE(hint
, directoryhint_t
*, sizeof(directoryhint_t
), M_HFSDIRHINT
, M_WAITOK
);
1595 ++dcp
->c_dirhintcnt
;
1596 need_remove
= false;
1597 } else { /* recycle the last (i.e., the oldest) hint */
1598 hint
= TAILQ_LAST(&dcp
->c_hintlist
, hfs_hinthead
);
1599 if ((name
= hint
->dh_desc
.cd_nameptr
)) {
1600 hint
->dh_desc
.cd_nameptr
= NULL
;
1601 vfs_removename(name
);
1608 TAILQ_REMOVE(&dcp
->c_hintlist
, hint
, dh_link
);
1610 TAILQ_INSERT_HEAD(&dcp
->c_hintlist
, hint
, dh_link
);
1613 hint
->dh_index
= index
;
1614 hint
->dh_desc
.cd_flags
= 0;
1615 hint
->dh_desc
.cd_encoding
= 0;
1616 hint
->dh_desc
.cd_namelen
= 0;
1617 hint
->dh_desc
.cd_nameptr
= NULL
;
1618 hint
->dh_desc
.cd_parentcnid
= dcp
->c_cnid
;
1619 hint
->dh_desc
.cd_hint
= dcp
->c_childhint
;
1620 hint
->dh_desc
.cd_cnid
= 0;
1622 hint
->dh_time
= tv
.tv_sec
;
1627 * Release a single directory hint.
1629 * Requires an exclusive lock on directory cnode.
1633 hfs_reldirhint(struct cnode
*dcp
, directoryhint_t
* relhint
)
1637 TAILQ_REMOVE(&dcp
->c_hintlist
, relhint
, dh_link
);
1638 name
= relhint
->dh_desc
.cd_nameptr
;
1640 relhint
->dh_desc
.cd_nameptr
= NULL
;
1641 vfs_removename(name
);
1643 FREE_ZONE(relhint
, sizeof(directoryhint_t
), M_HFSDIRHINT
);
1644 --dcp
->c_dirhintcnt
;
1648 * Release directory hints for given directory
1650 * Requires an exclusive lock on directory cnode.
1654 hfs_reldirhints(struct cnode
*dcp
, int stale_hints_only
)
1657 directoryhint_t
*hint
, *prev
;
1660 if (stale_hints_only
)
1663 /* searching from the oldest to the newest, so we can stop early when releasing stale hints only */
1664 for (hint
= TAILQ_LAST(&dcp
->c_hintlist
, hfs_hinthead
); hint
!= NULL
; hint
= prev
) {
1665 if (stale_hints_only
&& (tv
.tv_sec
- hint
->dh_time
) < HFS_DIRHINT_TTL
)
1666 break; /* stop here if this entry is too new */
1667 name
= hint
->dh_desc
.cd_nameptr
;
1669 hint
->dh_desc
.cd_nameptr
= NULL
;
1670 vfs_removename(name
);
1672 prev
= TAILQ_PREV(hint
, hfs_hinthead
, dh_link
); /* must save this pointer before calling FREE_ZONE on this node */
1673 TAILQ_REMOVE(&dcp
->c_hintlist
, hint
, dh_link
);
1674 FREE_ZONE(hint
, sizeof(directoryhint_t
), M_HFSDIRHINT
);
1675 --dcp
->c_dirhintcnt
;
1681 * Perform a case-insensitive compare of two UTF-8 filenames.
1683 * Returns 0 if the strings match.
1687 hfs_namecmp(const char *str1
, size_t len1
, const char *str2
, size_t len2
)
1689 u_int16_t
*ustr1
, *ustr2
;
1690 size_t ulen1
, ulen2
;
1697 maxbytes
= kHFSPlusMaxFileNameChars
<< 1;
1698 MALLOC(ustr1
, u_int16_t
*, maxbytes
<< 1, M_TEMP
, M_WAITOK
);
1699 ustr2
= ustr1
+ (maxbytes
>> 1);
1701 if (utf8_decodestr(str1
, len1
, ustr1
, &ulen1
, maxbytes
, ':', 0) != 0)
1703 if (utf8_decodestr(str2
, len2
, ustr2
, &ulen2
, maxbytes
, ':', 0) != 0)
1706 cmp
= FastUnicodeCompare(ustr1
, ulen1
>>1, ustr2
, ulen2
>>1);
1708 FREE(ustr1
, M_TEMP
);
1715 hfs_early_journal_init(struct hfsmount
*hfsmp
, HFSPlusVolumeHeader
*vhp
,
1716 void *_args
, off_t embeddedOffset
, daddr64_t mdb_offset
,
1717 HFSMasterDirectoryBlock
*mdbp
, kauth_cred_t cred
)
1719 JournalInfoBlock
*jibp
;
1720 struct buf
*jinfo_bp
, *bp
;
1721 int sectors_per_fsblock
, arg_flags
=0, arg_tbufsz
=0;
1722 int retval
, blksize
= hfsmp
->hfs_phys_block_size
;
1723 struct vnode
*devvp
;
1724 struct hfs_mount_args
*args
= _args
;
1726 devvp
= hfsmp
->hfs_devvp
;
1728 if (args
!= NULL
&& (args
->flags
& HFSFSMNT_EXTENDED_ARGS
)) {
1729 arg_flags
= args
->journal_flags
;
1730 arg_tbufsz
= args
->journal_tbuffer_size
;
1733 sectors_per_fsblock
= SWAP_BE32(vhp
->blockSize
) / blksize
;
1735 retval
= (int)buf_meta_bread(devvp
,
1736 (daddr64_t
)((embeddedOffset
/blksize
) +
1737 (SWAP_BE32(vhp
->journalInfoBlock
)*sectors_per_fsblock
)),
1738 SWAP_BE32(vhp
->blockSize
), cred
, &jinfo_bp
);
1742 jibp
= (JournalInfoBlock
*)buf_dataptr(jinfo_bp
);
1743 jibp
->flags
= SWAP_BE32(jibp
->flags
);
1744 jibp
->offset
= SWAP_BE64(jibp
->offset
);
1745 jibp
->size
= SWAP_BE64(jibp
->size
);
1747 if (jibp
->flags
& kJIJournalInFSMask
) {
1748 hfsmp
->jvp
= hfsmp
->hfs_devvp
;
1750 printf("hfs: journal not stored in fs! don't know what to do.\n");
1751 buf_brelse(jinfo_bp
);
1755 // save this off for the hack-y check in hfs_remove()
1756 hfsmp
->jnl_start
= jibp
->offset
/ SWAP_BE32(vhp
->blockSize
);
1757 hfsmp
->jnl_size
= jibp
->size
;
1759 if ((hfsmp
->hfs_flags
& HFS_READ_ONLY
) && (vfs_flags(hfsmp
->hfs_mp
) & MNT_ROOTFS
) == 0) {
1760 // if the file system is read-only, check if the journal is empty.
1761 // if it is, then we can allow the mount. otherwise we have to
1763 retval
= journal_is_clean(hfsmp
->jvp
,
1764 jibp
->offset
+ embeddedOffset
,
1767 hfsmp
->hfs_phys_block_size
);
1771 buf_brelse(jinfo_bp
);
1774 printf("hfs: early journal init: volume on %s is read-only and journal is dirty. Can not mount volume.\n",
1781 if (jibp
->flags
& kJIJournalNeedInitMask
) {
1782 printf("hfs: Initializing the journal (joffset 0x%llx sz 0x%llx)...\n",
1783 jibp
->offset
+ embeddedOffset
, jibp
->size
);
1784 hfsmp
->jnl
= journal_create(hfsmp
->jvp
,
1785 jibp
->offset
+ embeddedOffset
,
1791 hfs_sync_metadata
, hfsmp
->hfs_mp
);
1793 // no need to start a transaction here... if this were to fail
1794 // we'd just re-init it on the next mount.
1795 jibp
->flags
&= ~kJIJournalNeedInitMask
;
1796 jibp
->flags
= SWAP_BE32(jibp
->flags
);
1797 jibp
->offset
= SWAP_BE64(jibp
->offset
);
1798 jibp
->size
= SWAP_BE64(jibp
->size
);
1799 buf_bwrite(jinfo_bp
);
1803 //printf("hfs: Opening the journal (joffset 0x%llx sz 0x%llx vhp_blksize %d)...\n",
1804 // jibp->offset + embeddedOffset,
1805 // jibp->size, SWAP_BE32(vhp->blockSize));
1807 hfsmp
->jnl
= journal_open(hfsmp
->jvp
,
1808 jibp
->offset
+ embeddedOffset
,
1814 hfs_sync_metadata
, hfsmp
->hfs_mp
);
1816 buf_brelse(jinfo_bp
);
1820 if (hfsmp
->jnl
&& mdbp
) {
1821 // reload the mdb because it could have changed
1822 // if the journal had to be replayed.
1823 if (mdb_offset
== 0) {
1824 mdb_offset
= (daddr64_t
)((embeddedOffset
/ blksize
) + HFS_PRI_SECTOR(blksize
));
1826 retval
= (int)buf_meta_bread(devvp
, mdb_offset
, blksize
, cred
, &bp
);
1829 printf("hfs: failed to reload the mdb after opening the journal (retval %d)!\n",
1833 bcopy((char *)buf_dataptr(bp
) + HFS_PRI_OFFSET(blksize
), mdbp
, 512);
1840 //printf("journal @ 0x%x\n", hfsmp->jnl);
1842 // if we expected the journal to be there and we couldn't
1843 // create it or open it then we have to bail out.
1844 if (hfsmp
->jnl
== NULL
) {
1845 printf("hfs: early jnl init: failed to open/create the journal (retval %d).\n", retval
);
1854 // This function will go and re-locate the .journal_info_block and
1855 // the .journal files in case they moved (which can happen if you
1856 // run Norton SpeedDisk). If we fail to find either file we just
1857 // disable journaling for this volume and return. We turn off the
1858 // journaling bit in the vcb and assume it will get written to disk
1859 // later (if it doesn't on the next mount we'd do the same thing
1860 // again which is harmless). If we disable journaling we don't
1861 // return an error so that the volume is still mountable.
1863 // If the info we find for the .journal_info_block and .journal files
1864 // isn't what we had stored, we re-set our cached info and proceed
1865 // with opening the journal normally.
1868 hfs_late_journal_init(struct hfsmount
*hfsmp
, HFSPlusVolumeHeader
*vhp
, void *_args
)
1870 JournalInfoBlock
*jibp
;
1871 struct buf
*jinfo_bp
, *bp
;
1872 int sectors_per_fsblock
, arg_flags
=0, arg_tbufsz
=0;
1873 int retval
, need_flush
= 0, write_jibp
= 0;
1874 struct vnode
*devvp
;
1875 struct cat_attr jib_attr
, jattr
;
1876 struct cat_fork jib_fork
, jfork
;
1879 struct hfs_mount_args
*args
= _args
;
1881 devvp
= hfsmp
->hfs_devvp
;
1882 vcb
= HFSTOVCB(hfsmp
);
1884 if (args
!= NULL
&& (args
->flags
& HFSFSMNT_EXTENDED_ARGS
)) {
1885 if (args
->journal_disable
) {
1889 arg_flags
= args
->journal_flags
;
1890 arg_tbufsz
= args
->journal_tbuffer_size
;
1893 fid
= GetFileInfo(vcb
, kRootDirID
, ".journal_info_block", &jib_attr
, &jib_fork
);
1894 if (fid
== 0 || jib_fork
.cf_extents
[0].startBlock
== 0 || jib_fork
.cf_size
== 0) {
1895 printf("hfs: can't find the .journal_info_block! disabling journaling (start: %d).\n",
1896 jib_fork
.cf_extents
[0].startBlock
);
1897 vcb
->vcbAtrb
&= ~kHFSVolumeJournaledMask
;
1900 hfsmp
->hfs_jnlinfoblkid
= fid
;
1902 // make sure the journal_info_block begins where we think it should.
1903 if (SWAP_BE32(vhp
->journalInfoBlock
) != jib_fork
.cf_extents
[0].startBlock
) {
1904 printf("hfs: The journal_info_block moved (was: %d; is: %d). Fixing up\n",
1905 SWAP_BE32(vhp
->journalInfoBlock
), jib_fork
.cf_extents
[0].startBlock
);
1907 vcb
->vcbJinfoBlock
= jib_fork
.cf_extents
[0].startBlock
;
1908 vhp
->journalInfoBlock
= SWAP_BE32(jib_fork
.cf_extents
[0].startBlock
);
1912 sectors_per_fsblock
= SWAP_BE32(vhp
->blockSize
) / hfsmp
->hfs_phys_block_size
;
1913 retval
= (int)buf_meta_bread(devvp
,
1914 (daddr64_t
)(vcb
->hfsPlusIOPosOffset
/ hfsmp
->hfs_phys_block_size
+
1915 (SWAP_BE32(vhp
->journalInfoBlock
)*sectors_per_fsblock
)),
1916 SWAP_BE32(vhp
->blockSize
), NOCRED
, &jinfo_bp
);
1918 printf("hfs: can't read journal info block. disabling journaling.\n");
1919 vcb
->vcbAtrb
&= ~kHFSVolumeJournaledMask
;
1923 jibp
= (JournalInfoBlock
*)buf_dataptr(jinfo_bp
);
1924 jibp
->flags
= SWAP_BE32(jibp
->flags
);
1925 jibp
->offset
= SWAP_BE64(jibp
->offset
);
1926 jibp
->size
= SWAP_BE64(jibp
->size
);
1928 fid
= GetFileInfo(vcb
, kRootDirID
, ".journal", &jattr
, &jfork
);
1929 if (fid
== 0 || jfork
.cf_extents
[0].startBlock
== 0 || jfork
.cf_size
== 0) {
1930 printf("hfs: can't find the journal file! disabling journaling (start: %d)\n",
1931 jfork
.cf_extents
[0].startBlock
);
1932 buf_brelse(jinfo_bp
);
1933 vcb
->vcbAtrb
&= ~kHFSVolumeJournaledMask
;
1936 hfsmp
->hfs_jnlfileid
= fid
;
1938 // make sure the journal file begins where we think it should.
1939 if ((jibp
->offset
/ (u_int64_t
)vcb
->blockSize
) != jfork
.cf_extents
[0].startBlock
) {
1940 printf("hfs: The journal file moved (was: %lld; is: %d). Fixing up\n",
1941 (jibp
->offset
/ (u_int64_t
)vcb
->blockSize
), jfork
.cf_extents
[0].startBlock
);
1943 jibp
->offset
= (u_int64_t
)jfork
.cf_extents
[0].startBlock
* (u_int64_t
)vcb
->blockSize
;
1947 // check the size of the journal file.
1948 if (jibp
->size
!= (u_int64_t
)jfork
.cf_extents
[0].blockCount
*vcb
->blockSize
) {
1949 printf("hfs: The journal file changed size! (was %lld; is %lld). Fixing up.\n",
1950 jibp
->size
, (u_int64_t
)jfork
.cf_extents
[0].blockCount
*vcb
->blockSize
);
1952 jibp
->size
= (u_int64_t
)jfork
.cf_extents
[0].blockCount
* vcb
->blockSize
;
1956 if (jibp
->flags
& kJIJournalInFSMask
) {
1957 hfsmp
->jvp
= hfsmp
->hfs_devvp
;
1959 printf("hfs: journal not stored in fs! don't know what to do.\n");
1960 buf_brelse(jinfo_bp
);
1964 // save this off for the hack-y check in hfs_remove()
1965 hfsmp
->jnl_start
= jibp
->offset
/ SWAP_BE32(vhp
->blockSize
);
1966 hfsmp
->jnl_size
= jibp
->size
;
1968 if ((hfsmp
->hfs_flags
& HFS_READ_ONLY
) && (vfs_flags(hfsmp
->hfs_mp
) & MNT_ROOTFS
) == 0) {
1969 // if the file system is read-only, check if the journal is empty.
1970 // if it is, then we can allow the mount. otherwise we have to
1972 retval
= journal_is_clean(hfsmp
->jvp
,
1973 jibp
->offset
+ (off_t
)vcb
->hfsPlusIOPosOffset
,
1976 hfsmp
->hfs_phys_block_size
);
1980 buf_brelse(jinfo_bp
);
1983 printf("hfs: late journal init: volume on %s is read-only and journal is dirty. Can not mount volume.\n",
1990 if (jibp
->flags
& kJIJournalNeedInitMask
) {
1991 printf("hfs: Initializing the journal (joffset 0x%llx sz 0x%llx)...\n",
1992 jibp
->offset
+ (off_t
)vcb
->hfsPlusIOPosOffset
, jibp
->size
);
1993 hfsmp
->jnl
= journal_create(hfsmp
->jvp
,
1994 jibp
->offset
+ (off_t
)vcb
->hfsPlusIOPosOffset
,
1997 hfsmp
->hfs_phys_block_size
,
2000 hfs_sync_metadata
, hfsmp
->hfs_mp
);
2002 // no need to start a transaction here... if this were to fail
2003 // we'd just re-init it on the next mount.
2004 jibp
->flags
&= ~kJIJournalNeedInitMask
;
2009 // if we weren't the last person to mount this volume
2010 // then we need to throw away the journal because it
2011 // is likely that someone else mucked with the disk.
2012 // if the journal is empty this is no big deal. if the
2013 // disk is dirty this prevents us from replaying the
2014 // journal over top of changes that someone else made.
2016 arg_flags
|= JOURNAL_RESET
;
2018 //printf("hfs: Opening the journal (joffset 0x%llx sz 0x%llx vhp_blksize %d)...\n",
2019 // jibp->offset + (off_t)vcb->hfsPlusIOPosOffset,
2020 // jibp->size, SWAP_BE32(vhp->blockSize));
2022 hfsmp
->jnl
= journal_open(hfsmp
->jvp
,
2023 jibp
->offset
+ (off_t
)vcb
->hfsPlusIOPosOffset
,
2026 hfsmp
->hfs_phys_block_size
,
2029 hfs_sync_metadata
, hfsmp
->hfs_mp
);
2034 jibp
->flags
= SWAP_BE32(jibp
->flags
);
2035 jibp
->offset
= SWAP_BE64(jibp
->offset
);
2036 jibp
->size
= SWAP_BE64(jibp
->size
);
2038 buf_bwrite(jinfo_bp
);
2040 buf_brelse(jinfo_bp
);
2045 //printf("journal @ 0x%x\n", hfsmp->jnl);
2047 // if we expected the journal to be there and we couldn't
2048 // create it or open it then we have to bail out.
2049 if (hfsmp
->jnl
== NULL
) {
2050 printf("hfs: late jnl init: failed to open/create the journal (retval %d).\n", retval
);
2058 * Calculate the allocation zone for metadata.
2060 * This zone includes the following:
2061 * Allocation Bitmap file
2062 * Overflow Extents file
2065 * Clustered Hot files
2068 * METADATA ALLOCATION ZONE
2069 * ____________________________________________________________________________
2071 * | BM | JF | OEF | CATALOG |---> | HOT FILES |
2072 * |____|____|_____|_______________|______________________________|___________|
2074 * <------------------------------- N * 128 MB ------------------------------->
2077 #define GIGABYTE (u_int64_t)(1024*1024*1024)
2079 #define OVERFLOW_DEFAULT_SIZE (4*1024*1024)
2080 #define OVERFLOW_MAXIMUM_SIZE (128*1024*1024)
2081 #define JOURNAL_DEFAULT_SIZE (8*1024*1024)
2082 #define JOURNAL_MAXIMUM_SIZE (512*1024*1024)
2083 #define HOTBAND_MINIMUM_SIZE (10*1024*1024)
2084 #define HOTBAND_MAXIMUM_SIZE (512*1024*1024)
2087 hfs_metadatazone_init(struct hfsmount
*hfsmp
)
2097 vcb
= HFSTOVCB(hfsmp
);
2098 fs_size
= (u_int64_t
)vcb
->blockSize
* (u_int64_t
)vcb
->totalBlocks
;
2101 * For volumes less than 10 GB, don't bother.
2103 if (fs_size
< ((u_int64_t
)10 * GIGABYTE
))
2106 * Skip non-journaled volumes as well.
2108 if (hfsmp
->jnl
== NULL
)
2112 * Start with allocation bitmap (a fixed size).
2114 zonesize
= roundup(vcb
->totalBlocks
/ 8, vcb
->vcbVBMIOSize
);
2117 * Overflow Extents file gets 4 MB per 100 GB.
2119 items
= fs_size
/ ((u_int64_t
)100 * GIGABYTE
);
2120 filesize
= (u_int64_t
)(items
+ 1) * OVERFLOW_DEFAULT_SIZE
;
2121 if (filesize
> OVERFLOW_MAXIMUM_SIZE
)
2122 filesize
= OVERFLOW_MAXIMUM_SIZE
;
2123 zonesize
+= filesize
;
2124 hfsmp
->hfs_overflow_maxblks
= filesize
/ vcb
->blockSize
;
2127 * Plan for at least 8 MB of journal for each
2128 * 100 GB of disk space (up to a 512 MB).
2130 items
= fs_size
/ ((u_int64_t
)100 * GIGABYTE
);
2131 filesize
= (u_int64_t
)(items
+ 1) * JOURNAL_DEFAULT_SIZE
;
2132 if (filesize
> JOURNAL_MAXIMUM_SIZE
)
2133 filesize
= JOURNAL_MAXIMUM_SIZE
;
2134 zonesize
+= filesize
;
2137 * Catalog file gets 10 MB per 1 GB.
2139 * How about considering the current catalog size (used nodes * node size)
2140 * and the current file data size to help estimate the required
2143 filesize
= MIN((fs_size
/ 1024) * 10, GIGABYTE
);
2144 hfsmp
->hfs_catalog_maxblks
= filesize
/ vcb
->blockSize
;
2145 zonesize
+= filesize
;
2148 * Add space for hot file region.
2150 * ...for now, use 5 MB per 1 GB (0.5 %)
2152 filesize
= (fs_size
/ 1024) * 5;
2153 if (filesize
> HOTBAND_MAXIMUM_SIZE
)
2154 filesize
= HOTBAND_MAXIMUM_SIZE
;
2155 else if (filesize
< HOTBAND_MINIMUM_SIZE
)
2156 filesize
= HOTBAND_MINIMUM_SIZE
;
2158 * Calculate user quota file requirements.
2160 items
= QF_USERS_PER_GB
* (fs_size
/ GIGABYTE
);
2161 if (items
< QF_MIN_USERS
)
2162 items
= QF_MIN_USERS
;
2163 else if (items
> QF_MAX_USERS
)
2164 items
= QF_MAX_USERS
;
2165 if (!powerof2(items
)) {
2173 filesize
+= (items
+ 1) * sizeof(struct dqblk
);
2175 * Calculate group quota file requirements.
2178 items
= QF_GROUPS_PER_GB
* (fs_size
/ GIGABYTE
);
2179 if (items
< QF_MIN_GROUPS
)
2180 items
= QF_MIN_GROUPS
;
2181 else if (items
> QF_MAX_GROUPS
)
2182 items
= QF_MAX_GROUPS
;
2183 if (!powerof2(items
)) {
2191 filesize
+= (items
+ 1) * sizeof(struct dqblk
);
2192 zonesize
+= filesize
;
2195 * Round up entire zone to a bitmap block's worth.
2196 * The extra space goes to the catalog file and hot file area.
2199 zonesize
= roundup(zonesize
, vcb
->vcbVBMIOSize
* 8 * vcb
->blockSize
);
2200 temp
= zonesize
- temp
; /* temp has extra space */
2201 filesize
+= temp
/ 3;
2202 hfsmp
->hfs_catalog_maxblks
+= (temp
- (temp
/ 3)) / vcb
->blockSize
;
2204 hfsmp
->hfs_hotfile_maxblks
= filesize
/ vcb
->blockSize
;
2206 /* Convert to allocation blocks. */
2207 blk
= zonesize
/ vcb
->blockSize
;
2209 /* The default metadata zone location is at the start of volume. */
2210 hfsmp
->hfs_metazone_start
= 1;
2211 hfsmp
->hfs_metazone_end
= blk
- 1;
2213 /* The default hotfile area is at the end of the zone. */
2214 hfsmp
->hfs_hotfile_start
= blk
- (filesize
/ vcb
->blockSize
);
2215 hfsmp
->hfs_hotfile_end
= hfsmp
->hfs_metazone_end
;
2216 hfsmp
->hfs_hotfile_freeblks
= hfs_hotfile_freeblocks(hfsmp
);
2218 printf("HFS: metadata zone is %d to %d\n", hfsmp
->hfs_metazone_start
, hfsmp
->hfs_metazone_end
);
2219 printf("HFS: hot file band is %d to %d\n", hfsmp
->hfs_hotfile_start
, hfsmp
->hfs_hotfile_end
);
2220 printf("HFS: hot file band free blocks = %d\n", hfsmp
->hfs_hotfile_freeblks
);
2222 hfsmp
->hfs_flags
|= HFS_METADATA_ZONE
;
2227 hfs_hotfile_freeblocks(struct hfsmount
*hfsmp
)
2229 ExtendedVCB
*vcb
= HFSTOVCB(hfsmp
);
2233 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_BITMAP
, HFS_EXCLUSIVE_LOCK
);
2234 freeblocks
= MetaZoneFreeBlocks(vcb
);
2235 hfs_systemfile_unlock(hfsmp
, lockflags
);
2237 /* Minus Extents overflow file reserve. */
2239 hfsmp
->hfs_overflow_maxblks
- VTOF(hfsmp
->hfs_extents_vp
)->ff_blocks
;
2240 /* Minus catalog file reserve. */
2242 hfsmp
->hfs_catalog_maxblks
- VTOF(hfsmp
->hfs_catalog_vp
)->ff_blocks
;
2246 return MIN(freeblocks
, hfsmp
->hfs_hotfile_maxblks
);
2250 * Determine if a file is a "virtual" metadata file.
2251 * This includes journal and quota files.
2255 hfs_virtualmetafile(struct cnode
*cp
)
2260 if (cp
->c_parentcnid
!= kHFSRootFolderID
)
2263 filename
= cp
->c_desc
.cd_nameptr
;
2264 if (filename
== NULL
)
2267 if ((strcmp(filename
, ".journal") == 0) ||
2268 (strcmp(filename
, ".journal_info_block") == 0) ||
2269 (strcmp(filename
, ".quota.user") == 0) ||
2270 (strcmp(filename
, ".quota.group") == 0) ||
2271 (strcmp(filename
, ".hotfiles.btree") == 0))
2280 hfs_start_transaction(struct hfsmount
*hfsmp
)
2284 if (hfsmp
->jnl
== NULL
|| journal_owner(hfsmp
->jnl
) != current_thread()) {
2285 lck_rw_lock_shared(&hfsmp
->hfs_global_lock
);
2289 ret
= journal_start_transaction(hfsmp
->jnl
);
2291 OSAddAtomic(1, &hfsmp
->hfs_global_lock_nesting
);
2298 lck_rw_done(&hfsmp
->hfs_global_lock
);
2306 hfs_end_transaction(struct hfsmount
*hfsmp
)
2308 int need_unlock
=0, ret
;
2310 if ( hfsmp
->jnl
== NULL
2311 || ( journal_owner(hfsmp
->jnl
) == current_thread()
2312 && (OSAddAtomic(-1, &hfsmp
->hfs_global_lock_nesting
) == 1)) ) {
2318 ret
= journal_end_transaction(hfsmp
->jnl
);
2324 lck_rw_done(&hfsmp
->hfs_global_lock
);