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