]> git.saurik.com Git - apple/xnu.git/blame - bsd/hfs/hfs_vfsutils.c
xnu-1228.15.4.tar.gz
[apple/xnu.git] / bsd / hfs / hfs_vfsutils.c
CommitLineData
1c79356b 1/*
2d21ac55 2 * Copyright (c) 2000-2007 Apple Inc. All rights reserved.
5d5c5d0d 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
2d21ac55
A
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.
8f6c56a5 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
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.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
27 */
28/* @(#)hfs_vfsutils.c 4.0
29*
9bccf70c 30* (c) 1997-2002 Apple Computer, Inc. All Rights Reserved
1c79356b
A
31*
32* hfs_vfsutils.c -- Routines that go between the HFS layer and the VFS.
33*
1c79356b
A
34*/
35#include <sys/param.h>
36#include <sys/systm.h>
37#include <sys/kernel.h>
38#include <sys/malloc.h>
39#include <sys/stat.h>
1c79356b 40#include <sys/mount.h>
1c79356b 41#include <sys/buf.h>
2d21ac55 42#include <sys/buf_internal.h>
1c79356b
A
43#include <sys/ubc.h>
44#include <sys/unistd.h>
91447636
A
45#include <sys/utfconv.h>
46#include <sys/kauth.h>
2d21ac55
A
47#include <sys/fcntl.h>
48#include <sys/vnode_internal.h>
49
50#include <libkern/OSAtomic.h>
1c79356b
A
51
52#include "hfs.h"
9bccf70c 53#include "hfs_catalog.h"
1c79356b
A
54#include "hfs_dbg.h"
55#include "hfs_mount.h"
56#include "hfs_endian.h"
9bccf70c 57#include "hfs_cnode.h"
2d21ac55 58#include "hfs_fsctl.h"
1c79356b
A
59
60#include "hfscommon/headers/FileMgrInternal.h"
61#include "hfscommon/headers/BTreesInternal.h"
62#include "hfscommon/headers/HFSUnicodeWrappers.h"
63
1c79356b 64static void ReleaseMetaFileVNode(struct vnode *vp);
b4c24cb9 65static int hfs_late_journal_init(struct hfsmount *hfsmp, HFSPlusVolumeHeader *vhp, void *_args);
1c79356b 66
55e303ae
A
67static void hfs_metadatazone_init(struct hfsmount *);
68static u_int32_t hfs_hotfile_freeblocks(struct hfsmount *);
69
70
1c79356b
A
71//*******************************************************************************
72// Note: Finder information in the HFS/HFS+ metadata are considered opaque and
73// hence are not in the right byte order on little endian machines. It is
74// the responsibility of the finder and other clients to swap the data.
75//*******************************************************************************
76
77//*******************************************************************************
78// Routine: hfs_MountHFSVolume
79//
80//
81//*******************************************************************************
2d21ac55
A
82unsigned char hfs_catname[] = "Catalog B-tree";
83unsigned char hfs_extname[] = "Extents B-tree";
84unsigned char hfs_vbmname[] = "Volume Bitmap";
85unsigned char hfs_attrname[] = "Attribute B-tree";
86unsigned char hfs_startupname[] = "Startup File";
9bccf70c 87
1c79356b 88
55e303ae 89__private_extern__
1c79356b 90OSErr hfs_MountHFSVolume(struct hfsmount *hfsmp, HFSMasterDirectoryBlock *mdb,
2d21ac55 91 __unused struct proc *p)
1c79356b 92{
9bccf70c
A
93 ExtendedVCB *vcb = HFSTOVCB(hfsmp);
94 int error;
1c79356b 95 ByteCount utf8chars;
9bccf70c
A
96 struct cat_desc cndesc;
97 struct cat_attr cnattr;
98 struct cat_fork fork;
1c79356b 99
9bccf70c
A
100 /* Block size must be a multiple of 512 */
101 if (SWAP_BE32(mdb->drAlBlkSiz) == 0 ||
102 (SWAP_BE32(mdb->drAlBlkSiz) & 0x01FF) != 0)
1c79356b
A
103 return (EINVAL);
104
1c79356b 105 /* don't mount a writeable volume if its dirty, it must be cleaned by fsck_hfs */
55e303ae
A
106 if (((hfsmp->hfs_flags & HFS_READ_ONLY) == 0) &&
107 ((SWAP_BE16(mdb->drAtrb) & kHFSVolumeUnmountedMask) == 0)) {
1c79356b 108 return (EINVAL);
55e303ae
A
109 }
110 hfsmp->hfs_flags |= HFS_STANDARD;
1c79356b
A
111 /*
112 * The MDB seems OK: transfer info from it into VCB
113 * Note - the VCB starts out clear (all zeros)
114 *
115 */
9bccf70c
A
116 vcb->vcbSigWord = SWAP_BE16 (mdb->drSigWord);
117 vcb->vcbCrDate = to_bsd_time(LocalToUTC(SWAP_BE32(mdb->drCrDate)));
0b4e3aa0 118 vcb->localCreateDate = SWAP_BE32 (mdb->drCrDate);
9bccf70c
A
119 vcb->vcbLsMod = to_bsd_time(LocalToUTC(SWAP_BE32(mdb->drLsMod)));
120 vcb->vcbAtrb = SWAP_BE16 (mdb->drAtrb);
121 vcb->vcbNmFls = SWAP_BE16 (mdb->drNmFls);
122 vcb->vcbVBMSt = SWAP_BE16 (mdb->drVBMSt);
123 vcb->nextAllocation = SWAP_BE16 (mdb->drAllocPtr);
124 vcb->totalBlocks = SWAP_BE16 (mdb->drNmAlBlks);
2d21ac55 125 vcb->allocLimit = vcb->totalBlocks;
9bccf70c
A
126 vcb->blockSize = SWAP_BE32 (mdb->drAlBlkSiz);
127 vcb->vcbClpSiz = SWAP_BE32 (mdb->drClpSiz);
128 vcb->vcbAlBlSt = SWAP_BE16 (mdb->drAlBlSt);
129 vcb->vcbNxtCNID = SWAP_BE32 (mdb->drNxtCNID);
130 vcb->freeBlocks = SWAP_BE16 (mdb->drFreeBks);
131 vcb->vcbVolBkUp = to_bsd_time(LocalToUTC(SWAP_BE32(mdb->drVolBkUp)));
132 vcb->vcbWrCnt = SWAP_BE32 (mdb->drWrCnt);
133 vcb->vcbNmRtDirs = SWAP_BE16 (mdb->drNmRtDirs);
134 vcb->vcbFilCnt = SWAP_BE32 (mdb->drFilCnt);
135 vcb->vcbDirCnt = SWAP_BE32 (mdb->drDirCnt);
1c79356b 136 bcopy(mdb->drFndrInfo, vcb->vcbFndrInfo, sizeof(vcb->vcbFndrInfo));
55e303ae 137 if ((hfsmp->hfs_flags & HFS_READ_ONLY) == 0)
9bccf70c 138 vcb->vcbWrCnt++; /* Compensate for write of MDB on last flush */
1c79356b
A
139
140 /* convert hfs encoded name into UTF-8 string */
9bccf70c 141 error = hfs_to_utf8(vcb, mdb->drVN, NAME_MAX, &utf8chars, vcb->vcbVN);
1c79356b
A
142 /*
143 * When an HFS name cannot be encoded with the current
144 * volume encoding we use MacRoman as a fallback.
145 */
9bccf70c 146 if (error || (utf8chars == 0))
1c79356b
A
147 (void) mac_roman_to_utf8(mdb->drVN, NAME_MAX, &utf8chars, vcb->vcbVN);
148
593a1d5f 149 hfsmp->hfs_logBlockSize = BestBlockSizeFit(vcb->blockSize, MAXBSIZE, hfsmp->hfs_logical_block_size);
0b4e3aa0 150 vcb->vcbVBMIOSize = kHFSBlockSize;
1c79356b 151
593a1d5f
A
152 hfsmp->hfs_alt_id_sector = HFS_ALT_SECTOR(hfsmp->hfs_logical_block_size,
153 hfsmp->hfs_logical_block_count);
1c79356b 154
9bccf70c 155 bzero(&cndesc, sizeof(cndesc));
91447636 156 cndesc.cd_parentcnid = kHFSRootParentID;
55e303ae 157 cndesc.cd_flags |= CD_ISMETA;
9bccf70c 158 bzero(&cnattr, sizeof(cnattr));
2d21ac55 159 cnattr.ca_linkcount = 1;
9bccf70c
A
160 cnattr.ca_mode = S_IFREG;
161 bzero(&fork, sizeof(fork));
162
1c79356b 163 /*
9bccf70c
A
164 * Set up Extents B-tree vnode
165 */
166 cndesc.cd_nameptr = hfs_extname;
2d21ac55 167 cndesc.cd_namelen = strlen((char *)hfs_extname);
9bccf70c
A
168 cndesc.cd_cnid = cnattr.ca_fileid = kHFSExtentsFileID;
169 fork.cf_size = SWAP_BE32(mdb->drXTFlSize);
170 fork.cf_blocks = fork.cf_size / vcb->blockSize;
171 fork.cf_clump = SWAP_BE32(mdb->drXTClpSiz);
55e303ae 172 fork.cf_vblocks = 0;
9bccf70c
A
173 fork.cf_extents[0].startBlock = SWAP_BE16(mdb->drXTExtRec[0].startBlock);
174 fork.cf_extents[0].blockCount = SWAP_BE16(mdb->drXTExtRec[0].blockCount);
175 fork.cf_extents[1].startBlock = SWAP_BE16(mdb->drXTExtRec[1].startBlock);
176 fork.cf_extents[1].blockCount = SWAP_BE16(mdb->drXTExtRec[1].blockCount);
177 fork.cf_extents[2].startBlock = SWAP_BE16(mdb->drXTExtRec[2].startBlock);
178 fork.cf_extents[2].blockCount = SWAP_BE16(mdb->drXTExtRec[2].blockCount);
179 cnattr.ca_blocks = fork.cf_blocks;
180
91447636
A
181 error = hfs_getnewvnode(hfsmp, NULL, NULL, &cndesc, 0, &cnattr, &fork,
182 &hfsmp->hfs_extents_vp);
9bccf70c 183 if (error) goto MtVolErr;
91447636 184 error = MacToVFSError(BTOpenPath(VTOF(hfsmp->hfs_extents_vp),
55e303ae 185 (KeyCompareProcPtr)CompareExtentKeys));
9bccf70c 186 if (error) {
91447636 187 hfs_unlock(VTOC(hfsmp->hfs_extents_vp));
9bccf70c
A
188 goto MtVolErr;
189 }
2d21ac55 190 hfsmp->hfs_extents_cp = VTOC(hfsmp->hfs_extents_vp);
1c79356b
A
191
192 /*
193 * Set up Catalog B-tree vnode...
194 */
9bccf70c 195 cndesc.cd_nameptr = hfs_catname;
2d21ac55 196 cndesc.cd_namelen = strlen((char *)hfs_catname);
9bccf70c
A
197 cndesc.cd_cnid = cnattr.ca_fileid = kHFSCatalogFileID;
198 fork.cf_size = SWAP_BE32(mdb->drCTFlSize);
199 fork.cf_blocks = fork.cf_size / vcb->blockSize;
200 fork.cf_clump = SWAP_BE32(mdb->drCTClpSiz);
55e303ae 201 fork.cf_vblocks = 0;
9bccf70c
A
202 fork.cf_extents[0].startBlock = SWAP_BE16(mdb->drCTExtRec[0].startBlock);
203 fork.cf_extents[0].blockCount = SWAP_BE16(mdb->drCTExtRec[0].blockCount);
204 fork.cf_extents[1].startBlock = SWAP_BE16(mdb->drCTExtRec[1].startBlock);
205 fork.cf_extents[1].blockCount = SWAP_BE16(mdb->drCTExtRec[1].blockCount);
206 fork.cf_extents[2].startBlock = SWAP_BE16(mdb->drCTExtRec[2].startBlock);
207 fork.cf_extents[2].blockCount = SWAP_BE16(mdb->drCTExtRec[2].blockCount);
208 cnattr.ca_blocks = fork.cf_blocks;
209
91447636
A
210 error = hfs_getnewvnode(hfsmp, NULL, NULL, &cndesc, 0, &cnattr, &fork,
211 &hfsmp->hfs_catalog_vp);
9bccf70c 212 if (error) {
91447636 213 hfs_unlock(VTOC(hfsmp->hfs_extents_vp));
9bccf70c
A
214 goto MtVolErr;
215 }
91447636 216 error = MacToVFSError(BTOpenPath(VTOF(hfsmp->hfs_catalog_vp),
55e303ae 217 (KeyCompareProcPtr)CompareCatalogKeys));
9bccf70c 218 if (error) {
91447636
A
219 hfs_unlock(VTOC(hfsmp->hfs_catalog_vp));
220 hfs_unlock(VTOC(hfsmp->hfs_extents_vp));
221 goto MtVolErr;
222 }
2d21ac55 223 hfsmp->hfs_catalog_cp = VTOC(hfsmp->hfs_catalog_vp);
91447636
A
224
225 /*
226 * Set up dummy Allocation file vnode (used only for locking bitmap)
227 */
228 cndesc.cd_nameptr = hfs_vbmname;
2d21ac55 229 cndesc.cd_namelen = strlen((char *)hfs_vbmname);
91447636
A
230 cndesc.cd_cnid = cnattr.ca_fileid = kHFSAllocationFileID;
231 bzero(&fork, sizeof(fork));
232 cnattr.ca_blocks = 0;
233
234 error = hfs_getnewvnode(hfsmp, NULL, NULL, &cndesc, 0, &cnattr, &fork,
235 &hfsmp->hfs_allocation_vp);
236 if (error) {
237 hfs_unlock(VTOC(hfsmp->hfs_catalog_vp));
238 hfs_unlock(VTOC(hfsmp->hfs_extents_vp));
9bccf70c
A
239 goto MtVolErr;
240 }
2d21ac55 241 hfsmp->hfs_allocation_cp = VTOC(hfsmp->hfs_allocation_vp);
1c79356b
A
242
243 /* mark the volume dirty (clear clean unmount bit) */
244 vcb->vcbAtrb &= ~kHFSVolumeUnmountedMask;
245
2d21ac55
A
246 if (error == noErr)
247 {
248 error = cat_idlookup(hfsmp, kHFSRootFolderID, 0, NULL, NULL, NULL);
249 }
8f6c56a5 250
9bccf70c 251 if ( error == noErr )
1c79356b
A
252 {
253 if ( !(vcb->vcbAtrb & kHFSVolumeHardwareLockMask) ) // if the disk is not write protected
254 {
255 MarkVCBDirty( vcb ); // mark VCB dirty so it will be written
256 }
257 }
935ed37a
A
258
259 /*
260 * all done with system files so we can unlock now...
261 */
262 hfs_unlock(VTOC(hfsmp->hfs_allocation_vp));
263 hfs_unlock(VTOC(hfsmp->hfs_catalog_vp));
264 hfs_unlock(VTOC(hfsmp->hfs_extents_vp));
265
1c79356b
A
266 goto CmdDone;
267
268 //-- Release any resources allocated so far before exiting with an error:
9bccf70c 269MtVolErr:
91447636
A
270 ReleaseMetaFileVNode(hfsmp->hfs_catalog_vp);
271 ReleaseMetaFileVNode(hfsmp->hfs_extents_vp);
1c79356b 272
9bccf70c
A
273CmdDone:
274 return (error);
1c79356b
A
275}
276
277//*******************************************************************************
278// Routine: hfs_MountHFSPlusVolume
279//
280//
281//*******************************************************************************
282
55e303ae 283__private_extern__
1c79356b 284OSErr hfs_MountHFSPlusVolume(struct hfsmount *hfsmp, HFSPlusVolumeHeader *vhp,
2d21ac55 285 off_t embeddedOffset, u_int64_t disksize, __unused struct proc *p, void *args, kauth_cred_t cred)
1c79356b 286{
9bccf70c
A
287 register ExtendedVCB *vcb;
288 struct cat_desc cndesc;
289 struct cat_attr cnattr;
55e303ae 290 struct cat_fork cfork;
2d21ac55 291 u_int32_t blockSize;
91447636 292 daddr64_t spare_sectors;
55e303ae
A
293 struct BTreeInfoRec btinfo;
294 u_int16_t signature;
2d21ac55 295 u_int16_t hfs_version;
55e303ae 296 int i;
9bccf70c 297 OSErr retval;
1c79356b 298
55e303ae 299 signature = SWAP_BE16(vhp->signature);
2d21ac55 300 hfs_version = SWAP_BE16(vhp->version);
55e303ae
A
301
302 if (signature == kHFSPlusSigWord) {
2d21ac55
A
303 if (hfs_version != kHFSPlusVersion) {
304 printf("hfs_mount: invalid HFS+ version: %d\n", hfs_version);
55e303ae
A
305 return (EINVAL);
306 }
307 } else if (signature == kHFSXSigWord) {
2d21ac55
A
308 if (hfs_version != kHFSXVersion) {
309 printf("hfs_mount: invalid HFSX version: %d\n", hfs_version);
55e303ae
A
310 return (EINVAL);
311 }
312 /* The in-memory signature is always 'H+'. */
313 signature = kHFSPlusSigWord;
314 hfsmp->hfs_flags |= HFS_X;
315 } else {
91447636
A
316 /* Removed printf for invalid HFS+ signature because it gives
317 * false error for UFS root volume
318 */
b4c24cb9
A
319 return (EINVAL);
320 }
1c79356b 321
9bccf70c
A
322 /* Block size must be at least 512 and a power of 2 */
323 blockSize = SWAP_BE32(vhp->blockSize);
55e303ae 324 if (blockSize < 512 || !powerof2(blockSize))
9bccf70c 325 return (EINVAL);
1c79356b
A
326
327 /* don't mount a writable volume if its dirty, it must be cleaned by fsck_hfs */
55e303ae
A
328 if ((hfsmp->hfs_flags & HFS_READ_ONLY) == 0 && hfsmp->jnl == NULL &&
329 (SWAP_BE32(vhp->attributes) & kHFSVolumeUnmountedMask) == 0)
1c79356b 330 return (EINVAL);
d52fe63f
A
331
332 /* Make sure we can live with the physical block size. */
593a1d5f
A
333 if ((disksize & (hfsmp->hfs_logical_block_size - 1)) ||
334 (embeddedOffset & (hfsmp->hfs_logical_block_size - 1)) ||
335 (blockSize < hfsmp->hfs_logical_block_size)) {
d52fe63f
A
336 return (ENXIO);
337 }
593a1d5f
A
338
339 /* If allocation block size is less than the physical
340 * block size, we assume that the physical block size
341 * is same as logical block size. The physical block
342 * size value is used to round down the offsets for
343 * reading and writing the primary and alternate volume
344 * headers at physical block boundary and will cause
345 * problems if it is less than the block size.
346 */
347 if (blockSize < hfsmp->hfs_physical_block_size) {
348 hfsmp->hfs_physical_block_size = hfsmp->hfs_logical_block_size;
c910b4d9 349 hfsmp->hfs_log_per_phys = 1;
593a1d5f
A
350 }
351
1c79356b
A
352 /*
353 * The VolumeHeader seems OK: transfer info from it into VCB
354 * Note - the VCB starts out clear (all zeros)
355 */
356 vcb = HFSTOVCB(hfsmp);
357
55e303ae 358 vcb->vcbSigWord = signature;
b4c24cb9 359 vcb->vcbJinfoBlock = SWAP_BE32(vhp->journalInfoBlock);
9bccf70c 360 vcb->vcbLsMod = to_bsd_time(SWAP_BE32(vhp->modifyDate));
91447636 361 vcb->vcbAtrb = SWAP_BE32(vhp->attributes);
9bccf70c
A
362 vcb->vcbClpSiz = SWAP_BE32(vhp->rsrcClumpSize);
363 vcb->vcbNxtCNID = SWAP_BE32(vhp->nextCatalogID);
364 vcb->vcbVolBkUp = to_bsd_time(SWAP_BE32(vhp->backupDate));
365 vcb->vcbWrCnt = SWAP_BE32(vhp->writeCount);
366 vcb->vcbFilCnt = SWAP_BE32(vhp->fileCount);
367 vcb->vcbDirCnt = SWAP_BE32(vhp->folderCount);
1c79356b
A
368
369 /* copy 32 bytes of Finder info */
370 bcopy(vhp->finderInfo, vcb->vcbFndrInfo, sizeof(vhp->finderInfo));
371
372 vcb->vcbAlBlSt = 0; /* hfs+ allocation blocks start at first block of volume */
55e303ae 373 if ((hfsmp->hfs_flags & HFS_READ_ONLY) == 0)
9bccf70c 374 vcb->vcbWrCnt++; /* compensate for write of Volume Header on last flush */
1c79356b 375
9bccf70c
A
376 /* Now fill in the Extended VCB info */
377 vcb->nextAllocation = SWAP_BE32(vhp->nextAllocation);
378 vcb->totalBlocks = SWAP_BE32(vhp->totalBlocks);
2d21ac55 379 vcb->allocLimit = vcb->totalBlocks;
9bccf70c 380 vcb->freeBlocks = SWAP_BE32(vhp->freeBlocks);
55e303ae 381 vcb->blockSize = blockSize;
9bccf70c
A
382 vcb->encodingsBitmap = SWAP_BE64(vhp->encodingsBitmap);
383 vcb->localCreateDate = SWAP_BE32(vhp->createDate);
1c79356b 384
9bccf70c 385 vcb->hfsPlusIOPosOffset = embeddedOffset;
1c79356b 386
9bccf70c
A
387 /* Default to no free block reserve */
388 vcb->reserveBlocks = 0;
1c79356b 389
9bccf70c
A
390 /*
391 * Update the logical block size in the mount struct
392 * (currently set up from the wrapper MDB) using the
393 * new blocksize value:
394 */
593a1d5f 395 hfsmp->hfs_logBlockSize = BestBlockSizeFit(vcb->blockSize, MAXBSIZE, hfsmp->hfs_logical_block_size);
0b4e3aa0 396 vcb->vcbVBMIOSize = min(vcb->blockSize, MAXPHYSIO);
1c79356b 397
91447636
A
398 /*
399 * Validate and initialize the location of the alternate volume header.
400 */
593a1d5f 401 spare_sectors = hfsmp->hfs_logical_block_count -
91447636 402 (((daddr64_t)vcb->totalBlocks * blockSize) /
593a1d5f 403 hfsmp->hfs_logical_block_size);
91447636 404
593a1d5f 405 if (spare_sectors > (daddr64_t)(blockSize / hfsmp->hfs_logical_block_size)) {
91447636
A
406 hfsmp->hfs_alt_id_sector = 0; /* partition has grown! */
407 } else {
593a1d5f
A
408 hfsmp->hfs_alt_id_sector = (hfsmp->hfsPlusIOPosOffset / hfsmp->hfs_logical_block_size) +
409 HFS_ALT_SECTOR(hfsmp->hfs_logical_block_size,
410 hfsmp->hfs_logical_block_count);
91447636
A
411 }
412
9bccf70c 413 bzero(&cndesc, sizeof(cndesc));
91447636 414 cndesc.cd_parentcnid = kHFSRootParentID;
55e303ae 415 cndesc.cd_flags |= CD_ISMETA;
9bccf70c 416 bzero(&cnattr, sizeof(cnattr));
2d21ac55 417 cnattr.ca_linkcount = 1;
9bccf70c 418 cnattr.ca_mode = S_IFREG;
1c79356b
A
419
420 /*
9bccf70c
A
421 * Set up Extents B-tree vnode
422 */
423 cndesc.cd_nameptr = hfs_extname;
2d21ac55 424 cndesc.cd_namelen = strlen((char *)hfs_extname);
9bccf70c
A
425 cndesc.cd_cnid = cnattr.ca_fileid = kHFSExtentsFileID;
426
55e303ae 427 cfork.cf_size = SWAP_BE64 (vhp->extentsFile.logicalSize);
593a1d5f 428 cfork.cf_new_size= 0;
55e303ae
A
429 cfork.cf_clump = SWAP_BE32 (vhp->extentsFile.clumpSize);
430 cfork.cf_blocks = SWAP_BE32 (vhp->extentsFile.totalBlocks);
431 cfork.cf_vblocks = 0;
432 cnattr.ca_blocks = cfork.cf_blocks;
433 for (i = 0; i < kHFSPlusExtentDensity; i++) {
434 cfork.cf_extents[i].startBlock =
435 SWAP_BE32 (vhp->extentsFile.extents[i].startBlock);
436 cfork.cf_extents[i].blockCount =
437 SWAP_BE32 (vhp->extentsFile.extents[i].blockCount);
438 }
91447636
A
439 retval = hfs_getnewvnode(hfsmp, NULL, NULL, &cndesc, 0, &cnattr, &cfork,
440 &hfsmp->hfs_extents_vp);
2d21ac55 441 if (retval)
c910b4d9 442 {
2d21ac55 443 goto ErrorExit;
c910b4d9 444 }
2d21ac55
A
445 hfsmp->hfs_extents_cp = VTOC(hfsmp->hfs_extents_vp);
446 hfs_unlock(hfsmp->hfs_extents_cp);
9bccf70c 447
91447636 448 retval = MacToVFSError(BTOpenPath(VTOF(hfsmp->hfs_extents_vp),
55e303ae 449 (KeyCompareProcPtr) CompareExtentKeysPlus));
2d21ac55 450 if (retval)
c910b4d9 451 {
9bccf70c 452 goto ErrorExit;
c910b4d9 453 }
1c79356b 454 /*
9bccf70c 455 * Set up Catalog B-tree vnode
1c79356b 456 */
9bccf70c 457 cndesc.cd_nameptr = hfs_catname;
2d21ac55 458 cndesc.cd_namelen = strlen((char *)hfs_catname);
9bccf70c
A
459 cndesc.cd_cnid = cnattr.ca_fileid = kHFSCatalogFileID;
460
55e303ae
A
461 cfork.cf_size = SWAP_BE64 (vhp->catalogFile.logicalSize);
462 cfork.cf_clump = SWAP_BE32 (vhp->catalogFile.clumpSize);
463 cfork.cf_blocks = SWAP_BE32 (vhp->catalogFile.totalBlocks);
464 cfork.cf_vblocks = 0;
465 cnattr.ca_blocks = cfork.cf_blocks;
466 for (i = 0; i < kHFSPlusExtentDensity; i++) {
467 cfork.cf_extents[i].startBlock =
468 SWAP_BE32 (vhp->catalogFile.extents[i].startBlock);
469 cfork.cf_extents[i].blockCount =
470 SWAP_BE32 (vhp->catalogFile.extents[i].blockCount);
471 }
91447636
A
472 retval = hfs_getnewvnode(hfsmp, NULL, NULL, &cndesc, 0, &cnattr, &cfork,
473 &hfsmp->hfs_catalog_vp);
9bccf70c 474 if (retval) {
9bccf70c
A
475 goto ErrorExit;
476 }
2d21ac55
A
477 hfsmp->hfs_catalog_cp = VTOC(hfsmp->hfs_catalog_vp);
478 hfs_unlock(hfsmp->hfs_catalog_cp);
479
91447636 480 retval = MacToVFSError(BTOpenPath(VTOF(hfsmp->hfs_catalog_vp),
55e303ae 481 (KeyCompareProcPtr) CompareExtendedCatalogKeys));
9bccf70c 482 if (retval) {
9bccf70c
A
483 goto ErrorExit;
484 }
55e303ae 485 if ((hfsmp->hfs_flags & HFS_X) &&
91447636 486 BTGetInformation(VTOF(hfsmp->hfs_catalog_vp), 0, &btinfo) == 0) {
55e303ae
A
487 if (btinfo.keyCompareType == kHFSBinaryCompare) {
488 hfsmp->hfs_flags |= HFS_CASE_SENSITIVE;
489 /* Install a case-sensitive key compare */
91447636 490 (void) BTOpenPath(VTOF(hfsmp->hfs_catalog_vp),
55e303ae
A
491 (KeyCompareProcPtr)cat_binarykeycompare);
492 }
493 }
1c79356b
A
494
495 /*
9bccf70c 496 * Set up Allocation file vnode
1c79356b 497 */
9bccf70c 498 cndesc.cd_nameptr = hfs_vbmname;
2d21ac55 499 cndesc.cd_namelen = strlen((char *)hfs_vbmname);
9bccf70c
A
500 cndesc.cd_cnid = cnattr.ca_fileid = kHFSAllocationFileID;
501
55e303ae
A
502 cfork.cf_size = SWAP_BE64 (vhp->allocationFile.logicalSize);
503 cfork.cf_clump = SWAP_BE32 (vhp->allocationFile.clumpSize);
504 cfork.cf_blocks = SWAP_BE32 (vhp->allocationFile.totalBlocks);
505 cfork.cf_vblocks = 0;
506 cnattr.ca_blocks = cfork.cf_blocks;
507 for (i = 0; i < kHFSPlusExtentDensity; i++) {
508 cfork.cf_extents[i].startBlock =
509 SWAP_BE32 (vhp->allocationFile.extents[i].startBlock);
510 cfork.cf_extents[i].blockCount =
511 SWAP_BE32 (vhp->allocationFile.extents[i].blockCount);
512 }
91447636
A
513 retval = hfs_getnewvnode(hfsmp, NULL, NULL, &cndesc, 0, &cnattr, &cfork,
514 &hfsmp->hfs_allocation_vp);
9bccf70c 515 if (retval) {
9bccf70c
A
516 goto ErrorExit;
517 }
2d21ac55
A
518 hfsmp->hfs_allocation_cp = VTOC(hfsmp->hfs_allocation_vp);
519 hfs_unlock(hfsmp->hfs_allocation_cp);
9bccf70c 520
91447636
A
521 /*
522 * Set up Attribute B-tree vnode
523 */
524 if (vhp->attributesFile.totalBlocks != 0) {
525 cndesc.cd_nameptr = hfs_attrname;
2d21ac55 526 cndesc.cd_namelen = strlen((char *)hfs_attrname);
91447636
A
527 cndesc.cd_cnid = cnattr.ca_fileid = kHFSAttributesFileID;
528
529 cfork.cf_size = SWAP_BE64 (vhp->attributesFile.logicalSize);
530 cfork.cf_clump = SWAP_BE32 (vhp->attributesFile.clumpSize);
531 cfork.cf_blocks = SWAP_BE32 (vhp->attributesFile.totalBlocks);
532 cfork.cf_vblocks = 0;
533 cnattr.ca_blocks = cfork.cf_blocks;
534 for (i = 0; i < kHFSPlusExtentDensity; i++) {
535 cfork.cf_extents[i].startBlock =
536 SWAP_BE32 (vhp->attributesFile.extents[i].startBlock);
537 cfork.cf_extents[i].blockCount =
538 SWAP_BE32 (vhp->attributesFile.extents[i].blockCount);
539 }
540 retval = hfs_getnewvnode(hfsmp, NULL, NULL, &cndesc, 0, &cnattr, &cfork,
541 &hfsmp->hfs_attribute_vp);
542 if (retval) {
91447636
A
543 goto ErrorExit;
544 }
2d21ac55
A
545 hfsmp->hfs_attribute_cp = VTOC(hfsmp->hfs_attribute_vp);
546 hfs_unlock(hfsmp->hfs_attribute_cp);
91447636
A
547 retval = MacToVFSError(BTOpenPath(VTOF(hfsmp->hfs_attribute_vp),
548 (KeyCompareProcPtr) hfs_attrkeycompare));
549 if (retval) {
91447636
A
550 goto ErrorExit;
551 }
552 }
553
2d21ac55
A
554 /*
555 * Set up Startup file vnode
556 */
557 if (vhp->startupFile.totalBlocks != 0) {
558 cndesc.cd_nameptr = hfs_startupname;
559 cndesc.cd_namelen = strlen((char *)hfs_startupname);
560 cndesc.cd_cnid = cnattr.ca_fileid = kHFSStartupFileID;
561
562 cfork.cf_size = SWAP_BE64 (vhp->startupFile.logicalSize);
563 cfork.cf_clump = SWAP_BE32 (vhp->startupFile.clumpSize);
564 cfork.cf_blocks = SWAP_BE32 (vhp->startupFile.totalBlocks);
565 cfork.cf_vblocks = 0;
566 cnattr.ca_blocks = cfork.cf_blocks;
567 for (i = 0; i < kHFSPlusExtentDensity; i++) {
568 cfork.cf_extents[i].startBlock =
569 SWAP_BE32 (vhp->startupFile.extents[i].startBlock);
570 cfork.cf_extents[i].blockCount =
571 SWAP_BE32 (vhp->startupFile.extents[i].blockCount);
572 }
573 retval = hfs_getnewvnode(hfsmp, NULL, NULL, &cndesc, 0, &cnattr, &cfork,
574 &hfsmp->hfs_startup_vp);
575 if (retval) {
576 goto ErrorExit;
577 }
578 hfsmp->hfs_startup_cp = VTOC(hfsmp->hfs_startup_vp);
579 hfs_unlock(hfsmp->hfs_startup_cp);
580 }
581
9bccf70c 582 /* Pick up volume name and create date */
2d21ac55 583 retval = cat_idlookup(hfsmp, kHFSRootFolderID, 0, &cndesc, &cnattr, NULL);
9bccf70c 584 if (retval) {
9bccf70c
A
585 goto ErrorExit;
586 }
587 vcb->vcbCrDate = cnattr.ca_itime;
588 vcb->volumeNameEncodingHint = cndesc.cd_encoding;
589 bcopy(cndesc.cd_nameptr, vcb->vcbVN, min(255, cndesc.cd_namelen));
590 cat_releasedesc(&cndesc);
1c79356b
A
591
592 /* mark the volume dirty (clear clean unmount bit) */
593 vcb->vcbAtrb &= ~kHFSVolumeUnmountedMask;
55e303ae 594 if (hfsmp->jnl && (hfsmp->hfs_flags & HFS_READ_ONLY) == 0) {
91447636 595 hfs_flushvolumeheader(hfsmp, TRUE, 0);
b4c24cb9 596 }
1c79356b 597
2d21ac55
A
598 /* kHFSHasFolderCount is only supported/updated on HFSX volumes */
599 if ((hfsmp->hfs_flags & HFS_X) != 0) {
600 hfsmp->hfs_flags |= HFS_FOLDERCOUNT;
601 }
1c79356b 602
b4c24cb9
A
603 //
604 // Check if we need to do late journal initialization. This only
605 // happens if a previous version of MacOS X (or 9) touched the disk.
606 // In that case hfs_late_journal_init() will go re-locate the journal
607 // and journal_info_block files and validate that they're still kosher.
608 //
609 if ( (vcb->vcbAtrb & kHFSVolumeJournaledMask)
610 && (SWAP_BE32(vhp->lastMountedVersion) != kHFSJMountVersion)
2d21ac55 611 && (hfsmp->jnl == NULL)) {
b4c24cb9
A
612
613 retval = hfs_late_journal_init(hfsmp, vhp, args);
614 if (retval != 0) {
615 hfsmp->jnl = NULL;
91447636
A
616
617 // if the journal failed to open, then set the lastMountedVersion
618 // to be "FSK!" which fsck_hfs will see and force the fsck instead
619 // of just bailing out because the volume is journaled.
620 if (!(hfsmp->hfs_flags & HFS_READ_ONLY)) {
621 HFSPlusVolumeHeader *jvhp;
622 daddr64_t mdb_offset;
623 struct buf *bp = NULL;
624
625 hfsmp->hfs_flags |= HFS_NEED_JNL_RESET;
626
627 mdb_offset = (daddr64_t)((embeddedOffset / blockSize) + HFS_PRI_SECTOR(blockSize));
628
593a1d5f
A
629 retval = (int)buf_meta_bread(hfsmp->hfs_devvp,
630 HFS_PHYSBLK_ROUNDDOWN(mdb_offset, hfsmp->hfs_log_per_phys),
631 hfsmp->hfs_physical_block_size, cred, &bp);
91447636 632 if (retval == 0) {
593a1d5f 633 jvhp = (HFSPlusVolumeHeader *)(buf_dataptr(bp) + HFS_PRI_OFFSET(hfsmp->hfs_physical_block_size));
91447636
A
634
635 if (SWAP_BE16(jvhp->signature) == kHFSPlusSigWord || SWAP_BE16(jvhp->signature) == kHFSXSigWord) {
636 printf ("hfs(3): Journal replay fail. Writing lastMountVersion as FSK!\n");
637 jvhp->lastMountedVersion = SWAP_BE32(kFSKMountVersion);
638 buf_bwrite(bp);
639 } else {
640 buf_brelse(bp);
641 }
642 bp = NULL;
643 } else if (bp) {
644 buf_brelse(bp);
645 // clear this so the error exit path won't try to use it
646 bp = NULL;
647 }
648 }
649
650 retval = EINVAL;
b4c24cb9
A
651 goto ErrorExit;
652 } else if (hfsmp->jnl) {
2d21ac55 653 vfs_setflags(hfsmp->hfs_mp, (u_int64_t)((unsigned int)MNT_JOURNALED));
b4c24cb9 654 }
743b1565 655 } else if (hfsmp->jnl || ((vcb->vcbAtrb & kHFSVolumeJournaledMask) && (hfsmp->hfs_flags & HFS_READ_ONLY))) {
b4c24cb9
A
656 struct cat_attr jinfo_attr, jnl_attr;
657
743b1565
A
658 if (hfsmp->hfs_flags & HFS_READ_ONLY) {
659 vcb->vcbAtrb &= ~kHFSVolumeJournaledMask;
660 }
661
b4c24cb9
A
662 // if we're here we need to fill in the fileid's for the
663 // journal and journal_info_block.
664 hfsmp->hfs_jnlinfoblkid = GetFileInfo(vcb, kRootDirID, ".journal_info_block", &jinfo_attr, NULL);
665 hfsmp->hfs_jnlfileid = GetFileInfo(vcb, kRootDirID, ".journal", &jnl_attr, NULL);
666 if (hfsmp->hfs_jnlinfoblkid == 0 || hfsmp->hfs_jnlfileid == 0) {
667 printf("hfs: danger! couldn't find the file-id's for the journal or journal_info_block\n");
668 printf("hfs: jnlfileid %d, jnlinfoblkid %d\n", hfsmp->hfs_jnlfileid, hfsmp->hfs_jnlinfoblkid);
669 }
743b1565
A
670
671 if (hfsmp->hfs_flags & HFS_READ_ONLY) {
672 vcb->vcbAtrb |= kHFSVolumeJournaledMask;
673 }
2d21ac55
A
674
675 if (hfsmp->jnl == NULL) {
676 vfs_clearflags(hfsmp->hfs_mp, (u_int64_t)((unsigned int)MNT_JOURNALED));
677 }
b4c24cb9
A
678 }
679
55e303ae
A
680 /*
681 * Establish a metadata allocation zone.
682 */
683 hfs_metadatazone_init(hfsmp);
684
685 /*
686 * Make any metadata zone adjustments.
687 */
688 if (hfsmp->hfs_flags & HFS_METADATA_ZONE) {
689 /* Keep the roving allocator out of the metadata zone. */
690 if (vcb->nextAllocation >= hfsmp->hfs_metazone_start &&
691 vcb->nextAllocation <= hfsmp->hfs_metazone_end) {
2d21ac55 692 HFS_UPDATE_NEXT_ALLOCATION(hfsmp, hfsmp->hfs_metazone_end + 1);
55e303ae
A
693 }
694 }
695
2d21ac55
A
696 /* Setup private/hidden directories for hardlinks. */
697 hfs_privatedir_init(hfsmp, FILE_HARDLINKS);
698 hfs_privatedir_init(hfsmp, DIR_HARDLINKS);
699
91447636 700 if ((hfsmp->hfs_flags & HFS_READ_ONLY) == 0)
55e303ae
A
701 hfs_remove_orphans(hfsmp);
702
703 if ( !(vcb->vcbAtrb & kHFSVolumeHardwareLockMask) ) // if the disk is not write protected
704 {
705 MarkVCBDirty( vcb ); // mark VCB dirty so it will be written
706 }
707
55e303ae
A
708 /*
709 * Allow hot file clustering if conditions allow.
710 */
711 if ((hfsmp->hfs_flags & HFS_METADATA_ZONE) &&
712 ((hfsmp->hfs_flags & HFS_READ_ONLY) == 0)) {
91447636 713 (void) hfs_recording_init(hfsmp);
55e303ae 714 }
b4c24cb9 715
2d21ac55
A
716 /* Force ACLs on HFS+ file systems. */
717 vfs_setextendedsecurity(HFSTOVFS(hfsmp));
718
719 /* Check if volume supports writing of extent-based extended attributes */
720 hfs_check_volxattr(hfsmp, HFS_SET_XATTREXTENTS_STATE);
91447636 721
1c79356b
A
722 return (0);
723
1c79356b
A
724ErrorExit:
725 /*
91447636 726 * A fatal error occurred and the volume cannot be mounted
1c79356b
A
727 * release any resources that we aquired...
728 */
91447636
A
729 if (hfsmp->hfs_attribute_vp)
730 ReleaseMetaFileVNode(hfsmp->hfs_attribute_vp);
731 ReleaseMetaFileVNode(hfsmp->hfs_allocation_vp);
732 ReleaseMetaFileVNode(hfsmp->hfs_catalog_vp);
733 ReleaseMetaFileVNode(hfsmp->hfs_extents_vp);
1c79356b
A
734
735 return (retval);
736}
737
738
739/*
740 * ReleaseMetaFileVNode
741 *
742 * vp L - -
743 */
744static void ReleaseMetaFileVNode(struct vnode *vp)
745{
9bccf70c 746 struct filefork *fp;
1c79356b 747
9bccf70c 748 if (vp && (fp = VTOF(vp))) {
91447636
A
749 if (fp->fcbBTCBPtr != NULL) {
750 (void)hfs_lock(VTOC(vp), HFS_EXCLUSIVE_LOCK);
9bccf70c 751 (void) BTClosePath(fp);
91447636
A
752 hfs_unlock(VTOC(vp));
753 }
1c79356b
A
754
755 /* release the node even if BTClosePath fails */
91447636
A
756 vnode_recycle(vp);
757 vnode_put(vp);
1c79356b 758 }
1c79356b
A
759}
760
761
762/*************************************************************
763*
764* Unmounts a hfs volume.
765* At this point vflush() has been called (to dump all non-metadata files)
766*
767*************************************************************/
768
55e303ae
A
769__private_extern__
770int
2d21ac55 771hfsUnmount( register struct hfsmount *hfsmp, __unused struct proc *p)
1c79356b 772{
2d21ac55
A
773 /* Get rid of our attribute data vnode (if any). */
774 if (hfsmp->hfs_attrdata_vp) {
775 vnode_t advp = hfsmp->hfs_attrdata_vp;
776
777 if (vnode_get(advp) == 0) {
778 vnode_rele_ext(advp, O_EVTONLY, 0);
779 vnode_put(advp);
780 }
781 hfsmp->hfs_attrdata_vp = NULLVP;
782 }
783
784 if (hfsmp->hfs_startup_vp)
785 ReleaseMetaFileVNode(hfsmp->hfs_startup_vp);
786
91447636
A
787 if (hfsmp->hfs_allocation_vp)
788 ReleaseMetaFileVNode(hfsmp->hfs_allocation_vp);
1c79356b 789
91447636
A
790 if (hfsmp->hfs_attribute_vp)
791 ReleaseMetaFileVNode(hfsmp->hfs_attribute_vp);
1c79356b 792
91447636
A
793 ReleaseMetaFileVNode(hfsmp->hfs_catalog_vp);
794 ReleaseMetaFileVNode(hfsmp->hfs_extents_vp);
1c79356b 795
2d21ac55
A
796 /*
797 * Setting these pointers to NULL so that any references
798 * past this point will fail, and tell us the point of failure.
799 * Also, facilitates a check in hfs_update for a null catalog
800 * vp
801 */
802 hfsmp->hfs_allocation_vp = NULL;
803 hfsmp->hfs_attribute_vp = NULL;
804 hfsmp->hfs_catalog_vp = NULL;
805 hfsmp->hfs_extents_vp = NULL;
806 hfsmp->hfs_startup_vp = NULL;
807
91447636 808 return (0);
1c79356b
A
809}
810
811
0b4e3aa0 812/*
91447636 813 * Test if fork has overflow extents.
0b4e3aa0 814 */
55e303ae
A
815__private_extern__
816int
817overflow_extents(struct filefork *fp)
1c79356b 818{
9bccf70c 819 u_long blocks;
1c79356b 820
2d21ac55
A
821 //
822 // If the vnode pointer is NULL then we're being called
823 // from hfs_remove_orphans() with a faked-up filefork
824 // and therefore it has to be an HFS+ volume. Otherwise
825 // we check through the volume header to see what type
826 // of volume we're on.
827 //
828 if (FTOV(fp) == NULL || VTOVCB(FTOV(fp))->vcbSigWord == kHFSPlusSigWord) {
9bccf70c
A
829 if (fp->ff_extents[7].blockCount == 0)
830 return (0);
1c79356b 831
9bccf70c
A
832 blocks = fp->ff_extents[0].blockCount +
833 fp->ff_extents[1].blockCount +
834 fp->ff_extents[2].blockCount +
835 fp->ff_extents[3].blockCount +
836 fp->ff_extents[4].blockCount +
837 fp->ff_extents[5].blockCount +
838 fp->ff_extents[6].blockCount +
839 fp->ff_extents[7].blockCount;
840 } else {
841 if (fp->ff_extents[2].blockCount == 0)
842 return false;
843
844 blocks = fp->ff_extents[0].blockCount +
845 fp->ff_extents[1].blockCount +
846 fp->ff_extents[2].blockCount;
847 }
1c79356b 848
9bccf70c
A
849 return (fp->ff_blocks > blocks);
850}
1c79356b
A
851
852
55e303ae 853/*
91447636 854 * Lock HFS system file(s).
55e303ae
A
855 */
856__private_extern__
9bccf70c 857int
91447636 858hfs_systemfile_lock(struct hfsmount *hfsmp, int flags, enum hfslocktype locktype)
1c79356b 859{
91447636 860 /*
2d21ac55 861 * Locking order is Catalog file, Attributes file, Startup file, Bitmap file, Extents file
91447636
A
862 */
863 if (flags & SFL_CATALOG) {
2d21ac55
A
864
865#ifdef HFS_CHECK_LOCK_ORDER
866 if (hfsmp->hfs_attribute_cp && hfsmp->hfs_attribute_cp->c_lockowner == current_thread()) {
867 panic("hfs_systemfile_lock: bad lock order (Attributes before Catalog)");
868 }
869 if (hfsmp->hfs_startup_cp && hfsmp->hfs_startup_cp->c_lockowner == current_thread()) {
870 panic("hfs_systemfile_lock: bad lock order (Startup before Catalog)");
871 }
872 if (hfsmp-> hfs_extents_cp && hfsmp->hfs_extents_cp->c_lockowner == current_thread()) {
873 panic("hfs_systemfile_lock: bad lock order (Extents before Catalog)");
874 }
875#endif /* HFS_CHECK_LOCK_ORDER */
876
877 (void) hfs_lock(hfsmp->hfs_catalog_cp, locktype);
91447636
A
878 /*
879 * When the catalog file has overflow extents then
880 * also acquire the extents b-tree lock if its not
881 * already requested.
882 */
883 if ((flags & SFL_EXTENTS) == 0 &&
884 overflow_extents(VTOF(hfsmp->hfs_catalog_vp))) {
885 flags |= SFL_EXTENTS;
886 }
887 }
888 if (flags & SFL_ATTRIBUTE) {
2d21ac55
A
889
890#ifdef HFS_CHECK_LOCK_ORDER
891 if (hfsmp->hfs_startup_cp && hfsmp->hfs_startup_cp->c_lockowner == current_thread()) {
892 panic("hfs_systemfile_lock: bad lock order (Startup before Attributes)");
893 }
894 if (hfsmp->hfs_extents_cp && hfsmp->hfs_extents_cp->c_lockowner == current_thread()) {
895 panic("hfs_systemfile_lock: bad lock order (Extents before Attributes)");
896 }
897#endif /* HFS_CHECK_LOCK_ORDER */
898
899 if (hfsmp->hfs_attribute_cp) {
900 (void) hfs_lock(hfsmp->hfs_attribute_cp, locktype);
91447636
A
901 /*
902 * When the attribute file has overflow extents then
903 * also acquire the extents b-tree lock if its not
904 * already requested.
905 */
906 if ((flags & SFL_EXTENTS) == 0 &&
907 overflow_extents(VTOF(hfsmp->hfs_attribute_vp))) {
908 flags |= SFL_EXTENTS;
909 }
910 } else {
911 flags &= ~SFL_ATTRIBUTE;
912 }
913 }
2d21ac55
A
914 if (flags & SFL_STARTUP) {
915#ifdef HFS_CHECK_LOCK_ORDER
916 if (hfsmp-> hfs_extents_cp && hfsmp->hfs_extents_cp->c_lockowner == current_thread()) {
917 panic("hfs_systemfile_lock: bad lock order (Extents before Startup)");
918 }
919#endif /* HFS_CHECK_LOCK_ORDER */
920
921 (void) hfs_lock(hfsmp->hfs_startup_cp, locktype);
922 /*
923 * When the startup file has overflow extents then
924 * also acquire the extents b-tree lock if its not
925 * already requested.
926 */
927 if ((flags & SFL_EXTENTS) == 0 &&
928 overflow_extents(VTOF(hfsmp->hfs_startup_vp))) {
929 flags |= SFL_EXTENTS;
930 }
931 }
932 /*
933 * To prevent locks being taken in the wrong order, the extent lock
934 * gets a bitmap lock as well.
935 */
936 if (flags & (SFL_BITMAP | SFL_EXTENTS)) {
91447636
A
937 /*
938 * Since the only bitmap operations are clearing and
939 * setting bits we always need exclusive access. And
940 * when we have a journal, we can "hide" behind that
941 * lock since we can only change the bitmap from
942 * within a transaction.
943 */
2d21ac55 944 if (hfsmp->jnl || (hfsmp->hfs_allocation_cp == NULL)) {
91447636
A
945 flags &= ~SFL_BITMAP;
946 } else {
2d21ac55
A
947 (void) hfs_lock(hfsmp->hfs_allocation_cp, HFS_EXCLUSIVE_LOCK);
948 /* The bitmap lock is also grabbed when only extent lock
949 * was requested. Set the bitmap lock bit in the lock
950 * flags which callers will use during unlock.
951 */
952 flags |= SFL_BITMAP;
91447636
A
953 }
954 }
955 if (flags & SFL_EXTENTS) {
956 /*
957 * Since the extents btree lock is recursive we always
958 * need exclusive access.
959 */
2d21ac55 960 (void) hfs_lock(hfsmp->hfs_extents_cp, HFS_EXCLUSIVE_LOCK);
91447636
A
961 }
962 return (flags);
963}
9bccf70c 964
91447636
A
965/*
966 * unlock HFS system file(s).
967 */
968__private_extern__
969void
970hfs_systemfile_unlock(struct hfsmount *hfsmp, int flags)
971{
972 struct timeval tv;
973 u_int32_t lastfsync;
974 int numOfLockedBuffs;
9bccf70c 975
2d21ac55
A
976 if (hfsmp->jnl == NULL) {
977 microuptime(&tv);
978 lastfsync = tv.tv_sec;
979 }
980 if (flags & SFL_STARTUP && hfsmp->hfs_startup_cp) {
981 hfs_unlock(hfsmp->hfs_startup_cp);
982 }
983 if (flags & SFL_ATTRIBUTE && hfsmp->hfs_attribute_cp) {
91447636
A
984 if (hfsmp->jnl == NULL) {
985 BTGetLastSync((FCB*)VTOF(hfsmp->hfs_attribute_vp), &lastfsync);
986 numOfLockedBuffs = count_lock_queue();
987 if ((numOfLockedBuffs > kMaxLockedMetaBuffers) ||
988 ((numOfLockedBuffs > 1) && ((tv.tv_sec - lastfsync) >
989 kMaxSecsForFsync))) {
990 hfs_btsync(hfsmp->hfs_attribute_vp, HFS_SYNCTRANS);
991 }
992 }
2d21ac55 993 hfs_unlock(hfsmp->hfs_attribute_cp);
9bccf70c 994 }
91447636
A
995 if (flags & SFL_CATALOG) {
996 if (hfsmp->jnl == NULL) {
997 BTGetLastSync((FCB*)VTOF(hfsmp->hfs_catalog_vp), &lastfsync);
998 numOfLockedBuffs = count_lock_queue();
999 if ((numOfLockedBuffs > kMaxLockedMetaBuffers) ||
1000 ((numOfLockedBuffs > 1) && ((tv.tv_sec - lastfsync) >
1001 kMaxSecsForFsync))) {
1002 hfs_btsync(hfsmp->hfs_catalog_vp, HFS_SYNCTRANS);
1003 }
9bccf70c 1004 }
2d21ac55 1005 hfs_unlock(hfsmp->hfs_catalog_cp);
91447636
A
1006 }
1007 if (flags & SFL_BITMAP) {
2d21ac55 1008 hfs_unlock(hfsmp->hfs_allocation_cp);
91447636
A
1009 }
1010 if (flags & SFL_EXTENTS) {
1011 if (hfsmp->jnl == NULL) {
1012 BTGetLastSync((FCB*)VTOF(hfsmp->hfs_extents_vp), &lastfsync);
1013 numOfLockedBuffs = count_lock_queue();
1014 if ((numOfLockedBuffs > kMaxLockedMetaBuffers) ||
1015 ((numOfLockedBuffs > 1) && ((tv.tv_sec - lastfsync) >
1016 kMaxSecsForFsync))) {
1017 hfs_btsync(hfsmp->hfs_extents_vp, HFS_SYNCTRANS);
1018 }
1019 }
2d21ac55 1020 hfs_unlock(hfsmp->hfs_extents_cp);
9bccf70c 1021 }
9bccf70c 1022}
1c79356b 1023
91447636 1024
9bccf70c
A
1025/*
1026 * RequireFileLock
1027 *
1028 * Check to see if a vnode is locked in the current context
1029 * This is to be used for debugging purposes only!!
1030 */
1031#if HFS_DIAGNOSTIC
1032void RequireFileLock(FileReference vp, int shareable)
1c79356b 1033{
91447636 1034 int locked;
1c79356b 1035
91447636
A
1036 /* The extents btree and allocation bitmap are always exclusive. */
1037 if (VTOC(vp)->c_fileid == kHFSExtentsFileID ||
1038 VTOC(vp)->c_fileid == kHFSAllocationFileID) {
1039 shareable = 0;
1040 }
9bccf70c 1041
91447636 1042 locked = VTOC(vp)->c_lockowner == (void *)current_thread();
9bccf70c 1043
91447636 1044 if (!locked && !shareable) {
9bccf70c 1045 switch (VTOC(vp)->c_fileid) {
91447636
A
1046 case kHFSExtentsFileID:
1047 panic("extents btree not locked! v: 0x%08X\n #\n", (u_int)vp);
1048 break;
1049 case kHFSCatalogFileID:
1050 panic("catalog btree not locked! v: 0x%08X\n #\n", (u_int)vp);
1051 break;
1052 case kHFSAllocationFileID:
1053 /* The allocation file can hide behind the jornal lock. */
1054 if (VTOHFS(vp)->jnl == NULL)
1055 panic("allocation file not locked! v: 0x%08X\n #\n", (u_int)vp);
1056 break;
2d21ac55
A
1057 case kHFSStartupFileID:
1058 panic("startup file not locked! v: 0x%08X\n #\n", (u_int)vp);
91447636
A
1059 case kHFSAttributesFileID:
1060 panic("attributes btree not locked! v: 0x%08X\n #\n", (u_int)vp);
1061 break;
9bccf70c
A
1062 }
1063 }
1c79356b 1064}
9bccf70c 1065#endif
1c79356b
A
1066
1067
9bccf70c
A
1068/*
1069 * There are three ways to qualify for ownership rights on an object:
1070 *
1071 * 1. (a) Your UID matches the cnode's UID.
55e303ae 1072 * (b) The object in question is owned by "unknown"
9bccf70c
A
1073 * 2. (a) Permissions on the filesystem are being ignored and
1074 * your UID matches the replacement UID.
1075 * (b) Permissions on the filesystem are being ignored and
55e303ae 1076 * the replacement UID is "unknown".
9bccf70c
A
1077 * 3. You are root.
1078 *
1079 */
1080int
91447636 1081hfs_owner_rights(struct hfsmount *hfsmp, uid_t cnode_uid, kauth_cred_t cred,
2d21ac55 1082 __unused struct proc *p, int invokesuperuserstatus)
9bccf70c 1083{
91447636 1084 if ((kauth_cred_getuid(cred) == cnode_uid) || /* [1a] */
55e303ae 1085 (cnode_uid == UNKNOWNUID) || /* [1b] */
91447636
A
1086 ((((unsigned int)vfs_flags(HFSTOVFS(hfsmp))) & MNT_UNKNOWNPERMISSIONS) && /* [2] */
1087 ((kauth_cred_getuid(cred) == hfsmp->hfs_uid) || /* [2a] */
55e303ae 1088 (hfsmp->hfs_uid == UNKNOWNUID))) || /* [2b] */
91447636 1089 (invokesuperuserstatus && (suser(cred, 0) == 0))) { /* [3] */
9bccf70c
A
1090 return (0);
1091 } else {
1092 return (EPERM);
1093 }
1c79356b
A
1094}
1095
1096
9bccf70c
A
1097unsigned long BestBlockSizeFit(unsigned long allocationBlockSize,
1098 unsigned long blockSizeLimit,
1099 unsigned long baseMultiple) {
1100 /*
1101 Compute the optimal (largest) block size (no larger than allocationBlockSize) that is less than the
1102 specified limit but still an even multiple of the baseMultiple.
1103 */
1104 int baseBlockCount, blockCount;
1105 unsigned long trialBlockSize;
1c79356b 1106
9bccf70c
A
1107 if (allocationBlockSize % baseMultiple != 0) {
1108 /*
1109 Whoops: the allocation blocks aren't even multiples of the specified base:
1110 no amount of dividing them into even parts will be a multiple, either then!
1111 */
1112 return 512; /* Hope for the best */
1113 };
1c79356b 1114
9bccf70c
A
1115 /* Try the obvious winner first, to prevent 12K allocation blocks, for instance,
1116 from being handled as two 6K logical blocks instead of 3 4K logical blocks.
1117 Even though the former (the result of the loop below) is the larger allocation
1118 block size, the latter is more efficient: */
1119 if (allocationBlockSize % PAGE_SIZE == 0) return PAGE_SIZE;
1c79356b 1120
9bccf70c
A
1121 /* No clear winner exists: pick the largest even fraction <= MAXBSIZE: */
1122 baseBlockCount = allocationBlockSize / baseMultiple; /* Now guaranteed to be an even multiple */
1c79356b 1123
9bccf70c
A
1124 for (blockCount = baseBlockCount; blockCount > 0; --blockCount) {
1125 trialBlockSize = blockCount * baseMultiple;
1126 if (allocationBlockSize % trialBlockSize == 0) { /* An even multiple? */
1127 if ((trialBlockSize <= blockSizeLimit) &&
1128 (trialBlockSize % baseMultiple == 0)) {
1129 return trialBlockSize;
1130 };
1131 };
1132 };
1c79356b 1133
9bccf70c
A
1134 /* Note: we should never get here, since blockCount = 1 should always work,
1135 but this is nice and safe and makes the compiler happy, too ... */
1136 return 512;
1137}
1c79356b 1138
1c79356b 1139
b4c24cb9
A
1140__private_extern__
1141u_long
2d21ac55 1142GetFileInfo(ExtendedVCB *vcb, __unused u_int32_t dirid, const char *name,
b4c24cb9
A
1143 struct cat_attr *fattr, struct cat_fork *forkinfo)
1144{
1145 struct hfsmount * hfsmp;
b4c24cb9 1146 struct cat_desc jdesc;
91447636 1147 int lockflags;
b4c24cb9
A
1148 int error;
1149
1150 if (vcb->vcbSigWord != kHFSPlusSigWord)
1151 return (0);
1152
1153 hfsmp = VCBTOHFS(vcb);
1154
1155 memset(&jdesc, 0, sizeof(struct cat_desc));
1156 jdesc.cd_parentcnid = kRootDirID;
2d21ac55 1157 jdesc.cd_nameptr = (const u_int8_t *)name;
b4c24cb9
A
1158 jdesc.cd_namelen = strlen(name);
1159
91447636
A
1160 lockflags = hfs_systemfile_lock(hfsmp, SFL_CATALOG, HFS_SHARED_LOCK);
1161 error = cat_lookup(hfsmp, &jdesc, 0, NULL, fattr, forkinfo, NULL);
1162 hfs_systemfile_unlock(hfsmp, lockflags);
b4c24cb9
A
1163
1164 if (error == 0) {
1165 return (fattr->ca_fileid);
55e303ae 1166 } else if (hfsmp->hfs_flags & HFS_READ_ONLY) {
b4c24cb9
A
1167 return (0);
1168 }
91447636
A
1169
1170 return (0); /* XXX what callers expect on an error */
b4c24cb9
A
1171}
1172
1173
1174/*
2d21ac55
A
1175 * On HFS Plus Volumes, there can be orphaned files or directories
1176 * These are files or directories that were unlinked while busy.
1177 * If the volume was not cleanly unmounted then some of these may
b4c24cb9
A
1178 * have persisted and need to be removed.
1179 */
1180__private_extern__
1181void
1182hfs_remove_orphans(struct hfsmount * hfsmp)
1183{
1184 struct BTreeIterator * iterator = NULL;
1185 struct FSBufferDescriptor btdata;
1186 struct HFSPlusCatalogFile filerec;
1187 struct HFSPlusCatalogKey * keyp;
55e303ae 1188 struct proc *p = current_proc();
b4c24cb9
A
1189 FCB *fcb;
1190 ExtendedVCB *vcb;
1191 char filename[32];
1192 char tempname[32];
1193 size_t namelen;
91447636 1194 cat_cookie_t cookie;
b4c24cb9 1195 int catlock = 0;
55e303ae
A
1196 int catreserve = 0;
1197 int started_tr = 0;
91447636 1198 int lockflags;
55e303ae 1199 int result;
91447636
A
1200 int orphanedlinks = 0;
1201
1202 bzero(&cookie, sizeof(cookie));
1203
55e303ae 1204 if (hfsmp->hfs_flags & HFS_CLEANED_ORPHANS)
b4c24cb9
A
1205 return;
1206
1207 vcb = HFSTOVCB(hfsmp);
91447636 1208 fcb = VTOF(hfsmp->hfs_catalog_vp);
b4c24cb9
A
1209
1210 btdata.bufferAddress = &filerec;
1211 btdata.itemSize = sizeof(filerec);
1212 btdata.itemCount = 1;
1213
1214 MALLOC(iterator, struct BTreeIterator *, sizeof(*iterator), M_TEMP, M_WAITOK);
1215 bzero(iterator, sizeof(*iterator));
91447636
A
1216
1217 /* Build a key to "temp" */
b4c24cb9 1218 keyp = (HFSPlusCatalogKey*)&iterator->key;
2d21ac55 1219 keyp->parentID = hfsmp->hfs_private_desc[FILE_HARDLINKS].cd_cnid;
91447636
A
1220 keyp->nodeName.length = 4; /* "temp" */
1221 keyp->keyLength = kHFSPlusCatalogKeyMinimumLength + keyp->nodeName.length * 2;
1222 keyp->nodeName.unicode[0] = 't';
1223 keyp->nodeName.unicode[1] = 'e';
1224 keyp->nodeName.unicode[2] = 'm';
1225 keyp->nodeName.unicode[3] = 'p';
b4c24cb9 1226
b4c24cb9 1227 /*
2d21ac55 1228 * Position the iterator just before the first real temp file/dir.
b4c24cb9 1229 */
91447636
A
1230 lockflags = hfs_systemfile_lock(hfsmp, SFL_CATALOG, HFS_EXCLUSIVE_LOCK);
1231 (void) BTSearchRecord(fcb, iterator, NULL, NULL, iterator);
1232 hfs_systemfile_unlock(hfsmp, lockflags);
b4c24cb9 1233
2d21ac55 1234 /* Visit all the temp files/dirs in the HFS+ private directory. */
b4c24cb9 1235 for (;;) {
91447636 1236 lockflags = hfs_systemfile_lock(hfsmp, SFL_CATALOG, HFS_EXCLUSIVE_LOCK);
b4c24cb9 1237 result = BTIterateRecord(fcb, kBTreeNextRecord, iterator, &btdata, NULL);
91447636 1238 hfs_systemfile_unlock(hfsmp, lockflags);
b4c24cb9
A
1239 if (result)
1240 break;
2d21ac55 1241 if (keyp->parentID != hfsmp->hfs_private_desc[FILE_HARDLINKS].cd_cnid)
b4c24cb9 1242 break;
b4c24cb9
A
1243
1244 (void) utf8_encodestr(keyp->nodeName.unicode, keyp->nodeName.length * 2,
2d21ac55 1245 (u_int8_t *)filename, &namelen, sizeof(filename), 0, 0);
b4c24cb9 1246
2d21ac55
A
1247 (void) snprintf(tempname, sizeof(tempname), "%s%d",
1248 HFS_DELETE_PREFIX, filerec.fileID);
b4c24cb9
A
1249
1250 /*
2d21ac55
A
1251 * Delete all files (and directories) named "tempxxx",
1252 * where xxx is the file's cnid in decimal.
b4c24cb9 1253 *
b4c24cb9
A
1254 */
1255 if (bcmp(tempname, filename, namelen) == 0) {
91447636
A
1256 struct filefork dfork;
1257 struct filefork rfork;
1258 struct cnode cnode;
1259
1260 bzero(&dfork, sizeof(dfork));
1261 bzero(&rfork, sizeof(rfork));
1262 bzero(&cnode, sizeof(cnode));
1263
2d21ac55
A
1264 /* Delete any attributes, ignore errors */
1265 (void) hfs_removeallattr(hfsmp, filerec.fileID);
1266
91447636
A
1267 if (hfs_start_transaction(hfsmp) != 0) {
1268 printf("hfs_remove_orphans: failed to start transaction\n");
1269 goto exit;
55e303ae 1270 }
91447636 1271 started_tr = 1;
55e303ae
A
1272
1273 /*
1274 * Reserve some space in the Catalog file.
1275 */
1276 if (cat_preflight(hfsmp, CAT_DELETE, &cookie, p) != 0) {
91447636 1277 printf("hfs_remove_orphans: cat_preflight failed\n");
55e303ae
A
1278 goto exit;
1279 }
1280 catreserve = 1;
b4c24cb9 1281
91447636 1282 lockflags = hfs_systemfile_lock(hfsmp, SFL_CATALOG | SFL_ATTRIBUTE | SFL_EXTENTS | SFL_BITMAP, HFS_EXCLUSIVE_LOCK);
55e303ae 1283 catlock = 1;
b4c24cb9
A
1284
1285 /* Build a fake cnode */
55e303ae
A
1286 cat_convertattr(hfsmp, (CatalogRecord *)&filerec, &cnode.c_attr,
1287 &dfork.ff_data, &rfork.ff_data);
2d21ac55
A
1288 cnode.c_desc.cd_parentcnid = hfsmp->hfs_private_desc[FILE_HARDLINKS].cd_cnid;
1289 cnode.c_desc.cd_nameptr = (const u_int8_t *)filename;
b4c24cb9 1290 cnode.c_desc.cd_namelen = namelen;
55e303ae
A
1291 cnode.c_desc.cd_cnid = cnode.c_attr.ca_fileid;
1292 cnode.c_blocks = dfork.ff_blocks + rfork.ff_blocks;
b4c24cb9
A
1293
1294 /* Position iterator at previous entry */
1295 if (BTIterateRecord(fcb, kBTreePrevRecord, iterator,
55e303ae 1296 NULL, NULL) != 0) {
b4c24cb9 1297 break;
55e303ae
A
1298 }
1299
b4c24cb9 1300 /* Truncate the file to zero (both forks) */
55e303ae
A
1301 if (dfork.ff_blocks > 0) {
1302 u_int64_t fsize;
1303
1304 dfork.ff_cp = &cnode;
1305 cnode.c_datafork = &dfork;
1306 cnode.c_rsrcfork = NULL;
1307 fsize = (u_int64_t)dfork.ff_blocks * (u_int64_t)HFSTOVCB(hfsmp)->blockSize;
1308 while (fsize > 0) {
2d21ac55 1309 if (fsize > HFS_BIGFILE_SIZE && overflow_extents(&dfork)) {
55e303ae
A
1310 fsize -= HFS_BIGFILE_SIZE;
1311 } else {
1312 fsize = 0;
1313 }
1314
1315 if (TruncateFileC(vcb, (FCB*)&dfork, fsize, false) != 0) {
1316 printf("error truncting data fork!\n");
1317 break;
1318 }
1319
1320 //
1321 // if we're iteratively truncating this file down,
1322 // then end the transaction and start a new one so
1323 // that no one transaction gets too big.
1324 //
1325 if (fsize > 0 && started_tr) {
2d21ac55
A
1326 /* Drop system file locks before starting
1327 * another transaction to preserve lock order.
1328 */
1329 hfs_systemfile_unlock(hfsmp, lockflags);
1330 catlock = 0;
91447636 1331 hfs_end_transaction(hfsmp);
2d21ac55 1332
91447636 1333 if (hfs_start_transaction(hfsmp) != 0) {
55e303ae
A
1334 started_tr = 0;
1335 break;
1336 }
2d21ac55
A
1337 lockflags = hfs_systemfile_lock(hfsmp, SFL_CATALOG | SFL_ATTRIBUTE | SFL_EXTENTS | SFL_BITMAP, HFS_EXCLUSIVE_LOCK);
1338 catlock = 1;
55e303ae 1339 }
b4c24cb9
A
1340 }
1341 }
55e303ae
A
1342
1343 if (rfork.ff_blocks > 0) {
1344 rfork.ff_cp = &cnode;
b4c24cb9 1345 cnode.c_datafork = NULL;
55e303ae
A
1346 cnode.c_rsrcfork = &rfork;
1347 if (TruncateFileC(vcb, (FCB*)&rfork, 0, false) != 0) {
b4c24cb9
A
1348 printf("error truncting rsrc fork!\n");
1349 break;
1350 }
1351 }
1352
2d21ac55 1353 /* Remove the file or folder record from the Catalog */
b4c24cb9 1354 if (cat_delete(hfsmp, &cnode.c_desc, &cnode.c_attr) != 0) {
2d21ac55
A
1355 printf("hfs_remove_orphans: error deleting cat rec for id %d!\n", cnode.c_desc.cd_cnid);
1356 hfs_systemfile_unlock(hfsmp, lockflags);
1357 catlock = 0;
91447636 1358 hfs_volupdate(hfsmp, VOL_UPDATE, 0);
b4c24cb9
A
1359 break;
1360 }
91447636
A
1361 ++orphanedlinks;
1362
b4c24cb9 1363 /* Update parent and volume counts */
2d21ac55
A
1364 hfsmp->hfs_private_attr[FILE_HARDLINKS].ca_entries--;
1365 if (cnode.c_attr.ca_mode & S_IFDIR) {
1366 DEC_FOLDERCOUNT(hfsmp, hfsmp->hfs_private_attr[FILE_HARDLINKS]);
1367 }
1368
1369 (void)cat_update(hfsmp, &hfsmp->hfs_private_desc[FILE_HARDLINKS],
1370 &hfsmp->hfs_private_attr[FILE_HARDLINKS], NULL, NULL);
55e303ae
A
1371
1372 /* Drop locks and end the transaction */
91447636 1373 hfs_systemfile_unlock(hfsmp, lockflags);
55e303ae
A
1374 cat_postflight(hfsmp, &cookie, p);
1375 catlock = catreserve = 0;
2d21ac55
A
1376
1377 /*
1378 Now that Catalog is unlocked, update the volume info, making
1379 sure to differentiate between files and directories
1380 */
1381 if (cnode.c_attr.ca_mode & S_IFDIR) {
1382 hfs_volupdate(hfsmp, VOL_RMDIR, 0);
1383 }
1384 else{
1385 hfs_volupdate(hfsmp, VOL_RMFILE, 0);
1386 }
1387
55e303ae 1388 if (started_tr) {
91447636 1389 hfs_end_transaction(hfsmp);
55e303ae
A
1390 started_tr = 0;
1391 }
55e303ae
A
1392
1393 } /* end if */
1394 } /* end for */
91447636 1395 if (orphanedlinks > 0)
2d21ac55 1396 printf("HFS: Removed %d orphaned unlinked files or directories \n", orphanedlinks);
b4c24cb9 1397exit:
55e303ae 1398 if (catlock) {
91447636 1399 hfs_systemfile_unlock(hfsmp, lockflags);
55e303ae
A
1400 }
1401 if (catreserve) {
1402 cat_postflight(hfsmp, &cookie, p);
1403 }
b4c24cb9 1404 if (started_tr) {
91447636 1405 hfs_end_transaction(hfsmp);
55e303ae 1406 }
b4c24cb9
A
1407
1408 FREE(iterator, M_TEMP);
55e303ae 1409 hfsmp->hfs_flags |= HFS_CLEANED_ORPHANS;
b4c24cb9
A
1410}
1411
9bccf70c
A
1412
1413/*
1414 * This will return the correct logical block size for a given vnode.
1415 * For most files, it is the allocation block size, for meta data like
1416 * BTrees, this is kept as part of the BTree private nodeSize
1417 */
1418u_int32_t
1419GetLogicalBlockSize(struct vnode *vp)
1c79356b 1420{
9bccf70c 1421u_int32_t logBlockSize;
1c79356b 1422
9bccf70c 1423 DBG_ASSERT(vp != NULL);
1c79356b 1424
9bccf70c
A
1425 /* start with default */
1426 logBlockSize = VTOHFS(vp)->hfs_logBlockSize;
1c79356b 1427
91447636 1428 if (vnode_issystem(vp)) {
9bccf70c 1429 if (VTOF(vp)->fcbBTCBPtr != NULL) {
0b4e3aa0
A
1430 BTreeInfoRec bTreeInfo;
1431
1432 /*
1433 * We do not lock the BTrees, because if we are getting block..then the tree
1434 * should be locked in the first place.
1435 * We just want the nodeSize wich will NEVER change..so even if the world
1436 * is changing..the nodeSize should remain the same. Which argues why lock
1437 * it in the first place??
1438 */
1439
9bccf70c 1440 (void) BTGetInformation (VTOF(vp), kBTreeInfoVersion, &bTreeInfo);
0b4e3aa0
A
1441
1442 logBlockSize = bTreeInfo.nodeSize;
1443
9bccf70c 1444 } else if (VTOC(vp)->c_fileid == kHFSAllocationFileID) {
0b4e3aa0 1445 logBlockSize = VTOVCB(vp)->vcbVBMIOSize;
1c79356b 1446 }
0b4e3aa0
A
1447 }
1448
1c79356b
A
1449 DBG_ASSERT(logBlockSize > 0);
1450
1451 return logBlockSize;
1452}
1453
9bccf70c
A
1454__private_extern__
1455u_int32_t
1456hfs_freeblks(struct hfsmount * hfsmp, int wantreserve)
1457{
9bccf70c 1458 u_int32_t freeblks;
2d21ac55
A
1459 u_int32_t rsrvblks;
1460 u_int32_t loanblks;
9bccf70c 1461
2d21ac55
A
1462 /*
1463 * We don't bother taking the mount lock
1464 * to look at these values since the values
1465 * themselves are each updated automically
1466 * on aligned addresses.
1467 */
1468 freeblks = hfsmp->freeBlocks;
1469 rsrvblks = hfsmp->reserveBlocks;
1470 loanblks = hfsmp->loanedBlocks;
9bccf70c 1471 if (wantreserve) {
2d21ac55
A
1472 if (freeblks > rsrvblks)
1473 freeblks -= rsrvblks;
9bccf70c
A
1474 else
1475 freeblks = 0;
1476 }
2d21ac55
A
1477 if (freeblks > loanblks)
1478 freeblks -= loanblks;
55e303ae
A
1479 else
1480 freeblks = 0;
1481
1482#ifdef HFS_SPARSE_DEV
1483 /*
1484 * When the underlying device is sparse, check the
1485 * available space on the backing store volume.
1486 */
1487 if ((hfsmp->hfs_flags & HFS_HAS_SPARSE_DEVICE) && hfsmp->hfs_backingfs_rootvp) {
91447636 1488 struct vfsstatfs *vfsp; /* 272 bytes */
2d21ac55 1489 u_int64_t vfreeblks;
55e303ae
A
1490 u_int32_t loanedblks;
1491 struct mount * backingfs_mp;
2d21ac55 1492 struct timeval now;
55e303ae 1493
91447636 1494 backingfs_mp = vnode_mount(hfsmp->hfs_backingfs_rootvp);
55e303ae 1495
2d21ac55
A
1496 microtime(&now);
1497 if ((now.tv_sec - hfsmp->hfs_last_backingstatfs) >= 1) {
1498 vfs_update_vfsstat(backingfs_mp, vfs_context_kernel(), VFS_KERNEL_EVENT);
1499 hfsmp->hfs_last_backingstatfs = now.tv_sec;
1500 }
1501
1502 if ((vfsp = vfs_statfs(backingfs_mp))) {
91447636 1503 HFS_MOUNT_LOCK(hfsmp, TRUE);
2d21ac55 1504 vfreeblks = vfsp->f_bavail;
55e303ae 1505 /* Normalize block count if needed. */
2d21ac55
A
1506 if (vfsp->f_bsize != hfsmp->blockSize) {
1507 vfreeblks = ((u_int64_t)vfreeblks * (u_int64_t)(vfsp->f_bsize)) / hfsmp->blockSize;
55e303ae 1508 }
2d21ac55 1509 if (vfreeblks > (unsigned int)hfsmp->hfs_sparsebandblks)
55e303ae
A
1510 vfreeblks -= hfsmp->hfs_sparsebandblks;
1511 else
1512 vfreeblks = 0;
1513
1514 /* Take into account any delayed allocations. */
2d21ac55 1515 loanedblks = 2 * hfsmp->loanedBlocks;
55e303ae
A
1516 if (vfreeblks > loanedblks)
1517 vfreeblks -= loanedblks;
1518 else
1519 vfreeblks = 0;
1520
1521 freeblks = MIN(vfreeblks, freeblks);
91447636 1522 HFS_MOUNT_UNLOCK(hfsmp, TRUE);
55e303ae
A
1523 }
1524 }
1525#endif /* HFS_SPARSE_DEV */
9bccf70c 1526
9bccf70c
A
1527 return (freeblks);
1528}
1529
1c79356b
A
1530/*
1531 * Map HFS Common errors (negative) to BSD error codes (positive).
1532 * Positive errors (ie BSD errors) are passed through unchanged.
1533 */
1534short MacToVFSError(OSErr err)
1535{
9bccf70c
A
1536 if (err >= 0)
1537 return err;
1c79356b 1538
1c79356b 1539 switch (err) {
9bccf70c 1540 case dskFulErr: /* -34 */
55e303ae 1541 case btNoSpaceAvail: /* -32733 */
b4c24cb9 1542 return ENOSPC;
9bccf70c 1543 case fxOvFlErr: /* -32750 */
b4c24cb9 1544 return EOVERFLOW;
9bccf70c
A
1545
1546 case btBadNode: /* -32731 */
3a60a9f5 1547 return EIO;
9bccf70c
A
1548
1549 case memFullErr: /* -108 */
1550 return ENOMEM; /* +12 */
1551
1552 case cmExists: /* -32718 */
1553 case btExists: /* -32734 */
1554 return EEXIST; /* +17 */
1555
1556 case cmNotFound: /* -32719 */
1557 case btNotFound: /* -32735 */
1558 return ENOENT; /* 28 */
1559
1560 case cmNotEmpty: /* -32717 */
1561 return ENOTEMPTY; /* 66 */
1562
1563 case cmFThdDirErr: /* -32714 */
1564 return EISDIR; /* 21 */
1565
1566 case fxRangeErr: /* -32751 */
b4c24cb9 1567 return ERANGE;
9bccf70c
A
1568
1569 case bdNamErr: /* -37 */
1570 return ENAMETOOLONG; /* 63 */
1571
1572 case paramErr: /* -50 */
1573 case fileBoundsErr: /* -1309 */
1574 return EINVAL; /* +22 */
1575
1576 case fsBTBadNodeSize:
d52fe63f 1577 return ENXIO;
9bccf70c
A
1578
1579 default:
1580 return EIO; /* +5 */
1c79356b
A
1581 }
1582}
1583
1584
1585/*
91447636
A
1586 * Find the current thread's directory hint for a given index.
1587 *
1588 * Requires an exclusive lock on directory cnode.
2d21ac55
A
1589 *
1590 * Use detach if the cnode lock must be dropped while the hint is still active.
1c79356b 1591 */
9bccf70c 1592__private_extern__
91447636 1593directoryhint_t *
2d21ac55 1594hfs_getdirhint(struct cnode *dcp, int index, int detach)
1c79356b 1595{
91447636 1596 struct timeval tv;
b36670ce
A
1597 directoryhint_t *hint;
1598 boolean_t need_remove, need_init;
2d21ac55 1599 const u_int8_t * name;
91447636 1600
91447636
A
1601 microuptime(&tv);
1602
b36670ce
A
1603 /*
1604 * Look for an existing hint first. If not found, create a new one (when
1605 * the list is not full) or recycle the oldest hint. Since new hints are
1606 * always added to the head of the list, the last hint is always the
1607 * oldest.
1608 */
1609 TAILQ_FOREACH(hint, &dcp->c_hintlist, dh_link) {
1610 if (hint->dh_index == index)
1611 break;
9bccf70c 1612 }
b36670ce
A
1613 if (hint != NULL) { /* found an existing hint */
1614 need_init = false;
1615 need_remove = true;
1616 } else { /* cannot find an existing hint */
1617 need_init = true;
1618 if (dcp->c_dirhintcnt < HFS_MAXDIRHINTS) { /* we don't need recycling */
1619 /* Create a default directory hint */
1620 MALLOC_ZONE(hint, directoryhint_t *, sizeof(directoryhint_t), M_HFSDIRHINT, M_WAITOK);
1621 ++dcp->c_dirhintcnt;
1622 need_remove = false;
1623 } else { /* recycle the last (i.e., the oldest) hint */
1624 hint = TAILQ_LAST(&dcp->c_hintlist, hfs_hinthead);
2d21ac55
A
1625 if ((hint->dh_desc.cd_flags & CD_HASBUF) &&
1626 (name = hint->dh_desc.cd_nameptr)) {
b36670ce 1627 hint->dh_desc.cd_nameptr = NULL;
2d21ac55
A
1628 hint->dh_desc.cd_namelen = 0;
1629 hint->dh_desc.cd_flags &= ~CD_HASBUF;
1630 vfs_removename((const char *)name);
b36670ce
A
1631 }
1632 need_remove = true;
91447636 1633 }
9bccf70c 1634 }
91447636 1635
b36670ce
A
1636 if (need_remove)
1637 TAILQ_REMOVE(&dcp->c_hintlist, hint, dh_link);
1638
2d21ac55
A
1639 if (detach)
1640 --dcp->c_dirhintcnt;
1641 else
1642 TAILQ_INSERT_HEAD(&dcp->c_hintlist, hint, dh_link);
b36670ce
A
1643
1644 if (need_init) {
1645 hint->dh_index = index;
1646 hint->dh_desc.cd_flags = 0;
1647 hint->dh_desc.cd_encoding = 0;
1648 hint->dh_desc.cd_namelen = 0;
1649 hint->dh_desc.cd_nameptr = NULL;
2d21ac55 1650 hint->dh_desc.cd_parentcnid = dcp->c_fileid;
b36670ce
A
1651 hint->dh_desc.cd_hint = dcp->c_childhint;
1652 hint->dh_desc.cd_cnid = 0;
1653 }
91447636
A
1654 hint->dh_time = tv.tv_sec;
1655 return (hint);
1c79356b
A
1656}
1657
9bccf70c 1658/*
91447636
A
1659 * Release a single directory hint.
1660 *
1661 * Requires an exclusive lock on directory cnode.
9bccf70c
A
1662 */
1663__private_extern__
1664void
91447636 1665hfs_reldirhint(struct cnode *dcp, directoryhint_t * relhint)
9bccf70c 1666{
2d21ac55
A
1667 const u_int8_t * name;
1668 directoryhint_t *hint;
91447636 1669
2d21ac55
A
1670 /* Check if item is on list (could be detached) */
1671 TAILQ_FOREACH(hint, &dcp->c_hintlist, dh_link) {
1672 if (hint == relhint) {
1673 TAILQ_REMOVE(&dcp->c_hintlist, relhint, dh_link);
1674 --dcp->c_dirhintcnt;
1675 break;
1676 }
1677 }
b36670ce 1678 name = relhint->dh_desc.cd_nameptr;
2d21ac55 1679 if ((relhint->dh_desc.cd_flags & CD_HASBUF) && (name != NULL)) {
b36670ce 1680 relhint->dh_desc.cd_nameptr = NULL;
2d21ac55
A
1681 relhint->dh_desc.cd_namelen = 0;
1682 relhint->dh_desc.cd_flags &= ~CD_HASBUF;
1683 vfs_removename((const char *)name);
1c79356b 1684 }
b36670ce 1685 FREE_ZONE(relhint, sizeof(directoryhint_t), M_HFSDIRHINT);
1c79356b
A
1686}
1687
9bccf70c 1688/*
91447636
A
1689 * Release directory hints for given directory
1690 *
1691 * Requires an exclusive lock on directory cnode.
9bccf70c
A
1692 */
1693__private_extern__
1694void
91447636 1695hfs_reldirhints(struct cnode *dcp, int stale_hints_only)
9bccf70c 1696{
91447636 1697 struct timeval tv;
b36670ce 1698 directoryhint_t *hint, *prev;
2d21ac55 1699 const u_int8_t * name;
91447636
A
1700
1701 if (stale_hints_only)
1702 microuptime(&tv);
b36670ce
A
1703
1704 /* searching from the oldest to the newest, so we can stop early when releasing stale hints only */
1705 for (hint = TAILQ_LAST(&dcp->c_hintlist, hfs_hinthead); hint != NULL; hint = prev) {
1706 if (stale_hints_only && (tv.tv_sec - hint->dh_time) < HFS_DIRHINT_TTL)
1707 break; /* stop here if this entry is too new */
91447636 1708 name = hint->dh_desc.cd_nameptr;
2d21ac55 1709 if ((hint->dh_desc.cd_flags & CD_HASBUF) && (name != NULL)) {
91447636 1710 hint->dh_desc.cd_nameptr = NULL;
2d21ac55
A
1711 hint->dh_desc.cd_namelen = 0;
1712 hint->dh_desc.cd_flags &= ~CD_HASBUF;
1713 vfs_removename((const char *)name);
1c79356b 1714 }
b36670ce
A
1715 prev = TAILQ_PREV(hint, hfs_hinthead, dh_link); /* must save this pointer before calling FREE_ZONE on this node */
1716 TAILQ_REMOVE(&dcp->c_hintlist, hint, dh_link);
91447636
A
1717 FREE_ZONE(hint, sizeof(directoryhint_t), M_HFSDIRHINT);
1718 --dcp->c_dirhintcnt;
1c79356b 1719 }
9bccf70c 1720}
1c79356b 1721
2d21ac55
A
1722/*
1723 * Insert a detached directory hint back into the list of dirhints.
1724 *
1725 * Requires an exclusive lock on directory cnode.
1726 */
1727__private_extern__
1728void
1729hfs_insertdirhint(struct cnode *dcp, directoryhint_t * hint)
1730{
1731 directoryhint_t *test;
1732
1733 TAILQ_FOREACH(test, &dcp->c_hintlist, dh_link) {
1734 if (test == hint)
1735 panic("hfs_insertdirhint: hint %p already on list!", hint);
1736 }
1737
1738 TAILQ_INSERT_HEAD(&dcp->c_hintlist, hint, dh_link);
1739 ++dcp->c_dirhintcnt;
1740}
1c79356b 1741
d7e50217
A
1742/*
1743 * Perform a case-insensitive compare of two UTF-8 filenames.
1744 *
1745 * Returns 0 if the strings match.
1746 */
1747__private_extern__
1748int
2d21ac55 1749hfs_namecmp(const u_int8_t *str1, size_t len1, const u_int8_t *str2, size_t len2)
d7e50217
A
1750{
1751 u_int16_t *ustr1, *ustr2;
1752 size_t ulen1, ulen2;
1753 size_t maxbytes;
1754 int cmp = -1;
1755
1756 if (len1 != len2)
1757 return (cmp);
1758
1759 maxbytes = kHFSPlusMaxFileNameChars << 1;
1760 MALLOC(ustr1, u_int16_t *, maxbytes << 1, M_TEMP, M_WAITOK);
1761 ustr2 = ustr1 + (maxbytes >> 1);
1762
1763 if (utf8_decodestr(str1, len1, ustr1, &ulen1, maxbytes, ':', 0) != 0)
1764 goto out;
1765 if (utf8_decodestr(str2, len2, ustr2, &ulen2, maxbytes, ':', 0) != 0)
1766 goto out;
1767
1768 cmp = FastUnicodeCompare(ustr1, ulen1>>1, ustr2, ulen2>>1);
1769out:
1770 FREE(ustr1, M_TEMP);
1771 return (cmp);
1772}
1773
1774
b4c24cb9
A
1775__private_extern__
1776int
1777hfs_early_journal_init(struct hfsmount *hfsmp, HFSPlusVolumeHeader *vhp,
91447636
A
1778 void *_args, off_t embeddedOffset, daddr64_t mdb_offset,
1779 HFSMasterDirectoryBlock *mdbp, kauth_cred_t cred)
b4c24cb9
A
1780{
1781 JournalInfoBlock *jibp;
1782 struct buf *jinfo_bp, *bp;
1783 int sectors_per_fsblock, arg_flags=0, arg_tbufsz=0;
593a1d5f
A
1784 int retval;
1785 uint32_t blksize = hfsmp->hfs_logical_block_size;
b4c24cb9
A
1786 struct vnode *devvp;
1787 struct hfs_mount_args *args = _args;
2d21ac55
A
1788 u_int32_t jib_flags;
1789 u_int64_t jib_offset;
1790 u_int64_t jib_size;
1791
b4c24cb9
A
1792 devvp = hfsmp->hfs_devvp;
1793
1794 if (args != NULL && (args->flags & HFSFSMNT_EXTENDED_ARGS)) {
1795 arg_flags = args->journal_flags;
1796 arg_tbufsz = args->journal_tbuffer_size;
1797 }
1798
1799 sectors_per_fsblock = SWAP_BE32(vhp->blockSize) / blksize;
1800
91447636
A
1801 retval = (int)buf_meta_bread(devvp,
1802 (daddr64_t)((embeddedOffset/blksize) +
1803 (SWAP_BE32(vhp->journalInfoBlock)*sectors_per_fsblock)),
b4c24cb9
A
1804 SWAP_BE32(vhp->blockSize), cred, &jinfo_bp);
1805 if (retval)
1806 return retval;
1807
91447636 1808 jibp = (JournalInfoBlock *)buf_dataptr(jinfo_bp);
2d21ac55
A
1809 jib_flags = SWAP_BE32(jibp->flags);
1810 jib_offset = SWAP_BE64(jibp->offset);
1811 jib_size = SWAP_BE64(jibp->size);
b4c24cb9 1812
2d21ac55 1813 if (jib_flags & kJIJournalInFSMask) {
b4c24cb9
A
1814 hfsmp->jvp = hfsmp->hfs_devvp;
1815 } else {
1816 printf("hfs: journal not stored in fs! don't know what to do.\n");
91447636 1817 buf_brelse(jinfo_bp);
b4c24cb9
A
1818 return EINVAL;
1819 }
1820
1821 // save this off for the hack-y check in hfs_remove()
2d21ac55
A
1822 hfsmp->jnl_start = jib_offset / SWAP_BE32(vhp->blockSize);
1823 hfsmp->jnl_size = jib_size;
b4c24cb9 1824
743b1565
A
1825 if ((hfsmp->hfs_flags & HFS_READ_ONLY) && (vfs_flags(hfsmp->hfs_mp) & MNT_ROOTFS) == 0) {
1826 // if the file system is read-only, check if the journal is empty.
1827 // if it is, then we can allow the mount. otherwise we have to
1828 // return failure.
1829 retval = journal_is_clean(hfsmp->jvp,
2d21ac55
A
1830 jib_offset + embeddedOffset,
1831 jib_size,
743b1565 1832 devvp,
593a1d5f 1833 hfsmp->hfs_logical_block_size);
743b1565
A
1834
1835 hfsmp->jnl = NULL;
1836
1837 buf_brelse(jinfo_bp);
1838
1839 if (retval) {
1840 printf("hfs: early journal init: volume on %s is read-only and journal is dirty. Can not mount volume.\n",
1841 vnode_name(devvp));
1842 }
1843
1844 return retval;
1845 }
1846
2d21ac55 1847 if (jib_flags & kJIJournalNeedInitMask) {
b4c24cb9 1848 printf("hfs: Initializing the journal (joffset 0x%llx sz 0x%llx)...\n",
2d21ac55 1849 jib_offset + embeddedOffset, jib_size);
b4c24cb9 1850 hfsmp->jnl = journal_create(hfsmp->jvp,
2d21ac55
A
1851 jib_offset + embeddedOffset,
1852 jib_size,
b4c24cb9
A
1853 devvp,
1854 blksize,
1855 arg_flags,
1856 arg_tbufsz,
1857 hfs_sync_metadata, hfsmp->hfs_mp);
1858
1859 // no need to start a transaction here... if this were to fail
1860 // we'd just re-init it on the next mount.
2d21ac55
A
1861 jib_flags &= ~kJIJournalNeedInitMask;
1862 jibp->flags = SWAP_BE32(jib_flags);
91447636 1863 buf_bwrite(jinfo_bp);
b4c24cb9
A
1864 jinfo_bp = NULL;
1865 jibp = NULL;
1866 } else {
1867 //printf("hfs: Opening the journal (joffset 0x%llx sz 0x%llx vhp_blksize %d)...\n",
2d21ac55
A
1868 // jib_offset + embeddedOffset,
1869 // jib_size, SWAP_BE32(vhp->blockSize));
b4c24cb9
A
1870
1871 hfsmp->jnl = journal_open(hfsmp->jvp,
2d21ac55
A
1872 jib_offset + embeddedOffset,
1873 jib_size,
b4c24cb9
A
1874 devvp,
1875 blksize,
1876 arg_flags,
1877 arg_tbufsz,
1878 hfs_sync_metadata, hfsmp->hfs_mp);
1879
91447636 1880 buf_brelse(jinfo_bp);
b4c24cb9
A
1881 jinfo_bp = NULL;
1882 jibp = NULL;
1883
1884 if (hfsmp->jnl && mdbp) {
1885 // reload the mdb because it could have changed
1886 // if the journal had to be replayed.
55e303ae 1887 if (mdb_offset == 0) {
91447636 1888 mdb_offset = (daddr64_t)((embeddedOffset / blksize) + HFS_PRI_SECTOR(blksize));
55e303ae 1889 }
593a1d5f
A
1890 retval = (int)buf_meta_bread(devvp,
1891 HFS_PHYSBLK_ROUNDDOWN(mdb_offset, hfsmp->hfs_log_per_phys),
1892 hfsmp->hfs_physical_block_size, cred, &bp);
b4c24cb9 1893 if (retval) {
91447636 1894 buf_brelse(bp);
b4c24cb9
A
1895 printf("hfs: failed to reload the mdb after opening the journal (retval %d)!\n",
1896 retval);
1897 return retval;
1898 }
593a1d5f 1899 bcopy((char *)buf_dataptr(bp) + HFS_PRI_OFFSET(hfsmp->hfs_physical_block_size), mdbp, 512);
91447636 1900 buf_brelse(bp);
b4c24cb9
A
1901 bp = NULL;
1902 }
1903 }
1904
1905
1906 //printf("journal @ 0x%x\n", hfsmp->jnl);
1907
1908 // if we expected the journal to be there and we couldn't
1909 // create it or open it then we have to bail out.
1910 if (hfsmp->jnl == NULL) {
55e303ae 1911 printf("hfs: early jnl init: failed to open/create the journal (retval %d).\n", retval);
b4c24cb9
A
1912 return EINVAL;
1913 }
1c79356b 1914
b4c24cb9
A
1915 return 0;
1916}
1917
1918
1919//
1920// This function will go and re-locate the .journal_info_block and
1921// the .journal files in case they moved (which can happen if you
1922// run Norton SpeedDisk). If we fail to find either file we just
1923// disable journaling for this volume and return. We turn off the
1924// journaling bit in the vcb and assume it will get written to disk
1925// later (if it doesn't on the next mount we'd do the same thing
1926// again which is harmless). If we disable journaling we don't
1927// return an error so that the volume is still mountable.
1928//
1929// If the info we find for the .journal_info_block and .journal files
1930// isn't what we had stored, we re-set our cached info and proceed
1931// with opening the journal normally.
1932//
1933static int
1934hfs_late_journal_init(struct hfsmount *hfsmp, HFSPlusVolumeHeader *vhp, void *_args)
1935{
1936 JournalInfoBlock *jibp;
2d21ac55 1937 struct buf *jinfo_bp;
b4c24cb9 1938 int sectors_per_fsblock, arg_flags=0, arg_tbufsz=0;
2d21ac55 1939 int retval, write_jibp = 0, recreate_journal = 0;
b4c24cb9
A
1940 struct vnode *devvp;
1941 struct cat_attr jib_attr, jattr;
1942 struct cat_fork jib_fork, jfork;
1943 ExtendedVCB *vcb;
1944 u_long fid;
1945 struct hfs_mount_args *args = _args;
2d21ac55
A
1946 u_int32_t jib_flags;
1947 u_int64_t jib_offset;
1948 u_int64_t jib_size;
b4c24cb9
A
1949
1950 devvp = hfsmp->hfs_devvp;
1951 vcb = HFSTOVCB(hfsmp);
1952
1953 if (args != NULL && (args->flags & HFSFSMNT_EXTENDED_ARGS)) {
1954 if (args->journal_disable) {
1955 return 0;
1956 }
1957
1958 arg_flags = args->journal_flags;
1959 arg_tbufsz = args->journal_tbuffer_size;
1960 }
1961
1962 fid = GetFileInfo(vcb, kRootDirID, ".journal_info_block", &jib_attr, &jib_fork);
1963 if (fid == 0 || jib_fork.cf_extents[0].startBlock == 0 || jib_fork.cf_size == 0) {
1964 printf("hfs: can't find the .journal_info_block! disabling journaling (start: %d).\n",
1965 jib_fork.cf_extents[0].startBlock);
1966 vcb->vcbAtrb &= ~kHFSVolumeJournaledMask;
1967 return 0;
1968 }
1969 hfsmp->hfs_jnlinfoblkid = fid;
1970
1971 // make sure the journal_info_block begins where we think it should.
1972 if (SWAP_BE32(vhp->journalInfoBlock) != jib_fork.cf_extents[0].startBlock) {
1973 printf("hfs: The journal_info_block moved (was: %d; is: %d). Fixing up\n",
1974 SWAP_BE32(vhp->journalInfoBlock), jib_fork.cf_extents[0].startBlock);
1975
1976 vcb->vcbJinfoBlock = jib_fork.cf_extents[0].startBlock;
1977 vhp->journalInfoBlock = SWAP_BE32(jib_fork.cf_extents[0].startBlock);
2d21ac55 1978 recreate_journal = 1;
b4c24cb9
A
1979 }
1980
1981
593a1d5f 1982 sectors_per_fsblock = SWAP_BE32(vhp->blockSize) / hfsmp->hfs_logical_block_size;
91447636 1983 retval = (int)buf_meta_bread(devvp,
593a1d5f 1984 (daddr64_t)(vcb->hfsPlusIOPosOffset / hfsmp->hfs_logical_block_size +
91447636 1985 (SWAP_BE32(vhp->journalInfoBlock)*sectors_per_fsblock)),
b4c24cb9
A
1986 SWAP_BE32(vhp->blockSize), NOCRED, &jinfo_bp);
1987 if (retval) {
1988 printf("hfs: can't read journal info block. disabling journaling.\n");
1989 vcb->vcbAtrb &= ~kHFSVolumeJournaledMask;
1990 return 0;
1991 }
1992
91447636 1993 jibp = (JournalInfoBlock *)buf_dataptr(jinfo_bp);
2d21ac55
A
1994 jib_flags = SWAP_BE32(jibp->flags);
1995 jib_offset = SWAP_BE64(jibp->offset);
1996 jib_size = SWAP_BE64(jibp->size);
b4c24cb9
A
1997
1998 fid = GetFileInfo(vcb, kRootDirID, ".journal", &jattr, &jfork);
1999 if (fid == 0 || jfork.cf_extents[0].startBlock == 0 || jfork.cf_size == 0) {
2000 printf("hfs: can't find the journal file! disabling journaling (start: %d)\n",
2001 jfork.cf_extents[0].startBlock);
91447636 2002 buf_brelse(jinfo_bp);
b4c24cb9
A
2003 vcb->vcbAtrb &= ~kHFSVolumeJournaledMask;
2004 return 0;
2005 }
2006 hfsmp->hfs_jnlfileid = fid;
2007
2008 // make sure the journal file begins where we think it should.
2d21ac55 2009 if ((jib_offset / (u_int64_t)vcb->blockSize) != jfork.cf_extents[0].startBlock) {
b4c24cb9 2010 printf("hfs: The journal file moved (was: %lld; is: %d). Fixing up\n",
2d21ac55 2011 (jib_offset / (u_int64_t)vcb->blockSize), jfork.cf_extents[0].startBlock);
b4c24cb9 2012
2d21ac55 2013 jib_offset = (u_int64_t)jfork.cf_extents[0].startBlock * (u_int64_t)vcb->blockSize;
b4c24cb9 2014 write_jibp = 1;
2d21ac55 2015 recreate_journal = 1;
b4c24cb9
A
2016 }
2017
2018 // check the size of the journal file.
2d21ac55 2019 if (jib_size != (u_int64_t)jfork.cf_extents[0].blockCount*vcb->blockSize) {
b4c24cb9 2020 printf("hfs: The journal file changed size! (was %lld; is %lld). Fixing up.\n",
2d21ac55 2021 jib_size, (u_int64_t)jfork.cf_extents[0].blockCount*vcb->blockSize);
b4c24cb9 2022
2d21ac55 2023 jib_size = (u_int64_t)jfork.cf_extents[0].blockCount * vcb->blockSize;
b4c24cb9 2024 write_jibp = 1;
2d21ac55 2025 recreate_journal = 1;
b4c24cb9
A
2026 }
2027
2d21ac55 2028 if (jib_flags & kJIJournalInFSMask) {
b4c24cb9
A
2029 hfsmp->jvp = hfsmp->hfs_devvp;
2030 } else {
2031 printf("hfs: journal not stored in fs! don't know what to do.\n");
91447636 2032 buf_brelse(jinfo_bp);
b4c24cb9
A
2033 return EINVAL;
2034 }
2035
2036 // save this off for the hack-y check in hfs_remove()
2d21ac55
A
2037 hfsmp->jnl_start = jib_offset / SWAP_BE32(vhp->blockSize);
2038 hfsmp->jnl_size = jib_size;
b4c24cb9 2039
743b1565
A
2040 if ((hfsmp->hfs_flags & HFS_READ_ONLY) && (vfs_flags(hfsmp->hfs_mp) & MNT_ROOTFS) == 0) {
2041 // if the file system is read-only, check if the journal is empty.
2042 // if it is, then we can allow the mount. otherwise we have to
2043 // return failure.
2044 retval = journal_is_clean(hfsmp->jvp,
2d21ac55
A
2045 jib_offset + (off_t)vcb->hfsPlusIOPosOffset,
2046 jib_size,
743b1565 2047 devvp,
593a1d5f 2048 hfsmp->hfs_logical_block_size);
743b1565
A
2049
2050 hfsmp->jnl = NULL;
2051
2052 buf_brelse(jinfo_bp);
2053
2054 if (retval) {
2055 printf("hfs: late journal init: volume on %s is read-only and journal is dirty. Can not mount volume.\n",
2056 vnode_name(devvp));
2057 }
2058
2059 return retval;
2060 }
2061
2d21ac55 2062 if ((jib_flags & kJIJournalNeedInitMask) || recreate_journal) {
b4c24cb9 2063 printf("hfs: Initializing the journal (joffset 0x%llx sz 0x%llx)...\n",
2d21ac55 2064 jib_offset + (off_t)vcb->hfsPlusIOPosOffset, jib_size);
b4c24cb9 2065 hfsmp->jnl = journal_create(hfsmp->jvp,
2d21ac55
A
2066 jib_offset + (off_t)vcb->hfsPlusIOPosOffset,
2067 jib_size,
b4c24cb9 2068 devvp,
593a1d5f 2069 hfsmp->hfs_logical_block_size,
b4c24cb9
A
2070 arg_flags,
2071 arg_tbufsz,
2072 hfs_sync_metadata, hfsmp->hfs_mp);
2073
2074 // no need to start a transaction here... if this were to fail
2075 // we'd just re-init it on the next mount.
2d21ac55 2076 jib_flags &= ~kJIJournalNeedInitMask;
b4c24cb9
A
2077 write_jibp = 1;
2078
2079 } else {
2080 //
2081 // if we weren't the last person to mount this volume
2082 // then we need to throw away the journal because it
2083 // is likely that someone else mucked with the disk.
2084 // if the journal is empty this is no big deal. if the
2085 // disk is dirty this prevents us from replaying the
2086 // journal over top of changes that someone else made.
2087 //
2088 arg_flags |= JOURNAL_RESET;
2089
2090 //printf("hfs: Opening the journal (joffset 0x%llx sz 0x%llx vhp_blksize %d)...\n",
2d21ac55
A
2091 // jib_offset + (off_t)vcb->hfsPlusIOPosOffset,
2092 // jib_size, SWAP_BE32(vhp->blockSize));
b4c24cb9
A
2093
2094 hfsmp->jnl = journal_open(hfsmp->jvp,
2d21ac55
A
2095 jib_offset + (off_t)vcb->hfsPlusIOPosOffset,
2096 jib_size,
b4c24cb9 2097 devvp,
593a1d5f 2098 hfsmp->hfs_logical_block_size,
b4c24cb9
A
2099 arg_flags,
2100 arg_tbufsz,
2101 hfs_sync_metadata, hfsmp->hfs_mp);
2102 }
2103
2104
2105 if (write_jibp) {
2d21ac55
A
2106 jibp->flags = SWAP_BE32(jib_flags);
2107 jibp->offset = SWAP_BE64(jib_offset);
2108 jibp->size = SWAP_BE64(jib_size);
b4c24cb9 2109
91447636 2110 buf_bwrite(jinfo_bp);
b4c24cb9 2111 } else {
91447636 2112 buf_brelse(jinfo_bp);
b4c24cb9
A
2113 }
2114 jinfo_bp = NULL;
2115 jibp = NULL;
2116
2117 //printf("journal @ 0x%x\n", hfsmp->jnl);
2118
2119 // if we expected the journal to be there and we couldn't
2120 // create it or open it then we have to bail out.
2121 if (hfsmp->jnl == NULL) {
55e303ae 2122 printf("hfs: late jnl init: failed to open/create the journal (retval %d).\n", retval);
b4c24cb9
A
2123 return EINVAL;
2124 }
2125
2126 return 0;
2127}
55e303ae
A
2128
2129/*
2130 * Calculate the allocation zone for metadata.
2131 *
2132 * This zone includes the following:
2133 * Allocation Bitmap file
2134 * Overflow Extents file
2135 * Journal file
2136 * Quota files
2137 * Clustered Hot files
2138 * Catalog file
2139 *
2140 * METADATA ALLOCATION ZONE
2141 * ____________________________________________________________________________
2142 * | | | | | | |
2143 * | BM | JF | OEF | CATALOG |---> | HOT FILES |
2144 * |____|____|_____|_______________|______________________________|___________|
2145 *
2146 * <------------------------------- N * 128 MB ------------------------------->
2147 *
2148 */
2149#define GIGABYTE (u_int64_t)(1024*1024*1024)
2150
2151#define OVERFLOW_DEFAULT_SIZE (4*1024*1024)
2152#define OVERFLOW_MAXIMUM_SIZE (128*1024*1024)
2153#define JOURNAL_DEFAULT_SIZE (8*1024*1024)
2154#define JOURNAL_MAXIMUM_SIZE (512*1024*1024)
2155#define HOTBAND_MINIMUM_SIZE (10*1024*1024)
2156#define HOTBAND_MAXIMUM_SIZE (512*1024*1024)
2157
2158static void
2159hfs_metadatazone_init(struct hfsmount *hfsmp)
2160{
2161 ExtendedVCB *vcb;
55e303ae
A
2162 u_int64_t fs_size;
2163 u_int64_t zonesize;
2164 u_int64_t temp;
2165 u_int64_t filesize;
2166 u_int32_t blk;
2167 int items;
2168
2169 vcb = HFSTOVCB(hfsmp);
2170 fs_size = (u_int64_t)vcb->blockSize * (u_int64_t)vcb->totalBlocks;
2171
2172 /*
2173 * For volumes less than 10 GB, don't bother.
2174 */
2175 if (fs_size < ((u_int64_t)10 * GIGABYTE))
2176 return;
2177 /*
2178 * Skip non-journaled volumes as well.
2179 */
2180 if (hfsmp->jnl == NULL)
2181 return;
2182
2183 /*
2184 * Start with allocation bitmap (a fixed size).
2185 */
2186 zonesize = roundup(vcb->totalBlocks / 8, vcb->vcbVBMIOSize);
2187
2188 /*
2189 * Overflow Extents file gets 4 MB per 100 GB.
2190 */
2191 items = fs_size / ((u_int64_t)100 * GIGABYTE);
2192 filesize = (u_int64_t)(items + 1) * OVERFLOW_DEFAULT_SIZE;
2193 if (filesize > OVERFLOW_MAXIMUM_SIZE)
2194 filesize = OVERFLOW_MAXIMUM_SIZE;
2195 zonesize += filesize;
2196 hfsmp->hfs_overflow_maxblks = filesize / vcb->blockSize;
2197
2198 /*
2199 * Plan for at least 8 MB of journal for each
2200 * 100 GB of disk space (up to a 512 MB).
2201 */
2202 items = fs_size / ((u_int64_t)100 * GIGABYTE);
2203 filesize = (u_int64_t)(items + 1) * JOURNAL_DEFAULT_SIZE;
2204 if (filesize > JOURNAL_MAXIMUM_SIZE)
2205 filesize = JOURNAL_MAXIMUM_SIZE;
2206 zonesize += filesize;
2207
2208 /*
2209 * Catalog file gets 10 MB per 1 GB.
2210 *
2211 * How about considering the current catalog size (used nodes * node size)
2212 * and the current file data size to help estimate the required
2213 * catalog size.
2214 */
2215 filesize = MIN((fs_size / 1024) * 10, GIGABYTE);
2216 hfsmp->hfs_catalog_maxblks = filesize / vcb->blockSize;
2217 zonesize += filesize;
2218
2219 /*
2220 * Add space for hot file region.
2221 *
2222 * ...for now, use 5 MB per 1 GB (0.5 %)
2223 */
2224 filesize = (fs_size / 1024) * 5;
2225 if (filesize > HOTBAND_MAXIMUM_SIZE)
2226 filesize = HOTBAND_MAXIMUM_SIZE;
2227 else if (filesize < HOTBAND_MINIMUM_SIZE)
2228 filesize = HOTBAND_MINIMUM_SIZE;
2229 /*
2230 * Calculate user quota file requirements.
2231 */
2232 items = QF_USERS_PER_GB * (fs_size / GIGABYTE);
2233 if (items < QF_MIN_USERS)
2234 items = QF_MIN_USERS;
2235 else if (items > QF_MAX_USERS)
2236 items = QF_MAX_USERS;
2237 if (!powerof2(items)) {
2238 int x = items;
2239 items = 4;
2240 while (x>>1 != 1) {
2241 x = x >> 1;
2242 items = items << 1;
2243 }
2244 }
2245 filesize += (items + 1) * sizeof(struct dqblk);
2246 /*
2247 * Calculate group quota file requirements.
2248 *
2249 */
2250 items = QF_GROUPS_PER_GB * (fs_size / GIGABYTE);
2251 if (items < QF_MIN_GROUPS)
2252 items = QF_MIN_GROUPS;
2253 else if (items > QF_MAX_GROUPS)
2254 items = QF_MAX_GROUPS;
2255 if (!powerof2(items)) {
2256 int x = items;
2257 items = 4;
2258 while (x>>1 != 1) {
2259 x = x >> 1;
2260 items = items << 1;
2261 }
2262 }
2263 filesize += (items + 1) * sizeof(struct dqblk);
55e303ae
A
2264 zonesize += filesize;
2265
2266 /*
2267 * Round up entire zone to a bitmap block's worth.
2268 * The extra space goes to the catalog file and hot file area.
2269 */
2270 temp = zonesize;
6601e61a 2271 zonesize = roundup(zonesize, (u_int64_t)vcb->vcbVBMIOSize * 8 * vcb->blockSize);
55e303ae
A
2272 temp = zonesize - temp; /* temp has extra space */
2273 filesize += temp / 3;
2274 hfsmp->hfs_catalog_maxblks += (temp - (temp / 3)) / vcb->blockSize;
2275
91447636
A
2276 hfsmp->hfs_hotfile_maxblks = filesize / vcb->blockSize;
2277
55e303ae
A
2278 /* Convert to allocation blocks. */
2279 blk = zonesize / vcb->blockSize;
2280
2281 /* The default metadata zone location is at the start of volume. */
2282 hfsmp->hfs_metazone_start = 1;
2283 hfsmp->hfs_metazone_end = blk - 1;
2284
2285 /* The default hotfile area is at the end of the zone. */
2286 hfsmp->hfs_hotfile_start = blk - (filesize / vcb->blockSize);
2287 hfsmp->hfs_hotfile_end = hfsmp->hfs_metazone_end;
2288 hfsmp->hfs_hotfile_freeblks = hfs_hotfile_freeblocks(hfsmp);
2289#if 0
2290 printf("HFS: metadata zone is %d to %d\n", hfsmp->hfs_metazone_start, hfsmp->hfs_metazone_end);
2291 printf("HFS: hot file band is %d to %d\n", hfsmp->hfs_hotfile_start, hfsmp->hfs_hotfile_end);
2292 printf("HFS: hot file band free blocks = %d\n", hfsmp->hfs_hotfile_freeblks);
2293#endif
2294 hfsmp->hfs_flags |= HFS_METADATA_ZONE;
2295}
2296
2297
2298static u_int32_t
2299hfs_hotfile_freeblocks(struct hfsmount *hfsmp)
2300{
2301 ExtendedVCB *vcb = HFSTOVCB(hfsmp);
91447636 2302 int lockflags;
55e303ae
A
2303 int freeblocks;
2304
91447636 2305 lockflags = hfs_systemfile_lock(hfsmp, SFL_BITMAP, HFS_EXCLUSIVE_LOCK);
55e303ae 2306 freeblocks = MetaZoneFreeBlocks(vcb);
91447636
A
2307 hfs_systemfile_unlock(hfsmp, lockflags);
2308
55e303ae
A
2309 /* Minus Extents overflow file reserve. */
2310 freeblocks -=
91447636 2311 hfsmp->hfs_overflow_maxblks - VTOF(hfsmp->hfs_extents_vp)->ff_blocks;
55e303ae
A
2312 /* Minus catalog file reserve. */
2313 freeblocks -=
91447636 2314 hfsmp->hfs_catalog_maxblks - VTOF(hfsmp->hfs_catalog_vp)->ff_blocks;
55e303ae
A
2315 if (freeblocks < 0)
2316 freeblocks = 0;
2317
2318 return MIN(freeblocks, hfsmp->hfs_hotfile_maxblks);
2319}
2320
2321/*
2322 * Determine if a file is a "virtual" metadata file.
2323 * This includes journal and quota files.
2324 */
2325__private_extern__
2326int
2327hfs_virtualmetafile(struct cnode *cp)
2328{
2d21ac55 2329 const char * filename;
55e303ae
A
2330
2331
2332 if (cp->c_parentcnid != kHFSRootFolderID)
2333 return (0);
2334
2d21ac55 2335 filename = (const char *)cp->c_desc.cd_nameptr;
55e303ae
A
2336 if (filename == NULL)
2337 return (0);
2338
2d21ac55
A
2339 if ((strncmp(filename, ".journal", sizeof(".journal")) == 0) ||
2340 (strncmp(filename, ".journal_info_block", sizeof(".journal_info_block")) == 0) ||
2341 (strncmp(filename, ".quota.user", sizeof(".quota.user")) == 0) ||
2342 (strncmp(filename, ".quota.group", sizeof(".quota.group")) == 0) ||
2343 (strncmp(filename, ".hotfiles.btree", sizeof(".hotfiles.btree")) == 0))
55e303ae
A
2344 return (1);
2345
2346 return (0);
2347}
2348
91447636 2349
e2fac8b1
A
2350
2351//
2352// Fire off a timed callback to sync the disk if the
2353// volume is on ejectable media.
2354//
2355 __private_extern__
2356void
2357hfs_sync_ejectable(struct hfsmount *hfsmp)
2358{
2359 if (hfsmp->hfs_syncer) {
2360 uint32_t secs, usecs;
2361 uint64_t now;
2362
2363 clock_get_calendar_microtime(&secs, &usecs);
2364 now = ((uint64_t)secs * 1000000) + usecs;
2365
2366 if (hfsmp->hfs_sync_scheduled == 0) {
2367 uint64_t deadline;
2368
2369 hfsmp->hfs_last_sync_request_time = now;
2370
2371 clock_interval_to_deadline(HFS_META_DELAY, HFS_MILLISEC_SCALE, &deadline);
2372
2373 /*
2374 * Increment hfs_sync_scheduled on the assumption that we're the
2375 * first thread to schedule the timer. If some other thread beat
2376 * us, then we'll decrement it. If we *were* the first to
2377 * schedule the timer, then we need to keep track that the
2378 * callback is waiting to complete.
2379 */
2380 OSIncrementAtomic((volatile SInt32 *)&hfsmp->hfs_sync_scheduled);
2381 if (thread_call_enter_delayed(hfsmp->hfs_syncer, deadline))
2382 OSDecrementAtomic((volatile SInt32 *)&hfsmp->hfs_sync_scheduled);
2383 else
2384 OSIncrementAtomic((volatile SInt32 *)&hfsmp->hfs_sync_incomplete);
2385 }
2386 }
2387}
2388
2389
91447636
A
2390__private_extern__
2391int
2392hfs_start_transaction(struct hfsmount *hfsmp)
2393{
2d21ac55
A
2394 int ret, unlock_on_err=0;
2395 void * thread = current_thread();
2396
2397#ifdef HFS_CHECK_LOCK_ORDER
2398 /*
2399 * You cannot start a transaction while holding a system
2400 * file lock. (unless the transaction is nested.)
2401 */
2402 if (hfsmp->jnl && journal_owner(hfsmp->jnl) != thread) {
2403 if (hfsmp->hfs_catalog_cp && hfsmp->hfs_catalog_cp->c_lockowner == thread) {
2404 panic("hfs_start_transaction: bad lock order (cat before jnl)\n");
2405 }
2406 if (hfsmp->hfs_attribute_cp && hfsmp->hfs_attribute_cp->c_lockowner == thread) {
2407 panic("hfs_start_transaction: bad lock order (attr before jnl)\n");
2408 }
2409 if (hfsmp->hfs_extents_cp && hfsmp->hfs_extents_cp->c_lockowner == thread) {
2410 panic("hfs_start_transaction: bad lock order (ext before jnl)\n");
2411 }
2412 }
2413#endif /* HFS_CHECK_LOCK_ORDER */
91447636 2414
2d21ac55 2415 if (hfsmp->jnl == NULL || journal_owner(hfsmp->jnl) != thread) {
91447636 2416 lck_rw_lock_shared(&hfsmp->hfs_global_lock);
e2fac8b1 2417 OSAddAtomic(1, (SInt32 *)&hfsmp->hfs_active_threads);
2d21ac55 2418 unlock_on_err = 1;
91447636
A
2419 }
2420
c910b4d9
A
2421 /* If a downgrade to read-only mount is in progress, no other
2422 * process than the downgrade process is allowed to modify
2423 * the file system.
2424 */
2425 if ((hfsmp->hfs_flags & HFS_RDONLY_DOWNGRADE) &&
2426 (hfsmp->hfs_downgrading_proc != thread)) {
2427 ret = EROFS;
2428 goto out;
2429 }
2430
91447636
A
2431 if (hfsmp->jnl) {
2432 ret = journal_start_transaction(hfsmp->jnl);
2433 if (ret == 0) {
2d21ac55 2434 OSAddAtomic(1, (SInt32 *)&hfsmp->hfs_global_lock_nesting);
91447636
A
2435 }
2436 } else {
2437 ret = 0;
2438 }
2439
c910b4d9 2440out:
2d21ac55
A
2441 if (ret != 0 && unlock_on_err) {
2442 lck_rw_unlock_shared(&hfsmp->hfs_global_lock);
e2fac8b1 2443 OSAddAtomic(-1, (SInt32 *)&hfsmp->hfs_active_threads);
91447636
A
2444 }
2445
2446 return ret;
2447}
2448
2449__private_extern__
2450int
2451hfs_end_transaction(struct hfsmount *hfsmp)
2452{
2453 int need_unlock=0, ret;
2454
2455 if ( hfsmp->jnl == NULL
2456 || ( journal_owner(hfsmp->jnl) == current_thread()
2d21ac55 2457 && (OSAddAtomic(-1, (SInt32 *)&hfsmp->hfs_global_lock_nesting) == 1)) ) {
91447636
A
2458
2459 need_unlock = 1;
2460 }
2461
2462 if (hfsmp->jnl) {
2463 ret = journal_end_transaction(hfsmp->jnl);
2464 } else {
2465 ret = 0;
2466 }
2467
2468 if (need_unlock) {
e2fac8b1 2469 OSAddAtomic(-1, (SInt32 *)&hfsmp->hfs_active_threads);
2d21ac55 2470 lck_rw_unlock_shared(&hfsmp->hfs_global_lock);
e2fac8b1 2471 hfs_sync_ejectable(hfsmp);
91447636
A
2472 }
2473
2474 return ret;
2475}