This is an automated email from the ASF dual-hosted git repository.

acassis pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit cb60949d9af61ecb870b6e320e9990169c425af6
Author: Marco Casaroli <[email protected]>
AuthorDate: Wed Nov 11 06:22:28 2026 +0100

    fs/procfs: Give every path a file serial number.
    
    stat() and fstat() in procfs gave st_ino 0 for every path, and readdir()
    left d_ino alone.  A program that walks a tree compares the device and
    the serial number of a directory with those of its parent directories.
    Each subdirectory of /proc then looked like /proc: find said "File
    system loop detected" and did not go into it.
    
    Make the number the hash of the path in procfs (inode_hash()).  stat()
    computes it from the path, and open() keeps it in struct procfs_file_s
    for fstat(); the dup() of each handler copies that structure.  opendir()
    keeps the hash of the directory path in struct procfs_dir_priv_s, and
    readdir() adds the name of the entry to it.  So readdir() gives in
    d_ino the same number that stat() gives in st_ino.
    
    Each open file and each open directory of procfs takes 4 bytes more.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 fs/procfs/fs_procfs.c     | 33 ++++++++++++++++++++++++++++++---
 include/nuttx/fs/procfs.h |  2 ++
 2 files changed, 32 insertions(+), 3 deletions(-)

diff --git a/fs/procfs/fs_procfs.c b/fs/procfs/fs_procfs.c
index 91c0e83750d..d344a213cf1 100644
--- a/fs/procfs/fs_procfs.c
+++ b/fs/procfs/fs_procfs.c
@@ -48,6 +48,7 @@
 #include <nuttx/fs/fs.h>
 #include <nuttx/fs/procfs.h>
 
+#include "inode/inode.h"
 #include "mount/mount.h"
 #include "sched/sched.h"
 #include "fs_heap.h"
@@ -419,6 +420,7 @@ static void procfs_sort_pid(FAR struct procfs_level0_s 
*level0)
 static int procfs_open(FAR struct file *filep, FAR const char *relpath,
                        int oflags, mode_t mode)
 {
+  FAR struct procfs_file_s *attr;
   int x;
   int ret = -ENOENT;
 
@@ -447,8 +449,9 @@ static int procfs_open(FAR struct file *filep, FAR const 
char *relpath,
             {
               DEBUGASSERT(filep->f_priv);
 
-              ((FAR struct procfs_file_s *)filep->f_priv)->procfsentry =
-                                    &g_procfs_entries[x];
+              attr = filep->f_priv;
+              attr->procfsentry = &g_procfs_entries[x];
+              attr->ino = inode_hash(INODE_HASH_INIT, relpath);
               break;
             }
         }
@@ -623,6 +626,7 @@ static int procfs_fstat(FAR const struct file *filep, FAR 
struct stat *buf)
    */
 
   memset(buf, 0, sizeof(struct stat));
+  buf->st_ino  = handler->ino;
   buf->st_mode = S_IFREG | S_IROTH | S_IRGRP | S_IRUSR;
 
   /* If the write method is provided, then let's also claim that the file is
@@ -649,10 +653,19 @@ static int procfs_opendir(FAR struct inode *mountpt, FAR 
const char *relpath,
                           FAR struct fs_dirent_s **dir)
 {
   FAR struct procfs_level0_s *level0;
+  uint32_t hash;
 
   finfo("relpath: \"%s\"\n", relpath ? relpath : "NULL");
   DEBUGASSERT(mountpt && dir && relpath);
 
+  /* readdir() adds the name of each entry to the hash of this path */
+
+  hash = inode_hash(INODE_HASH_INIT, relpath);
+  if (relpath[0] != '\0')
+    {
+      hash = inode_hash(hash, "/");
+    }
+
   /* The relative must be either:
    *
    * ""      - The top level directory of task/thread IDs
@@ -703,6 +716,7 @@ static int procfs_opendir(FAR struct inode *mountpt, FAR 
const char *relpath,
       level0->lastread = "";
       level0->lastlen = 0;
       level0->base.procfsentry = NULL;
+      level0->base.hash = hash;
 
       *dir = (FAR struct fs_dirent_s *)level0;
     }
@@ -743,6 +757,7 @@ static int procfs_opendir(FAR struct inode *mountpt, FAR 
const char *relpath,
 
                   dirpriv = (FAR struct procfs_dir_priv_s *)(*dir);
                   dirpriv->procfsentry = &g_procfs_entries[x];
+                  dirpriv->hash = hash;
                 }
 
               return ret;
@@ -778,6 +793,7 @@ static int procfs_opendir(FAR struct inode *mountpt, FAR 
const char *relpath,
               level1->lastread = "";
               level1->lastlen = 0;
               level1->base.procfsentry = NULL;
+              level1->base.hash = hash;
 
               *dir = (FAR struct fs_dirent_s *)level1;
               break;
@@ -1088,6 +1104,11 @@ static int procfs_readdir(FAR struct inode *mountpt,
       ret = priv->procfsentry->ops->readdir(dir, entry);
     }
 
+  if (ret >= 0)
+    {
+      entry->d_ino = inode_hash(priv->hash, entry->d_name);
+    }
+
   return ret;
 }
 
@@ -1225,7 +1246,8 @@ static int procfs_stat(FAR struct inode *mountpt, FAR 
const char *relpath,
               DEBUGASSERT(g_procfs_entries[x].ops &&
                           g_procfs_entries[x].ops->stat);
 
-              return g_procfs_entries[x].ops->stat(relpath, buf);
+              ret = g_procfs_entries[x].ops->stat(relpath, buf);
+              break;
             }
 
           /* Test for an internal subdirectory stat */
@@ -1244,6 +1266,11 @@ static int procfs_stat(FAR struct inode *mountpt, FAR 
const char *relpath,
         }
     }
 
+  if (ret >= 0)
+    {
+      buf->st_ino = inode_hash(INODE_HASH_INIT, relpath);
+    }
+
   return ret;
 }
 
diff --git a/include/nuttx/fs/procfs.h b/include/nuttx/fs/procfs.h
index ed80e5782a3..ae2e1edf663 100644
--- a/include/nuttx/fs/procfs.h
+++ b/include/nuttx/fs/procfs.h
@@ -116,6 +116,7 @@ struct procfs_entry_s
 struct procfs_file_s
 {
   FAR const struct procfs_entry_s *procfsentry;
+  ino_t ino;                                    /* File serial number */
 };
 
 /* The generic proc/ pseudo directory structure */
@@ -126,6 +127,7 @@ struct procfs_dir_priv_s
   uint8_t level;                                /* Directory level.  Currently 
0 or 1 */
   uint16_t index;                               /* Index to the next directory 
entry */
   uint16_t nentries;                            /* Number of directory entries 
*/
+  uint32_t hash;                                /* Path hash, for d_ino */
   FAR const struct procfs_entry_s *procfsentry; /* Pointer to procfs handler 
entry */
 };
 

Reply via email to