2 * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
22 /* @(#)hfs_vfsutils.c 4.0
24 * (c) 1997-2002 Apple Computer, Inc. All Rights Reserved
26 * hfs_vfsutils.c -- Routines that go between the HFS layer and the VFS.
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/kernel.h>
32 #include <sys/malloc.h>
34 #include <sys/mount.h>
37 #include <sys/unistd.h>
38 #include <sys/utfconv.h>
39 #include <sys/kauth.h>
42 #include "hfs_catalog.h"
44 #include "hfs_mount.h"
45 #include "hfs_endian.h"
46 #include "hfs_cnode.h"
48 #include "hfscommon/headers/FileMgrInternal.h"
49 #include "hfscommon/headers/BTreesInternal.h"
50 #include "hfscommon/headers/HFSUnicodeWrappers.h"
53 extern int count_lock_queue(void);
56 static void ReleaseMetaFileVNode(struct vnode
*vp
);
57 static int hfs_late_journal_init(struct hfsmount
*hfsmp
, HFSPlusVolumeHeader
*vhp
, void *_args
);
59 static void hfs_metadatazone_init(struct hfsmount
*);
60 static u_int32_t
hfs_hotfile_freeblocks(struct hfsmount
*);
64 u_int32_t
GetLogicalBlockSize(struct vnode
*vp
);
66 extern int hfs_attrkeycompare(HFSPlusAttrKey
*searchKey
, HFSPlusAttrKey
*trialKey
);
69 //*******************************************************************************
70 // Note: Finder information in the HFS/HFS+ metadata are considered opaque and
71 // hence are not in the right byte order on little endian machines. It is
72 // the responsibility of the finder and other clients to swap the data.
73 //*******************************************************************************
75 //*******************************************************************************
76 // Routine: hfs_MountHFSVolume
79 //*******************************************************************************
80 char hfs_catname
[] = "Catalog B-tree";
81 char hfs_extname
[] = "Extents B-tree";
82 char hfs_vbmname
[] = "Volume Bitmap";
83 char hfs_attrname
[] = "Attribute B-tree";
85 char hfs_privdirname
[] =
86 "\xE2\x90\x80\xE2\x90\x80\xE2\x90\x80\xE2\x90\x80HFS+ Private Data";
89 OSErr
hfs_MountHFSVolume(struct hfsmount
*hfsmp
, HFSMasterDirectoryBlock
*mdb
,
92 ExtendedVCB
*vcb
= HFSTOVCB(hfsmp
);
95 struct cat_desc cndesc
;
96 struct cat_attr cnattr
;
99 /* Block size must be a multiple of 512 */
100 if (SWAP_BE32(mdb
->drAlBlkSiz
) == 0 ||
101 (SWAP_BE32(mdb
->drAlBlkSiz
) & 0x01FF) != 0)
104 /* don't mount a writeable volume if its dirty, it must be cleaned by fsck_hfs */
105 if (((hfsmp
->hfs_flags
& HFS_READ_ONLY
) == 0) &&
106 ((SWAP_BE16(mdb
->drAtrb
) & kHFSVolumeUnmountedMask
) == 0)) {
109 hfsmp
->hfs_flags
|= HFS_STANDARD
;
111 * The MDB seems OK: transfer info from it into VCB
112 * Note - the VCB starts out clear (all zeros)
115 vcb
->vcbSigWord
= SWAP_BE16 (mdb
->drSigWord
);
116 vcb
->vcbCrDate
= to_bsd_time(LocalToUTC(SWAP_BE32(mdb
->drCrDate
)));
117 vcb
->localCreateDate
= SWAP_BE32 (mdb
->drCrDate
);
118 vcb
->vcbLsMod
= to_bsd_time(LocalToUTC(SWAP_BE32(mdb
->drLsMod
)));
119 vcb
->vcbAtrb
= SWAP_BE16 (mdb
->drAtrb
);
120 vcb
->vcbNmFls
= SWAP_BE16 (mdb
->drNmFls
);
121 vcb
->vcbVBMSt
= SWAP_BE16 (mdb
->drVBMSt
);
122 vcb
->nextAllocation
= SWAP_BE16 (mdb
->drAllocPtr
);
123 vcb
->totalBlocks
= SWAP_BE16 (mdb
->drNmAlBlks
);
124 vcb
->blockSize
= SWAP_BE32 (mdb
->drAlBlkSiz
);
125 vcb
->vcbClpSiz
= SWAP_BE32 (mdb
->drClpSiz
);
126 vcb
->vcbAlBlSt
= SWAP_BE16 (mdb
->drAlBlSt
);
127 vcb
->vcbNxtCNID
= SWAP_BE32 (mdb
->drNxtCNID
);
128 vcb
->freeBlocks
= SWAP_BE16 (mdb
->drFreeBks
);
129 vcb
->vcbVolBkUp
= to_bsd_time(LocalToUTC(SWAP_BE32(mdb
->drVolBkUp
)));
130 vcb
->vcbWrCnt
= SWAP_BE32 (mdb
->drWrCnt
);
131 vcb
->vcbNmRtDirs
= SWAP_BE16 (mdb
->drNmRtDirs
);
132 vcb
->vcbFilCnt
= SWAP_BE32 (mdb
->drFilCnt
);
133 vcb
->vcbDirCnt
= SWAP_BE32 (mdb
->drDirCnt
);
134 bcopy(mdb
->drFndrInfo
, vcb
->vcbFndrInfo
, sizeof(vcb
->vcbFndrInfo
));
135 if ((hfsmp
->hfs_flags
& HFS_READ_ONLY
) == 0)
136 vcb
->vcbWrCnt
++; /* Compensate for write of MDB on last flush */
138 /* convert hfs encoded name into UTF-8 string */
139 error
= hfs_to_utf8(vcb
, mdb
->drVN
, NAME_MAX
, &utf8chars
, vcb
->vcbVN
);
141 * When an HFS name cannot be encoded with the current
142 * volume encoding we use MacRoman as a fallback.
144 if (error
|| (utf8chars
== 0))
145 (void) mac_roman_to_utf8(mdb
->drVN
, NAME_MAX
, &utf8chars
, vcb
->vcbVN
);
147 hfsmp
->hfs_logBlockSize
= BestBlockSizeFit(vcb
->blockSize
, MAXBSIZE
, hfsmp
->hfs_phys_block_size
);
148 vcb
->vcbVBMIOSize
= kHFSBlockSize
;
150 hfsmp
->hfs_alt_id_sector
= HFS_ALT_SECTOR(hfsmp
->hfs_phys_block_size
,
151 hfsmp
->hfs_phys_block_count
);
153 bzero(&cndesc
, sizeof(cndesc
));
154 cndesc
.cd_parentcnid
= kHFSRootParentID
;
155 cndesc
.cd_flags
|= CD_ISMETA
;
156 bzero(&cnattr
, sizeof(cnattr
));
158 cnattr
.ca_mode
= S_IFREG
;
159 bzero(&fork
, sizeof(fork
));
162 * Set up Extents B-tree vnode
164 cndesc
.cd_nameptr
= hfs_extname
;
165 cndesc
.cd_namelen
= strlen(hfs_extname
);
166 cndesc
.cd_cnid
= cnattr
.ca_fileid
= kHFSExtentsFileID
;
167 fork
.cf_size
= SWAP_BE32(mdb
->drXTFlSize
);
168 fork
.cf_blocks
= fork
.cf_size
/ vcb
->blockSize
;
169 fork
.cf_clump
= SWAP_BE32(mdb
->drXTClpSiz
);
171 fork
.cf_extents
[0].startBlock
= SWAP_BE16(mdb
->drXTExtRec
[0].startBlock
);
172 fork
.cf_extents
[0].blockCount
= SWAP_BE16(mdb
->drXTExtRec
[0].blockCount
);
173 fork
.cf_extents
[1].startBlock
= SWAP_BE16(mdb
->drXTExtRec
[1].startBlock
);
174 fork
.cf_extents
[1].blockCount
= SWAP_BE16(mdb
->drXTExtRec
[1].blockCount
);
175 fork
.cf_extents
[2].startBlock
= SWAP_BE16(mdb
->drXTExtRec
[2].startBlock
);
176 fork
.cf_extents
[2].blockCount
= SWAP_BE16(mdb
->drXTExtRec
[2].blockCount
);
177 cnattr
.ca_blocks
= fork
.cf_blocks
;
179 error
= hfs_getnewvnode(hfsmp
, NULL
, NULL
, &cndesc
, 0, &cnattr
, &fork
,
180 &hfsmp
->hfs_extents_vp
);
181 if (error
) goto MtVolErr
;
182 error
= MacToVFSError(BTOpenPath(VTOF(hfsmp
->hfs_extents_vp
),
183 (KeyCompareProcPtr
)CompareExtentKeys
));
185 hfs_unlock(VTOC(hfsmp
->hfs_extents_vp
));
190 * Set up Catalog B-tree vnode...
192 cndesc
.cd_nameptr
= hfs_catname
;
193 cndesc
.cd_namelen
= strlen(hfs_catname
);
194 cndesc
.cd_cnid
= cnattr
.ca_fileid
= kHFSCatalogFileID
;
195 fork
.cf_size
= SWAP_BE32(mdb
->drCTFlSize
);
196 fork
.cf_blocks
= fork
.cf_size
/ vcb
->blockSize
;
197 fork
.cf_clump
= SWAP_BE32(mdb
->drCTClpSiz
);
199 fork
.cf_extents
[0].startBlock
= SWAP_BE16(mdb
->drCTExtRec
[0].startBlock
);
200 fork
.cf_extents
[0].blockCount
= SWAP_BE16(mdb
->drCTExtRec
[0].blockCount
);
201 fork
.cf_extents
[1].startBlock
= SWAP_BE16(mdb
->drCTExtRec
[1].startBlock
);
202 fork
.cf_extents
[1].blockCount
= SWAP_BE16(mdb
->drCTExtRec
[1].blockCount
);
203 fork
.cf_extents
[2].startBlock
= SWAP_BE16(mdb
->drCTExtRec
[2].startBlock
);
204 fork
.cf_extents
[2].blockCount
= SWAP_BE16(mdb
->drCTExtRec
[2].blockCount
);
205 cnattr
.ca_blocks
= fork
.cf_blocks
;
207 error
= hfs_getnewvnode(hfsmp
, NULL
, NULL
, &cndesc
, 0, &cnattr
, &fork
,
208 &hfsmp
->hfs_catalog_vp
);
210 hfs_unlock(VTOC(hfsmp
->hfs_extents_vp
));
213 error
= MacToVFSError(BTOpenPath(VTOF(hfsmp
->hfs_catalog_vp
),
214 (KeyCompareProcPtr
)CompareCatalogKeys
));
216 hfs_unlock(VTOC(hfsmp
->hfs_catalog_vp
));
217 hfs_unlock(VTOC(hfsmp
->hfs_extents_vp
));
222 * Set up dummy Allocation file vnode (used only for locking bitmap)
224 cndesc
.cd_nameptr
= hfs_vbmname
;
225 cndesc
.cd_namelen
= strlen(hfs_vbmname
);
226 cndesc
.cd_cnid
= cnattr
.ca_fileid
= kHFSAllocationFileID
;
227 bzero(&fork
, sizeof(fork
));
228 cnattr
.ca_blocks
= 0;
230 error
= hfs_getnewvnode(hfsmp
, NULL
, NULL
, &cndesc
, 0, &cnattr
, &fork
,
231 &hfsmp
->hfs_allocation_vp
);
233 hfs_unlock(VTOC(hfsmp
->hfs_catalog_vp
));
234 hfs_unlock(VTOC(hfsmp
->hfs_extents_vp
));
238 /* mark the volume dirty (clear clean unmount bit) */
239 vcb
->vcbAtrb
&= ~kHFSVolumeUnmountedMask
;
242 * all done with system files so we can unlock now...
244 hfs_unlock(VTOC(hfsmp
->hfs_allocation_vp
));
245 hfs_unlock(VTOC(hfsmp
->hfs_catalog_vp
));
246 hfs_unlock(VTOC(hfsmp
->hfs_extents_vp
));
250 error
= cat_idlookup(hfsmp
, kHFSRootFolderID
, NULL
, NULL
, NULL
);
253 if ( error
== noErr
)
255 if ( !(vcb
->vcbAtrb
& kHFSVolumeHardwareLockMask
) ) // if the disk is not write protected
257 MarkVCBDirty( vcb
); // mark VCB dirty so it will be written
262 //-- Release any resources allocated so far before exiting with an error:
264 ReleaseMetaFileVNode(hfsmp
->hfs_catalog_vp
);
265 ReleaseMetaFileVNode(hfsmp
->hfs_extents_vp
);
271 //*******************************************************************************
272 // Routine: hfs_MountHFSPlusVolume
275 //*******************************************************************************
278 OSErr
hfs_MountHFSPlusVolume(struct hfsmount
*hfsmp
, HFSPlusVolumeHeader
*vhp
,
279 off_t embeddedOffset
, u_int64_t disksize
, struct proc
*p
, void *args
, kauth_cred_t cred
)
281 register ExtendedVCB
*vcb
;
282 struct cat_desc cndesc
;
283 struct cat_attr cnattr
;
284 struct cat_fork cfork
;
286 daddr64_t spare_sectors
;
287 struct BTreeInfoRec btinfo
;
293 signature
= SWAP_BE16(vhp
->signature
);
294 version
= SWAP_BE16(vhp
->version
);
296 if (signature
== kHFSPlusSigWord
) {
297 if (version
!= kHFSPlusVersion
) {
298 printf("hfs_mount: invalid HFS+ version: %d\n", version
);
301 } else if (signature
== kHFSXSigWord
) {
302 if (version
!= kHFSXVersion
) {
303 printf("hfs_mount: invalid HFSX version: %d\n", version
);
306 /* The in-memory signature is always 'H+'. */
307 signature
= kHFSPlusSigWord
;
308 hfsmp
->hfs_flags
|= HFS_X
;
310 /* Removed printf for invalid HFS+ signature because it gives
311 * false error for UFS root volume
316 /* Block size must be at least 512 and a power of 2 */
317 blockSize
= SWAP_BE32(vhp
->blockSize
);
318 if (blockSize
< 512 || !powerof2(blockSize
))
321 /* don't mount a writable volume if its dirty, it must be cleaned by fsck_hfs */
322 if ((hfsmp
->hfs_flags
& HFS_READ_ONLY
) == 0 && hfsmp
->jnl
== NULL
&&
323 (SWAP_BE32(vhp
->attributes
) & kHFSVolumeUnmountedMask
) == 0)
326 /* Make sure we can live with the physical block size. */
327 if ((disksize
& (hfsmp
->hfs_phys_block_size
- 1)) ||
328 (embeddedOffset
& (hfsmp
->hfs_phys_block_size
- 1)) ||
329 (blockSize
< hfsmp
->hfs_phys_block_size
)) {
333 * The VolumeHeader seems OK: transfer info from it into VCB
334 * Note - the VCB starts out clear (all zeros)
336 vcb
= HFSTOVCB(hfsmp
);
338 vcb
->vcbSigWord
= signature
;
339 vcb
->vcbJinfoBlock
= SWAP_BE32(vhp
->journalInfoBlock
);
340 vcb
->vcbLsMod
= to_bsd_time(SWAP_BE32(vhp
->modifyDate
));
341 vcb
->vcbAtrb
= SWAP_BE32(vhp
->attributes
);
342 vcb
->vcbClpSiz
= SWAP_BE32(vhp
->rsrcClumpSize
);
343 vcb
->vcbNxtCNID
= SWAP_BE32(vhp
->nextCatalogID
);
344 vcb
->vcbVolBkUp
= to_bsd_time(SWAP_BE32(vhp
->backupDate
));
345 vcb
->vcbWrCnt
= SWAP_BE32(vhp
->writeCount
);
346 vcb
->vcbFilCnt
= SWAP_BE32(vhp
->fileCount
);
347 vcb
->vcbDirCnt
= SWAP_BE32(vhp
->folderCount
);
349 /* copy 32 bytes of Finder info */
350 bcopy(vhp
->finderInfo
, vcb
->vcbFndrInfo
, sizeof(vhp
->finderInfo
));
352 vcb
->vcbAlBlSt
= 0; /* hfs+ allocation blocks start at first block of volume */
353 if ((hfsmp
->hfs_flags
& HFS_READ_ONLY
) == 0)
354 vcb
->vcbWrCnt
++; /* compensate for write of Volume Header on last flush */
356 /* Now fill in the Extended VCB info */
357 vcb
->nextAllocation
= SWAP_BE32(vhp
->nextAllocation
);
358 vcb
->totalBlocks
= SWAP_BE32(vhp
->totalBlocks
);
359 vcb
->freeBlocks
= SWAP_BE32(vhp
->freeBlocks
);
360 vcb
->blockSize
= blockSize
;
361 vcb
->encodingsBitmap
= SWAP_BE64(vhp
->encodingsBitmap
);
362 vcb
->localCreateDate
= SWAP_BE32(vhp
->createDate
);
364 vcb
->hfsPlusIOPosOffset
= embeddedOffset
;
366 /* Default to no free block reserve */
367 vcb
->reserveBlocks
= 0;
370 * Update the logical block size in the mount struct
371 * (currently set up from the wrapper MDB) using the
372 * new blocksize value:
374 hfsmp
->hfs_logBlockSize
= BestBlockSizeFit(vcb
->blockSize
, MAXBSIZE
, hfsmp
->hfs_phys_block_size
);
375 vcb
->vcbVBMIOSize
= min(vcb
->blockSize
, MAXPHYSIO
);
378 * Validate and initialize the location of the alternate volume header.
380 spare_sectors
= hfsmp
->hfs_phys_block_count
-
381 (((daddr64_t
)vcb
->totalBlocks
* blockSize
) /
382 hfsmp
->hfs_phys_block_size
);
384 if (spare_sectors
> (blockSize
/ hfsmp
->hfs_phys_block_size
)) {
385 hfsmp
->hfs_alt_id_sector
= 0; /* partition has grown! */
387 hfsmp
->hfs_alt_id_sector
= (hfsmp
->hfsPlusIOPosOffset
/ hfsmp
->hfs_phys_block_size
) +
388 HFS_ALT_SECTOR(hfsmp
->hfs_phys_block_size
,
389 hfsmp
->hfs_phys_block_count
);
392 bzero(&cndesc
, sizeof(cndesc
));
393 cndesc
.cd_parentcnid
= kHFSRootParentID
;
394 cndesc
.cd_flags
|= CD_ISMETA
;
395 bzero(&cnattr
, sizeof(cnattr
));
397 cnattr
.ca_mode
= S_IFREG
;
400 * Set up Extents B-tree vnode
402 cndesc
.cd_nameptr
= hfs_extname
;
403 cndesc
.cd_namelen
= strlen(hfs_extname
);
404 cndesc
.cd_cnid
= cnattr
.ca_fileid
= kHFSExtentsFileID
;
406 cfork
.cf_size
= SWAP_BE64 (vhp
->extentsFile
.logicalSize
);
407 cfork
.cf_clump
= SWAP_BE32 (vhp
->extentsFile
.clumpSize
);
408 cfork
.cf_blocks
= SWAP_BE32 (vhp
->extentsFile
.totalBlocks
);
409 cfork
.cf_vblocks
= 0;
410 cnattr
.ca_blocks
= cfork
.cf_blocks
;
411 for (i
= 0; i
< kHFSPlusExtentDensity
; i
++) {
412 cfork
.cf_extents
[i
].startBlock
=
413 SWAP_BE32 (vhp
->extentsFile
.extents
[i
].startBlock
);
414 cfork
.cf_extents
[i
].blockCount
=
415 SWAP_BE32 (vhp
->extentsFile
.extents
[i
].blockCount
);
417 retval
= hfs_getnewvnode(hfsmp
, NULL
, NULL
, &cndesc
, 0, &cnattr
, &cfork
,
418 &hfsmp
->hfs_extents_vp
);
420 if (retval
) goto ErrorExit
;
421 retval
= MacToVFSError(BTOpenPath(VTOF(hfsmp
->hfs_extents_vp
),
422 (KeyCompareProcPtr
) CompareExtentKeysPlus
));
424 hfs_unlock(VTOC(hfsmp
->hfs_extents_vp
));
429 * Set up Catalog B-tree vnode
431 cndesc
.cd_nameptr
= hfs_catname
;
432 cndesc
.cd_namelen
= strlen(hfs_catname
);
433 cndesc
.cd_cnid
= cnattr
.ca_fileid
= kHFSCatalogFileID
;
435 cfork
.cf_size
= SWAP_BE64 (vhp
->catalogFile
.logicalSize
);
436 cfork
.cf_clump
= SWAP_BE32 (vhp
->catalogFile
.clumpSize
);
437 cfork
.cf_blocks
= SWAP_BE32 (vhp
->catalogFile
.totalBlocks
);
438 cfork
.cf_vblocks
= 0;
439 cnattr
.ca_blocks
= cfork
.cf_blocks
;
440 for (i
= 0; i
< kHFSPlusExtentDensity
; i
++) {
441 cfork
.cf_extents
[i
].startBlock
=
442 SWAP_BE32 (vhp
->catalogFile
.extents
[i
].startBlock
);
443 cfork
.cf_extents
[i
].blockCount
=
444 SWAP_BE32 (vhp
->catalogFile
.extents
[i
].blockCount
);
446 retval
= hfs_getnewvnode(hfsmp
, NULL
, NULL
, &cndesc
, 0, &cnattr
, &cfork
,
447 &hfsmp
->hfs_catalog_vp
);
449 hfs_unlock(VTOC(hfsmp
->hfs_extents_vp
));
452 retval
= MacToVFSError(BTOpenPath(VTOF(hfsmp
->hfs_catalog_vp
),
453 (KeyCompareProcPtr
) CompareExtendedCatalogKeys
));
455 hfs_unlock(VTOC(hfsmp
->hfs_catalog_vp
));
456 hfs_unlock(VTOC(hfsmp
->hfs_extents_vp
));
459 if ((hfsmp
->hfs_flags
& HFS_X
) &&
460 BTGetInformation(VTOF(hfsmp
->hfs_catalog_vp
), 0, &btinfo
) == 0) {
461 if (btinfo
.keyCompareType
== kHFSBinaryCompare
) {
462 hfsmp
->hfs_flags
|= HFS_CASE_SENSITIVE
;
463 /* Install a case-sensitive key compare */
464 (void) BTOpenPath(VTOF(hfsmp
->hfs_catalog_vp
),
465 (KeyCompareProcPtr
)cat_binarykeycompare
);
470 * Set up Allocation file vnode
472 cndesc
.cd_nameptr
= hfs_vbmname
;
473 cndesc
.cd_namelen
= strlen(hfs_vbmname
);
474 cndesc
.cd_cnid
= cnattr
.ca_fileid
= kHFSAllocationFileID
;
476 cfork
.cf_size
= SWAP_BE64 (vhp
->allocationFile
.logicalSize
);
477 cfork
.cf_clump
= SWAP_BE32 (vhp
->allocationFile
.clumpSize
);
478 cfork
.cf_blocks
= SWAP_BE32 (vhp
->allocationFile
.totalBlocks
);
479 cfork
.cf_vblocks
= 0;
480 cnattr
.ca_blocks
= cfork
.cf_blocks
;
481 for (i
= 0; i
< kHFSPlusExtentDensity
; i
++) {
482 cfork
.cf_extents
[i
].startBlock
=
483 SWAP_BE32 (vhp
->allocationFile
.extents
[i
].startBlock
);
484 cfork
.cf_extents
[i
].blockCount
=
485 SWAP_BE32 (vhp
->allocationFile
.extents
[i
].blockCount
);
487 retval
= hfs_getnewvnode(hfsmp
, NULL
, NULL
, &cndesc
, 0, &cnattr
, &cfork
,
488 &hfsmp
->hfs_allocation_vp
);
490 hfs_unlock(VTOC(hfsmp
->hfs_catalog_vp
));
491 hfs_unlock(VTOC(hfsmp
->hfs_extents_vp
));
496 * Set up Attribute B-tree vnode
498 if (vhp
->attributesFile
.totalBlocks
!= 0) {
499 cndesc
.cd_nameptr
= hfs_attrname
;
500 cndesc
.cd_namelen
= strlen(hfs_attrname
);
501 cndesc
.cd_cnid
= cnattr
.ca_fileid
= kHFSAttributesFileID
;
503 cfork
.cf_size
= SWAP_BE64 (vhp
->attributesFile
.logicalSize
);
504 cfork
.cf_clump
= SWAP_BE32 (vhp
->attributesFile
.clumpSize
);
505 cfork
.cf_blocks
= SWAP_BE32 (vhp
->attributesFile
.totalBlocks
);
506 cfork
.cf_vblocks
= 0;
507 cnattr
.ca_blocks
= cfork
.cf_blocks
;
508 for (i
= 0; i
< kHFSPlusExtentDensity
; i
++) {
509 cfork
.cf_extents
[i
].startBlock
=
510 SWAP_BE32 (vhp
->attributesFile
.extents
[i
].startBlock
);
511 cfork
.cf_extents
[i
].blockCount
=
512 SWAP_BE32 (vhp
->attributesFile
.extents
[i
].blockCount
);
514 retval
= hfs_getnewvnode(hfsmp
, NULL
, NULL
, &cndesc
, 0, &cnattr
, &cfork
,
515 &hfsmp
->hfs_attribute_vp
);
517 hfs_unlock(VTOC(hfsmp
->hfs_allocation_vp
));
518 hfs_unlock(VTOC(hfsmp
->hfs_catalog_vp
));
519 hfs_unlock(VTOC(hfsmp
->hfs_extents_vp
));
522 retval
= MacToVFSError(BTOpenPath(VTOF(hfsmp
->hfs_attribute_vp
),
523 (KeyCompareProcPtr
) hfs_attrkeycompare
));
525 hfs_unlock(VTOC(hfsmp
->hfs_attribute_vp
));
526 hfs_unlock(VTOC(hfsmp
->hfs_allocation_vp
));
527 hfs_unlock(VTOC(hfsmp
->hfs_catalog_vp
));
528 hfs_unlock(VTOC(hfsmp
->hfs_extents_vp
));
534 /* Pick up volume name and create date */
535 retval
= cat_idlookup(hfsmp
, kHFSRootFolderID
, &cndesc
, &cnattr
, NULL
);
537 if (hfsmp
->hfs_attribute_vp
)
538 hfs_unlock(VTOC(hfsmp
->hfs_attribute_vp
));
539 hfs_unlock(VTOC(hfsmp
->hfs_allocation_vp
));
540 hfs_unlock(VTOC(hfsmp
->hfs_catalog_vp
));
541 hfs_unlock(VTOC(hfsmp
->hfs_extents_vp
));
544 vcb
->vcbCrDate
= cnattr
.ca_itime
;
545 vcb
->volumeNameEncodingHint
= cndesc
.cd_encoding
;
546 bcopy(cndesc
.cd_nameptr
, vcb
->vcbVN
, min(255, cndesc
.cd_namelen
));
547 cat_releasedesc(&cndesc
);
549 /* mark the volume dirty (clear clean unmount bit) */
550 vcb
->vcbAtrb
&= ~kHFSVolumeUnmountedMask
;
551 if (hfsmp
->jnl
&& (hfsmp
->hfs_flags
& HFS_READ_ONLY
) == 0) {
552 hfs_flushvolumeheader(hfsmp
, TRUE
, 0);
556 * all done with metadata files so we can unlock now...
558 if (hfsmp
->hfs_attribute_vp
)
559 hfs_unlock(VTOC(hfsmp
->hfs_attribute_vp
));
560 hfs_unlock(VTOC(hfsmp
->hfs_allocation_vp
));
561 hfs_unlock(VTOC(hfsmp
->hfs_catalog_vp
));
562 hfs_unlock(VTOC(hfsmp
->hfs_extents_vp
));
565 // Check if we need to do late journal initialization. This only
566 // happens if a previous version of MacOS X (or 9) touched the disk.
567 // In that case hfs_late_journal_init() will go re-locate the journal
568 // and journal_info_block files and validate that they're still kosher.
570 if ( (vcb
->vcbAtrb
& kHFSVolumeJournaledMask
)
571 && (SWAP_BE32(vhp
->lastMountedVersion
) != kHFSJMountVersion
)
572 && (hfsmp
->jnl
== NULL
)) {
574 retval
= hfs_late_journal_init(hfsmp
, vhp
, args
);
578 // if the journal failed to open, then set the lastMountedVersion
579 // to be "FSK!" which fsck_hfs will see and force the fsck instead
580 // of just bailing out because the volume is journaled.
581 if (!(hfsmp
->hfs_flags
& HFS_READ_ONLY
)) {
582 HFSPlusVolumeHeader
*jvhp
;
583 daddr64_t mdb_offset
;
584 struct buf
*bp
= NULL
;
586 hfsmp
->hfs_flags
|= HFS_NEED_JNL_RESET
;
588 mdb_offset
= (daddr64_t
)((embeddedOffset
/ blockSize
) + HFS_PRI_SECTOR(blockSize
));
590 retval
= (int)buf_meta_bread(hfsmp
->hfs_devvp
, mdb_offset
, blockSize
, cred
, &bp
);
592 jvhp
= (HFSPlusVolumeHeader
*)(buf_dataptr(bp
) + HFS_PRI_OFFSET(blockSize
));
594 if (SWAP_BE16(jvhp
->signature
) == kHFSPlusSigWord
|| SWAP_BE16(jvhp
->signature
) == kHFSXSigWord
) {
595 printf ("hfs(3): Journal replay fail. Writing lastMountVersion as FSK!\n");
596 jvhp
->lastMountedVersion
= SWAP_BE32(kFSKMountVersion
);
604 // clear this so the error exit path won't try to use it
611 } else if (hfsmp
->jnl
) {
612 vfs_setflags(hfsmp
->hfs_mp
, (uint64_t)((unsigned int)MNT_JOURNALED
));
614 } else if (hfsmp
->jnl
|| ((vcb
->vcbAtrb
& kHFSVolumeJournaledMask
) && (hfsmp
->hfs_flags
& HFS_READ_ONLY
))) {
615 struct cat_attr jinfo_attr
, jnl_attr
;
617 if (hfsmp
->hfs_flags
& HFS_READ_ONLY
) {
618 vcb
->vcbAtrb
&= ~kHFSVolumeJournaledMask
;
621 // if we're here we need to fill in the fileid's for the
622 // journal and journal_info_block.
623 hfsmp
->hfs_jnlinfoblkid
= GetFileInfo(vcb
, kRootDirID
, ".journal_info_block", &jinfo_attr
, NULL
);
624 hfsmp
->hfs_jnlfileid
= GetFileInfo(vcb
, kRootDirID
, ".journal", &jnl_attr
, NULL
);
625 if (hfsmp
->hfs_jnlinfoblkid
== 0 || hfsmp
->hfs_jnlfileid
== 0) {
626 printf("hfs: danger! couldn't find the file-id's for the journal or journal_info_block\n");
627 printf("hfs: jnlfileid %d, jnlinfoblkid %d\n", hfsmp
->hfs_jnlfileid
, hfsmp
->hfs_jnlinfoblkid
);
630 if (hfsmp
->hfs_flags
& HFS_READ_ONLY
) {
631 vcb
->vcbAtrb
|= kHFSVolumeJournaledMask
;
636 * Establish a metadata allocation zone.
638 hfs_metadatazone_init(hfsmp
);
641 * Make any metadata zone adjustments.
643 if (hfsmp
->hfs_flags
& HFS_METADATA_ZONE
) {
644 /* Keep the roving allocator out of the metadata zone. */
645 if (vcb
->nextAllocation
>= hfsmp
->hfs_metazone_start
&&
646 vcb
->nextAllocation
<= hfsmp
->hfs_metazone_end
) {
647 vcb
->nextAllocation
= hfsmp
->hfs_metazone_end
+ 1;
651 /* setup private/hidden directory for unlinked files */
652 FindMetaDataDirectory(vcb
);
653 if ((hfsmp
->hfs_flags
& HFS_READ_ONLY
) == 0)
654 hfs_remove_orphans(hfsmp
);
656 if ( !(vcb
->vcbAtrb
& kHFSVolumeHardwareLockMask
) ) // if the disk is not write protected
658 MarkVCBDirty( vcb
); // mark VCB dirty so it will be written
662 * Allow hot file clustering if conditions allow.
664 if ((hfsmp
->hfs_flags
& HFS_METADATA_ZONE
) &&
665 ((hfsmp
->hfs_flags
& HFS_READ_ONLY
) == 0)) {
666 (void) hfs_recording_init(hfsmp
);
669 hfs_checkextendedsecurity(hfsmp
);
675 * A fatal error occurred and the volume cannot be mounted
676 * release any resources that we aquired...
678 if (hfsmp
->hfs_attribute_vp
)
679 ReleaseMetaFileVNode(hfsmp
->hfs_attribute_vp
);
680 ReleaseMetaFileVNode(hfsmp
->hfs_allocation_vp
);
681 ReleaseMetaFileVNode(hfsmp
->hfs_catalog_vp
);
682 ReleaseMetaFileVNode(hfsmp
->hfs_extents_vp
);
689 * ReleaseMetaFileVNode
693 static void ReleaseMetaFileVNode(struct vnode
*vp
)
697 if (vp
&& (fp
= VTOF(vp
))) {
698 if (fp
->fcbBTCBPtr
!= NULL
) {
699 (void)hfs_lock(VTOC(vp
), HFS_EXCLUSIVE_LOCK
);
700 (void) BTClosePath(fp
);
701 hfs_unlock(VTOC(vp
));
704 /* release the node even if BTClosePath fails */
711 /*************************************************************
713 * Unmounts a hfs volume.
714 * At this point vflush() has been called (to dump all non-metadata files)
716 *************************************************************/
720 hfsUnmount( register struct hfsmount
*hfsmp
, struct proc
*p
)
722 if (hfsmp
->hfs_allocation_vp
)
723 ReleaseMetaFileVNode(hfsmp
->hfs_allocation_vp
);
725 if (hfsmp
->hfs_attribute_vp
)
726 ReleaseMetaFileVNode(hfsmp
->hfs_attribute_vp
);
728 ReleaseMetaFileVNode(hfsmp
->hfs_catalog_vp
);
729 ReleaseMetaFileVNode(hfsmp
->hfs_extents_vp
);
736 * Test if fork has overflow extents.
740 overflow_extents(struct filefork
*fp
)
744 if (VTOVCB(FTOV(fp
))->vcbSigWord
== kHFSPlusSigWord
) {
745 if (fp
->ff_extents
[7].blockCount
== 0)
748 blocks
= fp
->ff_extents
[0].blockCount
+
749 fp
->ff_extents
[1].blockCount
+
750 fp
->ff_extents
[2].blockCount
+
751 fp
->ff_extents
[3].blockCount
+
752 fp
->ff_extents
[4].blockCount
+
753 fp
->ff_extents
[5].blockCount
+
754 fp
->ff_extents
[6].blockCount
+
755 fp
->ff_extents
[7].blockCount
;
757 if (fp
->ff_extents
[2].blockCount
== 0)
760 blocks
= fp
->ff_extents
[0].blockCount
+
761 fp
->ff_extents
[1].blockCount
+
762 fp
->ff_extents
[2].blockCount
;
765 return (fp
->ff_blocks
> blocks
);
770 * Lock HFS system file(s).
774 hfs_systemfile_lock(struct hfsmount
*hfsmp
, int flags
, enum hfslocktype locktype
)
776 if (flags
& ~SFL_VALIDMASK
)
777 panic("hfs_systemfile_lock: invalid lock request (0x%x)", (unsigned long) flags
);
779 * Locking order is Catalog file, Attributes file, Bitmap file, Extents file
781 if (flags
& SFL_CATALOG
) {
782 (void) hfs_lock(VTOC(hfsmp
->hfs_catalog_vp
), locktype
);
784 * When the catalog file has overflow extents then
785 * also acquire the extents b-tree lock if its not
788 if ((flags
& SFL_EXTENTS
) == 0 &&
789 overflow_extents(VTOF(hfsmp
->hfs_catalog_vp
))) {
790 flags
|= SFL_EXTENTS
;
793 if (flags
& SFL_ATTRIBUTE
) {
794 if (hfsmp
->hfs_attribute_vp
) {
795 (void) hfs_lock(VTOC(hfsmp
->hfs_attribute_vp
), locktype
);
797 * When the attribute 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_attribute_vp
))) {
803 flags
|= SFL_EXTENTS
;
806 flags
&= ~SFL_ATTRIBUTE
;
809 if (flags
& SFL_BITMAP
) {
811 * Since the only bitmap operations are clearing and
812 * setting bits we always need exclusive access. And
813 * when we have a journal, we can "hide" behind that
814 * lock since we can only change the bitmap from
815 * within a transaction.
818 flags
&= ~SFL_BITMAP
;
820 (void) hfs_lock(VTOC(hfsmp
->hfs_allocation_vp
), HFS_EXCLUSIVE_LOCK
);
823 if (flags
& SFL_EXTENTS
) {
825 * Since the extents btree lock is recursive we always
826 * need exclusive access.
828 (void) hfs_lock(VTOC(hfsmp
->hfs_extents_vp
), HFS_EXCLUSIVE_LOCK
);
834 * unlock HFS system file(s).
838 hfs_systemfile_unlock(struct hfsmount
*hfsmp
, int flags
)
842 int numOfLockedBuffs
;
845 lastfsync
= tv
.tv_sec
;
847 if (flags
& ~SFL_VALIDMASK
)
848 panic("hfs_systemfile_unlock: invalid lock request (0x%x)", (unsigned long) flags
);
850 if (flags
& SFL_ATTRIBUTE
&& hfsmp
->hfs_attribute_vp
) {
851 if (hfsmp
->jnl
== NULL
) {
852 BTGetLastSync((FCB
*)VTOF(hfsmp
->hfs_attribute_vp
), &lastfsync
);
853 numOfLockedBuffs
= count_lock_queue();
854 if ((numOfLockedBuffs
> kMaxLockedMetaBuffers
) ||
855 ((numOfLockedBuffs
> 1) && ((tv
.tv_sec
- lastfsync
) >
856 kMaxSecsForFsync
))) {
857 hfs_btsync(hfsmp
->hfs_attribute_vp
, HFS_SYNCTRANS
);
860 hfs_unlock(VTOC(hfsmp
->hfs_attribute_vp
));
862 if (flags
& SFL_CATALOG
) {
863 if (hfsmp
->jnl
== NULL
) {
864 BTGetLastSync((FCB
*)VTOF(hfsmp
->hfs_catalog_vp
), &lastfsync
);
865 numOfLockedBuffs
= count_lock_queue();
866 if ((numOfLockedBuffs
> kMaxLockedMetaBuffers
) ||
867 ((numOfLockedBuffs
> 1) && ((tv
.tv_sec
- lastfsync
) >
868 kMaxSecsForFsync
))) {
869 hfs_btsync(hfsmp
->hfs_catalog_vp
, HFS_SYNCTRANS
);
872 hfs_unlock(VTOC(hfsmp
->hfs_catalog_vp
));
874 if (flags
& SFL_BITMAP
) {
875 hfs_unlock(VTOC(hfsmp
->hfs_allocation_vp
));
877 if (flags
& SFL_EXTENTS
) {
878 if (hfsmp
->jnl
== NULL
) {
879 BTGetLastSync((FCB
*)VTOF(hfsmp
->hfs_extents_vp
), &lastfsync
);
880 numOfLockedBuffs
= count_lock_queue();
881 if ((numOfLockedBuffs
> kMaxLockedMetaBuffers
) ||
882 ((numOfLockedBuffs
> 1) && ((tv
.tv_sec
- lastfsync
) >
883 kMaxSecsForFsync
))) {
884 hfs_btsync(hfsmp
->hfs_extents_vp
, HFS_SYNCTRANS
);
887 hfs_unlock(VTOC(hfsmp
->hfs_extents_vp
));
895 * Check to see if a vnode is locked in the current context
896 * This is to be used for debugging purposes only!!
899 void RequireFileLock(FileReference vp
, int shareable
)
903 /* The extents btree and allocation bitmap are always exclusive. */
904 if (VTOC(vp
)->c_fileid
== kHFSExtentsFileID
||
905 VTOC(vp
)->c_fileid
== kHFSAllocationFileID
) {
909 locked
= VTOC(vp
)->c_lockowner
== (void *)current_thread();
911 if (!locked
&& !shareable
) {
912 switch (VTOC(vp
)->c_fileid
) {
913 case kHFSExtentsFileID
:
914 panic("extents btree not locked! v: 0x%08X\n #\n", (u_int
)vp
);
916 case kHFSCatalogFileID
:
917 panic("catalog btree not locked! v: 0x%08X\n #\n", (u_int
)vp
);
919 case kHFSAllocationFileID
:
920 /* The allocation file can hide behind the jornal lock. */
921 if (VTOHFS(vp
)->jnl
== NULL
)
922 panic("allocation file not locked! v: 0x%08X\n #\n", (u_int
)vp
);
924 case kHFSAttributesFileID
:
925 panic("attributes btree not locked! v: 0x%08X\n #\n", (u_int
)vp
);
934 * There are three ways to qualify for ownership rights on an object:
936 * 1. (a) Your UID matches the cnode's UID.
937 * (b) The object in question is owned by "unknown"
938 * 2. (a) Permissions on the filesystem are being ignored and
939 * your UID matches the replacement UID.
940 * (b) Permissions on the filesystem are being ignored and
941 * the replacement UID is "unknown".
946 hfs_owner_rights(struct hfsmount
*hfsmp
, uid_t cnode_uid
, kauth_cred_t cred
,
947 struct proc
*p
, int invokesuperuserstatus
)
949 if ((kauth_cred_getuid(cred
) == cnode_uid
) || /* [1a] */
950 (cnode_uid
== UNKNOWNUID
) || /* [1b] */
951 ((((unsigned int)vfs_flags(HFSTOVFS(hfsmp
))) & MNT_UNKNOWNPERMISSIONS
) && /* [2] */
952 ((kauth_cred_getuid(cred
) == hfsmp
->hfs_uid
) || /* [2a] */
953 (hfsmp
->hfs_uid
== UNKNOWNUID
))) || /* [2b] */
954 (invokesuperuserstatus
&& (suser(cred
, 0) == 0))) { /* [3] */
962 unsigned long BestBlockSizeFit(unsigned long allocationBlockSize
,
963 unsigned long blockSizeLimit
,
964 unsigned long baseMultiple
) {
966 Compute the optimal (largest) block size (no larger than allocationBlockSize) that is less than the
967 specified limit but still an even multiple of the baseMultiple.
969 int baseBlockCount
, blockCount
;
970 unsigned long trialBlockSize
;
972 if (allocationBlockSize
% baseMultiple
!= 0) {
974 Whoops: the allocation blocks aren't even multiples of the specified base:
975 no amount of dividing them into even parts will be a multiple, either then!
977 return 512; /* Hope for the best */
980 /* Try the obvious winner first, to prevent 12K allocation blocks, for instance,
981 from being handled as two 6K logical blocks instead of 3 4K logical blocks.
982 Even though the former (the result of the loop below) is the larger allocation
983 block size, the latter is more efficient: */
984 if (allocationBlockSize
% PAGE_SIZE
== 0) return PAGE_SIZE
;
986 /* No clear winner exists: pick the largest even fraction <= MAXBSIZE: */
987 baseBlockCount
= allocationBlockSize
/ baseMultiple
; /* Now guaranteed to be an even multiple */
989 for (blockCount
= baseBlockCount
; blockCount
> 0; --blockCount
) {
990 trialBlockSize
= blockCount
* baseMultiple
;
991 if (allocationBlockSize
% trialBlockSize
== 0) { /* An even multiple? */
992 if ((trialBlockSize
<= blockSizeLimit
) &&
993 (trialBlockSize
% baseMultiple
== 0)) {
994 return trialBlockSize
;
999 /* Note: we should never get here, since blockCount = 1 should always work,
1000 but this is nice and safe and makes the compiler happy, too ... */
1006 * To make the HFS Plus filesystem follow UFS unlink semantics, a remove
1007 * of an active vnode is translated to a move/rename so the file appears
1008 * deleted. The destination folder for these move/renames is setup here
1009 * and a reference to it is place in hfsmp->hfs_privdir_desc.
1013 FindMetaDataDirectory(ExtendedVCB
*vcb
)
1015 struct hfsmount
* hfsmp
;
1016 struct vnode
* dvp
= NULL
;
1017 struct cnode
* dcp
= NULL
;
1018 struct FndrDirInfo
* fndrinfo
;
1019 struct cat_desc out_desc
= {0};
1020 struct proc
*p
= current_proc();
1022 cat_cookie_t cookie
;
1026 if (vcb
->vcbSigWord
!= kHFSPlusSigWord
)
1029 hfsmp
= VCBTOHFS(vcb
);
1031 if (hfsmp
->hfs_privdir_desc
.cd_parentcnid
== 0) {
1032 hfsmp
->hfs_privdir_desc
.cd_parentcnid
= kRootDirID
;
1033 hfsmp
->hfs_privdir_desc
.cd_nameptr
= hfs_privdirname
;
1034 hfsmp
->hfs_privdir_desc
.cd_namelen
= strlen(hfs_privdirname
);
1035 hfsmp
->hfs_privdir_desc
.cd_flags
= CD_ISDIR
;
1038 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_SHARED_LOCK
);
1040 error
= cat_lookup(hfsmp
, &hfsmp
->hfs_privdir_desc
, 0, NULL
,
1041 &hfsmp
->hfs_privdir_attr
, NULL
, NULL
);
1043 hfs_systemfile_unlock(hfsmp
, lockflags
);
1046 hfsmp
->hfs_metadata_createdate
= hfsmp
->hfs_privdir_attr
.ca_itime
;
1047 hfsmp
->hfs_privdir_desc
.cd_cnid
= hfsmp
->hfs_privdir_attr
.ca_fileid
;
1049 * Clear the system immutable flag if set...
1051 if ((hfsmp
->hfs_privdir_attr
.ca_flags
& SF_IMMUTABLE
) &&
1052 (hfsmp
->hfs_flags
& HFS_READ_ONLY
) == 0) {
1053 hfsmp
->hfs_privdir_attr
.ca_flags
&= ~SF_IMMUTABLE
;
1055 if ((error
= hfs_start_transaction(hfsmp
)) != 0) {
1056 return (hfsmp
->hfs_privdir_attr
.ca_fileid
);
1059 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_SHARED_LOCK
);
1060 (void) cat_update(hfsmp
, &hfsmp
->hfs_privdir_desc
,
1061 &hfsmp
->hfs_privdir_attr
, NULL
, NULL
);
1062 hfs_systemfile_unlock(hfsmp
, lockflags
);
1064 hfs_end_transaction(hfsmp
);
1066 return (hfsmp
->hfs_privdir_attr
.ca_fileid
);
1068 } else if (hfsmp
->hfs_flags
& HFS_READ_ONLY
) {
1073 /* Setup the default attributes */
1074 bzero(&hfsmp
->hfs_privdir_attr
, sizeof(struct cat_attr
));
1075 hfsmp
->hfs_privdir_attr
.ca_mode
= S_IFDIR
;
1076 hfsmp
->hfs_privdir_attr
.ca_nlink
= 2;
1077 hfsmp
->hfs_privdir_attr
.ca_itime
= vcb
->vcbCrDate
;
1079 hfsmp
->hfs_privdir_attr
.ca_mtime
= tv
.tv_sec
;
1081 /* hidden and off the desktop view */
1082 fndrinfo
= (struct FndrDirInfo
*)&hfsmp
->hfs_privdir_attr
.ca_finderinfo
;
1083 fndrinfo
->frLocation
.v
= SWAP_BE16 (22460);
1084 fndrinfo
->frLocation
.h
= SWAP_BE16 (22460);
1085 fndrinfo
->frFlags
|= SWAP_BE16 (kIsInvisible
+ kNameLocked
);
1087 if ((error
= hfs_start_transaction(hfsmp
)) != 0) {
1090 /* Reserve some space in the Catalog file. */
1091 if (cat_preflight(hfsmp
, CAT_CREATE
, &cookie
, p
) != 0) {
1092 hfs_end_transaction(hfsmp
);
1097 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_EXCLUSIVE_LOCK
);
1099 error
= cat_create(hfsmp
, &hfsmp
->hfs_privdir_desc
,
1100 &hfsmp
->hfs_privdir_attr
, &out_desc
);
1102 hfs_systemfile_unlock(hfsmp
, lockflags
);
1104 cat_postflight(hfsmp
, &cookie
, p
);
1107 hfs_volupdate(hfsmp
, VOL_UPDATE
, 0);
1109 hfs_end_transaction(hfsmp
);
1114 hfsmp
->hfs_privdir_desc
.cd_hint
= out_desc
.cd_hint
;
1115 hfsmp
->hfs_privdir_desc
.cd_cnid
= out_desc
.cd_cnid
;
1116 hfsmp
->hfs_privdir_attr
.ca_fileid
= out_desc
.cd_cnid
;
1117 hfsmp
->hfs_metadata_createdate
= vcb
->vcbCrDate
;
1119 if (hfs_vget(hfsmp
, kRootDirID
, &dvp
, 0) == 0) {
1121 dcp
->c_childhint
= out_desc
.cd_hint
;
1124 dcp
->c_touch_chgtime
= TRUE
;
1125 dcp
->c_touch_modtime
= TRUE
;
1126 (void) hfs_update(dvp
, 0);
1130 hfs_volupdate(hfsmp
, VOL_MKDIR
, 1);
1131 hfs_end_transaction(hfsmp
);
1133 cat_releasedesc(&out_desc
);
1135 return (out_desc
.cd_cnid
);
1140 GetFileInfo(ExtendedVCB
*vcb
, u_int32_t dirid
, const char *name
,
1141 struct cat_attr
*fattr
, struct cat_fork
*forkinfo
)
1143 struct hfsmount
* hfsmp
;
1144 struct vnode
* dvp
= NULL
;
1145 struct cnode
* dcp
= NULL
;
1146 struct FndrDirInfo
* fndrinfo
;
1147 struct cat_desc jdesc
;
1151 if (vcb
->vcbSigWord
!= kHFSPlusSigWord
)
1154 hfsmp
= VCBTOHFS(vcb
);
1156 memset(&jdesc
, 0, sizeof(struct cat_desc
));
1157 jdesc
.cd_parentcnid
= kRootDirID
;
1158 jdesc
.cd_nameptr
= name
;
1159 jdesc
.cd_namelen
= strlen(name
);
1161 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_SHARED_LOCK
);
1162 error
= cat_lookup(hfsmp
, &jdesc
, 0, NULL
, fattr
, forkinfo
, NULL
);
1163 hfs_systemfile_unlock(hfsmp
, lockflags
);
1166 return (fattr
->ca_fileid
);
1167 } else if (hfsmp
->hfs_flags
& HFS_READ_ONLY
) {
1171 return (0); /* XXX what callers expect on an error */
1176 * On HFS Plus Volume, there can be orphaned files. These
1177 * are files that were unlinked while busy. If the volume
1178 * was not cleanly unmounted then some of these files may
1179 * have persisted and need to be removed.
1183 hfs_remove_orphans(struct hfsmount
* hfsmp
)
1185 struct BTreeIterator
* iterator
= NULL
;
1186 struct FSBufferDescriptor btdata
;
1187 struct HFSPlusCatalogFile filerec
;
1188 struct HFSPlusCatalogKey
* keyp
;
1189 struct proc
*p
= current_proc();
1195 cat_cookie_t cookie
;
1201 int orphanedlinks
= 0;
1203 bzero(&cookie
, sizeof(cookie
));
1205 if (hfsmp
->hfs_flags
& HFS_CLEANED_ORPHANS
)
1208 vcb
= HFSTOVCB(hfsmp
);
1209 fcb
= VTOF(hfsmp
->hfs_catalog_vp
);
1211 btdata
.bufferAddress
= &filerec
;
1212 btdata
.itemSize
= sizeof(filerec
);
1213 btdata
.itemCount
= 1;
1215 MALLOC(iterator
, struct BTreeIterator
*, sizeof(*iterator
), M_TEMP
, M_WAITOK
);
1216 bzero(iterator
, sizeof(*iterator
));
1218 /* Build a key to "temp" */
1219 keyp
= (HFSPlusCatalogKey
*)&iterator
->key
;
1220 keyp
->parentID
= hfsmp
->hfs_privdir_desc
.cd_cnid
;
1221 keyp
->nodeName
.length
= 4; /* "temp" */
1222 keyp
->keyLength
= kHFSPlusCatalogKeyMinimumLength
+ keyp
->nodeName
.length
* 2;
1223 keyp
->nodeName
.unicode
[0] = 't';
1224 keyp
->nodeName
.unicode
[1] = 'e';
1225 keyp
->nodeName
.unicode
[2] = 'm';
1226 keyp
->nodeName
.unicode
[3] = 'p';
1229 * Position the iterator just before the first real temp file.
1231 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_EXCLUSIVE_LOCK
);
1232 (void) BTSearchRecord(fcb
, iterator
, NULL
, NULL
, iterator
);
1233 hfs_systemfile_unlock(hfsmp
, lockflags
);
1235 /* Visit all the temp files in the HFS+ private directory. */
1237 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_EXCLUSIVE_LOCK
);
1238 result
= BTIterateRecord(fcb
, kBTreeNextRecord
, iterator
, &btdata
, NULL
);
1239 hfs_systemfile_unlock(hfsmp
, lockflags
);
1242 if (keyp
->parentID
!= hfsmp
->hfs_privdir_desc
.cd_cnid
)
1244 if (filerec
.recordType
!= kHFSPlusFileRecord
)
1247 (void) utf8_encodestr(keyp
->nodeName
.unicode
, keyp
->nodeName
.length
* 2,
1248 filename
, &namelen
, sizeof(filename
), 0, 0);
1250 (void) sprintf(tempname
, "%s%d", HFS_DELETE_PREFIX
, filerec
.fileID
);
1253 * Delete all files named "tempxxx", where
1254 * xxx is the file's cnid in decimal.
1257 if (bcmp(tempname
, filename
, namelen
) == 0) {
1258 struct filefork dfork
;
1259 struct filefork rfork
;
1262 bzero(&dfork
, sizeof(dfork
));
1263 bzero(&rfork
, sizeof(rfork
));
1264 bzero(&cnode
, sizeof(cnode
));
1266 if (hfs_start_transaction(hfsmp
) != 0) {
1267 printf("hfs_remove_orphans: failed to start transaction\n");
1273 * Reserve some space in the Catalog file.
1275 if (cat_preflight(hfsmp
, CAT_DELETE
, &cookie
, p
) != 0) {
1276 printf("hfs_remove_orphans: cat_preflight failed\n");
1281 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
| SFL_ATTRIBUTE
| SFL_EXTENTS
| SFL_BITMAP
, HFS_EXCLUSIVE_LOCK
);
1284 /* Build a fake cnode */
1285 cat_convertattr(hfsmp
, (CatalogRecord
*)&filerec
, &cnode
.c_attr
,
1286 &dfork
.ff_data
, &rfork
.ff_data
);
1287 cnode
.c_desc
.cd_parentcnid
= hfsmp
->hfs_privdir_desc
.cd_cnid
;
1288 cnode
.c_desc
.cd_nameptr
= filename
;
1289 cnode
.c_desc
.cd_namelen
= namelen
;
1290 cnode
.c_desc
.cd_cnid
= cnode
.c_attr
.ca_fileid
;
1291 cnode
.c_blocks
= dfork
.ff_blocks
+ rfork
.ff_blocks
;
1293 /* Position iterator at previous entry */
1294 if (BTIterateRecord(fcb
, kBTreePrevRecord
, iterator
,
1299 /* Truncate the file to zero (both forks) */
1300 if (dfork
.ff_blocks
> 0) {
1303 dfork
.ff_cp
= &cnode
;
1304 cnode
.c_datafork
= &dfork
;
1305 cnode
.c_rsrcfork
= NULL
;
1306 fsize
= (u_int64_t
)dfork
.ff_blocks
* (u_int64_t
)HFSTOVCB(hfsmp
)->blockSize
;
1308 if (fsize
> HFS_BIGFILE_SIZE
) {
1309 fsize
-= HFS_BIGFILE_SIZE
;
1314 if (TruncateFileC(vcb
, (FCB
*)&dfork
, fsize
, false) != 0) {
1315 printf("error truncting data fork!\n");
1320 // if we're iteratively truncating this file down,
1321 // then end the transaction and start a new one so
1322 // that no one transaction gets too big.
1324 if (fsize
> 0 && started_tr
) {
1325 hfs_end_transaction(hfsmp
);
1326 if (hfs_start_transaction(hfsmp
) != 0) {
1334 if (rfork
.ff_blocks
> 0) {
1335 rfork
.ff_cp
= &cnode
;
1336 cnode
.c_datafork
= NULL
;
1337 cnode
.c_rsrcfork
= &rfork
;
1338 if (TruncateFileC(vcb
, (FCB
*)&rfork
, 0, false) != 0) {
1339 printf("error truncting rsrc fork!\n");
1344 /* Remove the file record from the Catalog */
1345 if (cat_delete(hfsmp
, &cnode
.c_desc
, &cnode
.c_attr
) != 0) {
1346 printf("hfs_remove_oprhans: error deleting cat rec for id %d!\n", cnode
.c_desc
.cd_cnid
);
1347 hfs_volupdate(hfsmp
, VOL_UPDATE
, 0);
1352 /* Delete any attributes, ignore errors */
1353 (void) hfs_removeallattr(hfsmp
, cnode
.c_fileid
);
1355 /* Update parent and volume counts */
1356 hfsmp
->hfs_privdir_attr
.ca_entries
--;
1357 (void)cat_update(hfsmp
, &hfsmp
->hfs_privdir_desc
,
1358 &hfsmp
->hfs_privdir_attr
, NULL
, NULL
);
1359 hfs_volupdate(hfsmp
, VOL_RMFILE
, 0);
1361 /* Drop locks and end the transaction */
1362 hfs_systemfile_unlock(hfsmp
, lockflags
);
1363 cat_postflight(hfsmp
, &cookie
, p
);
1364 catlock
= catreserve
= 0;
1366 hfs_end_transaction(hfsmp
);
1372 if (orphanedlinks
> 0)
1373 printf("HFS: Removed %d orphaned unlinked files\n", orphanedlinks
);
1376 hfs_systemfile_unlock(hfsmp
, lockflags
);
1379 cat_postflight(hfsmp
, &cookie
, p
);
1382 hfs_end_transaction(hfsmp
);
1385 FREE(iterator
, M_TEMP
);
1386 hfsmp
->hfs_flags
|= HFS_CLEANED_ORPHANS
;
1391 * This will return the correct logical block size for a given vnode.
1392 * For most files, it is the allocation block size, for meta data like
1393 * BTrees, this is kept as part of the BTree private nodeSize
1396 GetLogicalBlockSize(struct vnode
*vp
)
1398 u_int32_t logBlockSize
;
1400 DBG_ASSERT(vp
!= NULL
);
1402 /* start with default */
1403 logBlockSize
= VTOHFS(vp
)->hfs_logBlockSize
;
1405 if (vnode_issystem(vp
)) {
1406 if (VTOF(vp
)->fcbBTCBPtr
!= NULL
) {
1407 BTreeInfoRec bTreeInfo
;
1410 * We do not lock the BTrees, because if we are getting block..then the tree
1411 * should be locked in the first place.
1412 * We just want the nodeSize wich will NEVER change..so even if the world
1413 * is changing..the nodeSize should remain the same. Which argues why lock
1414 * it in the first place??
1417 (void) BTGetInformation (VTOF(vp
), kBTreeInfoVersion
, &bTreeInfo
);
1419 logBlockSize
= bTreeInfo
.nodeSize
;
1421 } else if (VTOC(vp
)->c_fileid
== kHFSAllocationFileID
) {
1422 logBlockSize
= VTOVCB(vp
)->vcbVBMIOSize
;
1426 DBG_ASSERT(logBlockSize
> 0);
1428 return logBlockSize
;
1433 hfs_freeblks(struct hfsmount
* hfsmp
, int wantreserve
)
1435 ExtendedVCB
*vcb
= HFSTOVCB(hfsmp
);
1438 HFS_MOUNT_LOCK(hfsmp
, TRUE
);
1439 freeblks
= vcb
->freeBlocks
;
1441 if (freeblks
> vcb
->reserveBlocks
)
1442 freeblks
-= vcb
->reserveBlocks
;
1446 if (freeblks
> vcb
->loanedBlocks
)
1447 freeblks
-= vcb
->loanedBlocks
;
1450 HFS_MOUNT_UNLOCK(hfsmp
, TRUE
);
1452 #ifdef HFS_SPARSE_DEV
1454 * When the underlying device is sparse, check the
1455 * available space on the backing store volume.
1457 if ((hfsmp
->hfs_flags
& HFS_HAS_SPARSE_DEVICE
) && hfsmp
->hfs_backingfs_rootvp
) {
1458 struct vfsstatfs
*vfsp
; /* 272 bytes */
1459 u_int32_t vfreeblks
;
1460 u_int32_t loanedblks
;
1461 struct mount
* backingfs_mp
;
1463 backingfs_mp
= vnode_mount(hfsmp
->hfs_backingfs_rootvp
);
1465 if (vfsp
= vfs_statfs(backingfs_mp
)) {
1466 HFS_MOUNT_LOCK(hfsmp
, TRUE
);
1467 vfreeblks
= (u_int32_t
)vfsp
->f_bavail
;
1468 /* Normalize block count if needed. */
1469 if (vfsp
->f_bsize
!= vcb
->blockSize
) {
1470 vfreeblks
= ((u_int64_t
)vfreeblks
* (u_int64_t
)(vfsp
->f_bsize
)) / vcb
->blockSize
;
1472 if (vfreeblks
> hfsmp
->hfs_sparsebandblks
)
1473 vfreeblks
-= hfsmp
->hfs_sparsebandblks
;
1477 /* Take into account any delayed allocations. */
1478 loanedblks
= 2 * vcb
->loanedBlocks
;
1479 if (vfreeblks
> loanedblks
)
1480 vfreeblks
-= loanedblks
;
1484 freeblks
= MIN(vfreeblks
, freeblks
);
1485 HFS_MOUNT_UNLOCK(hfsmp
, TRUE
);
1488 #endif /* HFS_SPARSE_DEV */
1494 * Map HFS Common errors (negative) to BSD error codes (positive).
1495 * Positive errors (ie BSD errors) are passed through unchanged.
1497 short MacToVFSError(OSErr err
)
1503 case dskFulErr
: /* -34 */
1504 case btNoSpaceAvail
: /* -32733 */
1506 case fxOvFlErr
: /* -32750 */
1509 case btBadNode
: /* -32731 */
1512 case memFullErr
: /* -108 */
1513 return ENOMEM
; /* +12 */
1515 case cmExists
: /* -32718 */
1516 case btExists
: /* -32734 */
1517 return EEXIST
; /* +17 */
1519 case cmNotFound
: /* -32719 */
1520 case btNotFound
: /* -32735 */
1521 return ENOENT
; /* 28 */
1523 case cmNotEmpty
: /* -32717 */
1524 return ENOTEMPTY
; /* 66 */
1526 case cmFThdDirErr
: /* -32714 */
1527 return EISDIR
; /* 21 */
1529 case fxRangeErr
: /* -32751 */
1532 case bdNamErr
: /* -37 */
1533 return ENAMETOOLONG
; /* 63 */
1535 case paramErr
: /* -50 */
1536 case fileBoundsErr
: /* -1309 */
1537 return EINVAL
; /* +22 */
1539 case fsBTBadNodeSize
:
1543 return EIO
; /* +5 */
1549 * Find the current thread's directory hint for a given index.
1551 * Requires an exclusive lock on directory cnode.
1555 hfs_getdirhint(struct cnode
*dcp
, int index
)
1558 directoryhint_t
*hint
;
1559 boolean_t need_remove
, need_init
;
1565 * Look for an existing hint first. If not found, create a new one (when
1566 * the list is not full) or recycle the oldest hint. Since new hints are
1567 * always added to the head of the list, the last hint is always the
1570 TAILQ_FOREACH(hint
, &dcp
->c_hintlist
, dh_link
) {
1571 if (hint
->dh_index
== index
)
1574 if (hint
!= NULL
) { /* found an existing hint */
1577 } else { /* cannot find an existing hint */
1579 if (dcp
->c_dirhintcnt
< HFS_MAXDIRHINTS
) { /* we don't need recycling */
1580 /* Create a default directory hint */
1581 MALLOC_ZONE(hint
, directoryhint_t
*, sizeof(directoryhint_t
), M_HFSDIRHINT
, M_WAITOK
);
1582 ++dcp
->c_dirhintcnt
;
1583 need_remove
= false;
1584 } else { /* recycle the last (i.e., the oldest) hint */
1585 hint
= TAILQ_LAST(&dcp
->c_hintlist
, hfs_hinthead
);
1586 if ((name
= hint
->dh_desc
.cd_nameptr
)) {
1587 hint
->dh_desc
.cd_nameptr
= NULL
;
1588 vfs_removename(name
);
1595 TAILQ_REMOVE(&dcp
->c_hintlist
, hint
, dh_link
);
1597 TAILQ_INSERT_HEAD(&dcp
->c_hintlist
, hint
, dh_link
);
1600 hint
->dh_index
= index
;
1601 hint
->dh_desc
.cd_flags
= 0;
1602 hint
->dh_desc
.cd_encoding
= 0;
1603 hint
->dh_desc
.cd_namelen
= 0;
1604 hint
->dh_desc
.cd_nameptr
= NULL
;
1605 hint
->dh_desc
.cd_parentcnid
= dcp
->c_cnid
;
1606 hint
->dh_desc
.cd_hint
= dcp
->c_childhint
;
1607 hint
->dh_desc
.cd_cnid
= 0;
1609 hint
->dh_time
= tv
.tv_sec
;
1614 * Release a single directory hint.
1616 * Requires an exclusive lock on directory cnode.
1620 hfs_reldirhint(struct cnode
*dcp
, directoryhint_t
* relhint
)
1624 TAILQ_REMOVE(&dcp
->c_hintlist
, relhint
, dh_link
);
1625 name
= relhint
->dh_desc
.cd_nameptr
;
1627 relhint
->dh_desc
.cd_nameptr
= NULL
;
1628 vfs_removename(name
);
1630 FREE_ZONE(relhint
, sizeof(directoryhint_t
), M_HFSDIRHINT
);
1631 --dcp
->c_dirhintcnt
;
1635 * Release directory hints for given directory
1637 * Requires an exclusive lock on directory cnode.
1641 hfs_reldirhints(struct cnode
*dcp
, int stale_hints_only
)
1644 directoryhint_t
*hint
, *prev
;
1647 if (stale_hints_only
)
1650 /* searching from the oldest to the newest, so we can stop early when releasing stale hints only */
1651 for (hint
= TAILQ_LAST(&dcp
->c_hintlist
, hfs_hinthead
); hint
!= NULL
; hint
= prev
) {
1652 if (stale_hints_only
&& (tv
.tv_sec
- hint
->dh_time
) < HFS_DIRHINT_TTL
)
1653 break; /* stop here if this entry is too new */
1654 name
= hint
->dh_desc
.cd_nameptr
;
1656 hint
->dh_desc
.cd_nameptr
= NULL
;
1657 vfs_removename(name
);
1659 prev
= TAILQ_PREV(hint
, hfs_hinthead
, dh_link
); /* must save this pointer before calling FREE_ZONE on this node */
1660 TAILQ_REMOVE(&dcp
->c_hintlist
, hint
, dh_link
);
1661 FREE_ZONE(hint
, sizeof(directoryhint_t
), M_HFSDIRHINT
);
1662 --dcp
->c_dirhintcnt
;
1668 * Perform a case-insensitive compare of two UTF-8 filenames.
1670 * Returns 0 if the strings match.
1674 hfs_namecmp(const char *str1
, size_t len1
, const char *str2
, size_t len2
)
1676 u_int16_t
*ustr1
, *ustr2
;
1677 size_t ulen1
, ulen2
;
1684 maxbytes
= kHFSPlusMaxFileNameChars
<< 1;
1685 MALLOC(ustr1
, u_int16_t
*, maxbytes
<< 1, M_TEMP
, M_WAITOK
);
1686 ustr2
= ustr1
+ (maxbytes
>> 1);
1688 if (utf8_decodestr(str1
, len1
, ustr1
, &ulen1
, maxbytes
, ':', 0) != 0)
1690 if (utf8_decodestr(str2
, len2
, ustr2
, &ulen2
, maxbytes
, ':', 0) != 0)
1693 cmp
= FastUnicodeCompare(ustr1
, ulen1
>>1, ustr2
, ulen2
>>1);
1695 FREE(ustr1
, M_TEMP
);
1702 hfs_early_journal_init(struct hfsmount
*hfsmp
, HFSPlusVolumeHeader
*vhp
,
1703 void *_args
, off_t embeddedOffset
, daddr64_t mdb_offset
,
1704 HFSMasterDirectoryBlock
*mdbp
, kauth_cred_t cred
)
1706 JournalInfoBlock
*jibp
;
1707 struct buf
*jinfo_bp
, *bp
;
1708 int sectors_per_fsblock
, arg_flags
=0, arg_tbufsz
=0;
1709 int retval
, blksize
= hfsmp
->hfs_phys_block_size
;
1710 struct vnode
*devvp
;
1711 struct hfs_mount_args
*args
= _args
;
1713 devvp
= hfsmp
->hfs_devvp
;
1715 if (args
!= NULL
&& (args
->flags
& HFSFSMNT_EXTENDED_ARGS
)) {
1716 arg_flags
= args
->journal_flags
;
1717 arg_tbufsz
= args
->journal_tbuffer_size
;
1720 sectors_per_fsblock
= SWAP_BE32(vhp
->blockSize
) / blksize
;
1722 retval
= (int)buf_meta_bread(devvp
,
1723 (daddr64_t
)((embeddedOffset
/blksize
) +
1724 (SWAP_BE32(vhp
->journalInfoBlock
)*sectors_per_fsblock
)),
1725 SWAP_BE32(vhp
->blockSize
), cred
, &jinfo_bp
);
1729 jibp
= (JournalInfoBlock
*)buf_dataptr(jinfo_bp
);
1730 jibp
->flags
= SWAP_BE32(jibp
->flags
);
1731 jibp
->offset
= SWAP_BE64(jibp
->offset
);
1732 jibp
->size
= SWAP_BE64(jibp
->size
);
1734 if (jibp
->flags
& kJIJournalInFSMask
) {
1735 hfsmp
->jvp
= hfsmp
->hfs_devvp
;
1737 printf("hfs: journal not stored in fs! don't know what to do.\n");
1738 buf_brelse(jinfo_bp
);
1742 // save this off for the hack-y check in hfs_remove()
1743 hfsmp
->jnl_start
= jibp
->offset
/ SWAP_BE32(vhp
->blockSize
);
1744 hfsmp
->jnl_size
= jibp
->size
;
1746 if ((hfsmp
->hfs_flags
& HFS_READ_ONLY
) && (vfs_flags(hfsmp
->hfs_mp
) & MNT_ROOTFS
) == 0) {
1747 // if the file system is read-only, check if the journal is empty.
1748 // if it is, then we can allow the mount. otherwise we have to
1750 retval
= journal_is_clean(hfsmp
->jvp
,
1751 jibp
->offset
+ embeddedOffset
,
1754 hfsmp
->hfs_phys_block_size
);
1758 buf_brelse(jinfo_bp
);
1761 printf("hfs: early journal init: volume on %s is read-only and journal is dirty. Can not mount volume.\n",
1768 if (jibp
->flags
& kJIJournalNeedInitMask
) {
1769 printf("hfs: Initializing the journal (joffset 0x%llx sz 0x%llx)...\n",
1770 jibp
->offset
+ embeddedOffset
, jibp
->size
);
1771 hfsmp
->jnl
= journal_create(hfsmp
->jvp
,
1772 jibp
->offset
+ embeddedOffset
,
1778 hfs_sync_metadata
, hfsmp
->hfs_mp
);
1780 // no need to start a transaction here... if this were to fail
1781 // we'd just re-init it on the next mount.
1782 jibp
->flags
&= ~kJIJournalNeedInitMask
;
1783 jibp
->flags
= SWAP_BE32(jibp
->flags
);
1784 jibp
->offset
= SWAP_BE64(jibp
->offset
);
1785 jibp
->size
= SWAP_BE64(jibp
->size
);
1786 buf_bwrite(jinfo_bp
);
1790 //printf("hfs: Opening the journal (joffset 0x%llx sz 0x%llx vhp_blksize %d)...\n",
1791 // jibp->offset + embeddedOffset,
1792 // jibp->size, SWAP_BE32(vhp->blockSize));
1794 hfsmp
->jnl
= journal_open(hfsmp
->jvp
,
1795 jibp
->offset
+ embeddedOffset
,
1801 hfs_sync_metadata
, hfsmp
->hfs_mp
);
1803 buf_brelse(jinfo_bp
);
1807 if (hfsmp
->jnl
&& mdbp
) {
1808 // reload the mdb because it could have changed
1809 // if the journal had to be replayed.
1810 if (mdb_offset
== 0) {
1811 mdb_offset
= (daddr64_t
)((embeddedOffset
/ blksize
) + HFS_PRI_SECTOR(blksize
));
1813 retval
= (int)buf_meta_bread(devvp
, mdb_offset
, blksize
, cred
, &bp
);
1816 printf("hfs: failed to reload the mdb after opening the journal (retval %d)!\n",
1820 bcopy((char *)buf_dataptr(bp
) + HFS_PRI_OFFSET(blksize
), mdbp
, 512);
1827 //printf("journal @ 0x%x\n", hfsmp->jnl);
1829 // if we expected the journal to be there and we couldn't
1830 // create it or open it then we have to bail out.
1831 if (hfsmp
->jnl
== NULL
) {
1832 printf("hfs: early jnl init: failed to open/create the journal (retval %d).\n", retval
);
1841 // This function will go and re-locate the .journal_info_block and
1842 // the .journal files in case they moved (which can happen if you
1843 // run Norton SpeedDisk). If we fail to find either file we just
1844 // disable journaling for this volume and return. We turn off the
1845 // journaling bit in the vcb and assume it will get written to disk
1846 // later (if it doesn't on the next mount we'd do the same thing
1847 // again which is harmless). If we disable journaling we don't
1848 // return an error so that the volume is still mountable.
1850 // If the info we find for the .journal_info_block and .journal files
1851 // isn't what we had stored, we re-set our cached info and proceed
1852 // with opening the journal normally.
1855 hfs_late_journal_init(struct hfsmount
*hfsmp
, HFSPlusVolumeHeader
*vhp
, void *_args
)
1857 JournalInfoBlock
*jibp
;
1858 struct buf
*jinfo_bp
, *bp
;
1859 int sectors_per_fsblock
, arg_flags
=0, arg_tbufsz
=0;
1860 int retval
, need_flush
= 0, write_jibp
= 0;
1861 struct vnode
*devvp
;
1862 struct cat_attr jib_attr
, jattr
;
1863 struct cat_fork jib_fork
, jfork
;
1866 struct hfs_mount_args
*args
= _args
;
1868 devvp
= hfsmp
->hfs_devvp
;
1869 vcb
= HFSTOVCB(hfsmp
);
1871 if (args
!= NULL
&& (args
->flags
& HFSFSMNT_EXTENDED_ARGS
)) {
1872 if (args
->journal_disable
) {
1876 arg_flags
= args
->journal_flags
;
1877 arg_tbufsz
= args
->journal_tbuffer_size
;
1880 fid
= GetFileInfo(vcb
, kRootDirID
, ".journal_info_block", &jib_attr
, &jib_fork
);
1881 if (fid
== 0 || jib_fork
.cf_extents
[0].startBlock
== 0 || jib_fork
.cf_size
== 0) {
1882 printf("hfs: can't find the .journal_info_block! disabling journaling (start: %d).\n",
1883 jib_fork
.cf_extents
[0].startBlock
);
1884 vcb
->vcbAtrb
&= ~kHFSVolumeJournaledMask
;
1887 hfsmp
->hfs_jnlinfoblkid
= fid
;
1889 // make sure the journal_info_block begins where we think it should.
1890 if (SWAP_BE32(vhp
->journalInfoBlock
) != jib_fork
.cf_extents
[0].startBlock
) {
1891 printf("hfs: The journal_info_block moved (was: %d; is: %d). Fixing up\n",
1892 SWAP_BE32(vhp
->journalInfoBlock
), jib_fork
.cf_extents
[0].startBlock
);
1894 vcb
->vcbJinfoBlock
= jib_fork
.cf_extents
[0].startBlock
;
1895 vhp
->journalInfoBlock
= SWAP_BE32(jib_fork
.cf_extents
[0].startBlock
);
1899 sectors_per_fsblock
= SWAP_BE32(vhp
->blockSize
) / hfsmp
->hfs_phys_block_size
;
1900 retval
= (int)buf_meta_bread(devvp
,
1901 (daddr64_t
)(vcb
->hfsPlusIOPosOffset
/ hfsmp
->hfs_phys_block_size
+
1902 (SWAP_BE32(vhp
->journalInfoBlock
)*sectors_per_fsblock
)),
1903 SWAP_BE32(vhp
->blockSize
), NOCRED
, &jinfo_bp
);
1905 printf("hfs: can't read journal info block. disabling journaling.\n");
1906 vcb
->vcbAtrb
&= ~kHFSVolumeJournaledMask
;
1910 jibp
= (JournalInfoBlock
*)buf_dataptr(jinfo_bp
);
1911 jibp
->flags
= SWAP_BE32(jibp
->flags
);
1912 jibp
->offset
= SWAP_BE64(jibp
->offset
);
1913 jibp
->size
= SWAP_BE64(jibp
->size
);
1915 fid
= GetFileInfo(vcb
, kRootDirID
, ".journal", &jattr
, &jfork
);
1916 if (fid
== 0 || jfork
.cf_extents
[0].startBlock
== 0 || jfork
.cf_size
== 0) {
1917 printf("hfs: can't find the journal file! disabling journaling (start: %d)\n",
1918 jfork
.cf_extents
[0].startBlock
);
1919 buf_brelse(jinfo_bp
);
1920 vcb
->vcbAtrb
&= ~kHFSVolumeJournaledMask
;
1923 hfsmp
->hfs_jnlfileid
= fid
;
1925 // make sure the journal file begins where we think it should.
1926 if ((jibp
->offset
/ (u_int64_t
)vcb
->blockSize
) != jfork
.cf_extents
[0].startBlock
) {
1927 printf("hfs: The journal file moved (was: %lld; is: %d). Fixing up\n",
1928 (jibp
->offset
/ (u_int64_t
)vcb
->blockSize
), jfork
.cf_extents
[0].startBlock
);
1930 jibp
->offset
= (u_int64_t
)jfork
.cf_extents
[0].startBlock
* (u_int64_t
)vcb
->blockSize
;
1934 // check the size of the journal file.
1935 if (jibp
->size
!= (u_int64_t
)jfork
.cf_extents
[0].blockCount
*vcb
->blockSize
) {
1936 printf("hfs: The journal file changed size! (was %lld; is %lld). Fixing up.\n",
1937 jibp
->size
, (u_int64_t
)jfork
.cf_extents
[0].blockCount
*vcb
->blockSize
);
1939 jibp
->size
= (u_int64_t
)jfork
.cf_extents
[0].blockCount
* vcb
->blockSize
;
1943 if (jibp
->flags
& kJIJournalInFSMask
) {
1944 hfsmp
->jvp
= hfsmp
->hfs_devvp
;
1946 printf("hfs: journal not stored in fs! don't know what to do.\n");
1947 buf_brelse(jinfo_bp
);
1951 // save this off for the hack-y check in hfs_remove()
1952 hfsmp
->jnl_start
= jibp
->offset
/ SWAP_BE32(vhp
->blockSize
);
1953 hfsmp
->jnl_size
= jibp
->size
;
1955 if ((hfsmp
->hfs_flags
& HFS_READ_ONLY
) && (vfs_flags(hfsmp
->hfs_mp
) & MNT_ROOTFS
) == 0) {
1956 // if the file system is read-only, check if the journal is empty.
1957 // if it is, then we can allow the mount. otherwise we have to
1959 retval
= journal_is_clean(hfsmp
->jvp
,
1960 jibp
->offset
+ (off_t
)vcb
->hfsPlusIOPosOffset
,
1963 hfsmp
->hfs_phys_block_size
);
1967 buf_brelse(jinfo_bp
);
1970 printf("hfs: late journal init: volume on %s is read-only and journal is dirty. Can not mount volume.\n",
1977 if (jibp
->flags
& kJIJournalNeedInitMask
) {
1978 printf("hfs: Initializing the journal (joffset 0x%llx sz 0x%llx)...\n",
1979 jibp
->offset
+ (off_t
)vcb
->hfsPlusIOPosOffset
, jibp
->size
);
1980 hfsmp
->jnl
= journal_create(hfsmp
->jvp
,
1981 jibp
->offset
+ (off_t
)vcb
->hfsPlusIOPosOffset
,
1984 hfsmp
->hfs_phys_block_size
,
1987 hfs_sync_metadata
, hfsmp
->hfs_mp
);
1989 // no need to start a transaction here... if this were to fail
1990 // we'd just re-init it on the next mount.
1991 jibp
->flags
&= ~kJIJournalNeedInitMask
;
1996 // if we weren't the last person to mount this volume
1997 // then we need to throw away the journal because it
1998 // is likely that someone else mucked with the disk.
1999 // if the journal is empty this is no big deal. if the
2000 // disk is dirty this prevents us from replaying the
2001 // journal over top of changes that someone else made.
2003 arg_flags
|= JOURNAL_RESET
;
2005 //printf("hfs: Opening the journal (joffset 0x%llx sz 0x%llx vhp_blksize %d)...\n",
2006 // jibp->offset + (off_t)vcb->hfsPlusIOPosOffset,
2007 // jibp->size, SWAP_BE32(vhp->blockSize));
2009 hfsmp
->jnl
= journal_open(hfsmp
->jvp
,
2010 jibp
->offset
+ (off_t
)vcb
->hfsPlusIOPosOffset
,
2013 hfsmp
->hfs_phys_block_size
,
2016 hfs_sync_metadata
, hfsmp
->hfs_mp
);
2021 jibp
->flags
= SWAP_BE32(jibp
->flags
);
2022 jibp
->offset
= SWAP_BE64(jibp
->offset
);
2023 jibp
->size
= SWAP_BE64(jibp
->size
);
2025 buf_bwrite(jinfo_bp
);
2027 buf_brelse(jinfo_bp
);
2032 //printf("journal @ 0x%x\n", hfsmp->jnl);
2034 // if we expected the journal to be there and we couldn't
2035 // create it or open it then we have to bail out.
2036 if (hfsmp
->jnl
== NULL
) {
2037 printf("hfs: late jnl init: failed to open/create the journal (retval %d).\n", retval
);
2045 * Calculate the allocation zone for metadata.
2047 * This zone includes the following:
2048 * Allocation Bitmap file
2049 * Overflow Extents file
2052 * Clustered Hot files
2055 * METADATA ALLOCATION ZONE
2056 * ____________________________________________________________________________
2058 * | BM | JF | OEF | CATALOG |---> | HOT FILES |
2059 * |____|____|_____|_______________|______________________________|___________|
2061 * <------------------------------- N * 128 MB ------------------------------->
2064 #define GIGABYTE (u_int64_t)(1024*1024*1024)
2066 #define OVERFLOW_DEFAULT_SIZE (4*1024*1024)
2067 #define OVERFLOW_MAXIMUM_SIZE (128*1024*1024)
2068 #define JOURNAL_DEFAULT_SIZE (8*1024*1024)
2069 #define JOURNAL_MAXIMUM_SIZE (512*1024*1024)
2070 #define HOTBAND_MINIMUM_SIZE (10*1024*1024)
2071 #define HOTBAND_MAXIMUM_SIZE (512*1024*1024)
2074 hfs_metadatazone_init(struct hfsmount
*hfsmp
)
2084 vcb
= HFSTOVCB(hfsmp
);
2085 fs_size
= (u_int64_t
)vcb
->blockSize
* (u_int64_t
)vcb
->totalBlocks
;
2088 * For volumes less than 10 GB, don't bother.
2090 if (fs_size
< ((u_int64_t
)10 * GIGABYTE
))
2093 * Skip non-journaled volumes as well.
2095 if (hfsmp
->jnl
== NULL
)
2099 * Start with allocation bitmap (a fixed size).
2101 zonesize
= roundup(vcb
->totalBlocks
/ 8, vcb
->vcbVBMIOSize
);
2104 * Overflow Extents file gets 4 MB per 100 GB.
2106 items
= fs_size
/ ((u_int64_t
)100 * GIGABYTE
);
2107 filesize
= (u_int64_t
)(items
+ 1) * OVERFLOW_DEFAULT_SIZE
;
2108 if (filesize
> OVERFLOW_MAXIMUM_SIZE
)
2109 filesize
= OVERFLOW_MAXIMUM_SIZE
;
2110 zonesize
+= filesize
;
2111 hfsmp
->hfs_overflow_maxblks
= filesize
/ vcb
->blockSize
;
2114 * Plan for at least 8 MB of journal for each
2115 * 100 GB of disk space (up to a 512 MB).
2117 items
= fs_size
/ ((u_int64_t
)100 * GIGABYTE
);
2118 filesize
= (u_int64_t
)(items
+ 1) * JOURNAL_DEFAULT_SIZE
;
2119 if (filesize
> JOURNAL_MAXIMUM_SIZE
)
2120 filesize
= JOURNAL_MAXIMUM_SIZE
;
2121 zonesize
+= filesize
;
2124 * Catalog file gets 10 MB per 1 GB.
2126 * How about considering the current catalog size (used nodes * node size)
2127 * and the current file data size to help estimate the required
2130 filesize
= MIN((fs_size
/ 1024) * 10, GIGABYTE
);
2131 hfsmp
->hfs_catalog_maxblks
= filesize
/ vcb
->blockSize
;
2132 zonesize
+= filesize
;
2135 * Add space for hot file region.
2137 * ...for now, use 5 MB per 1 GB (0.5 %)
2139 filesize
= (fs_size
/ 1024) * 5;
2140 if (filesize
> HOTBAND_MAXIMUM_SIZE
)
2141 filesize
= HOTBAND_MAXIMUM_SIZE
;
2142 else if (filesize
< HOTBAND_MINIMUM_SIZE
)
2143 filesize
= HOTBAND_MINIMUM_SIZE
;
2145 * Calculate user quota file requirements.
2147 items
= QF_USERS_PER_GB
* (fs_size
/ GIGABYTE
);
2148 if (items
< QF_MIN_USERS
)
2149 items
= QF_MIN_USERS
;
2150 else if (items
> QF_MAX_USERS
)
2151 items
= QF_MAX_USERS
;
2152 if (!powerof2(items
)) {
2160 filesize
+= (items
+ 1) * sizeof(struct dqblk
);
2162 * Calculate group quota file requirements.
2165 items
= QF_GROUPS_PER_GB
* (fs_size
/ GIGABYTE
);
2166 if (items
< QF_MIN_GROUPS
)
2167 items
= QF_MIN_GROUPS
;
2168 else if (items
> QF_MAX_GROUPS
)
2169 items
= QF_MAX_GROUPS
;
2170 if (!powerof2(items
)) {
2178 filesize
+= (items
+ 1) * sizeof(struct dqblk
);
2179 zonesize
+= filesize
;
2182 * Round up entire zone to a bitmap block's worth.
2183 * The extra space goes to the catalog file and hot file area.
2186 zonesize
= roundup(zonesize
, (u_int64_t
)vcb
->vcbVBMIOSize
* 8 * vcb
->blockSize
);
2187 temp
= zonesize
- temp
; /* temp has extra space */
2188 filesize
+= temp
/ 3;
2189 hfsmp
->hfs_catalog_maxblks
+= (temp
- (temp
/ 3)) / vcb
->blockSize
;
2191 hfsmp
->hfs_hotfile_maxblks
= filesize
/ vcb
->blockSize
;
2193 /* Convert to allocation blocks. */
2194 blk
= zonesize
/ vcb
->blockSize
;
2196 /* The default metadata zone location is at the start of volume. */
2197 hfsmp
->hfs_metazone_start
= 1;
2198 hfsmp
->hfs_metazone_end
= blk
- 1;
2200 /* The default hotfile area is at the end of the zone. */
2201 hfsmp
->hfs_hotfile_start
= blk
- (filesize
/ vcb
->blockSize
);
2202 hfsmp
->hfs_hotfile_end
= hfsmp
->hfs_metazone_end
;
2203 hfsmp
->hfs_hotfile_freeblks
= hfs_hotfile_freeblocks(hfsmp
);
2205 printf("HFS: metadata zone is %d to %d\n", hfsmp
->hfs_metazone_start
, hfsmp
->hfs_metazone_end
);
2206 printf("HFS: hot file band is %d to %d\n", hfsmp
->hfs_hotfile_start
, hfsmp
->hfs_hotfile_end
);
2207 printf("HFS: hot file band free blocks = %d\n", hfsmp
->hfs_hotfile_freeblks
);
2209 hfsmp
->hfs_flags
|= HFS_METADATA_ZONE
;
2214 hfs_hotfile_freeblocks(struct hfsmount
*hfsmp
)
2216 ExtendedVCB
*vcb
= HFSTOVCB(hfsmp
);
2220 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_BITMAP
, HFS_EXCLUSIVE_LOCK
);
2221 freeblocks
= MetaZoneFreeBlocks(vcb
);
2222 hfs_systemfile_unlock(hfsmp
, lockflags
);
2224 /* Minus Extents overflow file reserve. */
2226 hfsmp
->hfs_overflow_maxblks
- VTOF(hfsmp
->hfs_extents_vp
)->ff_blocks
;
2227 /* Minus catalog file reserve. */
2229 hfsmp
->hfs_catalog_maxblks
- VTOF(hfsmp
->hfs_catalog_vp
)->ff_blocks
;
2233 return MIN(freeblocks
, hfsmp
->hfs_hotfile_maxblks
);
2237 * Determine if a file is a "virtual" metadata file.
2238 * This includes journal and quota files.
2242 hfs_virtualmetafile(struct cnode
*cp
)
2247 if (cp
->c_parentcnid
!= kHFSRootFolderID
)
2250 filename
= cp
->c_desc
.cd_nameptr
;
2251 if (filename
== NULL
)
2254 if ((strcmp(filename
, ".journal") == 0) ||
2255 (strcmp(filename
, ".journal_info_block") == 0) ||
2256 (strcmp(filename
, ".quota.user") == 0) ||
2257 (strcmp(filename
, ".quota.group") == 0) ||
2258 (strcmp(filename
, ".hotfiles.btree") == 0))
2267 hfs_start_transaction(struct hfsmount
*hfsmp
)
2271 if (hfsmp
->jnl
== NULL
|| journal_owner(hfsmp
->jnl
) != current_thread()) {
2272 lck_rw_lock_shared(&hfsmp
->hfs_global_lock
);
2276 ret
= journal_start_transaction(hfsmp
->jnl
);
2278 OSAddAtomic(1, &hfsmp
->hfs_global_lock_nesting
);
2285 lck_rw_done(&hfsmp
->hfs_global_lock
);
2293 hfs_end_transaction(struct hfsmount
*hfsmp
)
2295 int need_unlock
=0, ret
;
2297 if ( hfsmp
->jnl
== NULL
2298 || ( journal_owner(hfsmp
->jnl
) == current_thread()
2299 && (OSAddAtomic(-1, &hfsmp
->hfs_global_lock_nesting
) == 1)) ) {
2305 ret
= journal_end_transaction(hfsmp
->jnl
);
2311 lck_rw_done(&hfsmp
->hfs_global_lock
);