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
)
750 if (VTOVCB(FTOV(fp
))->vcbSigWord
== kHFSPlusSigWord
) {
751 if (fp
->ff_extents
[7].blockCount
== 0)
754 blocks
= fp
->ff_extents
[0].blockCount
+
755 fp
->ff_extents
[1].blockCount
+
756 fp
->ff_extents
[2].blockCount
+
757 fp
->ff_extents
[3].blockCount
+
758 fp
->ff_extents
[4].blockCount
+
759 fp
->ff_extents
[5].blockCount
+
760 fp
->ff_extents
[6].blockCount
+
761 fp
->ff_extents
[7].blockCount
;
763 if (fp
->ff_extents
[2].blockCount
== 0)
766 blocks
= fp
->ff_extents
[0].blockCount
+
767 fp
->ff_extents
[1].blockCount
+
768 fp
->ff_extents
[2].blockCount
;
771 return (fp
->ff_blocks
> blocks
);
776 * Lock HFS system file(s).
780 hfs_systemfile_lock(struct hfsmount
*hfsmp
, int flags
, enum hfslocktype locktype
)
782 if (flags
& ~SFL_VALIDMASK
)
783 panic("hfs_systemfile_lock: invalid lock request (0x%x)", (unsigned long) flags
);
785 * Locking order is Catalog file, Attributes file, Bitmap file, Extents file
787 if (flags
& SFL_CATALOG
) {
788 (void) hfs_lock(VTOC(hfsmp
->hfs_catalog_vp
), locktype
);
790 * When the catalog file has overflow extents then
791 * also acquire the extents b-tree lock if its not
794 if ((flags
& SFL_EXTENTS
) == 0 &&
795 overflow_extents(VTOF(hfsmp
->hfs_catalog_vp
))) {
796 flags
|= SFL_EXTENTS
;
799 if (flags
& SFL_ATTRIBUTE
) {
800 if (hfsmp
->hfs_attribute_vp
) {
801 (void) hfs_lock(VTOC(hfsmp
->hfs_attribute_vp
), locktype
);
803 * When the attribute file has overflow extents then
804 * also acquire the extents b-tree lock if its not
807 if ((flags
& SFL_EXTENTS
) == 0 &&
808 overflow_extents(VTOF(hfsmp
->hfs_attribute_vp
))) {
809 flags
|= SFL_EXTENTS
;
812 flags
&= ~SFL_ATTRIBUTE
;
815 if (flags
& SFL_BITMAP
) {
817 * Since the only bitmap operations are clearing and
818 * setting bits we always need exclusive access. And
819 * when we have a journal, we can "hide" behind that
820 * lock since we can only change the bitmap from
821 * within a transaction.
824 flags
&= ~SFL_BITMAP
;
826 (void) hfs_lock(VTOC(hfsmp
->hfs_allocation_vp
), HFS_EXCLUSIVE_LOCK
);
829 if (flags
& SFL_EXTENTS
) {
831 * Since the extents btree lock is recursive we always
832 * need exclusive access.
834 (void) hfs_lock(VTOC(hfsmp
->hfs_extents_vp
), HFS_EXCLUSIVE_LOCK
);
840 * unlock HFS system file(s).
844 hfs_systemfile_unlock(struct hfsmount
*hfsmp
, int flags
)
848 int numOfLockedBuffs
;
851 lastfsync
= tv
.tv_sec
;
853 if (flags
& ~SFL_VALIDMASK
)
854 panic("hfs_systemfile_unlock: invalid lock request (0x%x)", (unsigned long) flags
);
856 if (flags
& SFL_ATTRIBUTE
&& hfsmp
->hfs_attribute_vp
) {
857 if (hfsmp
->jnl
== NULL
) {
858 BTGetLastSync((FCB
*)VTOF(hfsmp
->hfs_attribute_vp
), &lastfsync
);
859 numOfLockedBuffs
= count_lock_queue();
860 if ((numOfLockedBuffs
> kMaxLockedMetaBuffers
) ||
861 ((numOfLockedBuffs
> 1) && ((tv
.tv_sec
- lastfsync
) >
862 kMaxSecsForFsync
))) {
863 hfs_btsync(hfsmp
->hfs_attribute_vp
, HFS_SYNCTRANS
);
866 hfs_unlock(VTOC(hfsmp
->hfs_attribute_vp
));
868 if (flags
& SFL_CATALOG
) {
869 if (hfsmp
->jnl
== NULL
) {
870 BTGetLastSync((FCB
*)VTOF(hfsmp
->hfs_catalog_vp
), &lastfsync
);
871 numOfLockedBuffs
= count_lock_queue();
872 if ((numOfLockedBuffs
> kMaxLockedMetaBuffers
) ||
873 ((numOfLockedBuffs
> 1) && ((tv
.tv_sec
- lastfsync
) >
874 kMaxSecsForFsync
))) {
875 hfs_btsync(hfsmp
->hfs_catalog_vp
, HFS_SYNCTRANS
);
878 hfs_unlock(VTOC(hfsmp
->hfs_catalog_vp
));
880 if (flags
& SFL_BITMAP
) {
881 hfs_unlock(VTOC(hfsmp
->hfs_allocation_vp
));
883 if (flags
& SFL_EXTENTS
) {
884 if (hfsmp
->jnl
== NULL
) {
885 BTGetLastSync((FCB
*)VTOF(hfsmp
->hfs_extents_vp
), &lastfsync
);
886 numOfLockedBuffs
= count_lock_queue();
887 if ((numOfLockedBuffs
> kMaxLockedMetaBuffers
) ||
888 ((numOfLockedBuffs
> 1) && ((tv
.tv_sec
- lastfsync
) >
889 kMaxSecsForFsync
))) {
890 hfs_btsync(hfsmp
->hfs_extents_vp
, HFS_SYNCTRANS
);
893 hfs_unlock(VTOC(hfsmp
->hfs_extents_vp
));
901 * Check to see if a vnode is locked in the current context
902 * This is to be used for debugging purposes only!!
905 void RequireFileLock(FileReference vp
, int shareable
)
909 /* The extents btree and allocation bitmap are always exclusive. */
910 if (VTOC(vp
)->c_fileid
== kHFSExtentsFileID
||
911 VTOC(vp
)->c_fileid
== kHFSAllocationFileID
) {
915 locked
= VTOC(vp
)->c_lockowner
== (void *)current_thread();
917 if (!locked
&& !shareable
) {
918 switch (VTOC(vp
)->c_fileid
) {
919 case kHFSExtentsFileID
:
920 panic("extents btree not locked! v: 0x%08X\n #\n", (u_int
)vp
);
922 case kHFSCatalogFileID
:
923 panic("catalog btree not locked! v: 0x%08X\n #\n", (u_int
)vp
);
925 case kHFSAllocationFileID
:
926 /* The allocation file can hide behind the jornal lock. */
927 if (VTOHFS(vp
)->jnl
== NULL
)
928 panic("allocation file not locked! v: 0x%08X\n #\n", (u_int
)vp
);
930 case kHFSAttributesFileID
:
931 panic("attributes btree not locked! v: 0x%08X\n #\n", (u_int
)vp
);
940 * There are three ways to qualify for ownership rights on an object:
942 * 1. (a) Your UID matches the cnode's UID.
943 * (b) The object in question is owned by "unknown"
944 * 2. (a) Permissions on the filesystem are being ignored and
945 * your UID matches the replacement UID.
946 * (b) Permissions on the filesystem are being ignored and
947 * the replacement UID is "unknown".
952 hfs_owner_rights(struct hfsmount
*hfsmp
, uid_t cnode_uid
, kauth_cred_t cred
,
953 struct proc
*p
, int invokesuperuserstatus
)
955 if ((kauth_cred_getuid(cred
) == cnode_uid
) || /* [1a] */
956 (cnode_uid
== UNKNOWNUID
) || /* [1b] */
957 ((((unsigned int)vfs_flags(HFSTOVFS(hfsmp
))) & MNT_UNKNOWNPERMISSIONS
) && /* [2] */
958 ((kauth_cred_getuid(cred
) == hfsmp
->hfs_uid
) || /* [2a] */
959 (hfsmp
->hfs_uid
== UNKNOWNUID
))) || /* [2b] */
960 (invokesuperuserstatus
&& (suser(cred
, 0) == 0))) { /* [3] */
968 unsigned long BestBlockSizeFit(unsigned long allocationBlockSize
,
969 unsigned long blockSizeLimit
,
970 unsigned long baseMultiple
) {
972 Compute the optimal (largest) block size (no larger than allocationBlockSize) that is less than the
973 specified limit but still an even multiple of the baseMultiple.
975 int baseBlockCount
, blockCount
;
976 unsigned long trialBlockSize
;
978 if (allocationBlockSize
% baseMultiple
!= 0) {
980 Whoops: the allocation blocks aren't even multiples of the specified base:
981 no amount of dividing them into even parts will be a multiple, either then!
983 return 512; /* Hope for the best */
986 /* Try the obvious winner first, to prevent 12K allocation blocks, for instance,
987 from being handled as two 6K logical blocks instead of 3 4K logical blocks.
988 Even though the former (the result of the loop below) is the larger allocation
989 block size, the latter is more efficient: */
990 if (allocationBlockSize
% PAGE_SIZE
== 0) return PAGE_SIZE
;
992 /* No clear winner exists: pick the largest even fraction <= MAXBSIZE: */
993 baseBlockCount
= allocationBlockSize
/ baseMultiple
; /* Now guaranteed to be an even multiple */
995 for (blockCount
= baseBlockCount
; blockCount
> 0; --blockCount
) {
996 trialBlockSize
= blockCount
* baseMultiple
;
997 if (allocationBlockSize
% trialBlockSize
== 0) { /* An even multiple? */
998 if ((trialBlockSize
<= blockSizeLimit
) &&
999 (trialBlockSize
% baseMultiple
== 0)) {
1000 return trialBlockSize
;
1005 /* Note: we should never get here, since blockCount = 1 should always work,
1006 but this is nice and safe and makes the compiler happy, too ... */
1012 * To make the HFS Plus filesystem follow UFS unlink semantics, a remove
1013 * of an active vnode is translated to a move/rename so the file appears
1014 * deleted. The destination folder for these move/renames is setup here
1015 * and a reference to it is place in hfsmp->hfs_privdir_desc.
1019 FindMetaDataDirectory(ExtendedVCB
*vcb
)
1021 struct hfsmount
* hfsmp
;
1022 struct vnode
* dvp
= NULL
;
1023 struct cnode
* dcp
= NULL
;
1024 struct FndrDirInfo
* fndrinfo
;
1025 struct cat_desc out_desc
= {0};
1026 struct proc
*p
= current_proc();
1028 cat_cookie_t cookie
;
1032 if (vcb
->vcbSigWord
!= kHFSPlusSigWord
)
1035 hfsmp
= VCBTOHFS(vcb
);
1037 if (hfsmp
->hfs_privdir_desc
.cd_parentcnid
== 0) {
1038 hfsmp
->hfs_privdir_desc
.cd_parentcnid
= kRootDirID
;
1039 hfsmp
->hfs_privdir_desc
.cd_nameptr
= hfs_privdirname
;
1040 hfsmp
->hfs_privdir_desc
.cd_namelen
= strlen(hfs_privdirname
);
1041 hfsmp
->hfs_privdir_desc
.cd_flags
= CD_ISDIR
;
1044 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_SHARED_LOCK
);
1046 error
= cat_lookup(hfsmp
, &hfsmp
->hfs_privdir_desc
, 0, NULL
,
1047 &hfsmp
->hfs_privdir_attr
, NULL
, NULL
);
1049 hfs_systemfile_unlock(hfsmp
, lockflags
);
1052 hfsmp
->hfs_metadata_createdate
= hfsmp
->hfs_privdir_attr
.ca_itime
;
1053 hfsmp
->hfs_privdir_desc
.cd_cnid
= hfsmp
->hfs_privdir_attr
.ca_fileid
;
1055 * Clear the system immutable flag if set...
1057 if ((hfsmp
->hfs_privdir_attr
.ca_flags
& SF_IMMUTABLE
) &&
1058 (hfsmp
->hfs_flags
& HFS_READ_ONLY
) == 0) {
1059 hfsmp
->hfs_privdir_attr
.ca_flags
&= ~SF_IMMUTABLE
;
1061 if ((error
= hfs_start_transaction(hfsmp
)) != 0) {
1062 return (hfsmp
->hfs_privdir_attr
.ca_fileid
);
1065 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_SHARED_LOCK
);
1066 (void) cat_update(hfsmp
, &hfsmp
->hfs_privdir_desc
,
1067 &hfsmp
->hfs_privdir_attr
, NULL
, NULL
);
1068 hfs_systemfile_unlock(hfsmp
, lockflags
);
1070 hfs_end_transaction(hfsmp
);
1072 return (hfsmp
->hfs_privdir_attr
.ca_fileid
);
1074 } else if (hfsmp
->hfs_flags
& HFS_READ_ONLY
) {
1079 /* Setup the default attributes */
1080 bzero(&hfsmp
->hfs_privdir_attr
, sizeof(struct cat_attr
));
1081 hfsmp
->hfs_privdir_attr
.ca_mode
= S_IFDIR
;
1082 hfsmp
->hfs_privdir_attr
.ca_nlink
= 2;
1083 hfsmp
->hfs_privdir_attr
.ca_itime
= vcb
->vcbCrDate
;
1085 hfsmp
->hfs_privdir_attr
.ca_mtime
= tv
.tv_sec
;
1087 /* hidden and off the desktop view */
1088 fndrinfo
= (struct FndrDirInfo
*)&hfsmp
->hfs_privdir_attr
.ca_finderinfo
;
1089 fndrinfo
->frLocation
.v
= SWAP_BE16 (22460);
1090 fndrinfo
->frLocation
.h
= SWAP_BE16 (22460);
1091 fndrinfo
->frFlags
|= SWAP_BE16 (kIsInvisible
+ kNameLocked
);
1093 if ((error
= hfs_start_transaction(hfsmp
)) != 0) {
1096 /* Reserve some space in the Catalog file. */
1097 if (cat_preflight(hfsmp
, CAT_CREATE
, &cookie
, p
) != 0) {
1098 hfs_end_transaction(hfsmp
);
1103 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_EXCLUSIVE_LOCK
);
1105 error
= cat_create(hfsmp
, &hfsmp
->hfs_privdir_desc
,
1106 &hfsmp
->hfs_privdir_attr
, &out_desc
);
1108 hfs_systemfile_unlock(hfsmp
, lockflags
);
1110 cat_postflight(hfsmp
, &cookie
, p
);
1113 hfs_volupdate(hfsmp
, VOL_UPDATE
, 0);
1115 hfs_end_transaction(hfsmp
);
1120 hfsmp
->hfs_privdir_desc
.cd_hint
= out_desc
.cd_hint
;
1121 hfsmp
->hfs_privdir_desc
.cd_cnid
= out_desc
.cd_cnid
;
1122 hfsmp
->hfs_privdir_attr
.ca_fileid
= out_desc
.cd_cnid
;
1123 hfsmp
->hfs_metadata_createdate
= vcb
->vcbCrDate
;
1125 if (hfs_vget(hfsmp
, kRootDirID
, &dvp
, 0) == 0) {
1127 dcp
->c_childhint
= out_desc
.cd_hint
;
1130 dcp
->c_touch_chgtime
= TRUE
;
1131 dcp
->c_touch_modtime
= TRUE
;
1132 (void) hfs_update(dvp
, 0);
1136 hfs_volupdate(hfsmp
, VOL_MKDIR
, 1);
1137 hfs_end_transaction(hfsmp
);
1139 cat_releasedesc(&out_desc
);
1141 return (out_desc
.cd_cnid
);
1146 GetFileInfo(ExtendedVCB
*vcb
, u_int32_t dirid
, const char *name
,
1147 struct cat_attr
*fattr
, struct cat_fork
*forkinfo
)
1149 struct hfsmount
* hfsmp
;
1150 struct vnode
* dvp
= NULL
;
1151 struct cnode
* dcp
= NULL
;
1152 struct FndrDirInfo
* fndrinfo
;
1153 struct cat_desc jdesc
;
1157 if (vcb
->vcbSigWord
!= kHFSPlusSigWord
)
1160 hfsmp
= VCBTOHFS(vcb
);
1162 memset(&jdesc
, 0, sizeof(struct cat_desc
));
1163 jdesc
.cd_parentcnid
= kRootDirID
;
1164 jdesc
.cd_nameptr
= name
;
1165 jdesc
.cd_namelen
= strlen(name
);
1167 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_SHARED_LOCK
);
1168 error
= cat_lookup(hfsmp
, &jdesc
, 0, NULL
, fattr
, forkinfo
, NULL
);
1169 hfs_systemfile_unlock(hfsmp
, lockflags
);
1172 return (fattr
->ca_fileid
);
1173 } else if (hfsmp
->hfs_flags
& HFS_READ_ONLY
) {
1177 return (0); /* XXX what callers expect on an error */
1182 * On HFS Plus Volume, there can be orphaned files. These
1183 * are files that were unlinked while busy. If the volume
1184 * was not cleanly unmounted then some of these files may
1185 * have persisted and need to be removed.
1189 hfs_remove_orphans(struct hfsmount
* hfsmp
)
1191 struct BTreeIterator
* iterator
= NULL
;
1192 struct FSBufferDescriptor btdata
;
1193 struct HFSPlusCatalogFile filerec
;
1194 struct HFSPlusCatalogKey
* keyp
;
1195 struct proc
*p
= current_proc();
1201 cat_cookie_t cookie
;
1207 int orphanedlinks
= 0;
1209 bzero(&cookie
, sizeof(cookie
));
1211 if (hfsmp
->hfs_flags
& HFS_CLEANED_ORPHANS
)
1214 vcb
= HFSTOVCB(hfsmp
);
1215 fcb
= VTOF(hfsmp
->hfs_catalog_vp
);
1217 btdata
.bufferAddress
= &filerec
;
1218 btdata
.itemSize
= sizeof(filerec
);
1219 btdata
.itemCount
= 1;
1221 MALLOC(iterator
, struct BTreeIterator
*, sizeof(*iterator
), M_TEMP
, M_WAITOK
);
1222 bzero(iterator
, sizeof(*iterator
));
1224 /* Build a key to "temp" */
1225 keyp
= (HFSPlusCatalogKey
*)&iterator
->key
;
1226 keyp
->parentID
= hfsmp
->hfs_privdir_desc
.cd_cnid
;
1227 keyp
->nodeName
.length
= 4; /* "temp" */
1228 keyp
->keyLength
= kHFSPlusCatalogKeyMinimumLength
+ keyp
->nodeName
.length
* 2;
1229 keyp
->nodeName
.unicode
[0] = 't';
1230 keyp
->nodeName
.unicode
[1] = 'e';
1231 keyp
->nodeName
.unicode
[2] = 'm';
1232 keyp
->nodeName
.unicode
[3] = 'p';
1235 * Position the iterator just before the first real temp file.
1237 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_EXCLUSIVE_LOCK
);
1238 (void) BTSearchRecord(fcb
, iterator
, NULL
, NULL
, iterator
);
1239 hfs_systemfile_unlock(hfsmp
, lockflags
);
1241 /* Visit all the temp files in the HFS+ private directory. */
1243 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_EXCLUSIVE_LOCK
);
1244 result
= BTIterateRecord(fcb
, kBTreeNextRecord
, iterator
, &btdata
, NULL
);
1245 hfs_systemfile_unlock(hfsmp
, lockflags
);
1248 if (keyp
->parentID
!= hfsmp
->hfs_privdir_desc
.cd_cnid
)
1250 if (filerec
.recordType
!= kHFSPlusFileRecord
)
1253 (void) utf8_encodestr(keyp
->nodeName
.unicode
, keyp
->nodeName
.length
* 2,
1254 filename
, &namelen
, sizeof(filename
), 0, 0);
1256 (void) sprintf(tempname
, "%s%d", HFS_DELETE_PREFIX
, filerec
.fileID
);
1259 * Delete all files named "tempxxx", where
1260 * xxx is the file's cnid in decimal.
1263 if (bcmp(tempname
, filename
, namelen
) == 0) {
1264 struct filefork dfork
;
1265 struct filefork rfork
;
1268 bzero(&dfork
, sizeof(dfork
));
1269 bzero(&rfork
, sizeof(rfork
));
1270 bzero(&cnode
, sizeof(cnode
));
1272 if (hfs_start_transaction(hfsmp
) != 0) {
1273 printf("hfs_remove_orphans: failed to start transaction\n");
1279 * Reserve some space in the Catalog file.
1281 if (cat_preflight(hfsmp
, CAT_DELETE
, &cookie
, p
) != 0) {
1282 printf("hfs_remove_orphans: cat_preflight failed\n");
1287 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
| SFL_ATTRIBUTE
| SFL_EXTENTS
| SFL_BITMAP
, HFS_EXCLUSIVE_LOCK
);
1290 /* Build a fake cnode */
1291 cat_convertattr(hfsmp
, (CatalogRecord
*)&filerec
, &cnode
.c_attr
,
1292 &dfork
.ff_data
, &rfork
.ff_data
);
1293 cnode
.c_desc
.cd_parentcnid
= hfsmp
->hfs_privdir_desc
.cd_cnid
;
1294 cnode
.c_desc
.cd_nameptr
= filename
;
1295 cnode
.c_desc
.cd_namelen
= namelen
;
1296 cnode
.c_desc
.cd_cnid
= cnode
.c_attr
.ca_fileid
;
1297 cnode
.c_blocks
= dfork
.ff_blocks
+ rfork
.ff_blocks
;
1299 /* Position iterator at previous entry */
1300 if (BTIterateRecord(fcb
, kBTreePrevRecord
, iterator
,
1305 /* Truncate the file to zero (both forks) */
1306 if (dfork
.ff_blocks
> 0) {
1309 dfork
.ff_cp
= &cnode
;
1310 cnode
.c_datafork
= &dfork
;
1311 cnode
.c_rsrcfork
= NULL
;
1312 fsize
= (u_int64_t
)dfork
.ff_blocks
* (u_int64_t
)HFSTOVCB(hfsmp
)->blockSize
;
1314 if (fsize
> HFS_BIGFILE_SIZE
) {
1315 fsize
-= HFS_BIGFILE_SIZE
;
1320 if (TruncateFileC(vcb
, (FCB
*)&dfork
, fsize
, false) != 0) {
1321 printf("error truncting data fork!\n");
1326 // if we're iteratively truncating this file down,
1327 // then end the transaction and start a new one so
1328 // that no one transaction gets too big.
1330 if (fsize
> 0 && started_tr
) {
1331 hfs_end_transaction(hfsmp
);
1332 if (hfs_start_transaction(hfsmp
) != 0) {
1340 if (rfork
.ff_blocks
> 0) {
1341 rfork
.ff_cp
= &cnode
;
1342 cnode
.c_datafork
= NULL
;
1343 cnode
.c_rsrcfork
= &rfork
;
1344 if (TruncateFileC(vcb
, (FCB
*)&rfork
, 0, false) != 0) {
1345 printf("error truncting rsrc fork!\n");
1350 /* Remove the file record from the Catalog */
1351 if (cat_delete(hfsmp
, &cnode
.c_desc
, &cnode
.c_attr
) != 0) {
1352 printf("hfs_remove_oprhans: error deleting cat rec for id %d!\n", cnode
.c_desc
.cd_cnid
);
1353 hfs_volupdate(hfsmp
, VOL_UPDATE
, 0);
1358 /* Delete any attributes, ignore errors */
1359 (void) hfs_removeallattr(hfsmp
, cnode
.c_fileid
);
1361 /* Update parent and volume counts */
1362 hfsmp
->hfs_privdir_attr
.ca_entries
--;
1363 (void)cat_update(hfsmp
, &hfsmp
->hfs_privdir_desc
,
1364 &hfsmp
->hfs_privdir_attr
, NULL
, NULL
);
1365 hfs_volupdate(hfsmp
, VOL_RMFILE
, 0);
1367 /* Drop locks and end the transaction */
1368 hfs_systemfile_unlock(hfsmp
, lockflags
);
1369 cat_postflight(hfsmp
, &cookie
, p
);
1370 catlock
= catreserve
= 0;
1372 hfs_end_transaction(hfsmp
);
1378 if (orphanedlinks
> 0)
1379 printf("HFS: Removed %d orphaned unlinked files\n", orphanedlinks
);
1382 hfs_systemfile_unlock(hfsmp
, lockflags
);
1385 cat_postflight(hfsmp
, &cookie
, p
);
1388 hfs_end_transaction(hfsmp
);
1391 FREE(iterator
, M_TEMP
);
1392 hfsmp
->hfs_flags
|= HFS_CLEANED_ORPHANS
;
1397 * This will return the correct logical block size for a given vnode.
1398 * For most files, it is the allocation block size, for meta data like
1399 * BTrees, this is kept as part of the BTree private nodeSize
1402 GetLogicalBlockSize(struct vnode
*vp
)
1404 u_int32_t logBlockSize
;
1406 DBG_ASSERT(vp
!= NULL
);
1408 /* start with default */
1409 logBlockSize
= VTOHFS(vp
)->hfs_logBlockSize
;
1411 if (vnode_issystem(vp
)) {
1412 if (VTOF(vp
)->fcbBTCBPtr
!= NULL
) {
1413 BTreeInfoRec bTreeInfo
;
1416 * We do not lock the BTrees, because if we are getting block..then the tree
1417 * should be locked in the first place.
1418 * We just want the nodeSize wich will NEVER change..so even if the world
1419 * is changing..the nodeSize should remain the same. Which argues why lock
1420 * it in the first place??
1423 (void) BTGetInformation (VTOF(vp
), kBTreeInfoVersion
, &bTreeInfo
);
1425 logBlockSize
= bTreeInfo
.nodeSize
;
1427 } else if (VTOC(vp
)->c_fileid
== kHFSAllocationFileID
) {
1428 logBlockSize
= VTOVCB(vp
)->vcbVBMIOSize
;
1432 DBG_ASSERT(logBlockSize
> 0);
1434 return logBlockSize
;
1439 hfs_freeblks(struct hfsmount
* hfsmp
, int wantreserve
)
1441 ExtendedVCB
*vcb
= HFSTOVCB(hfsmp
);
1444 HFS_MOUNT_LOCK(hfsmp
, TRUE
);
1445 freeblks
= vcb
->freeBlocks
;
1447 if (freeblks
> vcb
->reserveBlocks
)
1448 freeblks
-= vcb
->reserveBlocks
;
1452 if (freeblks
> vcb
->loanedBlocks
)
1453 freeblks
-= vcb
->loanedBlocks
;
1456 HFS_MOUNT_UNLOCK(hfsmp
, TRUE
);
1458 #ifdef HFS_SPARSE_DEV
1460 * When the underlying device is sparse, check the
1461 * available space on the backing store volume.
1463 if ((hfsmp
->hfs_flags
& HFS_HAS_SPARSE_DEVICE
) && hfsmp
->hfs_backingfs_rootvp
) {
1464 struct vfsstatfs
*vfsp
; /* 272 bytes */
1465 u_int32_t vfreeblks
;
1466 u_int32_t loanedblks
;
1467 struct mount
* backingfs_mp
;
1469 backingfs_mp
= vnode_mount(hfsmp
->hfs_backingfs_rootvp
);
1471 if (vfsp
= vfs_statfs(backingfs_mp
)) {
1472 HFS_MOUNT_LOCK(hfsmp
, TRUE
);
1473 vfreeblks
= (u_int32_t
)vfsp
->f_bavail
;
1474 /* Normalize block count if needed. */
1475 if (vfsp
->f_bsize
!= vcb
->blockSize
) {
1476 vfreeblks
= ((u_int64_t
)vfreeblks
* (u_int64_t
)(vfsp
->f_bsize
)) / vcb
->blockSize
;
1478 if (vfreeblks
> hfsmp
->hfs_sparsebandblks
)
1479 vfreeblks
-= hfsmp
->hfs_sparsebandblks
;
1483 /* Take into account any delayed allocations. */
1484 loanedblks
= 2 * vcb
->loanedBlocks
;
1485 if (vfreeblks
> loanedblks
)
1486 vfreeblks
-= loanedblks
;
1490 freeblks
= MIN(vfreeblks
, freeblks
);
1491 HFS_MOUNT_UNLOCK(hfsmp
, TRUE
);
1494 #endif /* HFS_SPARSE_DEV */
1500 * Map HFS Common errors (negative) to BSD error codes (positive).
1501 * Positive errors (ie BSD errors) are passed through unchanged.
1503 short MacToVFSError(OSErr err
)
1509 case dskFulErr
: /* -34 */
1510 case btNoSpaceAvail
: /* -32733 */
1512 case fxOvFlErr
: /* -32750 */
1515 case btBadNode
: /* -32731 */
1518 case memFullErr
: /* -108 */
1519 return ENOMEM
; /* +12 */
1521 case cmExists
: /* -32718 */
1522 case btExists
: /* -32734 */
1523 return EEXIST
; /* +17 */
1525 case cmNotFound
: /* -32719 */
1526 case btNotFound
: /* -32735 */
1527 return ENOENT
; /* 28 */
1529 case cmNotEmpty
: /* -32717 */
1530 return ENOTEMPTY
; /* 66 */
1532 case cmFThdDirErr
: /* -32714 */
1533 return EISDIR
; /* 21 */
1535 case fxRangeErr
: /* -32751 */
1538 case bdNamErr
: /* -37 */
1539 return ENAMETOOLONG
; /* 63 */
1541 case paramErr
: /* -50 */
1542 case fileBoundsErr
: /* -1309 */
1543 return EINVAL
; /* +22 */
1545 case fsBTBadNodeSize
:
1549 return EIO
; /* +5 */
1555 * Find the current thread's directory hint for a given index.
1557 * Requires an exclusive lock on directory cnode.
1561 hfs_getdirhint(struct cnode
*dcp
, int index
)
1564 directoryhint_t
*hint
;
1565 boolean_t need_remove
, need_init
;
1571 * Look for an existing hint first. If not found, create a new one (when
1572 * the list is not full) or recycle the oldest hint. Since new hints are
1573 * always added to the head of the list, the last hint is always the
1576 TAILQ_FOREACH(hint
, &dcp
->c_hintlist
, dh_link
) {
1577 if (hint
->dh_index
== index
)
1580 if (hint
!= NULL
) { /* found an existing hint */
1583 } else { /* cannot find an existing hint */
1585 if (dcp
->c_dirhintcnt
< HFS_MAXDIRHINTS
) { /* we don't need recycling */
1586 /* Create a default directory hint */
1587 MALLOC_ZONE(hint
, directoryhint_t
*, sizeof(directoryhint_t
), M_HFSDIRHINT
, M_WAITOK
);
1588 ++dcp
->c_dirhintcnt
;
1589 need_remove
= false;
1590 } else { /* recycle the last (i.e., the oldest) hint */
1591 hint
= TAILQ_LAST(&dcp
->c_hintlist
, hfs_hinthead
);
1592 if ((name
= hint
->dh_desc
.cd_nameptr
)) {
1593 hint
->dh_desc
.cd_nameptr
= NULL
;
1594 vfs_removename(name
);
1601 TAILQ_REMOVE(&dcp
->c_hintlist
, hint
, dh_link
);
1603 TAILQ_INSERT_HEAD(&dcp
->c_hintlist
, hint
, dh_link
);
1606 hint
->dh_index
= index
;
1607 hint
->dh_desc
.cd_flags
= 0;
1608 hint
->dh_desc
.cd_encoding
= 0;
1609 hint
->dh_desc
.cd_namelen
= 0;
1610 hint
->dh_desc
.cd_nameptr
= NULL
;
1611 hint
->dh_desc
.cd_parentcnid
= dcp
->c_cnid
;
1612 hint
->dh_desc
.cd_hint
= dcp
->c_childhint
;
1613 hint
->dh_desc
.cd_cnid
= 0;
1615 hint
->dh_time
= tv
.tv_sec
;
1620 * Release a single directory hint.
1622 * Requires an exclusive lock on directory cnode.
1626 hfs_reldirhint(struct cnode
*dcp
, directoryhint_t
* relhint
)
1630 TAILQ_REMOVE(&dcp
->c_hintlist
, relhint
, dh_link
);
1631 name
= relhint
->dh_desc
.cd_nameptr
;
1633 relhint
->dh_desc
.cd_nameptr
= NULL
;
1634 vfs_removename(name
);
1636 FREE_ZONE(relhint
, sizeof(directoryhint_t
), M_HFSDIRHINT
);
1637 --dcp
->c_dirhintcnt
;
1641 * Release directory hints for given directory
1643 * Requires an exclusive lock on directory cnode.
1647 hfs_reldirhints(struct cnode
*dcp
, int stale_hints_only
)
1650 directoryhint_t
*hint
, *prev
;
1653 if (stale_hints_only
)
1656 /* searching from the oldest to the newest, so we can stop early when releasing stale hints only */
1657 for (hint
= TAILQ_LAST(&dcp
->c_hintlist
, hfs_hinthead
); hint
!= NULL
; hint
= prev
) {
1658 if (stale_hints_only
&& (tv
.tv_sec
- hint
->dh_time
) < HFS_DIRHINT_TTL
)
1659 break; /* stop here if this entry is too new */
1660 name
= hint
->dh_desc
.cd_nameptr
;
1662 hint
->dh_desc
.cd_nameptr
= NULL
;
1663 vfs_removename(name
);
1665 prev
= TAILQ_PREV(hint
, hfs_hinthead
, dh_link
); /* must save this pointer before calling FREE_ZONE on this node */
1666 TAILQ_REMOVE(&dcp
->c_hintlist
, hint
, dh_link
);
1667 FREE_ZONE(hint
, sizeof(directoryhint_t
), M_HFSDIRHINT
);
1668 --dcp
->c_dirhintcnt
;
1674 * Perform a case-insensitive compare of two UTF-8 filenames.
1676 * Returns 0 if the strings match.
1680 hfs_namecmp(const char *str1
, size_t len1
, const char *str2
, size_t len2
)
1682 u_int16_t
*ustr1
, *ustr2
;
1683 size_t ulen1
, ulen2
;
1690 maxbytes
= kHFSPlusMaxFileNameChars
<< 1;
1691 MALLOC(ustr1
, u_int16_t
*, maxbytes
<< 1, M_TEMP
, M_WAITOK
);
1692 ustr2
= ustr1
+ (maxbytes
>> 1);
1694 if (utf8_decodestr(str1
, len1
, ustr1
, &ulen1
, maxbytes
, ':', 0) != 0)
1696 if (utf8_decodestr(str2
, len2
, ustr2
, &ulen2
, maxbytes
, ':', 0) != 0)
1699 cmp
= FastUnicodeCompare(ustr1
, ulen1
>>1, ustr2
, ulen2
>>1);
1701 FREE(ustr1
, M_TEMP
);
1708 hfs_early_journal_init(struct hfsmount
*hfsmp
, HFSPlusVolumeHeader
*vhp
,
1709 void *_args
, off_t embeddedOffset
, daddr64_t mdb_offset
,
1710 HFSMasterDirectoryBlock
*mdbp
, kauth_cred_t cred
)
1712 JournalInfoBlock
*jibp
;
1713 struct buf
*jinfo_bp
, *bp
;
1714 int sectors_per_fsblock
, arg_flags
=0, arg_tbufsz
=0;
1715 int retval
, blksize
= hfsmp
->hfs_phys_block_size
;
1716 struct vnode
*devvp
;
1717 struct hfs_mount_args
*args
= _args
;
1719 devvp
= hfsmp
->hfs_devvp
;
1721 if (args
!= NULL
&& (args
->flags
& HFSFSMNT_EXTENDED_ARGS
)) {
1722 arg_flags
= args
->journal_flags
;
1723 arg_tbufsz
= args
->journal_tbuffer_size
;
1726 sectors_per_fsblock
= SWAP_BE32(vhp
->blockSize
) / blksize
;
1728 retval
= (int)buf_meta_bread(devvp
,
1729 (daddr64_t
)((embeddedOffset
/blksize
) +
1730 (SWAP_BE32(vhp
->journalInfoBlock
)*sectors_per_fsblock
)),
1731 SWAP_BE32(vhp
->blockSize
), cred
, &jinfo_bp
);
1735 jibp
= (JournalInfoBlock
*)buf_dataptr(jinfo_bp
);
1736 jibp
->flags
= SWAP_BE32(jibp
->flags
);
1737 jibp
->offset
= SWAP_BE64(jibp
->offset
);
1738 jibp
->size
= SWAP_BE64(jibp
->size
);
1740 if (jibp
->flags
& kJIJournalInFSMask
) {
1741 hfsmp
->jvp
= hfsmp
->hfs_devvp
;
1743 printf("hfs: journal not stored in fs! don't know what to do.\n");
1744 buf_brelse(jinfo_bp
);
1748 // save this off for the hack-y check in hfs_remove()
1749 hfsmp
->jnl_start
= jibp
->offset
/ SWAP_BE32(vhp
->blockSize
);
1750 hfsmp
->jnl_size
= jibp
->size
;
1752 if ((hfsmp
->hfs_flags
& HFS_READ_ONLY
) && (vfs_flags(hfsmp
->hfs_mp
) & MNT_ROOTFS
) == 0) {
1753 // if the file system is read-only, check if the journal is empty.
1754 // if it is, then we can allow the mount. otherwise we have to
1756 retval
= journal_is_clean(hfsmp
->jvp
,
1757 jibp
->offset
+ embeddedOffset
,
1760 hfsmp
->hfs_phys_block_size
);
1764 buf_brelse(jinfo_bp
);
1767 printf("hfs: early journal init: volume on %s is read-only and journal is dirty. Can not mount volume.\n",
1774 if (jibp
->flags
& kJIJournalNeedInitMask
) {
1775 printf("hfs: Initializing the journal (joffset 0x%llx sz 0x%llx)...\n",
1776 jibp
->offset
+ embeddedOffset
, jibp
->size
);
1777 hfsmp
->jnl
= journal_create(hfsmp
->jvp
,
1778 jibp
->offset
+ embeddedOffset
,
1784 hfs_sync_metadata
, hfsmp
->hfs_mp
);
1786 // no need to start a transaction here... if this were to fail
1787 // we'd just re-init it on the next mount.
1788 jibp
->flags
&= ~kJIJournalNeedInitMask
;
1789 jibp
->flags
= SWAP_BE32(jibp
->flags
);
1790 jibp
->offset
= SWAP_BE64(jibp
->offset
);
1791 jibp
->size
= SWAP_BE64(jibp
->size
);
1792 buf_bwrite(jinfo_bp
);
1796 //printf("hfs: Opening the journal (joffset 0x%llx sz 0x%llx vhp_blksize %d)...\n",
1797 // jibp->offset + embeddedOffset,
1798 // jibp->size, SWAP_BE32(vhp->blockSize));
1800 hfsmp
->jnl
= journal_open(hfsmp
->jvp
,
1801 jibp
->offset
+ embeddedOffset
,
1807 hfs_sync_metadata
, hfsmp
->hfs_mp
);
1809 buf_brelse(jinfo_bp
);
1813 if (hfsmp
->jnl
&& mdbp
) {
1814 // reload the mdb because it could have changed
1815 // if the journal had to be replayed.
1816 if (mdb_offset
== 0) {
1817 mdb_offset
= (daddr64_t
)((embeddedOffset
/ blksize
) + HFS_PRI_SECTOR(blksize
));
1819 retval
= (int)buf_meta_bread(devvp
, mdb_offset
, blksize
, cred
, &bp
);
1822 printf("hfs: failed to reload the mdb after opening the journal (retval %d)!\n",
1826 bcopy((char *)buf_dataptr(bp
) + HFS_PRI_OFFSET(blksize
), mdbp
, 512);
1833 //printf("journal @ 0x%x\n", hfsmp->jnl);
1835 // if we expected the journal to be there and we couldn't
1836 // create it or open it then we have to bail out.
1837 if (hfsmp
->jnl
== NULL
) {
1838 printf("hfs: early jnl init: failed to open/create the journal (retval %d).\n", retval
);
1847 // This function will go and re-locate the .journal_info_block and
1848 // the .journal files in case they moved (which can happen if you
1849 // run Norton SpeedDisk). If we fail to find either file we just
1850 // disable journaling for this volume and return. We turn off the
1851 // journaling bit in the vcb and assume it will get written to disk
1852 // later (if it doesn't on the next mount we'd do the same thing
1853 // again which is harmless). If we disable journaling we don't
1854 // return an error so that the volume is still mountable.
1856 // If the info we find for the .journal_info_block and .journal files
1857 // isn't what we had stored, we re-set our cached info and proceed
1858 // with opening the journal normally.
1861 hfs_late_journal_init(struct hfsmount
*hfsmp
, HFSPlusVolumeHeader
*vhp
, void *_args
)
1863 JournalInfoBlock
*jibp
;
1864 struct buf
*jinfo_bp
, *bp
;
1865 int sectors_per_fsblock
, arg_flags
=0, arg_tbufsz
=0;
1866 int retval
, need_flush
= 0, write_jibp
= 0;
1867 struct vnode
*devvp
;
1868 struct cat_attr jib_attr
, jattr
;
1869 struct cat_fork jib_fork
, jfork
;
1872 struct hfs_mount_args
*args
= _args
;
1874 devvp
= hfsmp
->hfs_devvp
;
1875 vcb
= HFSTOVCB(hfsmp
);
1877 if (args
!= NULL
&& (args
->flags
& HFSFSMNT_EXTENDED_ARGS
)) {
1878 if (args
->journal_disable
) {
1882 arg_flags
= args
->journal_flags
;
1883 arg_tbufsz
= args
->journal_tbuffer_size
;
1886 fid
= GetFileInfo(vcb
, kRootDirID
, ".journal_info_block", &jib_attr
, &jib_fork
);
1887 if (fid
== 0 || jib_fork
.cf_extents
[0].startBlock
== 0 || jib_fork
.cf_size
== 0) {
1888 printf("hfs: can't find the .journal_info_block! disabling journaling (start: %d).\n",
1889 jib_fork
.cf_extents
[0].startBlock
);
1890 vcb
->vcbAtrb
&= ~kHFSVolumeJournaledMask
;
1893 hfsmp
->hfs_jnlinfoblkid
= fid
;
1895 // make sure the journal_info_block begins where we think it should.
1896 if (SWAP_BE32(vhp
->journalInfoBlock
) != jib_fork
.cf_extents
[0].startBlock
) {
1897 printf("hfs: The journal_info_block moved (was: %d; is: %d). Fixing up\n",
1898 SWAP_BE32(vhp
->journalInfoBlock
), jib_fork
.cf_extents
[0].startBlock
);
1900 vcb
->vcbJinfoBlock
= jib_fork
.cf_extents
[0].startBlock
;
1901 vhp
->journalInfoBlock
= SWAP_BE32(jib_fork
.cf_extents
[0].startBlock
);
1905 sectors_per_fsblock
= SWAP_BE32(vhp
->blockSize
) / hfsmp
->hfs_phys_block_size
;
1906 retval
= (int)buf_meta_bread(devvp
,
1907 (daddr64_t
)(vcb
->hfsPlusIOPosOffset
/ hfsmp
->hfs_phys_block_size
+
1908 (SWAP_BE32(vhp
->journalInfoBlock
)*sectors_per_fsblock
)),
1909 SWAP_BE32(vhp
->blockSize
), NOCRED
, &jinfo_bp
);
1911 printf("hfs: can't read journal info block. disabling journaling.\n");
1912 vcb
->vcbAtrb
&= ~kHFSVolumeJournaledMask
;
1916 jibp
= (JournalInfoBlock
*)buf_dataptr(jinfo_bp
);
1917 jibp
->flags
= SWAP_BE32(jibp
->flags
);
1918 jibp
->offset
= SWAP_BE64(jibp
->offset
);
1919 jibp
->size
= SWAP_BE64(jibp
->size
);
1921 fid
= GetFileInfo(vcb
, kRootDirID
, ".journal", &jattr
, &jfork
);
1922 if (fid
== 0 || jfork
.cf_extents
[0].startBlock
== 0 || jfork
.cf_size
== 0) {
1923 printf("hfs: can't find the journal file! disabling journaling (start: %d)\n",
1924 jfork
.cf_extents
[0].startBlock
);
1925 buf_brelse(jinfo_bp
);
1926 vcb
->vcbAtrb
&= ~kHFSVolumeJournaledMask
;
1929 hfsmp
->hfs_jnlfileid
= fid
;
1931 // make sure the journal file begins where we think it should.
1932 if ((jibp
->offset
/ (u_int64_t
)vcb
->blockSize
) != jfork
.cf_extents
[0].startBlock
) {
1933 printf("hfs: The journal file moved (was: %lld; is: %d). Fixing up\n",
1934 (jibp
->offset
/ (u_int64_t
)vcb
->blockSize
), jfork
.cf_extents
[0].startBlock
);
1936 jibp
->offset
= (u_int64_t
)jfork
.cf_extents
[0].startBlock
* (u_int64_t
)vcb
->blockSize
;
1940 // check the size of the journal file.
1941 if (jibp
->size
!= (u_int64_t
)jfork
.cf_extents
[0].blockCount
*vcb
->blockSize
) {
1942 printf("hfs: The journal file changed size! (was %lld; is %lld). Fixing up.\n",
1943 jibp
->size
, (u_int64_t
)jfork
.cf_extents
[0].blockCount
*vcb
->blockSize
);
1945 jibp
->size
= (u_int64_t
)jfork
.cf_extents
[0].blockCount
* vcb
->blockSize
;
1949 if (jibp
->flags
& kJIJournalInFSMask
) {
1950 hfsmp
->jvp
= hfsmp
->hfs_devvp
;
1952 printf("hfs: journal not stored in fs! don't know what to do.\n");
1953 buf_brelse(jinfo_bp
);
1957 // save this off for the hack-y check in hfs_remove()
1958 hfsmp
->jnl_start
= jibp
->offset
/ SWAP_BE32(vhp
->blockSize
);
1959 hfsmp
->jnl_size
= jibp
->size
;
1961 if ((hfsmp
->hfs_flags
& HFS_READ_ONLY
) && (vfs_flags(hfsmp
->hfs_mp
) & MNT_ROOTFS
) == 0) {
1962 // if the file system is read-only, check if the journal is empty.
1963 // if it is, then we can allow the mount. otherwise we have to
1965 retval
= journal_is_clean(hfsmp
->jvp
,
1966 jibp
->offset
+ (off_t
)vcb
->hfsPlusIOPosOffset
,
1969 hfsmp
->hfs_phys_block_size
);
1973 buf_brelse(jinfo_bp
);
1976 printf("hfs: late journal init: volume on %s is read-only and journal is dirty. Can not mount volume.\n",
1983 if (jibp
->flags
& kJIJournalNeedInitMask
) {
1984 printf("hfs: Initializing the journal (joffset 0x%llx sz 0x%llx)...\n",
1985 jibp
->offset
+ (off_t
)vcb
->hfsPlusIOPosOffset
, jibp
->size
);
1986 hfsmp
->jnl
= journal_create(hfsmp
->jvp
,
1987 jibp
->offset
+ (off_t
)vcb
->hfsPlusIOPosOffset
,
1990 hfsmp
->hfs_phys_block_size
,
1993 hfs_sync_metadata
, hfsmp
->hfs_mp
);
1995 // no need to start a transaction here... if this were to fail
1996 // we'd just re-init it on the next mount.
1997 jibp
->flags
&= ~kJIJournalNeedInitMask
;
2002 // if we weren't the last person to mount this volume
2003 // then we need to throw away the journal because it
2004 // is likely that someone else mucked with the disk.
2005 // if the journal is empty this is no big deal. if the
2006 // disk is dirty this prevents us from replaying the
2007 // journal over top of changes that someone else made.
2009 arg_flags
|= JOURNAL_RESET
;
2011 //printf("hfs: Opening the journal (joffset 0x%llx sz 0x%llx vhp_blksize %d)...\n",
2012 // jibp->offset + (off_t)vcb->hfsPlusIOPosOffset,
2013 // jibp->size, SWAP_BE32(vhp->blockSize));
2015 hfsmp
->jnl
= journal_open(hfsmp
->jvp
,
2016 jibp
->offset
+ (off_t
)vcb
->hfsPlusIOPosOffset
,
2019 hfsmp
->hfs_phys_block_size
,
2022 hfs_sync_metadata
, hfsmp
->hfs_mp
);
2027 jibp
->flags
= SWAP_BE32(jibp
->flags
);
2028 jibp
->offset
= SWAP_BE64(jibp
->offset
);
2029 jibp
->size
= SWAP_BE64(jibp
->size
);
2031 buf_bwrite(jinfo_bp
);
2033 buf_brelse(jinfo_bp
);
2038 //printf("journal @ 0x%x\n", hfsmp->jnl);
2040 // if we expected the journal to be there and we couldn't
2041 // create it or open it then we have to bail out.
2042 if (hfsmp
->jnl
== NULL
) {
2043 printf("hfs: late jnl init: failed to open/create the journal (retval %d).\n", retval
);
2051 * Calculate the allocation zone for metadata.
2053 * This zone includes the following:
2054 * Allocation Bitmap file
2055 * Overflow Extents file
2058 * Clustered Hot files
2061 * METADATA ALLOCATION ZONE
2062 * ____________________________________________________________________________
2064 * | BM | JF | OEF | CATALOG |---> | HOT FILES |
2065 * |____|____|_____|_______________|______________________________|___________|
2067 * <------------------------------- N * 128 MB ------------------------------->
2070 #define GIGABYTE (u_int64_t)(1024*1024*1024)
2072 #define OVERFLOW_DEFAULT_SIZE (4*1024*1024)
2073 #define OVERFLOW_MAXIMUM_SIZE (128*1024*1024)
2074 #define JOURNAL_DEFAULT_SIZE (8*1024*1024)
2075 #define JOURNAL_MAXIMUM_SIZE (512*1024*1024)
2076 #define HOTBAND_MINIMUM_SIZE (10*1024*1024)
2077 #define HOTBAND_MAXIMUM_SIZE (512*1024*1024)
2080 hfs_metadatazone_init(struct hfsmount
*hfsmp
)
2090 vcb
= HFSTOVCB(hfsmp
);
2091 fs_size
= (u_int64_t
)vcb
->blockSize
* (u_int64_t
)vcb
->totalBlocks
;
2094 * For volumes less than 10 GB, don't bother.
2096 if (fs_size
< ((u_int64_t
)10 * GIGABYTE
))
2099 * Skip non-journaled volumes as well.
2101 if (hfsmp
->jnl
== NULL
)
2105 * Start with allocation bitmap (a fixed size).
2107 zonesize
= roundup(vcb
->totalBlocks
/ 8, vcb
->vcbVBMIOSize
);
2110 * Overflow Extents file gets 4 MB per 100 GB.
2112 items
= fs_size
/ ((u_int64_t
)100 * GIGABYTE
);
2113 filesize
= (u_int64_t
)(items
+ 1) * OVERFLOW_DEFAULT_SIZE
;
2114 if (filesize
> OVERFLOW_MAXIMUM_SIZE
)
2115 filesize
= OVERFLOW_MAXIMUM_SIZE
;
2116 zonesize
+= filesize
;
2117 hfsmp
->hfs_overflow_maxblks
= filesize
/ vcb
->blockSize
;
2120 * Plan for at least 8 MB of journal for each
2121 * 100 GB of disk space (up to a 512 MB).
2123 items
= fs_size
/ ((u_int64_t
)100 * GIGABYTE
);
2124 filesize
= (u_int64_t
)(items
+ 1) * JOURNAL_DEFAULT_SIZE
;
2125 if (filesize
> JOURNAL_MAXIMUM_SIZE
)
2126 filesize
= JOURNAL_MAXIMUM_SIZE
;
2127 zonesize
+= filesize
;
2130 * Catalog file gets 10 MB per 1 GB.
2132 * How about considering the current catalog size (used nodes * node size)
2133 * and the current file data size to help estimate the required
2136 filesize
= MIN((fs_size
/ 1024) * 10, GIGABYTE
);
2137 hfsmp
->hfs_catalog_maxblks
= filesize
/ vcb
->blockSize
;
2138 zonesize
+= filesize
;
2141 * Add space for hot file region.
2143 * ...for now, use 5 MB per 1 GB (0.5 %)
2145 filesize
= (fs_size
/ 1024) * 5;
2146 if (filesize
> HOTBAND_MAXIMUM_SIZE
)
2147 filesize
= HOTBAND_MAXIMUM_SIZE
;
2148 else if (filesize
< HOTBAND_MINIMUM_SIZE
)
2149 filesize
= HOTBAND_MINIMUM_SIZE
;
2151 * Calculate user quota file requirements.
2153 items
= QF_USERS_PER_GB
* (fs_size
/ GIGABYTE
);
2154 if (items
< QF_MIN_USERS
)
2155 items
= QF_MIN_USERS
;
2156 else if (items
> QF_MAX_USERS
)
2157 items
= QF_MAX_USERS
;
2158 if (!powerof2(items
)) {
2166 filesize
+= (items
+ 1) * sizeof(struct dqblk
);
2168 * Calculate group quota file requirements.
2171 items
= QF_GROUPS_PER_GB
* (fs_size
/ GIGABYTE
);
2172 if (items
< QF_MIN_GROUPS
)
2173 items
= QF_MIN_GROUPS
;
2174 else if (items
> QF_MAX_GROUPS
)
2175 items
= QF_MAX_GROUPS
;
2176 if (!powerof2(items
)) {
2184 filesize
+= (items
+ 1) * sizeof(struct dqblk
);
2185 zonesize
+= filesize
;
2188 * Round up entire zone to a bitmap block's worth.
2189 * The extra space goes to the catalog file and hot file area.
2192 zonesize
= roundup(zonesize
, vcb
->vcbVBMIOSize
* 8 * vcb
->blockSize
);
2193 temp
= zonesize
- temp
; /* temp has extra space */
2194 filesize
+= temp
/ 3;
2195 hfsmp
->hfs_catalog_maxblks
+= (temp
- (temp
/ 3)) / vcb
->blockSize
;
2197 hfsmp
->hfs_hotfile_maxblks
= filesize
/ vcb
->blockSize
;
2199 /* Convert to allocation blocks. */
2200 blk
= zonesize
/ vcb
->blockSize
;
2202 /* The default metadata zone location is at the start of volume. */
2203 hfsmp
->hfs_metazone_start
= 1;
2204 hfsmp
->hfs_metazone_end
= blk
- 1;
2206 /* The default hotfile area is at the end of the zone. */
2207 hfsmp
->hfs_hotfile_start
= blk
- (filesize
/ vcb
->blockSize
);
2208 hfsmp
->hfs_hotfile_end
= hfsmp
->hfs_metazone_end
;
2209 hfsmp
->hfs_hotfile_freeblks
= hfs_hotfile_freeblocks(hfsmp
);
2211 printf("HFS: metadata zone is %d to %d\n", hfsmp
->hfs_metazone_start
, hfsmp
->hfs_metazone_end
);
2212 printf("HFS: hot file band is %d to %d\n", hfsmp
->hfs_hotfile_start
, hfsmp
->hfs_hotfile_end
);
2213 printf("HFS: hot file band free blocks = %d\n", hfsmp
->hfs_hotfile_freeblks
);
2215 hfsmp
->hfs_flags
|= HFS_METADATA_ZONE
;
2220 hfs_hotfile_freeblocks(struct hfsmount
*hfsmp
)
2222 ExtendedVCB
*vcb
= HFSTOVCB(hfsmp
);
2226 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_BITMAP
, HFS_EXCLUSIVE_LOCK
);
2227 freeblocks
= MetaZoneFreeBlocks(vcb
);
2228 hfs_systemfile_unlock(hfsmp
, lockflags
);
2230 /* Minus Extents overflow file reserve. */
2232 hfsmp
->hfs_overflow_maxblks
- VTOF(hfsmp
->hfs_extents_vp
)->ff_blocks
;
2233 /* Minus catalog file reserve. */
2235 hfsmp
->hfs_catalog_maxblks
- VTOF(hfsmp
->hfs_catalog_vp
)->ff_blocks
;
2239 return MIN(freeblocks
, hfsmp
->hfs_hotfile_maxblks
);
2243 * Determine if a file is a "virtual" metadata file.
2244 * This includes journal and quota files.
2248 hfs_virtualmetafile(struct cnode
*cp
)
2253 if (cp
->c_parentcnid
!= kHFSRootFolderID
)
2256 filename
= cp
->c_desc
.cd_nameptr
;
2257 if (filename
== NULL
)
2260 if ((strcmp(filename
, ".journal") == 0) ||
2261 (strcmp(filename
, ".journal_info_block") == 0) ||
2262 (strcmp(filename
, ".quota.user") == 0) ||
2263 (strcmp(filename
, ".quota.group") == 0) ||
2264 (strcmp(filename
, ".hotfiles.btree") == 0))
2273 hfs_start_transaction(struct hfsmount
*hfsmp
)
2277 if (hfsmp
->jnl
== NULL
|| journal_owner(hfsmp
->jnl
) != current_thread()) {
2278 lck_rw_lock_shared(&hfsmp
->hfs_global_lock
);
2282 ret
= journal_start_transaction(hfsmp
->jnl
);
2284 OSAddAtomic(1, &hfsmp
->hfs_global_lock_nesting
);
2291 lck_rw_done(&hfsmp
->hfs_global_lock
);
2299 hfs_end_transaction(struct hfsmount
*hfsmp
)
2301 int need_unlock
=0, ret
;
2303 if ( hfsmp
->jnl
== NULL
2304 || ( journal_owner(hfsmp
->jnl
) == current_thread()
2305 && (OSAddAtomic(-1, &hfsmp
->hfs_global_lock_nesting
) == 1)) ) {
2311 ret
= journal_end_transaction(hfsmp
->jnl
);
2317 lck_rw_done(&hfsmp
->hfs_global_lock
);