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