mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH 1/5] tftp: skip memory allocation when seeking to current position
@ 2026-03-12 14:44 Ahmad Fatoum
  2026-03-12 14:44 ` [PATCH 2/5] tftp: warn when seek-heavy access wastes network bandwidth Ahmad Fatoum
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: Ahmad Fatoum @ 2026-03-12 14:44 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

We currently allocate 1K of memory, only to discard it when seeking to
the same offset, we are currently at.

Restructure tftp_lseek() to use early returns for the unsupported
backward-seek and early-exit also for the no-op case of new pos being
the same as the old pos.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 fs/tftp.c | 63 +++++++++++++++++++++++++++++--------------------------
 1 file changed, 33 insertions(+), 30 deletions(-)

diff --git a/fs/tftp.c b/fs/tftp.c
index a454306b4b5a..dd4804041cde 100644
--- a/fs/tftp.c
+++ b/fs/tftp.c
@@ -902,42 +902,45 @@ static int tftp_read(struct file *f, void *buf, size_t insize)
 
 static int tftp_lseek(struct file *f, loff_t pos)
 {
-	/* We cannot seek backwards without reloading or caching the file */
+	int ret = 0;
+	char *buf;
 	loff_t f_pos = f->f_pos;
 
-	if (pos >= f_pos) {
-		int ret = 0;
-		char *buf = xmalloc(1024);
-
-		while (pos > f_pos) {
-			size_t len = min_t(size_t, 1024, pos - f_pos);
-
-			ret = tftp_read(f, buf, len);
-
-			if (!ret)
-				/* EOF, so the desired pos is invalid. */
-				ret = -EINVAL;
-			if (ret < 0)
-				goto out_free;
-
-			f_pos += ret;
-		}
-
-out_free:
-		free(buf);
-		if (ret < 0) {
-			/*
-			 * Update f->pos even if the overall request
-			 * failed since we can't move backwards
-			 */
-			f->f_pos = f_pos;
-			return ret;
-		}
+	/* We cannot seek backwards without reloading or caching the file */
+	if (pos < f_pos)
+		return -ENOSYS;
 
+	if (pos == f_pos)
 		return 0;
+
+	buf = xmalloc(1024);
+
+	while (pos > f_pos) {
+		size_t len = min_t(size_t, 1024, pos - f_pos);
+
+		ret = tftp_read(f, buf, len);
+
+		if (!ret)
+			/* EOF, so the desired pos is invalid. */
+			ret = -EINVAL;
+		if (ret < 0)
+			goto out_free;
+
+		f_pos += ret;
 	}
 
-	return -ENOSYS;
+out_free:
+	free(buf);
+	if (ret < 0) {
+		/*
+		 * Update f->pos even if the overall request
+		 * failed since we can't move backwards
+		 */
+		f->f_pos = f_pos;
+		return ret;
+	}
+
+	return 0;
 }
 
 static const struct inode_operations tftp_file_inode_operations;
-- 
2.47.3




^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2026-03-17 10:21 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-03-12 14:44 [PATCH 1/5] tftp: skip memory allocation when seeking to current position Ahmad Fatoum
2026-03-12 14:44 ` [PATCH 2/5] tftp: warn when seek-heavy access wastes network bandwidth Ahmad Fatoum
2026-03-12 14:44 ` [PATCH 3/5] tftp: emulate backwards seek by reopening the file Ahmad Fatoum
2026-03-12 14:44 ` [PATCH 4/5] libfile: drop TFTP-specific hack in open_fdt() Ahmad Fatoum
2026-03-12 14:44 ` [PATCH 5/5] fs: fix relative path resolution when CWD is on a TFTP mount Ahmad Fatoum
2026-03-17 10:21 ` [PATCH 1/5] tftp: skip memory allocation when seeking to current position Sascha Hauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox