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