Linux Btrfs filesystem development
 help / color / mirror / Atom feed
From: Qu Wenruo <wqu@suse.com>
To: linux-btrfs@vger.kernel.org
Subject: [PATCH 1/4] btrfs-progs: mkfs/rootdir: use a helper to read from source fs
Date: Mon, 30 Mar 2026 17:39:35 +1030	[thread overview]
Message-ID: <2c49617dd22bc3884f397f2ce3c939ec5750b7d3.1774854459.git.wqu@suse.com> (raw)
In-Reply-To: <cover.1774854459.git.wqu@suse.com>

We have 3 pread() call sites that are doing a simple loop to read data
from the source fs.

They have similar and duplicated handling, extract a simple helper,
read_from_source() to handle them all.

Signed-off-by: Qu Wenruo <wqu@suse.com>
---
 mkfs/rootdir.c | 85 +++++++++++++++++++++++---------------------------
 1 file changed, 39 insertions(+), 46 deletions(-)

diff --git a/mkfs/rootdir.c b/mkfs/rootdir.c
index ed0fcfccd42a..f7712d75aa6f 100644
--- a/mkfs/rootdir.c
+++ b/mkfs/rootdir.c
@@ -780,6 +780,29 @@ static int do_reflink_write(struct btrfs_fs_info *info,
 	return 0;
 }
 
+static int read_from_source(const struct btrfs_fs_info *fs_info,
+			    const char *path, int fd,
+			    char *buf, u64 filepos, u32 length)
+{
+	u32 cur = 0;
+
+	UASSERT(IS_ALIGNED(filepos, fs_info->sectorsize));
+	UASSERT(length <= MAX_EXTENT_SIZE);
+
+	while (cur < length) {
+		ssize_t ret;
+
+		ret = pread(fd, buf, length - cur, filepos + cur);
+		if (ret < 0) {
+			error("cannot read %s at offset %llu length %u: %m",
+			      path, filepos + cur, length - cur);
+			return -errno;
+		}
+		cur += ret;
+	}
+	return length;
+}
+
 static int add_file_item_extent(struct btrfs_trans_handle *trans,
 				struct btrfs_root *root,
 				struct btrfs_inode_item *btrfs_inode,
@@ -789,7 +812,7 @@ static int add_file_item_extent(struct btrfs_trans_handle *trans,
 {
 	int ret;
 	u32 sectorsize = root->fs_info->sectorsize;
-	u64 bytes_read, first_block, to_read, to_write;
+	u64 first_block, to_read, to_write;
 	struct btrfs_key key;
 	struct btrfs_file_extent_item stack_fi = { 0 };
 	u64 buf_size;
@@ -840,24 +863,11 @@ static int add_file_item_extent(struct btrfs_trans_handle *trans,
 
 	buf_size = do_comp ? BTRFS_MAX_COMPRESSED : MAX_EXTENT_SIZE;
 	to_read = min_t(u64, file_pos + buf_size, next) - file_pos;
-	bytes_read = 0;
-
-	while (bytes_read < to_read) {
-		ssize_t ret_read;
-
-		ret_read = pread(source->fd, source->buf + bytes_read,
-				 to_read - bytes_read, file_pos + bytes_read);
-		if (ret_read < 0) {
-			error("cannot read %s at offset %llu length %llu: %m",
-			      source->path_name, file_pos + bytes_read,
-			      to_read - bytes_read);
-			return -errno;
-		}
-
-		bytes_read += ret_read;
-	}
-
-	if (bytes_read <= sectorsize)
+	ret = read_from_source(root->fs_info, source->path_name, source->fd,
+			       source->buf, file_pos, to_read);
+	if (ret < 0)
+		return ret;
+	if (to_read <= sectorsize)
 		do_comp = false;
 
 	if (do_comp) {
@@ -866,21 +876,20 @@ static int add_file_item_extent(struct btrfs_trans_handle *trans,
 		switch (g_compression) {
 		case BTRFS_COMPRESS_ZLIB:
 			comp_ret = zlib_compress_extent(first_sector, sectorsize,
-							source->buf, bytes_read,
+							source->buf, to_read,
 							source->comp_buf);
 			break;
 #if COMPRESSION_LZO
 		case BTRFS_COMPRESS_LZO:
 			comp_ret = lzo_compress_extent(sectorsize, source->buf,
-						       bytes_read,
-						       source->comp_buf,
+						       to_read, source->comp_buf,
 						       source->wrkmem);
 			break;
 #endif
 #if COMPRESSION_ZSTD
 		case BTRFS_COMPRESS_ZSTD:
 			comp_ret = zstd_compress_extent(first_sector, sectorsize,
-							source->buf, bytes_read,
+							source->buf, to_read,
 							source->comp_buf);
 			break;
 #endif
@@ -905,24 +914,10 @@ static int add_file_item_extent(struct btrfs_trans_handle *trans,
 
 			buf_size = MAX_EXTENT_SIZE;
 			to_read = min_t(u64, file_pos + buf_size, next) - file_pos;
-
-			while (bytes_read < to_read) {
-				ssize_t ret_read;
-
-				ret_read = pread(source->fd,
-						 source->buf + bytes_read,
-						 to_read - bytes_read,
-						 file_pos + bytes_read);
-				if (ret_read < 0) {
-					error("cannot read %s at offset %llu length %llu: %m",
-					      source->path_name,
-					      file_pos + bytes_read,
-					      to_read - bytes_read);
-					return -errno;
-				}
-
-				bytes_read += ret_read;
-			}
+			ret = read_from_source(root->fs_info, source->path_name, source->fd,
+					       source->buf, file_pos, to_read);
+			if (ret < 0)
+				return ret;
 		}
 	}
 
@@ -1301,7 +1296,6 @@ static int add_file_items(struct btrfs_trans_handle *trans,
 {
 	struct btrfs_fs_info *fs_info = trans->fs_info;
 	int ret = -1;
-	ssize_t ret_read;
 	u32 sectorsize = fs_info->sectorsize;
 	u64 file_pos = 0;
 	char *buf = NULL, *comp_buf = NULL, *wrkmem = NULL;
@@ -1340,10 +1334,9 @@ static int add_file_items(struct btrfs_trans_handle *trans,
 			goto end;
 		}
 
-		ret_read = pread(fd, buffer, st->st_size, 0);
-		if (ret_read == -1) {
-			error("cannot read %s at offset %u length %zu: %m",
-			      path_name, 0, st->st_size);
+		ret = read_from_source(fs_info, path_name, fd, buffer,
+					    0, st->st_size);
+		if (ret < 0) {
 			free(buffer);
 			goto end;
 		}
-- 
2.53.0


  reply	other threads:[~2026-03-30  7:10 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-03-30  7:09 [PATCH 0/4] btrfs-progs: mkfs/rootdir: cleanup and new fiemap based prealloc detection Qu Wenruo
2026-03-30  7:09 ` Qu Wenruo [this message]
2026-03-30  7:09 ` [PATCH 2/4] btrfs-progs: mkfs/rootdir: extract compressed write path Qu Wenruo
2026-03-30  7:09 ` [PATCH 3/4] btrfs-progs: mkfs/rootdir: use fiemap to do prealloc detection Qu Wenruo
2026-03-30  7:09 ` [PATCH 4/4] btrfs-progs: mkfs-tests: add a new test case for fiemap based detection Qu Wenruo

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=2c49617dd22bc3884f397f2ce3c939ec5750b7d3.1774854459.git.wqu@suse.com \
    --to=wqu@suse.com \
    --cc=linux-btrfs@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox