Skip to contentSkip to conversation

hfs: fix writes to b-tree nodes larger than a page

PATCH In review Mar Dominguez 2 participants 6 messages Dec 4

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover hfs: fix writes to b-tree nodes larger than a page

hfs_btree_open() accepts b-tree node sizes up to 32 KiB, and the read side handles nodes that span several pages, but hfs_bnode_write(), hfs_bnode_clear(), hfs_bnode_copy() and hfs_bnode_move() only use the node's first page. Any change to a catalog or extents tree whose nodes are larger than PAGE_SIZE then writes past that page.

Patch 1 makes the four helpers walk the node's pages. Patch 2 rejects node sizes below 512 bytes, as hfsplus does. Patch 3 adds KUnit tests for the four helpers.

Patch 1 fixes the write helpers instead of refusing nodes larger than 512 bytes: hfs_btree_open() has accepted them since 2.6.12, and the read and node creation paths already support them. With patch 1 the write helpers do too, as the hfsplus ones do.

Tested with x86_64 KASAN builds under QEMU: hfs/for-next (a2c27efbbd43) with CONFIG_DEBUG_VM, and mainline (007722b8211f) without it. The test volumes were made with hformat, with the catalog tree rewritten to use 8 KiB or 32 KiB nodes. Without the series, mkdir and rmdir on them hit the VM_BUG_ON in memcpy_to_page() with CONFIG_DEBUG_VM. Without CONFIG_DEBUG_VM, the volume can't be mounted again after mkdir (8 KiB nodes), and rmdir (32 KiB nodes) crashes init through corrupted memory. With the series, both work and the volume mounts again with the expected directories. A volume with the usual 512-byte nodes behaves the same with and without the series. The hfs KUnit suites pass on UML with KASAN; without patch 1 the four new cases fail.

I have not tested on 32-bit HIGHMEM, on architectures with pages larger than 4 KiB, or with volumes written by Mac OS, and I have not run xfstests. Patches 1 and 2 also apply to mainline; patch 3 needs the KUnit setup in hfs/for-next. I can send the test images privately.

Found and written with an LLM coding assistant.

Mar Dominguez (3):
hfs: fix b-tree node writes past the first page
hfs: check the b-tree node size range in hfs_btree_open()
hfs: add KUnit tests for b-tree node operations

 fs/hfs/Makefile            |   2 +-
 fs/hfs/bnode.c             | 132 ++++++++++++++---
 fs/hfs/bnode_test.c        | 293 +++++++++++++++++++++++++++++++++++++
 fs/hfs/btree.c             |   5 +-
 include/linux/hfs_common.h |   6 +-
 5 files changed, 416 insertions(+), 22 deletions(-)
 create mode 100644 fs/hfs/bnode_test.c

base-commit: a2c27efbbd43d2e3756860ebd1bf891d7481f2c5

Signature · 1 line
2.53.0
#1 lore raw

PATCH 1/3 hfs: fix b-tree node writes past the first page

hfs_btree_open() accepts any power-of-two node size from the b-tree header, so a catalog or extents tree can have 8, 16 or 32 KiB nodes, which span several pages on 4 KiB page systems. hfs_bnode_find() and hfs_bnode_read() handle such nodes: commit 54a5ead6f5e2 ("hfs: fix high memory mapping in hfs_bnode_read") made the read side walk node->page[]. hfs_bnode_write(), hfs_bnode_clear(), hfs_bnode_copy() and hfs_bnode_move() still use only node->page[0], with offsets and lengths that are bounded by the node size, not by the page size. Every change to such a tree then writes up to 28 KiB past the end of that page, into whatever memory follows it. For example, hfs_brec_insert() updates the record offset table at the end of the node, and hfs_bnode_put() clears the whole node when a node is deleted. The node's other pages are never updated, so the tree on disk is left inconsistent too.

With CONFIG_DEBUG_VM, mkdir on a volume whose catalog tree has 8 KiB nodes trips the check in memcpy_to_page():

kernel BUG at include/linux/highmem.h:552!

  RIP: 0010:hfs_bnode_write+0x4c5/0x560
  Call Trace:
   hfs_bnode_write_u16+0x6d/0xa0
   hfs_brec_insert+0x3c4/0xb60
   hfs_cat_create+0x3bd/0xb00
   hfs_mkdir+0x59/0xc0
   vfs_mkdir+0x2b4/0x710

Without CONFIG_DEBUG_VM the pages after node->page[0] are silently overwritten or zeroed.

Walk the node's pages in all four helpers, as hfs_bnode_read() and the hfsplus helpers do. hfs_bnode_move() copies backwards when the destination is above the source, because the ranges may overlap across pages.

Found and written with an LLM coding assistant.

Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")
---
 fs/hfs/bnode.c | 124 ++++++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 106 insertions(+), 18 deletions(-)
fs/hfs/bnode.c +106 -18
index ec1b0189a4..d12be972ce 100644
@@ -95,6 +95,9 @@ void hfs_bnode_read_key(struct hfs_bnode *node, void *key, u32 off)
 void hfs_bnode_write(struct hfs_bnode *node, void *buf, u32 off, u32 len)
 {
 	struct page *page;
+	u32 pagenum;
+	u32 bytes_written;
+	u32 bytes_to_write;
 
 	if (!is_bnode_offset_valid(node, off))
 		return;
@@ -111,10 +114,22 @@ void hfs_bnode_write(struct hfs_bnode *node, void *buf, u32 off, u32 len)
 	len = check_and_correct_requested_length(node, off, len);
 
 	off += node->page_offset;
-	page = node->page[0];
+	pagenum = off >> PAGE_SHIFT;
+	off &= ~PAGE_MASK; /* compute page offset for the first page */
+
+	for (bytes_written = 0; bytes_written < len;
+	     bytes_written += bytes_to_write) {
+		if (pagenum >= node->tree->pages_per_bnode)
+			break;
+		page = node->page[pagenum];
+		bytes_to_write = min_t(u32, len - bytes_written, PAGE_SIZE - off);
+
+		memcpy_to_page(page, off, buf + bytes_written, bytes_to_write);
+		set_page_dirty(page);
 
-	memcpy_to_page(page, off, buf, len);
-	set_page_dirty(page);
+		pagenum++;
+		off = 0; /* page offset only applies to the first page */
+	}
 }
 
 void hfs_bnode_write_u16(struct hfs_bnode *node, u32 off, u16 data)
@@ -133,6 +148,9 @@ void hfs_bnode_write_u8(struct hfs_bnode *node, u32 off, u8 data)
 void hfs_bnode_clear(struct hfs_bnode *node, u32 off, u32 len)
 {
 	struct page *page;
+	u32 pagenum;
+	u32 bytes_cleared;
+	u32 bytes_to_clear;
 
 	if (!is_bnode_offset_valid(node, off))
 		return;
@@ -149,16 +167,65 @@ void hfs_bnode_clear(struct hfs_bnode *node, u32 off, u32 len)
 	len = check_and_correct_requested_length(node, off, len);
 
 	off += node->page_offset;
-	page = node->page[0];
+	pagenum = off >> PAGE_SHIFT;
+	off &= ~PAGE_MASK; /* compute page offset for the first page */
+
+	for (bytes_cleared = 0; bytes_cleared < len;
+	     bytes_cleared += bytes_to_clear) {
+		if (pagenum >= node->tree->pages_per_bnode)
+			break;
+		page = node->page[pagenum];
+		bytes_to_clear = min_t(u32, len - bytes_cleared, PAGE_SIZE - off);
+
+		memzero_page(page, off, bytes_to_clear);
+		set_page_dirty(page);
+
+		pagenum++;
+		off = 0; /* page offset only applies to the first page */
+	}
+}
 
-	memzero_page(page, off, len);
-	set_page_dirty(page);
+/* Number of bytes from @off to the end of the page that contains @off */
+static inline u32 hfs_bnode_page_tail(u32 off)
+{
+	return PAGE_SIZE - (off & ~PAGE_MASK);
+}
+
+/* Number of bytes before @end in the page that contains byte @end - 1 */
+static inline u32 hfs_bnode_page_head(u32 end)
+{
+	return ((end - 1) & ~PAGE_MASK) + 1;
+}
+
+/*
+ * Copy @len bytes from offset @src of @src_node to offset @dst of @dst_node.
+ * The offsets include the nodes' page_offset, and neither range may cross
+ * a page boundary.
+ */
+static void hfs_bnode_copy_chunk(struct hfs_bnode *dst_node, u32 dst,
+				 struct hfs_bnode *src_node, u32 src, u32 len)
+{
+	struct page *src_page = src_node->page[src >> PAGE_SHIFT];
+	struct page *dst_page = dst_node->page[dst >> PAGE_SHIFT];
+	void *ptr;
+
+	src &= ~PAGE_MASK;
+	dst &= ~PAGE_MASK;
+
+	if (src_page == dst_page) {
+		ptr = kmap_local_page(dst_page);
+		memmove(ptr + dst, ptr + src, len);
+		kunmap_local(ptr);
+	} else {
+		memcpy_page(dst_page, dst, src_page, src, len);
+	}
+	set_page_dirty(dst_page);
 }
 
 void hfs_bnode_copy(struct hfs_bnode *dst_node, u32 dst,
 		    struct hfs_bnode *src_node, u32 src, u32 len)
 {
-	struct page *src_page, *dst_page;
+	u32 chunk;
 
 	hfs_dbg("dst %u, src %u, len %u\n", dst, src, len);
 	if (!len)
@@ -169,17 +236,20 @@ void hfs_bnode_copy(struct hfs_bnode *dst_node, u32 dst,
 
 	src += src_node->page_offset;
 	dst += dst_node->page_offset;
-	src_page = src_node->page[0];
-	dst_page = dst_node->page[0];
 
-	memcpy_page(dst_page, dst, src_page, src, len);
-	set_page_dirty(dst_page);
+	while (len) {
+		chunk = min3(len, hfs_bnode_page_tail(src),
+			     hfs_bnode_page_tail(dst));
+		hfs_bnode_copy_chunk(dst_node, dst, src_node, src, chunk);
+		src += chunk;
+		dst += chunk;
+		len -= chunk;
+	}
 }
 
 void hfs_bnode_move(struct hfs_bnode *node, u32 dst, u32 src, u32 len)
 {
-	struct page *page;
-	void *ptr;
+	u32 chunk;
 
 	hfs_dbg("dst %u, src %u, len %u\n", dst, src, len);
 	if (!len)
@@ -190,11 +260,29 @@ void hfs_bnode_move(struct hfs_bnode *node, u32 dst, u32 src, u32 len)
 
 	src += node->page_offset;
 	dst += node->page_offset;
-	page = node->page[0];
-	ptr = kmap_local_page(page);
-	memmove(ptr + dst, ptr + src, len);
-	kunmap_local(ptr);
-	set_page_dirty(page);
+
+	if (dst > src) {
+		/* The ranges may overlap: move the end first */
+		src += len;
+		dst += len;
+		while (len) {
+			chunk = min3(len, hfs_bnode_page_head(src),
+				     hfs_bnode_page_head(dst));
+			src -= chunk;
+			dst -= chunk;
+			len -= chunk;
+			hfs_bnode_copy_chunk(node, dst, node, src, chunk);
+		}
+	} else {
+		while (len) {
+			chunk = min3(len, hfs_bnode_page_tail(src),
+				     hfs_bnode_page_tail(dst));
+			hfs_bnode_copy_chunk(node, dst, node, src, chunk);
+			src += chunk;
+			dst += chunk;
+			len -= chunk;
+		}
+	}
 }
 
 void hfs_bnode_dump(struct hfs_bnode *node)
Signature · 1 line
2.53.0
#2 lore raw

PATCH 2/3 hfs: check the b-tree node size range in hfs_btree_open()

In reply to Mar Dominguez

hfs_btree_open() only checks that the node size in the b-tree header is a power of two, so it also accepts nodes of 1 to 256 bytes, which can't hold a node descriptor and the header node's records. HFS b-tree nodes are 512 bytes, and hfsplus already rejects node sizes outside 512 to 32768 bytes. Apply the same range to HFS, with the constants moved to hfs_common.h and shared by both, and log the size that is rejected.

Found and written with an LLM coding assistant.

---
 fs/hfs/btree.c             | 5 ++++-
 include/linux/hfs_common.h | 6 ++++--
 2 files changed, 8 insertions(+), 3 deletions(-)
fs/hfs/btree.c +4 -1
index f070384031..62c9094ab4 100644
@@ -229,8 +229,11 @@ struct hfs_btree *hfs_btree_open(struct super_block *sb, u32 id, btree_keycmp ke
 	tree->depth = be16_to_cpu(head->depth);
 
 	size = tree->node_size;
-	if (!is_power_of_2(size))
+	if (size < HFS_NODE_MINSZ || size > HFS_NODE_MXSZ ||
+	    !is_power_of_2(size)) {
+		pr_err("invalid node size %u\n", size);
 		goto fail_folio;
+	}
 	if (!tree->node_count)
 		goto fail_folio;
 	switch (id) {
include/linux/hfs_common.h +4 -2
index eb119a1c56..3502552d75 100644
@@ -532,13 +532,15 @@ struct hfs_btree_header_rec {
 
 /* HFS BTree misc info */
 #define HFS_TREE_HEAD				0
+#define HFS_NODE_MINSZ				512
+#define HFS_NODE_MXSZ				32768
 #define HFS_BTREE_HDR_MAP_REC_INDEX		2	/* Map (bitmap) record in Header node */
 #define HFS_BTREE_MAP_NODE_REC_INDEX		0	/* Map record in Map Node */
 
 /* HFS+ BTree misc info */
 #define HFSPLUS_TREE_HEAD			HFS_TREE_HEAD
-#define HFSPLUS_NODE_MXSZ			32768
-#define HFSPLUS_NODE_MINSZ			512
+#define HFSPLUS_NODE_MXSZ			HFS_NODE_MXSZ
+#define HFSPLUS_NODE_MINSZ			HFS_NODE_MINSZ
 #define HFSPLUS_ATTR_TREE_NODE_SIZE		8192
 #define HFSPLUS_BTREE_HDR_NODE_RECS_COUNT	3
 /* Map (bitmap) record in Header node */
Signature · 1 line
2.53.0
#3 lore raw

PATCH 3/3 hfs: add KUnit tests for b-tree node operations

In reply to Mar Dominguez

Add a KUnit suite for hfs_bnode_write(), hfs_bnode_clear(), hfs_bnode_copy() and hfs_bnode_move() on 32 KiB nodes, with ranges that cross page boundaries and reach the end of the node, checking the node's contents page by page. The first page of each test node is the start of a higher-order allocation, so an operation that runs off the end of node->page[0] lands in the test's own guard pages, which are checked, instead of in other memory. Without the multi-page fix all four cases fail on systems with 4 KiB pages.

Export the helpers with EXPORT_SYMBOL_IF_KUNIT(), as the existing HFS tests do for string.c and inode.c.

Found and written with an LLM coding assistant.

---
 fs/hfs/Makefile     |   2 +-
 fs/hfs/bnode.c      |   8 ++
 fs/hfs/bnode_test.c | 293 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 302 insertions(+), 1 deletion(-)
 create mode 100644 fs/hfs/bnode_test.c
fs/hfs/Makefile +1 -1
index be68c5305f..c19138944f 100644
@@ -10,4 +10,4 @@ hfs-objs := bitmap.o bfind.o bnode.o brec.o btree.o \
             part_tbl.o string.o super.o sysdep.o trans.o
 
 # KUnit tests
-obj-$(CONFIG_HFS_KUNIT_TEST) += string_test.o hfs_time_test.o
+obj-$(CONFIG_HFS_KUNIT_TEST) += string_test.o hfs_time_test.o bnode_test.o
fs/hfs/bnode.c +8 -0
index d12be972ce..d1d61d7f46 100644
@@ -12,6 +12,7 @@
 #include <linux/pagemap.h>
 #include <linux/slab.h>
 #include <linux/swap.h>
+#include <kunit/visibility.h>
 
 #include "btree.h"
 
@@ -54,6 +55,7 @@ void hfs_bnode_read(struct hfs_bnode *node, void *buf, u32 off, u32 len)
 		off = 0; /* page offset only applies to the first page */
 	}
 }
+EXPORT_SYMBOL_IF_KUNIT(hfs_bnode_read);
 
 u16 hfs_bnode_read_u16(struct hfs_bnode *node, u32 off)
 {
@@ -62,6 +64,7 @@ u16 hfs_bnode_read_u16(struct hfs_bnode *node, u32 off)
 	hfs_bnode_read(node, &data, off, 2);
 	return be16_to_cpu(data);
 }
+EXPORT_SYMBOL_IF_KUNIT(hfs_bnode_read_u16);
 
 u8 hfs_bnode_read_u8(struct hfs_bnode *node, u32 off)
 {
@@ -131,6 +134,7 @@ void hfs_bnode_write(struct hfs_bnode *node, void *buf, u32 off, u32 len)
 		off = 0; /* page offset only applies to the first page */
 	}
 }
+EXPORT_SYMBOL_IF_KUNIT(hfs_bnode_write);
 
 void hfs_bnode_write_u16(struct hfs_bnode *node, u32 off, u16 data)
 {
@@ -138,6 +142,7 @@ void hfs_bnode_write_u16(struct hfs_bnode *node, u32 off, u16 data)
 	// optimize later...
 	hfs_bnode_write(node, &v, off, 2);
 }
+EXPORT_SYMBOL_IF_KUNIT(hfs_bnode_write_u16);
 
 void hfs_bnode_write_u8(struct hfs_bnode *node, u32 off, u8 data)
 {
@@ -184,6 +189,7 @@ void hfs_bnode_clear(struct hfs_bnode *node, u32 off, u32 len)
 		off = 0; /* page offset only applies to the first page */
 	}
 }
+EXPORT_SYMBOL_IF_KUNIT(hfs_bnode_clear);
 
 /* Number of bytes from @off to the end of the page that contains @off */
 static inline u32 hfs_bnode_page_tail(u32 off)
@@ -246,6 +252,7 @@ void hfs_bnode_copy(struct hfs_bnode *dst_node, u32 dst,
 		len -= chunk;
 	}
 }
+EXPORT_SYMBOL_IF_KUNIT(hfs_bnode_copy);
 
 void hfs_bnode_move(struct hfs_bnode *node, u32 dst, u32 src, u32 len)
 {
@@ -284,6 +291,7 @@ void hfs_bnode_move(struct hfs_bnode *node, u32 dst, u32 src, u32 len)
 		}
 	}
 }
+EXPORT_SYMBOL_IF_KUNIT(hfs_bnode_move);
 
 void hfs_bnode_dump(struct hfs_bnode *node)
 {
fs/hfs/bnode_test.c new file +293 -0

293 lines not on this page · raw message

Signature · 1 line
2.53.0
#4 lore raw
In reply to Mar Dominguez
Quoted 19 lines · Mar Dominguez

hfs_btree_open() accepts b-tree node sizes up to 32 KiB, and the read side handles nodes that span several pages, but hfs_bnode_write(), hfs_bnode_clear(), hfs_bnode_copy() and hfs_bnode_move() only use the node's first page.  Any change to a catalog or extents tree whose nodes
are larger than PAGE_SIZE then writes past that page.

Patch 1 makes the four helpers walk the node's pages.  Patch 2 rejects
node sizes below 512 bytes, as hfsplus does.  Patch 3 adds KUnit tests
for the four helpers.

Patch 1 fixes the write helpers instead of refusing nodes larger than 512 bytes: hfs_btree_open() has accepted them since 2.6.12, and the read
and node creation paths already support them.  With patch 1 the write helpers do too, as the hfsplus ones do.

Quoted 7 lines

Tested with x86_64 KASAN builds under QEMU: hfs/for-next (a2c27efbbd43)
with CONFIG_DEBUG_VM, and mainline (007722b8211f) without it.  The test
volumes were made with hformat, with the catalog tree rewritten to use
8 KiB or 32 KiB nodes.  Without the series, mkdir and rmdir on them

Please, correct me if I wrong. Have you implemented support of node size bigger than 512 bytes in HFS?

Inside Macintosh: Files (the original HFS spec)

Chapter 2, "Data Organization on Volumes", in the B*-Trees section (Files-104):

- Under Nodes:
▎ "A B*-tree file consists entirely of objects called nodes, each of which is 512 bytes long."
- Under Header Nodes, describing the BTHdrRec field bthNodeSize:
Integer; {size of a node}:
▎ "The size (in bytes) of a node. Currently, this is always 512."
- Under Map Nodes, the map record size is worked out from the 512-byte node:
▎ "occupies 494 bytes (512 bytes in the node, less 14 bytes for the node descriptor and 2 bytes for each of the two record offsets at the end of the node)."

TN1150 "HFS Plus Volume Format"

TN1150 is Apple's HFS+ spec, and it also restates the HFS rule (tn1150).

- In B-Trees › HFS and HFS Plus B-Trees Compared › Node Sizes:
▎ "In an HFS B-tree, nodes always have a fixed size of 512 bytes."

▎ "In an HFS Plus B-tree, the node size is determined by a field (nodeSize) in the header node. The node size must be a power from 512 through 32,768. An implementation must use the nodeSize field to determine the actual node size."
- Table 1, "HFS and HFS Plus Compared", lists the catalog node size as 512 bytes (HFS) against 4 KB (HFS Plus).
- In the Header Record section, nodeSize is described as "a power of two, from 512 through 32,768, inclusive". That describes the HFS+ header.

- The format has one valid node size for HFS: 512. TN1150 says it without any qualifier.
- bthNodeSize is still a field in the header, and Inside Macintosh says "currently" always 512. So the format left room for other sizes, but no Apple HFS implementation ever used them.

So, we should support only 512 bytes node size. This a difference between HFS and HFS+. HFS supports only 512 bytes b-tree nodes and nodes share memory page. For the case of HFS+, b-tree node cannot be smaller than 4K, if I remember correctly. I assume that we could have issue in HFS b-tree nodes management code. But node size should be 512 bytes always for the case of HFS.

Thanks,
Slava.

Quoted 33 lines

hit
the VM_BUG_ON in memcpy_to_page() with CONFIG_DEBUG_VM.  Without CONFIG_DEBUG_VM, the volume can't be mounted again after mkdir (8 KiB nodes), and rmdir (32 KiB nodes) crashes init through corrupted memory.  With the series, both work and the volume mounts again with the expected directories.  A volume with the usual 512-byte nodes behaves the same with and without the series.  The hfs KUnit suites pass on UML with KASAN; without patch 1 the four new cases fail.

I have not tested on 32-bit HIGHMEM, on architectures with pages larger
than 4 KiB, or with volumes written by Mac OS, and I have not run xfstests.  Patches 1 and 2 also apply to mainline; patch 3 needs the KUnit setup in hfs/for-next.  I can send the test images privately.

Found and written with an LLM coding assistant.

Mar Dominguez (3):
  hfs: fix b-tree node writes past the first page
  hfs: check the b-tree node size range in hfs_btree_open()
  hfs: add KUnit tests for b-tree node operations

 fs/hfs/Makefile            |   2 +- fs/hfs/bnode.c             | 132 ++++++++++++++--- fs/hfs/bnode_test.c        | 293
+++++++++++++++++++++++++++++++++++++
 fs/hfs/btree.c             |   5 +- include/linux/hfs_common.h |   6 +-

 5 files changed, 416 insertions(+), 22 deletions(-)

 create mode 100644 fs/hfs/bnode_test.c

base-commit: a2c27efbbd43d2e3756860ebd1bf891d7481f2c5

#5 lore raw
Quoted 2 lines · Viacheslav Dubeyko

Please, correct me if I wrong. Have you implemented support of node size bigger than 512 bytes in HFS?

No, patch 1 didn't add a new feature. hfs_btree_open() has accepted any power-of-two node size since 2.6.12, and the read and node creation paths already loop over the pages of a node, so patch 1 only made the four write helpers do the same.

[...]

Quoted 6 lines

So, we should support only 512 bytes node size. This a difference between HFS and HFS+. HFS supports only 512 bytes b-tree nodes and nodes share memory page. For the case of HFS+, b-tree node cannot be smaller than 4K, if I remember correctly. I assume that we could have issue in HFS b-tree nodes management code. But node size should be 512 bytes always for the case of HFS.

Agreed, thanks for the spec references. I've sent a v2 that replaces the series with one patch: hfs_btree_open() now rejects any node size other than 512 bytes, for both the catalog and the extents tree. That also closes the out-of-bounds write on crafted images, since a 512-byte node never crosses a page boundary:

https://lore.kernel.org/linux-fsdevel/[email protected]/

v2 drops the KUnit patch, since its cases were for nodes larger than a page. If you'd like KUnit tests for the write helpers on 512-byte nodes, I can send them as a separate patch.

Thanks,
Mar