Index: uspace/srv/bd/hr/hr.c
===================================================================
--- uspace/srv/bd/hr/hr.c	(revision a19d7fc4b9a9e9429e475bb951803a662991726d)
+++ uspace/srv/bd/hr/hr.c	(revision 4a2a6b8b9267feafb10763f60d2b4685bb8bbf67)
@@ -166,4 +166,8 @@
 		new_volume->strip_size = HR_STRIP_SIZE;
 		break;
+	case hr_l_4:
+		new_volume->hr_ops.create = hr_raid4_create;
+		new_volume->strip_size = HR_STRIP_SIZE;
+		break;
 	default:
 		log_msg(LOG_DEFAULT, LVL_ERROR,
@@ -271,4 +275,7 @@
 	case hr_l_0:
 		new_volume->hr_ops.create = hr_raid0_create;
+		break;
+	case hr_l_4:
+		new_volume->hr_ops.create = hr_raid4_create;
 		break;
 	default:
Index: uspace/srv/bd/hr/meson.build
===================================================================
--- uspace/srv/bd/hr/meson.build	(revision a19d7fc4b9a9e9429e475bb951803a662991726d)
+++ uspace/srv/bd/hr/meson.build	(revision 4a2a6b8b9267feafb10763f60d2b4685bb8bbf67)
@@ -28,3 +28,3 @@
 
 deps = [ 'block', 'device' ]
-src = files('hr.c', 'raid0.c', 'raid1.c', 'superblock.c', 'util.c')
+src = files('hr.c', 'raid0.c', 'raid1.c', 'raid4.c', 'superblock.c', 'util.c')
Index: uspace/srv/bd/hr/raid0.c
===================================================================
--- uspace/srv/bd/hr/raid0.c	(revision a19d7fc4b9a9e9429e475bb951803a662991726d)
+++ uspace/srv/bd/hr/raid0.c	(revision 4a2a6b8b9267feafb10763f60d2b4685bb8bbf67)
@@ -105,18 +105,21 @@
 	errno_t rc;
 	uint64_t phys_block;
-	size_t extent;
+	size_t extent, left;
+
+	rc = hr_check_ba_range(vol, cnt, ba);
+	if (rc != EOK)
+		return rc;
 
 	fibril_mutex_lock(&big_lock);
 
-	size_t left = cnt;
+	left = cnt;
 	while (left != 0) {
-		raid0_geometry(ba++, vol, &extent, &phys_block);
-		rc = hr_calc_ba(vol, cnt, &ba);
-		if (rc != EOK)
-			break;
+		raid0_geometry(ba, vol, &extent, &phys_block);
+		hr_add_ba_offset(vol, &phys_block);
 		rc = block_sync_cache(vol->devs[extent], phys_block, 1);
 		if (rc != EOK)
 			break;
 		left--;
+		ba++;
 	}
 
@@ -131,17 +134,19 @@
 	errno_t rc;
 	uint64_t phys_block;
-	size_t extent;
+	size_t extent, left;
 
 	if (size < cnt * vol->bsize)
 		return EINVAL;
 
+	rc = hr_check_ba_range(vol, cnt, ba);
+	if (rc != EOK)
+		return rc;
+
 	fibril_mutex_lock(&big_lock);
 
-	size_t left = cnt;
+	left = cnt;
 	while (left != 0) {
-		raid0_geometry(ba++, vol, &extent, &phys_block);
-		rc = hr_calc_ba(vol, cnt, &ba);
-		if (rc != EOK)
-			break;
+		raid0_geometry(ba, vol, &extent, &phys_block);
+		hr_add_ba_offset(vol, &phys_block);
 		rc = block_read_direct(vol->devs[extent], phys_block, 1, buf);
 		buf = buf + vol->bsize;
@@ -149,4 +154,5 @@
 			break;
 		left--;
+		ba++;
 	}
 
@@ -161,17 +167,19 @@
 	errno_t rc;
 	uint64_t phys_block;
-	size_t extent;
+	size_t extent, left;
 
 	if (size < cnt * vol->bsize)
 		return EINVAL;
 
+	rc = hr_check_ba_range(vol, cnt, ba);
+	if (rc != EOK)
+		return rc;
+
 	fibril_mutex_lock(&big_lock);
 
-	size_t left = cnt;
+	left = cnt;
 	while (left != 0) {
-		raid0_geometry(ba++, vol, &extent, &phys_block);
-		rc = hr_calc_ba(vol, cnt, &phys_block);
-		if (rc != EOK)
-			break;
+		raid0_geometry(ba, vol, &extent, &phys_block);
+		hr_add_ba_offset(vol, &phys_block);
 		rc = block_write_direct(vol->devs[extent], phys_block, 1, data);
 		data = data + vol->bsize;
@@ -179,4 +187,5 @@
 			break;
 		left--;
+		ba++;
 	}
 
Index: uspace/srv/bd/hr/raid1.c
===================================================================
--- uspace/srv/bd/hr/raid1.c	(revision a19d7fc4b9a9e9429e475bb951803a662991726d)
+++ uspace/srv/bd/hr/raid1.c	(revision 4a2a6b8b9267feafb10763f60d2b4685bb8bbf67)
@@ -87,15 +87,16 @@
 static errno_t hr_raid1_bd_sync_cache(bd_srv_t *bd, aoff64_t ba, size_t cnt)
 {
+	hr_volume_t *vol = bd->srvs->sarg;
+
+	errno_t rc;
+	size_t i;
+
+	rc = hr_check_ba_range(vol, cnt, ba);
+	if (rc != EOK)
+		return rc;
+
+	hr_add_ba_offset(vol, &ba);
+
 	fibril_mutex_lock(&big_lock);
-	hr_volume_t *vol = bd->srvs->sarg;
-
-	errno_t rc;
-	size_t i;
-
-	rc = hr_calc_ba(vol, cnt, &ba);
-	if (rc != EOK) {
-		fibril_mutex_unlock(&big_lock);
-		return rc;
-	}
 
 	for (i = 0; i < vol->dev_no; i++) {
@@ -112,15 +113,16 @@
     void *buf, size_t size)
 {
+	hr_volume_t *vol = bd->srvs->sarg;
+
+	errno_t rc;
+	size_t i;
+
+	rc = hr_check_ba_range(vol, cnt, ba);
+	if (rc != EOK)
+		return rc;
+
+	hr_add_ba_offset(vol, &ba);
+
 	fibril_mutex_lock(&big_lock);
-	hr_volume_t *vol = bd->srvs->sarg;
-
-	errno_t rc;
-	size_t i;
-
-	rc = hr_calc_ba(vol, cnt, &ba);
-	if (rc != EOK) {
-		fibril_mutex_unlock(&big_lock);
-		return rc;
-	}
 
 	for (i = 0; i < vol->dev_no; i++) {
@@ -137,15 +139,16 @@
     const void *data, size_t size)
 {
+	hr_volume_t *vol = bd->srvs->sarg;
+
+	errno_t rc;
+	size_t i;
+
+	rc = hr_check_ba_range(vol, cnt, ba);
+	if (rc != EOK)
+		return rc;
+
+	hr_add_ba_offset(vol, &ba);
+
 	fibril_mutex_lock(&big_lock);
-	hr_volume_t *vol = bd->srvs->sarg;
-
-	errno_t rc;
-	size_t i;
-
-	rc = hr_calc_ba(vol, cnt, &ba);
-	if (rc != EOK) {
-		fibril_mutex_unlock(&big_lock);
-		return rc;
-	}
 
 	for (i = 0; i < vol->dev_no; i++) {
Index: uspace/srv/bd/hr/raid4.c
===================================================================
--- uspace/srv/bd/hr/raid4.c	(revision 4a2a6b8b9267feafb10763f60d2b4685bb8bbf67)
+++ uspace/srv/bd/hr/raid4.c	(revision 4a2a6b8b9267feafb10763f60d2b4685bb8bbf67)
@@ -0,0 +1,285 @@
+/*
+ * Copyright (c) 2024 Miroslav Cimerman
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ * - The name of the author may not be used to endorse or promote products
+ *   derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/** @addtogroup hr
+ * @{
+ */
+/**
+ * @file
+ */
+
+#include <abi/ipc/ipc.h>
+#include <bd_srv.h>
+#include <block.h>
+#include <errno.h>
+#include <hr.h>
+#include <io/log.h>
+#include <ipc/hr.h>
+#include <ipc/services.h>
+#include <loc.h>
+#include <task.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <str_error.h>
+
+#include "util.h"
+#include "var.h"
+
+extern fibril_mutex_t big_lock;
+extern loc_srv_t *hr_srv;
+
+static errno_t hr_raid4_bd_open(bd_srvs_t *, bd_srv_t *);
+static errno_t hr_raid4_bd_close(bd_srv_t *);
+static errno_t hr_raid4_bd_read_blocks(bd_srv_t *, aoff64_t, size_t, void *,
+    size_t);
+static errno_t hr_raid4_bd_sync_cache(bd_srv_t *, aoff64_t, size_t);
+static errno_t hr_raid4_bd_write_blocks(bd_srv_t *, aoff64_t, size_t,
+    const void *, size_t);
+static errno_t hr_raid4_bd_get_block_size(bd_srv_t *, size_t *);
+static errno_t hr_raid4_bd_get_num_blocks(bd_srv_t *, aoff64_t *);
+
+static bd_ops_t hr_raid4_bd_ops = {
+	.open = hr_raid4_bd_open,
+	.close = hr_raid4_bd_close,
+	.sync_cache = hr_raid4_bd_sync_cache,
+	.read_blocks = hr_raid4_bd_read_blocks,
+	.write_blocks = hr_raid4_bd_write_blocks,
+	.get_block_size = hr_raid4_bd_get_block_size,
+	.get_num_blocks = hr_raid4_bd_get_num_blocks
+};
+
+static void xor(void *dst, const void *src, size_t size)
+{
+	size_t i;
+	uint64_t *d = dst;
+	const uint64_t *s = src;
+
+	for (i = 0; i < size / sizeof(uint64_t); ++i)
+		*d++ ^= *s++;
+}
+
+static errno_t write_parity(hr_volume_t *vol, uint64_t extent, uint64_t block,
+    const void *data)
+{
+	errno_t rc;
+	size_t i;
+	void *xorbuf;
+	void *buf;
+
+	xorbuf = calloc(1, vol->bsize);
+	if (xorbuf == NULL)
+		return ENOMEM;
+
+	buf = malloc(vol->bsize);
+	if (buf == NULL)
+		return ENOMEM;
+
+	for (i = 1; i < vol->dev_no; i++) {
+		if (i == extent) {
+			xor(xorbuf, data, vol->bsize);
+		} else {
+			rc = block_read_direct(vol->devs[i], block, 1, buf);
+			if (rc != EOK)
+				goto end;
+			xor(xorbuf, buf, vol->bsize);
+		}
+	}
+
+	rc = block_write_direct(vol->devs[0], block, 1, xorbuf);
+
+end:
+	free(xorbuf);
+	free(buf);
+	return EOK;
+}
+
+static void raid4_geometry(uint64_t x, hr_volume_t *vol, size_t *extent,
+    uint64_t *phys_block)
+{
+	uint64_t N = vol->dev_no; /* extents */
+	uint64_t L = vol->strip_size / vol->bsize; /* size of strip in blocks */
+
+	uint64_t i = ((x / L) % (N - 1)) + 1; /* extent */
+	uint64_t j = (x / L) / (N - 1); /* stripe */
+	uint64_t k = x % L; /* strip offset */
+
+	*extent = i;
+	*phys_block = j * L + k;
+}
+
+static errno_t hr_raid4_bd_open(bd_srvs_t *bds, bd_srv_t *bd)
+{
+	log_msg(LOG_DEFAULT, LVL_NOTE, "hr_bd_open()");
+	return EOK;
+}
+
+static errno_t hr_raid4_bd_close(bd_srv_t *bd)
+{
+	log_msg(LOG_DEFAULT, LVL_NOTE, "hr_bd_close()");
+	return EOK;
+}
+
+static errno_t hr_raid4_bd_sync_cache(bd_srv_t *bd, aoff64_t ba, size_t cnt)
+{
+	hr_volume_t *vol = bd->srvs->sarg;
+	errno_t rc;
+	uint64_t phys_block;
+	size_t extent;
+
+	rc = hr_check_ba_range(vol, cnt, ba);
+	if (rc != EOK)
+		return rc;
+
+	fibril_mutex_lock(&big_lock);
+
+	size_t left = cnt;
+	while (left != 0) {
+		raid4_geometry(ba, vol, &extent, &phys_block);
+		hr_add_ba_offset(vol, &phys_block);
+		rc = block_sync_cache(vol->devs[extent], phys_block, 1);
+		if (rc != EOK)
+			break;
+		left--;
+		ba++;
+	}
+
+	fibril_mutex_unlock(&big_lock);
+	return rc;
+}
+
+static errno_t hr_raid4_bd_read_blocks(bd_srv_t *bd, aoff64_t ba, size_t cnt,
+    void *buf, size_t size)
+{
+	hr_volume_t *vol = bd->srvs->sarg;
+	errno_t rc;
+	uint64_t phys_block;
+	size_t extent;
+
+	if (size < cnt * vol->bsize)
+		return EINVAL;
+
+	rc = hr_check_ba_range(vol, cnt, ba);
+	if (rc != EOK)
+		return rc;
+
+	fibril_mutex_lock(&big_lock);
+
+	size_t left = cnt;
+	while (left != 0) {
+		raid4_geometry(ba, vol, &extent, &phys_block);
+		hr_add_ba_offset(vol, &phys_block);
+		rc = block_read_direct(vol->devs[extent], phys_block, 1, buf);
+		buf = buf + vol->bsize;
+		if (rc != EOK)
+			break;
+		left--;
+		ba++;
+	}
+
+	fibril_mutex_unlock(&big_lock);
+	return rc;
+}
+
+
+static errno_t hr_raid4_bd_write_blocks(bd_srv_t *bd, aoff64_t ba, size_t cnt,
+    const void *data, size_t size)
+{
+	hr_volume_t *vol = bd->srvs->sarg;
+	errno_t rc;
+	uint64_t phys_block;
+	size_t extent;
+
+	if (size < cnt * vol->bsize)
+		return EINVAL;
+
+	rc = hr_check_ba_range(vol, cnt, ba);
+	if (rc != EOK)
+		return rc;
+
+	fibril_mutex_lock(&big_lock);
+
+	size_t left = cnt;
+	while (left != 0) {
+		raid4_geometry(ba, vol, &extent, &phys_block);
+		hr_add_ba_offset(vol, &phys_block);
+		rc = block_write_direct(vol->devs[extent], phys_block, 1, data);
+		if (rc != EOK)
+			break;
+		rc = write_parity(vol, extent, phys_block, data);
+		if (rc != EOK)
+			break;
+		data = data + vol->bsize;
+		left--;
+		ba++;
+	}
+
+	fibril_mutex_unlock(&big_lock);
+	return rc;
+}
+
+static errno_t hr_raid4_bd_get_block_size(bd_srv_t *bd, size_t *rsize)
+{
+	hr_volume_t *vol = bd->srvs->sarg;
+
+	*rsize = vol->bsize;
+	return EOK;
+}
+
+static errno_t hr_raid4_bd_get_num_blocks(bd_srv_t *bd, aoff64_t *rnb)
+{
+	hr_volume_t *vol = bd->srvs->sarg;
+
+	*rnb = vol->data_blkno;
+	return EOK;
+}
+
+errno_t hr_raid4_create(hr_volume_t *new_volume)
+{
+	errno_t rc;
+
+	assert(new_volume->level == hr_l_4);
+
+	if (new_volume->dev_no < 3) {
+		log_msg(LOG_DEFAULT, LVL_ERROR,
+		    "RAID 4 array needs at least 3 devices");
+		return EINVAL;
+	}
+
+	bd_srvs_init(&new_volume->hr_bds);
+	new_volume->hr_bds.ops = &hr_raid4_bd_ops;
+	new_volume->hr_bds.sarg = new_volume;
+
+	rc = hr_register_volume(new_volume);
+	if (rc != EOK)
+		return rc;
+
+	return EOK;
+}
+
+/** @}
+ */
Index: uspace/srv/bd/hr/superblock.c
===================================================================
--- uspace/srv/bd/hr/superblock.c	(revision a19d7fc4b9a9e9429e475bb951803a662991726d)
+++ uspace/srv/bd/hr/superblock.c	(revision 4a2a6b8b9267feafb10763f60d2b4685bb8bbf67)
@@ -78,4 +78,6 @@
 	} else if (vol->level == hr_l_0) {
 		data_blkno = vol->nblocks - (data_offset * vol->dev_no);
+	} else if (vol->level == hr_l_4) {
+		data_blkno = vol->nblocks - (data_offset * vol->dev_no) - (vol->nblocks / vol->dev_no);
 	} else {
 		log_msg(LOG_DEFAULT, LVL_ERROR,
Index: uspace/srv/bd/hr/util.c
===================================================================
--- uspace/srv/bd/hr/util.c	(revision a19d7fc4b9a9e9429e475bb951803a662991726d)
+++ uspace/srv/bd/hr/util.c	(revision 4a2a6b8b9267feafb10763f60d2b4685bb8bbf67)
@@ -159,5 +159,5 @@
 	if (vol->level == hr_l_1) {
 		vol->nblocks = total_blocks / vol->dev_no;
-	} else if (vol->level == hr_l_0) {
+	} else if (vol->level == hr_l_0 || vol->level == hr_l_4) {
 		vol->nblocks = total_blocks;
 	} else {
@@ -172,11 +172,14 @@
 }
 
-errno_t hr_calc_ba(hr_volume_t *vol, size_t cnt, uint64_t *ba)
+errno_t hr_check_ba_range(hr_volume_t *vol, size_t cnt, uint64_t ba)
 {
-	if (*ba + cnt > vol->data_blkno)
+	if (ba + cnt > vol->data_blkno)
 		return ERANGE;
+	return EOK;
+}
 
+void hr_add_ba_offset(hr_volume_t *vol, uint64_t *ba)
+{
 	*ba = *ba + vol->data_offset;
-	return EOK;
 }
 
Index: uspace/srv/bd/hr/util.h
===================================================================
--- uspace/srv/bd/hr/util.h	(revision a19d7fc4b9a9e9429e475bb951803a662991726d)
+++ uspace/srv/bd/hr/util.h	(revision 4a2a6b8b9267feafb10763f60d2b4685bb8bbf67)
@@ -45,5 +45,6 @@
 extern errno_t hr_register_volume(hr_volume_t *);
 extern errno_t hr_check_devs(hr_volume_t *vol);
-errno_t hr_calc_ba(hr_volume_t *vol, size_t cnt, uint64_t *ba);
+errno_t hr_check_ba_range(hr_volume_t *vol, size_t cnt, uint64_t ba);
+void hr_add_ba_offset(hr_volume_t *vol, uint64_t *ba);
 
 #endif
Index: uspace/srv/bd/hr/var.h
===================================================================
--- uspace/srv/bd/hr/var.h	(revision a19d7fc4b9a9e9429e475bb951803a662991726d)
+++ uspace/srv/bd/hr/var.h	(revision 4a2a6b8b9267feafb10763f60d2b4685bb8bbf67)
@@ -72,4 +72,5 @@
 extern errno_t hr_raid0_create(hr_volume_t *);
 extern errno_t hr_raid1_create(hr_volume_t *);
+extern errno_t hr_raid4_create(hr_volume_t *);
 
 #endif
