Skip to contentSkip to conversation

9pfs: add some file operation hooks

PATCH Superseded Juergen Gross 2 participants 7 messages Mar 20, 2025

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover 9pfs: add some file operation hooks

For being able to store the Xenstore state in preparation of a live update operation, 9pfront is needing support for fstat and lseek.

Juergen Gross (2):
Mini-OS: 9pfs: add fstat file operation hook
Mini-OS: 9pfs: add lseek file operation hook

 9pfront.c | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)
Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/2 9pfs: add fstat file operation hook

Add a file operations fstat hook to the 9pfs frontend.

---
 9pfront.c | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)
9pfront.c +29 -0
index 1741d600..a65fe26d 100644
@@ -85,6 +85,8 @@ struct file_9pfs {
 
 #define P9_QID_SIZE    13
 
+#define QID_TYPE_DIR   0x80     /* Applies to qid[0]. */
+
 struct p9_header {
     uint32_t size;
     uint8_t cmd;
@@ -950,6 +952,32 @@ static int write_9pfs(struct file *file, const void *buf, size_t nbytes)
     return ret;
 }
 
+static int fstat_9pfs(struct file *file, struct stat *buf)
+{
+    struct file_9pfs *f9pfs = file->filedata;
+    struct p9_stat stat;
+    int ret;
+
+    ret = p9_stat(f9pfs->dev, f9pfs->fid, &stat);
+    if ( ret )
+    {
+        errno = EIO;
+        return -1;
+    }
+
+    buf->st_mode = (stat.qid[0] == QID_TYPE_DIR) ? S_IFDIR : S_IFREG;
+    buf->st_mode = stat.mode & 0777;
+    buf->st_atime = stat.atime;
+    buf->st_mtime = stat.mtime;
+    buf->st_size = stat.length;
+    buf->st_uid = stat.n_uid;
+    buf->st_gid = stat.n_gid;
+
+    free_stat(&stat);
+
+    return 0;
+}
+
 static int close_9pfs(struct file *file)
 {
     struct file_9pfs *f9pfs = file->filedata;
@@ -1296,6 +1324,7 @@ static const struct file_ops ops_9pfs = {
     .read = read_9pfs,
     .write = write_9pfs,
     .close = close_9pfs,
+    .fstat = fstat_9pfs,
 };
 
 __attribute__((constructor))
Signature · 1 line
2.43.0
#2 lore raw

Re: [MINI-OS PATCH 1/2] 9pfs: add fstat file operation hook

Quoted 14 lines · Juergen Gross

Add a file operations fstat hook to the 9pfs frontend.

---

9pfront.c +5 -0
@@ -950,6 +952,32 @@ static int write_9pfs(struct file *file, const void *buf, size_t nbytes)
      return ret;
  }
 
+static int fstat_9pfs(struct file *file, struct stat *buf)
+{
+    struct file_9pfs *f9pfs = file->filedata;
+    struct p9_stat stat;
+    int ret;

9pfront.c static int write_9pfs(struct file *file, const void *buf, size_t nbytes) · line 969

Quoted 10 lines
9pfront.c +10 -0
+
+    ret = p9_stat(f9pfs->dev, f9pfs->fid, &stat);
+    if ( ret )
+    {
+        errno = EIO;
+        return -1;
+    }
+
+    buf->st_mode = (stat.qid[0] == QID_TYPE_DIR) ? S_IFDIR : S_IFREG;
+    buf->st_mode = stat.mode & 0777;

I think you want |= stat.mode & 0x777 here.

Regards,
Jason

#3 lore raw

Re: [MINI-OS PATCH 1/2] 9pfs: add fstat file operation hook

Quoted 19 lines · Jason Andryuk
Quoted 18 lines · Juergen Gross

Add a file operations fstat hook to the 9pfs frontend.

---

9pfront.c +9 -0
@@ -950,6 +952,32 @@ static int write_9pfs(struct file *file, const void *buf, 
 size_t nbytes)
       return ret;
   }
+static int fstat_9pfs(struct file *file, struct stat *buf)
+{
+    struct file_9pfs *f9pfs = file->filedata;
+    struct p9_stat stat;
+    int ret;
+
+    ret = p9_stat(f9pfs->dev, f9pfs->fid, &stat);
+    if ( ret )
+    {
Quoted 8 lines

9pfront.c static int write_9pfs(struct file *file, const void *buf, size_t nbytes) · line 969

Quoted 6 lines
9pfront.c +6 -0
+        errno = EIO;
+        return -1;
+    }
+
+    buf->st_mode = (stat.qid[0] == QID_TYPE_DIR) ? S_IFDIR : S_IFREG;
+    buf->st_mode = stat.mode & 0777;

I think you want |= stat.mode & 0x777 here.

Indeed. Thanks for catching this one.

Juergen

Reviewed-by
#4 lore raw

Re: [MINI-OS PATCH 1/2] 9pfs: add fstat file operation hook

Quoted 29 lines · Jürgen Groß
Quoted 19 lines · Jason Andryuk
Quoted 18 lines · Juergen Gross

Add a file operations fstat hook to the 9pfs frontend.

---

9pfront.c +9 -0
@@ -950,6 +952,32 @@ static int write_9pfs(struct file *file, const 
 void *buf, size_t nbytes)
       return ret;
   }
+static int fstat_9pfs(struct file *file, struct stat *buf)
+{
+    struct file_9pfs *f9pfs = file->filedata;
+    struct p9_stat stat;
+    int ret;
+
+    ret = p9_stat(f9pfs->dev, f9pfs->fid, &stat);
+    if ( ret )
+    {
Quoted 8 lines

9pfront.c static int write_9pfs(struct file *file, const void *buf, size_t nbytes) · line 969

Quoted 6 lines
9pfront.c +6 -0
+        errno = EIO;
+        return -1;
+    }
+
+    buf->st_mode = (stat.qid[0] == QID_TYPE_DIR) ? S_IFDIR : S_IFREG;
+    buf->st_mode = stat.mode & 0777;

I think you want |= stat.mode & 0x777 here.

Quoted 1 line

Indeed. Thanks for catching this one.

With that,

Regards,
Jason

#5 lore raw

PATCH 2/2 9pfs: add lseek file operation hook

In reply to Juergen Gross

Add a file operations lseek hook to the 9pfs frontend. Just use the lseek_default() implementation.

---
 9pfront.c | 1 +
 1 file changed, 1 insertion(+)
9pfront.c +1 -0
index a65fe26d..837e404b 100644
@@ -1325,6 +1325,7 @@ static const struct file_ops ops_9pfs = {
     .write = write_9pfs,
     .close = close_9pfs,
     .fstat = fstat_9pfs,
+    .lseek = lseek_default,
 };
 
 __attribute__((constructor))
Signature · 1 line
2.43.0
Reviewed-by on v1 2/2
#6 lore raw

Re: [MINI-OS PATCH 2/2] 9pfs: add lseek file operation hook

Quoted 4 lines · Juergen Gross

Add a file operations lseek hook to the 9pfs frontend. Just use the lseek_default() implementation.

lseek_default adjusts file->offset, and read_9pfs()/write_9pfs() are already using that.

Regards,
Jason