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