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