]> git.saurik.com Git - apple/xnu.git/blob - bsd/hfs/hfs_btreeio.c
xnu-1228.0.2.tar.gz
[apple/xnu.git] / bsd / hfs / hfs_btreeio.c
1 /*
2 * Copyright (c) 2000-2007 Apple 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
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/buf.h>
32 #include <sys/buf_internal.h>
33 #include <sys/kernel.h>
34 #include <sys/malloc.h>
35 #include <sys/mount.h>
36 #include <sys/vnode.h>
37
38
39 #include "hfs.h"
40 #include "hfs_cnode.h"
41 #include "hfs_dbg.h"
42 #include "hfs_endian.h"
43 #include "hfs_btreeio.h"
44
45 #include "hfscommon/headers/FileMgrInternal.h"
46 #include "hfscommon/headers/BTreesPrivate.h"
47
48 #define FORCESYNCBTREEWRITES 0
49
50 /* From bsd/vfs/vfs_bio.c */
51 extern int bdwrite_internal(struct buf *, int);
52
53 static int ClearBTNodes(struct vnode *vp, long blksize, off_t offset, off_t amount);
54 static int btree_journal_modify_block_end(struct hfsmount *hfsmp, struct buf *bp);
55
56
57 __private_extern__
58 OSStatus SetBTreeBlockSize(FileReference vp, ByteCount blockSize, __unused ItemCount minBlockCount)
59 {
60 BTreeControlBlockPtr bTreePtr;
61
62 DBG_ASSERT(vp != NULL);
63 DBG_ASSERT(blockSize >= kMinNodeSize);
64 if (blockSize > MAXBSIZE )
65 return (fsBTBadNodeSize);
66
67 bTreePtr = (BTreeControlBlockPtr)VTOF(vp)->fcbBTCBPtr;
68 bTreePtr->nodeSize = blockSize;
69
70 return (E_NONE);
71 }
72
73
74 __private_extern__
75 OSStatus GetBTreeBlock(FileReference vp, u_int32_t blockNum, GetBlockOptions options, BlockDescriptor *block)
76 {
77 OSStatus retval = E_NONE;
78 struct buf *bp = NULL;
79
80 if (options & kGetEmptyBlock) {
81 daddr64_t blkno;
82 off_t offset;
83
84 offset = (daddr64_t)blockNum * (daddr64_t)block->blockSize;
85 bp = buf_getblk(vp, (daddr64_t)blockNum, block->blockSize, 0, 0, BLK_META);
86 if (bp &&
87 VNOP_BLOCKMAP(vp, offset, block->blockSize, &blkno, NULL, NULL, 0, NULL) == 0) {
88 buf_setblkno(bp, blkno);
89 }
90 } else {
91 retval = buf_meta_bread(vp, (daddr64_t)blockNum, block->blockSize, NOCRED, &bp);
92 }
93 if (bp == NULL)
94 retval = -1; //XXX need better error
95
96 if (retval == E_NONE) {
97 block->blockHeader = bp;
98 block->buffer = (char *)buf_dataptr(bp);
99 block->blockNum = buf_lblkno(bp);
100 block->blockReadFromDisk = (buf_fromcache(bp) == 0); /* not found in cache ==> came from disk */
101
102 // XXXdbg
103 block->isModified = 0;
104
105 /* Check and endian swap B-Tree node (only if it's a valid block) */
106 if (!(options & kGetEmptyBlock)) {
107 /* This happens when we first open the b-tree, we might not have all the node data on hand */
108 if ((((BTNodeDescriptor *)block->buffer)->kind == kBTHeaderNode) &&
109 (((BTHeaderRec *)((char *)block->buffer + 14))->nodeSize != buf_count(bp)) &&
110 (SWAP_BE16 (((BTHeaderRec *)((char *)block->buffer + 14))->nodeSize) != buf_count(bp))) {
111
112 /*
113 * Don't swap the node descriptor, record offsets, or other records.
114 * This record will be invalidated and re-read with the correct node
115 * size once the B-tree control block is set up with the node size
116 * from the header record.
117 */
118 retval = hfs_swap_BTNode (block, vp, kSwapBTNodeHeaderRecordOnly);
119
120 } else if (block->blockReadFromDisk) {
121 /*
122 * The node was just read from disk, so always swap/check it.
123 * This is necessary on big endian since the test below won't trigger.
124 */
125 retval = hfs_swap_BTNode (block, vp, kSwapBTNodeBigToHost);
126 } else if (*((u_int16_t *)((char *)block->buffer + (block->blockSize - sizeof (u_int16_t)))) == 0x0e00) {
127 /*
128 * The node was left in the cache in non-native order, so swap it.
129 * This only happens on little endian, after the node is written
130 * back to disk.
131 */
132 retval = hfs_swap_BTNode (block, vp, kSwapBTNodeBigToHost);
133 }
134
135 /*
136 * If we got an error, then the node is only partially swapped.
137 * We mark the buffer invalid so that the next attempt to get the
138 * node will read it and attempt to swap again, and will notice
139 * the error again. If we didn't do this, the next attempt to get
140 * the node might use the partially swapped node as-is.
141 */
142 if (retval)
143 buf_markinvalid(bp);
144 }
145 }
146
147 if (retval) {
148 if (bp)
149 buf_brelse(bp);
150 block->blockHeader = NULL;
151 block->buffer = NULL;
152 }
153
154 return (retval);
155 }
156
157
158 __private_extern__
159 void ModifyBlockStart(FileReference vp, BlockDescPtr blockPtr)
160 {
161 struct hfsmount *hfsmp = VTOHFS(vp);
162 struct buf *bp = NULL;
163
164 if (hfsmp->jnl == NULL) {
165 return;
166 }
167
168 bp = (struct buf *) blockPtr->blockHeader;
169 if (bp == NULL) {
170 panic("ModifyBlockStart: null bp for blockdescptr %p?!?\n", blockPtr);
171 return;
172 }
173
174 journal_modify_block_start(hfsmp->jnl, bp);
175 blockPtr->isModified = 1;
176 }
177
178 static void
179 btree_swap_node(struct buf *bp, __unused void *arg)
180 {
181 // struct hfsmount *hfsmp = (struct hfsmount *)arg;
182 int retval;
183 struct vnode *vp = buf_vnode(bp);
184 BlockDescriptor block;
185
186 /* Prepare the block pointer */
187 block.blockHeader = bp;
188 block.buffer = (char *)buf_dataptr(bp);
189 block.blockNum = buf_lblkno(bp);
190 /* not found in cache ==> came from disk */
191 block.blockReadFromDisk = (buf_fromcache(bp) == 0);
192 block.blockSize = buf_count(bp);
193
194 // swap the data now that this node is ready to go to disk
195 retval = hfs_swap_BTNode (&block, vp, kSwapBTNodeHostToBig);
196 if (retval)
197 panic("btree_swap_node: about to write corrupt node!\n");
198 }
199
200
201 static int
202 btree_journal_modify_block_end(struct hfsmount *hfsmp, struct buf *bp)
203 {
204 return journal_modify_block_end(hfsmp->jnl, bp, btree_swap_node, hfsmp);
205 }
206
207
208 __private_extern__
209 OSStatus ReleaseBTreeBlock(FileReference vp, BlockDescPtr blockPtr, ReleaseBlockOptions options)
210 {
211 struct hfsmount *hfsmp = VTOHFS(vp);
212 OSStatus retval = E_NONE;
213 struct buf *bp = NULL;
214
215 bp = (struct buf *) blockPtr->blockHeader;
216
217 if (bp == NULL) {
218 retval = -1;
219 goto exit;
220 }
221
222 if (options & kTrashBlock) {
223 buf_markinvalid(bp);
224
225 if (hfsmp->jnl && (buf_flags(bp) & B_LOCKED)) {
226 journal_kill_block(hfsmp->jnl, bp);
227 } else {
228 buf_brelse(bp); /* note: B-tree code will clear blockPtr->blockHeader and blockPtr->buffer */
229 }
230 } else {
231 if (options & kForceWriteBlock) {
232 if (hfsmp->jnl) {
233 if (blockPtr->isModified == 0) {
234 panic("hfs: releaseblock: modified is 0 but forcewrite set! bp %p\n", bp);
235 }
236
237 retval = btree_journal_modify_block_end(hfsmp, bp);
238 blockPtr->isModified = 0;
239 } else {
240 retval = VNOP_BWRITE(bp);
241 }
242 } else if (options & kMarkBlockDirty) {
243 struct timeval tv;
244 microuptime(&tv);
245 if ((options & kLockTransaction) && hfsmp->jnl == NULL) {
246 /*
247 *
248 * Set the B_LOCKED flag and unlock the buffer, causing buf_brelse to move
249 * the buffer onto the LOCKED free list. This is necessary, otherwise
250 * getnewbuf() would try to reclaim the buffers using buf_bawrite, which
251 * isn't going to work.
252 *
253 */
254 /* Don't hog all the buffers... */
255 if (count_lock_queue() > kMaxLockedMetaBuffers) {
256 hfs_btsync(vp, HFS_SYNCTRANS);
257 /* Rollback sync time to cause a sync on lock release... */
258 (void) BTSetLastSync(VTOF(vp), tv.tv_sec - (kMaxSecsForFsync + 1));
259 }
260 buf_setflags(bp, B_LOCKED);
261 }
262
263 /*
264 * Delay-write this block.
265 * If the maximum delayed buffers has been exceeded then
266 * free up some buffers and fall back to an asynchronous write.
267 */
268 if (hfsmp->jnl) {
269 if (blockPtr->isModified == 0) {
270 panic("hfs: releaseblock: modified is 0 but markdirty set! bp %p\n", bp);
271 }
272 retval = btree_journal_modify_block_end(hfsmp, bp);
273 blockPtr->isModified = 0;
274 } else if (bdwrite_internal(bp, 1) != 0) {
275 hfs_btsync(vp, 0);
276 /* Rollback sync time to cause a sync on lock release... */
277 (void) BTSetLastSync(VTOF(vp), tv.tv_sec - (kMaxSecsForFsync + 1));
278
279 buf_clearflags(bp, B_LOCKED);
280 buf_bawrite(bp);
281 }
282 } else {
283 // check if we had previously called journal_modify_block_start()
284 // on this block and if so, abort it (which will call buf_brelse()).
285 if (hfsmp->jnl && blockPtr->isModified) {
286 // XXXdbg - I don't want to call modify_block_abort()
287 // because I think it may be screwing up the
288 // journal and blowing away a block that has
289 // valid data in it.
290 //
291 // journal_modify_block_abort(hfsmp->jnl, bp);
292 //panic("hfs: releaseblock called for 0x%x but mod_block_start previously called.\n", bp);
293 btree_journal_modify_block_end(hfsmp, bp);
294 blockPtr->isModified = 0;
295 } else {
296 buf_brelse(bp); /* note: B-tree code will clear blockPtr->blockHeader and blockPtr->buffer */
297 }
298 };
299 };
300
301 exit:
302 return (retval);
303 }
304
305
306 __private_extern__
307 OSStatus ExtendBTreeFile(FileReference vp, FSSize minEOF, FSSize maxEOF)
308 {
309 #pragma unused (maxEOF)
310
311 OSStatus retval = 0, ret = 0;
312 int64_t actualBytesAdded, origSize;
313 u_int64_t bytesToAdd;
314 u_int32_t startAllocation;
315 u_int32_t fileblocks;
316 BTreeInfoRec btInfo;
317 ExtendedVCB *vcb;
318 FCB *filePtr;
319 struct proc *p = NULL;
320 int64_t trim = 0;
321 int lockflags = 0;
322
323 filePtr = GetFileControlBlock(vp);
324
325 if ( (off_t)minEOF > filePtr->fcbEOF )
326 {
327 bytesToAdd = minEOF - filePtr->fcbEOF;
328
329 if (bytesToAdd < filePtr->ff_clumpsize)
330 bytesToAdd = filePtr->ff_clumpsize; //XXX why not always be a mutiple of clump size?
331 }
332 else
333 {
334 return -1;
335 }
336
337 vcb = VTOVCB(vp);
338
339 /*
340 * The Extents B-tree can't have overflow extents. ExtendFileC will
341 * return an error if an attempt is made to extend the Extents B-tree
342 * when the resident extents are exhausted.
343 */
344
345 /* Protect allocation bitmap and extents overflow file. */
346 lockflags = SFL_BITMAP;
347 if (VTOC(vp)->c_fileid != kHFSExtentsFileID)
348 lockflags |= SFL_EXTENTS;
349 lockflags = hfs_systemfile_lock(vcb, lockflags, HFS_EXCLUSIVE_LOCK);
350
351 (void) BTGetInformation(filePtr, 0, &btInfo);
352
353 #if 0 // XXXdbg
354 /*
355 * The b-tree code expects nodes to be contiguous. So when
356 * the allocation block size is less than the b-tree node
357 * size, we need to force disk allocations to be contiguous.
358 */
359 if (vcb->blockSize >= btInfo.nodeSize) {
360 extendFlags = 0;
361 } else {
362 /* Ensure that all b-tree nodes are contiguous on disk */
363 extendFlags = kEFContigMask;
364 }
365 #endif
366
367 origSize = filePtr->fcbEOF;
368 fileblocks = filePtr->ff_blocks;
369 startAllocation = vcb->nextAllocation;
370
371 // loop trying to get a contiguous chunk that's an integer multiple
372 // of the btree node size. if we can't get a contiguous chunk that
373 // is at least the node size then we break out of the loop and let
374 // the error propagate back up.
375 while((off_t)bytesToAdd >= btInfo.nodeSize) {
376 do {
377 retval = ExtendFileC(vcb, filePtr, bytesToAdd, 0,
378 kEFContigMask | kEFMetadataMask | kEFNoClumpMask,
379 (int64_t *)&actualBytesAdded);
380 if (retval == dskFulErr && actualBytesAdded == 0) {
381 bytesToAdd >>= 1;
382 if (bytesToAdd < btInfo.nodeSize) {
383 break;
384 } else if ((bytesToAdd % btInfo.nodeSize) != 0) {
385 // make sure it's an integer multiple of the nodeSize
386 bytesToAdd -= (bytesToAdd % btInfo.nodeSize);
387 }
388 }
389 } while (retval == dskFulErr && actualBytesAdded == 0);
390
391 if (retval == dskFulErr && actualBytesAdded == 0 && bytesToAdd <= btInfo.nodeSize) {
392 break;
393 }
394
395 filePtr->fcbEOF = (u_int64_t)filePtr->ff_blocks * (u_int64_t)vcb->blockSize;
396 bytesToAdd = minEOF - filePtr->fcbEOF;
397 }
398
399 /*
400 * If a new extent was added then move the roving allocator
401 * reference forward by the current b-tree file size so
402 * there's plenty of room to grow.
403 */
404 if ((retval == 0) &&
405 ((VCBTOHFS(vcb)->hfs_flags & HFS_METADATA_ZONE) == 0) &&
406 (vcb->nextAllocation > startAllocation) &&
407 ((vcb->nextAllocation + fileblocks) < vcb->allocLimit)) {
408 HFS_UPDATE_NEXT_ALLOCATION(vcb, vcb->nextAllocation + fileblocks);
409 }
410
411 filePtr->fcbEOF = (u_int64_t)filePtr->ff_blocks * (u_int64_t)vcb->blockSize;
412
413 // XXXdbg ExtendFileC() could have returned an error even though
414 // it grew the file to be big enough for our needs. If this is
415 // the case, we don't care about retval so we blow it away.
416 //
417 if (filePtr->fcbEOF >= (off_t)minEOF && retval != 0) {
418 retval = 0;
419 }
420
421 // XXXdbg if the file grew but isn't large enough or isn't an
422 // even multiple of the nodeSize then trim things back. if
423 // the file isn't large enough we trim back to the original
424 // size. otherwise we trim back to be an even multiple of the
425 // btree node size.
426 //
427 if ((filePtr->fcbEOF < (off_t)minEOF) || ((filePtr->fcbEOF - origSize) % btInfo.nodeSize) != 0) {
428
429 if (filePtr->fcbEOF < (off_t)minEOF) {
430 retval = dskFulErr;
431
432 if (filePtr->fcbEOF < origSize) {
433 panic("hfs: btree file eof %lld less than orig size %lld!\n",
434 filePtr->fcbEOF, origSize);
435 }
436
437 trim = filePtr->fcbEOF - origSize;
438 } else {
439 trim = ((filePtr->fcbEOF - origSize) % btInfo.nodeSize);
440 }
441
442 ret = TruncateFileC(vcb, filePtr, filePtr->fcbEOF - trim, 0);
443 filePtr->fcbEOF = (u_int64_t)filePtr->ff_blocks * (u_int64_t)vcb->blockSize;
444
445 // XXXdbg - panic if the file didn't get trimmed back properly
446 if ((filePtr->fcbEOF % btInfo.nodeSize) != 0) {
447 panic("hfs: truncate file didn't! fcbEOF %lld nsize %d fcb %p\n",
448 filePtr->fcbEOF, btInfo.nodeSize, filePtr);
449 }
450
451 if (ret) {
452 // XXXdbg - this probably doesn't need to be a panic()
453 panic("hfs: error truncating btree files (sz 0x%llx, trim %lld, ret %ld)\n",
454 filePtr->fcbEOF, trim, ret);
455 goto out;
456 }
457 }
458
459 if(VTOC(vp)->c_fileid != kHFSExtentsFileID) {
460 /*
461 * Get any extents overflow b-tree changes to disk ASAP!
462 */
463 (void) BTFlushPath(VTOF(vcb->extentsRefNum));
464 (void) hfs_fsync(vcb->extentsRefNum, MNT_WAIT, 0, p);
465 }
466 hfs_systemfile_unlock(vcb, lockflags);
467 lockflags = 0;
468
469 if ((filePtr->fcbEOF % btInfo.nodeSize) != 0) {
470 panic("hfs: extendbtree: fcb %p has eof 0x%llx not a multiple of 0x%x (trim %llx)\n",
471 filePtr, filePtr->fcbEOF, btInfo.nodeSize, trim);
472 }
473
474 /*
475 * Update the Alternate MDB or Alternate VolumeHeader
476 */
477 if ((VTOC(vp)->c_fileid == kHFSExtentsFileID) ||
478 (VTOC(vp)->c_fileid == kHFSCatalogFileID) ||
479 (VTOC(vp)->c_fileid == kHFSAttributesFileID)
480 ) {
481 VTOC(vp)->c_flag |= C_MODIFIED;
482 MarkVCBDirty( vcb );
483 ret = hfs_flushvolumeheader(VCBTOHFS(vcb), MNT_WAIT, HFS_ALTFLUSH);
484 } else {
485 VTOC(vp)->c_touch_chgtime = TRUE;
486 VTOC(vp)->c_touch_modtime = TRUE;
487 (void) hfs_update(vp, TRUE);
488 }
489
490 ret = ClearBTNodes(vp, btInfo.nodeSize, origSize, (filePtr->fcbEOF - origSize));
491 out:
492 if (retval == 0)
493 retval = ret;
494
495 if (lockflags)
496 hfs_systemfile_unlock(vcb, lockflags);
497
498 return retval;
499 }
500
501
502 /*
503 * Clear out (zero) new b-tree nodes on disk.
504 */
505 static int
506 ClearBTNodes(struct vnode *vp, long blksize, off_t offset, off_t amount)
507 {
508 struct hfsmount *hfsmp = VTOHFS(vp);
509 struct buf *bp = NULL;
510 daddr64_t blk;
511 daddr64_t blkcnt;
512
513 blk = offset / blksize;
514 blkcnt = amount / blksize;
515
516 while (blkcnt > 0) {
517 bp = buf_getblk(vp, blk, blksize, 0, 0, BLK_META);
518 if (bp == NULL)
519 continue;
520
521 // XXXdbg
522 if (hfsmp->jnl) {
523 // XXXdbg -- skipping this for now since it makes a transaction
524 // become *way* too large
525 //journal_modify_block_start(hfsmp->jnl, bp);
526 }
527 bzero((char *)buf_dataptr(bp), blksize);
528
529 buf_markaged(bp);
530
531 // XXXdbg
532 if (hfsmp->jnl) {
533 // XXXdbg -- skipping this for now since it makes a transaction
534 // become *way* too large
535 //journal_modify_block_end(hfsmp->jnl, bp);
536
537 // XXXdbg - remove this once we decide what to do with the
538 // writes to the journal
539 if ((blk % 32) == 0)
540 VNOP_BWRITE(bp);
541 else
542 buf_bawrite(bp);
543 } else {
544 /* wait/yield every 32 blocks so we don't hog all the buffers */
545 if ((blk % 32) == 0)
546 VNOP_BWRITE(bp);
547 else
548 buf_bawrite(bp);
549 }
550 --blkcnt;
551 ++blk;
552 }
553
554 return (0);
555 }
556
557
558 extern char hfs_attrname[];
559
560 /*
561 * Create an HFS+ Attribute B-tree File.
562 *
563 * No global resources should be held.
564 */
565 int
566 hfs_create_attr_btree(struct hfsmount *hfsmp, u_int32_t nodesize, u_int32_t nodecnt)
567 {
568 struct vnode* vp = NULLVP;
569 struct cat_desc cndesc;
570 struct cat_attr cnattr;
571 struct cat_fork cfork;
572 BlockDescriptor blkdesc;
573 BTNodeDescriptor *ndp;
574 BTHeaderRec *bthp;
575 BTreeControlBlockPtr btcb = NULL;
576 struct buf *bp = NULL;
577 void * buffer;
578 u_int16_t *index;
579 u_int16_t offset;
580 int intrans = 0;
581 int result;
582 again:
583 /*
584 * Serialize creation using HFS_CREATING_BTREE flag.
585 */
586 lck_mtx_lock(&hfsmp->hfs_mutex);
587 if (hfsmp->hfs_flags & HFS_CREATING_BTREE) {
588 /* Someone else beat us, wait for them to finish. */
589 (void) msleep(hfsmp->hfs_attribute_cp, &hfsmp->hfs_mutex,
590 PDROP | PINOD, "hfs_create_attr_btree", 0);
591 if (hfsmp->hfs_attribute_vp) {
592 return (0);
593 }
594 goto again;
595 }
596 hfsmp->hfs_flags |= HFS_CREATING_BTREE;
597 lck_mtx_unlock(&hfsmp->hfs_mutex);
598
599 /* Check if were out of usable disk space. */
600 if ((hfs_freeblks(hfsmp, 1) == 0)) {
601 result = ENOSPC;
602 goto exit;
603 }
604
605 /*
606 * Set up Attribute B-tree vnode
607 * (this must be done before we start a transaction
608 * or take any system file locks)
609 */
610 bzero(&cndesc, sizeof(cndesc));
611 cndesc.cd_parentcnid = kHFSRootParentID;
612 cndesc.cd_flags |= CD_ISMETA;
613 cndesc.cd_nameptr = (const u_int8_t *)hfs_attrname;
614 cndesc.cd_namelen = strlen(hfs_attrname);
615 cndesc.cd_cnid = kHFSAttributesFileID;
616
617 bzero(&cnattr, sizeof(cnattr));
618 cnattr.ca_linkcount = 1;
619 cnattr.ca_mode = S_IFREG;
620 cnattr.ca_fileid = cndesc.cd_cnid;
621
622 bzero(&cfork, sizeof(cfork));
623 cfork.cf_clump = nodesize * nodecnt;
624
625 result = hfs_getnewvnode(hfsmp, NULL, NULL, &cndesc, 0, &cnattr, &cfork, &vp);
626 if (result) {
627 goto exit;
628 }
629 /*
630 * Set up Attribute B-tree control block
631 */
632 MALLOC(btcb, BTreeControlBlock *, sizeof(BTreeControlBlock), M_TEMP, M_WAITOK);
633 bzero(btcb, sizeof(BTreeControlBlock));
634
635 btcb->nodeSize = nodesize;
636 btcb->maxKeyLength = kHFSPlusAttrKeyMaximumLength;
637 btcb->btreeType = 0xFF;
638 btcb->attributes = kBTVariableIndexKeysMask | kBTBigKeysMask;
639 btcb->version = kBTreeVersion;
640 btcb->writeCount = 1;
641 btcb->flags = 0; /* kBTHeaderDirty */
642 btcb->fileRefNum = vp;
643 btcb->getBlockProc = GetBTreeBlock;
644 btcb->releaseBlockProc = ReleaseBTreeBlock;
645 btcb->setEndOfForkProc = ExtendBTreeFile;
646 btcb->keyCompareProc = (KeyCompareProcPtr)hfs_attrkeycompare;
647 VTOF(vp)->fcbBTCBPtr = btcb;
648
649 /*
650 * Allocate some space
651 */
652 if (hfs_start_transaction(hfsmp) != 0) {
653 result = EINVAL;
654 goto exit;
655 }
656 intrans = 1;
657
658 /* Note ExtendBTreeFile will acquire the necessary system file locks. */
659 result = ExtendBTreeFile(vp, nodesize, cfork.cf_clump);
660 if (result)
661 goto exit;
662
663 btcb->totalNodes = VTOF(vp)->ff_size / nodesize;
664 btcb->freeNodes = btcb->totalNodes - 1;
665
666 /*
667 * Initialize the b-tree header on disk
668 */
669 bp = buf_getblk(vp, 0, nodesize, 0, 0, BLK_META);
670 if (bp == NULL) {
671 result = EIO;
672 goto exit;
673 }
674
675 buffer = (void *)buf_dataptr(bp);
676 blkdesc.buffer = buffer;
677 blkdesc.blockHeader = (void *)bp;
678 blkdesc.blockReadFromDisk = 0;
679 blkdesc.isModified = 0;
680
681 ModifyBlockStart(vp, &blkdesc);
682
683 if (buf_size(bp) != nodesize)
684 panic("hfs_create_attr_btree: bad buffer size (%d)\n", buf_size(bp));
685
686 bzero(buffer, nodesize);
687 index = (u_int16_t *)buffer;
688
689 /* FILL IN THE NODE DESCRIPTOR: */
690 ndp = (BTNodeDescriptor *)buffer;
691 ndp->kind = kBTHeaderNode;
692 ndp->numRecords = 3;
693 offset = sizeof(BTNodeDescriptor);
694 index[(nodesize / 2) - 1] = offset;
695
696 /* FILL IN THE HEADER RECORD: */
697 bthp = (BTHeaderRec *)((u_int8_t *)buffer + offset);
698 bthp->nodeSize = nodesize;
699 bthp->totalNodes = btcb->totalNodes;
700 bthp->freeNodes = btcb->freeNodes;
701 bthp->clumpSize = cfork.cf_clump;
702 bthp->btreeType = 0xFF;
703 bthp->attributes = kBTVariableIndexKeysMask | kBTBigKeysMask;
704 bthp->maxKeyLength = kHFSPlusAttrKeyMaximumLength;
705 bthp->keyCompareType = kHFSBinaryCompare;
706 offset += sizeof(BTHeaderRec);
707 index[(nodesize / 2) - 2] = offset;
708
709 /* FILL IN THE USER RECORD: */
710 offset += kBTreeHeaderUserBytes;
711 index[(nodesize / 2) - 3] = offset;
712
713 /* FILL IN THE MAP RECORD (only one node in use). */
714 *((u_int8_t *)buffer + offset) = 0x80;
715 offset += nodesize - sizeof(BTNodeDescriptor) - sizeof(BTHeaderRec)
716 - kBTreeHeaderUserBytes - (4 * sizeof(int16_t));
717 index[(nodesize / 2) - 4] = offset;
718
719 if (hfsmp->jnl) {
720 result = btree_journal_modify_block_end(hfsmp, bp);
721 } else {
722 result = VNOP_BWRITE(bp);
723 }
724 if (result)
725 goto exit;
726
727 /* Update vp/cp for attribute btree */
728 lck_mtx_lock(&hfsmp->hfs_mutex);
729 hfsmp->hfs_attribute_cp = VTOC(vp);
730 hfsmp->hfs_attribute_vp = vp;
731 lck_mtx_unlock(&hfsmp->hfs_mutex);
732
733 (void) hfs_flushvolumeheader(hfsmp, MNT_WAIT, HFS_ALTFLUSH);
734 exit:
735 if (vp) {
736 hfs_unlock(VTOC(vp));
737 }
738 if (result) {
739 if (btcb) {
740 FREE (btcb, M_TEMP);
741 }
742 if (vp) {
743 vnode_put(vp);
744 }
745 /* XXX need to give back blocks ? */
746 }
747 if (intrans) {
748 hfs_end_transaction(hfsmp);
749 }
750
751 /*
752 * All done, clear HFS_CREATING_BTREE, and wake up any sleepers.
753 */
754 lck_mtx_lock(&hfsmp->hfs_mutex);
755 hfsmp->hfs_flags &= ~HFS_CREATING_BTREE;
756 wakeup((caddr_t)hfsmp->hfs_attribute_cp);
757 lck_mtx_unlock(&hfsmp->hfs_mutex);
758
759 return (result);
760 }
761