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