+
+
+extern char hfs_attrname[];
+
+/*
+ * Create an HFS+ Attribute B-tree File.
+ *
+ * No global resources should be held.
+ */
+int
+hfs_create_attr_btree(struct hfsmount *hfsmp, u_int32_t nodesize, u_int32_t nodecnt)
+{
+ struct vnode* vp = NULLVP;
+ struct cat_desc cndesc;
+ struct cat_attr cnattr;
+ struct cat_fork cfork;
+ BlockDescriptor blkdesc;
+ BTNodeDescriptor *ndp;
+ BTHeaderRec *bthp;
+ BTreeControlBlockPtr btcb = NULL;
+ struct buf *bp = NULL;
+ void * buffer;
+ u_int8_t *bitmap;
+ u_int16_t *index;
+ u_int32_t node_num, num_map_nodes;
+ u_int32_t bytes_per_map_record;
+ u_int32_t temp;
+ u_int16_t offset;
+ int intrans = 0;
+ int result;
+ int newvnode_flags = 0;
+
+again:
+ /*
+ * Serialize creation using HFS_CREATING_BTREE flag.
+ */
+ hfs_lock_mount (hfsmp);
+ if (hfsmp->hfs_flags & HFS_CREATING_BTREE) {
+ /* Someone else beat us, wait for them to finish. */
+ (void) msleep(&hfsmp->hfs_attribute_cp, &hfsmp->hfs_mutex,
+ PDROP | PINOD, "hfs_create_attr_btree", 0);
+ if (hfsmp->hfs_attribute_vp) {
+ return (0);
+ }
+ goto again;
+ }
+ hfsmp->hfs_flags |= HFS_CREATING_BTREE;
+ hfs_unlock_mount (hfsmp);
+
+ /* Check if were out of usable disk space. */
+ if ((hfs_freeblks(hfsmp, 1) == 0)) {
+ result = ENOSPC;
+ goto exit;
+ }
+
+ /*
+ * Set up Attribute B-tree vnode
+ * (this must be done before we start a transaction
+ * or take any system file locks)
+ */
+ bzero(&cndesc, sizeof(cndesc));
+ cndesc.cd_parentcnid = kHFSRootParentID;
+ cndesc.cd_flags |= CD_ISMETA;
+ cndesc.cd_nameptr = (const u_int8_t *)hfs_attrname;
+ cndesc.cd_namelen = strlen(hfs_attrname);
+ cndesc.cd_cnid = kHFSAttributesFileID;
+
+ bzero(&cnattr, sizeof(cnattr));
+ cnattr.ca_linkcount = 1;
+ cnattr.ca_mode = S_IFREG;
+ cnattr.ca_fileid = cndesc.cd_cnid;
+
+ bzero(&cfork, sizeof(cfork));
+ cfork.cf_clump = nodesize * nodecnt;
+
+ result = hfs_getnewvnode(hfsmp, NULL, NULL, &cndesc, 0, &cnattr,
+ &cfork, &vp, &newvnode_flags);
+ if (result) {
+ goto exit;
+ }
+ /*
+ * Set up Attribute B-tree control block
+ */
+ MALLOC(btcb, BTreeControlBlock *, sizeof(BTreeControlBlock), M_TEMP, M_WAITOK);
+ bzero(btcb, sizeof(BTreeControlBlock));
+
+ btcb->nodeSize = nodesize;
+ btcb->maxKeyLength = kHFSPlusAttrKeyMaximumLength;
+ btcb->btreeType = 0xFF;
+ btcb->attributes = kBTVariableIndexKeysMask | kBTBigKeysMask;
+ btcb->version = kBTreeVersion;
+ btcb->writeCount = 1;
+ btcb->flags = 0; /* kBTHeaderDirty */
+ btcb->fileRefNum = vp;
+ btcb->getBlockProc = GetBTreeBlock;
+ btcb->releaseBlockProc = ReleaseBTreeBlock;
+ btcb->setEndOfForkProc = ExtendBTreeFile;
+ btcb->keyCompareProc = (KeyCompareProcPtr)hfs_attrkeycompare;
+ VTOF(vp)->fcbBTCBPtr = btcb;
+
+ /*
+ * Allocate some space
+ */
+ if (hfs_start_transaction(hfsmp) != 0) {
+ result = EINVAL;
+ goto exit;
+ }
+ intrans = 1;
+
+ /* Note ExtendBTreeFile will acquire the necessary system file locks. */
+ result = ExtendBTreeFile(vp, nodesize, cfork.cf_clump);
+ if (result)
+ goto exit;
+
+ btcb->totalNodes = VTOF(vp)->ff_size / nodesize;
+
+ /*
+ * Figure out how many map nodes we'll need.
+ *
+ * bytes_per_map_record = the number of bytes in the map record of a
+ * map node. Since that is the only record in the node, it is the size
+ * of the node minus the node descriptor at the start, and two record
+ * offsets at the end of the node. The "- 2" is to round the size down
+ * to a multiple of 4 bytes (since sizeof(BTNodeDescriptor) is not a
+ * multiple of 4).
+ *
+ * The value "temp" here is the number of *bits* in the map record of
+ * the header node.
+ */
+ bytes_per_map_record = nodesize - sizeof(BTNodeDescriptor) - 2*sizeof(u_int16_t) - 2;
+ temp = 8 * (nodesize - sizeof(BTNodeDescriptor)
+ - sizeof(BTHeaderRec)
+ - kBTreeHeaderUserBytes
+ - 4 * sizeof(u_int16_t));
+ if (btcb->totalNodes > temp) {
+ num_map_nodes = howmany(btcb->totalNodes - temp, bytes_per_map_record * 8);
+ }
+ else {
+ num_map_nodes = 0;
+ }
+
+ btcb->freeNodes = btcb->totalNodes - 1 - num_map_nodes;
+
+ /*
+ * Initialize the b-tree header on disk
+ */
+ bp = buf_getblk(vp, 0, nodesize, 0, 0, BLK_META);
+ if (bp == NULL) {
+ result = EIO;
+ goto exit;
+ }
+
+ buffer = (void *)buf_dataptr(bp);
+ blkdesc.buffer = buffer;
+ blkdesc.blockHeader = (void *)bp;
+ blkdesc.blockReadFromDisk = 0;
+ blkdesc.isModified = 0;
+
+ ModifyBlockStart(vp, &blkdesc);
+
+ if (buf_size(bp) != nodesize)
+ panic("hfs_create_attr_btree: bad buffer size (%d)\n", buf_size(bp));
+
+ bzero(buffer, nodesize);
+ index = (u_int16_t *)buffer;
+
+ /* FILL IN THE NODE DESCRIPTOR: */
+ ndp = (BTNodeDescriptor *)buffer;
+ if (num_map_nodes != 0)
+ ndp->fLink = 1;
+ ndp->kind = kBTHeaderNode;
+ ndp->numRecords = 3;
+ offset = sizeof(BTNodeDescriptor);
+ index[(nodesize / 2) - 1] = offset;
+
+ /* FILL IN THE HEADER RECORD: */
+ bthp = (BTHeaderRec *)((u_int8_t *)buffer + offset);
+ bthp->nodeSize = nodesize;
+ bthp->totalNodes = btcb->totalNodes;
+ bthp->freeNodes = btcb->freeNodes;
+ bthp->clumpSize = cfork.cf_clump;
+ bthp->btreeType = 0xFF;
+ bthp->attributes = kBTVariableIndexKeysMask | kBTBigKeysMask;
+ bthp->maxKeyLength = kHFSPlusAttrKeyMaximumLength;
+ bthp->keyCompareType = kHFSBinaryCompare;
+ offset += sizeof(BTHeaderRec);
+ index[(nodesize / 2) - 2] = offset;
+
+ /* FILL IN THE USER RECORD: */
+ offset += kBTreeHeaderUserBytes;
+ index[(nodesize / 2) - 3] = offset;
+
+ /* Mark the header node and map nodes in use in the map record.
+ *
+ * NOTE: Assumes that the header node's map record has at least
+ * (num_map_nodes + 1) bits.
+ */
+ bitmap = (u_int8_t *) buffer + offset;
+ temp = num_map_nodes + 1; /* +1 for the header node */
+ while (temp >= 8) {
+ *(bitmap++) = 0xFF;
+ temp -= 8;
+ }
+ *bitmap = ~(0xFF >> temp);
+
+ offset += nodesize - sizeof(BTNodeDescriptor) - sizeof(BTHeaderRec)
+ - kBTreeHeaderUserBytes - (4 * sizeof(int16_t));
+ index[(nodesize / 2) - 4] = offset;
+
+ if (hfsmp->jnl) {
+ result = btree_journal_modify_block_end(hfsmp, bp);
+ } else {
+ result = VNOP_BWRITE(bp);
+ }
+ if (result)
+ goto exit;
+
+ /* Create the map nodes: node numbers 1 .. num_map_nodes */
+ for (node_num=1; node_num <= num_map_nodes; ++node_num) {
+ bp = buf_getblk(vp, node_num, nodesize, 0, 0, BLK_META);
+ if (bp == NULL) {
+ result = EIO;
+ goto exit;
+ }
+ buffer = (void *)buf_dataptr(bp);
+ blkdesc.buffer = buffer;
+ blkdesc.blockHeader = (void *)bp;
+ blkdesc.blockReadFromDisk = 0;
+ blkdesc.isModified = 0;
+
+ ModifyBlockStart(vp, &blkdesc);
+
+ bzero(buffer, nodesize);
+ index = (u_int16_t *)buffer;
+
+ /* Fill in the node descriptor */
+ ndp = (BTNodeDescriptor *)buffer;
+ if (node_num != num_map_nodes)
+ ndp->fLink = node_num + 1;
+ ndp->kind = kBTMapNode;
+ ndp->numRecords = 1;
+ offset = sizeof(BTNodeDescriptor);
+ index[(nodesize / 2) - 1] = offset;
+
+
+ /* Fill in the map record's offset */
+ /* Note: We assume that the map record is all zeroes */
+ offset = sizeof(BTNodeDescriptor) + bytes_per_map_record;
+ index[(nodesize / 2) - 2] = offset;
+
+ if (hfsmp->jnl) {
+ result = btree_journal_modify_block_end(hfsmp, bp);
+ } else {
+ result = VNOP_BWRITE(bp);
+ }
+ if (result)
+ goto exit;
+ }
+
+ /* Update vp/cp for attribute btree */
+ hfs_lock_mount (hfsmp);
+ hfsmp->hfs_attribute_cp = VTOC(vp);
+ hfsmp->hfs_attribute_vp = vp;
+ hfs_unlock_mount (hfsmp);
+
+ (void) hfs_flushvolumeheader(hfsmp, MNT_WAIT, HFS_ALTFLUSH);
+
+ if (intrans) {
+ hfs_end_transaction(hfsmp);
+ intrans = 0;
+ }
+
+ /* Initialize the vnode for virtual attribute data file */
+ result = init_attrdata_vnode(hfsmp);
+ if (result) {
+ printf("hfs_create_attr_btree: vol=%s init_attrdata_vnode() error=%d\n", hfsmp->vcbVN, result);
+ }
+
+exit:
+ if (vp) {
+ hfs_unlock(VTOC(vp));
+ }
+ if (result) {
+ if (btcb) {
+ FREE (btcb, M_TEMP);
+ }
+ if (vp) {
+ vnode_put(vp);
+ }
+ /* XXX need to give back blocks ? */
+ }
+ if (intrans) {
+ hfs_end_transaction(hfsmp);
+ }
+
+ /*
+ * All done, clear HFS_CREATING_BTREE, and wake up any sleepers.
+ */
+ hfs_lock_mount (hfsmp);
+ hfsmp->hfs_flags &= ~HFS_CREATING_BTREE;
+ wakeup((caddr_t)&hfsmp->hfs_attribute_cp);
+ hfs_unlock_mount (hfsmp);
+
+ return (result);
+}
+