- retval = hfs_swap_BTNode (block, vp, kSwapBTNodeBigToHost);
- }
-
- /*
- * If we got an error, then the node is only partially swapped.
- * We mark the buffer invalid so that the next attempt to get the
- * node will read it and attempt to swap again, and will notice
- * the error again. If we didn't do this, the next attempt to get
- * the node might use the partially swapped node as-is.
- */
- if (retval)
+ retval = hfs_swap_BTNode (block, vp, kSwapBTNodeHeaderRecordOnly, allow_empty_node);
+
+ } else {
+ /*
+ * In this case, we have enough data in-hand to do basic validation
+ * on the B-Tree node.
+ */
+ if (block->blockReadFromDisk) {
+ /*
+ * The node was just read from disk, so always swap/check it.
+ * This is necessary on big endian since the test below won't trigger.
+ */
+ retval = hfs_swap_BTNode (block, vp, kSwapBTNodeBigToHost, allow_empty_node);
+ }
+ else {
+ /*
+ * Block wasn't read from disk; it was found in the cache.
+ */
+ if (*((u_int16_t *)((char *)block->buffer + (block->blockSize - sizeof (u_int16_t)))) == 0x0e00) {
+ /*
+ * The node was left in the cache in non-native order, so swap it.
+ * This only happens on little endian, after the node is written
+ * back to disk.
+ */
+ retval = hfs_swap_BTNode (block, vp, kSwapBTNodeBigToHost, allow_empty_node);
+ }
+ else if (*((u_int16_t *)((char *)block->buffer + (block->blockSize - sizeof (u_int16_t)))) == 0x000e) {
+ /*
+ * The node was in-cache in native-endianness. We don't need to do
+ * anything here, because the node is ready to use. Set retval == 0.
+ */
+ retval = 0;
+ }
+ /*
+ * If the node doesn't have hex 14 (0xe) in the last two bytes of the buffer,
+ * it doesn't necessarily mean that this is a bad node. Zeroed nodes that are
+ * marked as unused in the b-tree map node would be OK and not have valid content.
+ */
+ }
+ }
+
+ /*
+ * If we got an error, then the node is only partially swapped.
+ * We mark the buffer invalid so that the next attempt to get the
+ * node will read it and attempt to swap again, and will notice
+ * the error again. If we didn't do this, the next attempt to get
+ * the node might use the partially swapped node as-is.
+ */
+ if (retval)