[PATCH 3/5] tftp: emulate backwards seek by reopening the file
Ahmad Fatoum
a.fatoum at pengutronix.de
Thu Mar 12 07:44:23 PDT 2026
TFTP is a streaming protocol with no native seek support.
We emulate forward seeks are emulated by reading and discarding data.
Backwards seeks were previously rejected with -ENOSYS, breaking higher
level code that assumes seeking to be possible.
Given that we have a heuristic in-place for detecting excessive data
discards through seeking, let's implement backwards seeking to allow the
pattern of reading the start of a buffer to detect type at one location
and then reading everything at once at a later time.
Signed-off-by: Ahmad Fatoum <a.fatoum at pengutronix.de>
---
fs/tftp.c | 33 +++++++++++++++++++++++++++++++--
1 file changed, 31 insertions(+), 2 deletions(-)
diff --git a/fs/tftp.c b/fs/tftp.c
index e5a276ed5cae..8683c3841c68 100644
--- a/fs/tftp.c
+++ b/fs/tftp.c
@@ -822,6 +822,9 @@ static int tftp_close(struct inode *inode, struct file *f)
{
struct file_priv *priv = f->private_data;
+ if (!priv)
+ return 0;
+
return tftp_do_close(priv);
}
@@ -831,6 +834,9 @@ static int tftp_write(struct file *f, const void *inbuf, size_t insize)
size_t size, now;
int ret;
+ if (!priv)
+ return -EREMOTEIO;
+
pr_vdebug("%s: %zu\n", __func__, insize);
size = insize;
@@ -866,6 +872,9 @@ static int tftp_read(struct file *f, void *buf, size_t insize)
size_t outsize = 0, now;
int ret = 0;
+ if (!priv)
+ return -EREMOTEIO;
+
pr_vdebug("%s %zu\n", __func__, insize);
while (insize) {
@@ -902,14 +911,34 @@ static int tftp_read(struct file *f, void *buf, size_t insize)
static int tftp_lseek(struct file *f, loff_t pos)
{
+ struct file_priv *priv = f->private_data;
static loff_t seek_discard_total;
int ret = 0;
char *buf;
loff_t f_pos = f->f_pos;
+ if (!priv)
+ return -EREMOTEIO;
+
/* We cannot seek backwards without reloading or caching the file */
- if (pos < f_pos)
- return -ENOSYS;
+ if (pos < f_pos) {
+ /* We can reopen read streams, but not write streams */
+ if (priv->push)
+ return -ENOSYS;
+
+ tftp_do_close(priv);
+
+ priv = tftp_do_open(&f->fsdev->dev, f->f_flags,
+ f->f_path.dentry, false);
+ if (IS_ERR(priv)) {
+ f->private_data = NULL;
+ return PTR_ERR(priv);
+ }
+
+ f->private_data = priv;
+ f->f_pos = 0;
+ f_pos = 0;
+ }
if (pos == f_pos)
return 0;
--
2.47.3
More information about the barebox
mailing list