📄 bnode.c
字号:
case HFS_LOCK_NONE: while (bn->resrv) { hfs_sleep_on(&bn->rqueue); } bn->resrv = 1; ++bn->count; case HFS_LOCK_RESRV: while (bn->count > 1) { hfs_sleep_on(&bn->wqueue); } bn->lock = 1; break; } break; case HFS_LOCK_NONE: switch (bnr->lock_type) { default: goto bail; break; case HFS_LOCK_READ: /* This process was reading this node. If there is now exactly one other process using the node then hfs_wake_up() a (potentially nonexistent) waiting process. Note that I refer to "a" process since the reservation system ensures that only one process can get itself on the wait queue. */ if (bn->count == 2) { hfs_wake_up(&bn->wqueue); } break; case HFS_LOCK_WRITE: /* This process was modifying this node. Unlock the node and fall-through to the HFS_LOCK_RESRV case, since a 'reservation' is a prerequisite for HFS_LOCK_WRITE. */ bn->lock = 0; case HFS_LOCK_RESRV: /* This process had placed a 'reservation' on this node, indicating an intention to possibly modify the node. We can get to this spot directly (if the 'reservation' not converted to a HFS_LOCK_WRITE), or by falling through from the above case if the reservation was converted. Since HFS_LOCK_RESRV and HFS_LOCK_WRITE both block processes that want access (HFS_LOCK_RESRV blocks other processes that want reservations but allow HFS_LOCK_READ accesses, while HFS_LOCK_WRITE must have exclusive access and thus blocks both types) we hfs_wake_up() any processes that might be waiting for access. If multiple processes are waiting for a reservation then the magic of process scheduling will settle the dispute. */ bn->resrv = 0; hfs_wake_up(&bn->rqueue); break; } --bn->count; break; } bnr->lock_type = lock_type; return;bail: hfs_warn("hfs_bnode_lock: invalid lock change: %d->%d.\n", bnr->lock_type, lock_type); return;}/* * hfs_bnode_relse() * * Description: * This function is called when a process is done using a bnode. If * the proper conditions are met then we call hfs_bnode_delete() to remove * it from the cache. If it is not deleted then we update its state * to reflect one less process using it. * Input Variable(s): * struct hfs_bnode *bn: pointer to the (struct hfs_bnode) to release. * int lock_type: The type of lock held by the process releasing this node. * Output Variable(s): * NONE * Returns: * void * Preconditions: * 'bn' is NULL or points to a "valid" (struct hfs_bnode). * Postconditions: * If 'bn' meets the appropriate conditions (see below) then it is * kept in the cache and all fields are set to consistent values * which reflect one less process using the node than upon entry. * If 'bn' does not meet the conditions then it is deleted (see * hfs_bnode_delete() for postconditions). * In either case, if 'lock_type' is HFS_LOCK_WRITE * then the corresponding buffer is dirtied. */void hfs_bnode_relse(struct hfs_bnode_ref *bnr){ struct hfs_bnode *bn; if (!bnr || !(bn = bnr->bn)) { return; } /* We update the lock state of the node if it is still in use or if it is "sticky" (such as the B-tree head and root). Otherwise we just delete it. */ if ((bn->count > 1) || (waitqueue_active(&bn->rqueue)) || (bn->sticky != HFS_NOT_STICKY)) { hfs_bnode_lock(bnr, HFS_LOCK_NONE); } else { /* dirty buffer if we (might) have modified it */ if (bnr->lock_type == HFS_LOCK_WRITE) { hfs_bnode_commit(bn); } hfs_bnode_delete(bn); bnr->lock_type = HFS_LOCK_NONE; } bnr->bn = NULL;}/* * hfs_bnode_find() * * Description: * This function is called to obtain a bnode. The cache is * searched for the node. If it not found there it is added to * the cache by hfs_bnode_read(). There are two special cases node=0 * (the header node) and node='tree'->bthRoot (the root node), in * which the nodes are obtained from fields of 'tree' without * consulting or modifying the cache. * Input Variable(s): * struct hfs_tree *tree: pointer to the (struct hfs_btree) from * which to get a node. * int node: the node number to get from 'tree'. * int lock_type: The kind of access (HFS_LOCK_READ, or * HFS_LOCK_RESRV) to obtain to the node * Output Variable(s): * NONE * Returns: * (struct hfs_bnode_ref) Reference to the requested node. * Preconditions: * 'tree' points to a "valid" (struct hfs_btree). * Postconditions: * If 'node' refers to a valid node in 'tree' and 'lock_type' has * one of the values listed above and no I/O errors occur then the * value returned refers to a valid (struct hfs_bnode) corresponding * to the requested node with the requested access type. The node * is also added to the cache if not previously present and not the * root or header. * If the conditions given above are not met, the bnode in the * returned reference is NULL. */struct hfs_bnode_ref hfs_bnode_find(struct hfs_btree *tree, hfs_u32 node, int lock_type){ struct hfs_bnode *bn; struct hfs_bnode *empty = NULL; struct hfs_bnode_ref bnr; bnr.lock_type = HFS_LOCK_NONE; bnr.bn = NULL;#if defined(DEBUG_BNODES) || defined(DEBUG_ALL) hfs_warn("hfs_bnode_find: %c %d:%d\n", lock_type==HFS_LOCK_READ?'R': (lock_type==HFS_LOCK_RESRV?'V':'W'), (int)ntohl(tree->entry.cnid), node);#endif /* check special cases */ if (!node) { bn = &tree->head; goto return_it; } else if (node == tree->bthRoot) { bn = tree->root; goto return_it; } restart: /* look for the node in the cache. */ bn = bhash(tree, node); while (bn && (bn->magic == HFS_BNODE_MAGIC)) { if (bn->node == node) { goto found_it; } bn = bn->next; } if (!empty) {#if defined(DEBUG_BNODES) || defined(DEBUG_ALL) ++bnode_count;#endif if (HFS_NEW(empty)) { goto restart; } return bnr; } bn = empty; hfs_bnode_read(bn, tree, node, HFS_NOT_STICKY); goto return_it;found_it: /* check validity */ if (bn->magic != HFS_BNODE_MAGIC) { /* If we find a corrupt bnode then we return NULL. However, we don't try to remove it from the cache or release its resources since we have no idea what kind of trouble we could get into that way. */ hfs_warn("hfs_bnode_find: bnode cache is corrupt.\n"); return bnr; } if (empty) {#if defined(DEBUG_BNODES) || defined(DEBUG_ALL) --bnode_count;#endif HFS_DELETE(empty); } return_it: /* Wait our turn */ bnr.bn = bn; hfs_bnode_lock(&bnr, lock_type); /* Check for failure to read the node from disk */ if (!hfs_buffer_ok(bn->buf)) { hfs_bnode_relse(&bnr); }#if defined(DEBUG_BNODES) || defined(DEBUG_ALL) if (!bnr.bn) { hfs_warn("hfs_bnode_find: failed\n"); } else { hfs_warn("hfs_bnode_find: use %d(%d) lvl %d [%d]\n", bn->count, bn->buf->b_count, bn->ndNHeight, bnode_count); hfs_warn("hfs_bnode_find: blnk %u flnk %u recs %u\n", bn->ndBLink, bn->ndFLink, bn->ndNRecs); }#endif return bnr;}/* * hfs_bnode_commit() * * Called to write a possibly dirty bnode back to disk. */void hfs_bnode_commit(struct hfs_bnode *bn){ if (hfs_buffer_ok(bn->buf)) { struct NodeDescriptor *nd; nd = (struct NodeDescriptor *)hfs_buffer_data(bn->buf); hfs_put_hl(bn->ndFLink, nd->ndFLink); hfs_put_hl(bn->ndBLink, nd->ndBLink); nd->ndType = bn->ndType; nd->ndNHeight = bn->ndNHeight; hfs_put_hs(bn->ndNRecs, nd->ndNRecs); hfs_buffer_dirty(bn->buf); /* increment write count */ hfs_mdb_dirty(bn->tree->sys_mdb); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -