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

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

commit 2b1797eb96a2fefad06f66816289b77aee0ea3cf
Author: Marco Casaroli <[email protected]>
AuthorDate: Fri Nov 27 14:47:25 2026 +0100

    fs/tmpfs: Do not keep the directory of an unlinked open file.
    
    An open file that loses its name (unlink(), a rename() over it, or the
    unmount) stays until its last close.  It kept the pointer to its
    directory, but it holds no reference on that directory: rmdir() can
    remove the directory while the file is still open.  A later
    FIOC_FILEPATH on the file (F_GETPATH, readlink() of /proc/PID/fd/N, and
    every close() when CONFIG_FS_NOTIFY is set) then used the directory that
    rmdir() had removed.  To see it: open DIR/f, unlink DIR/f, rmdir DIR, then
    close the file.  The text file of a program that ran from DIR does the
    same when the last process of the program ends.
    
    Clear the directory pointer when the name of an open file goes, as the
    pseudo file system does in inode_unlink().  FIOC_FILEPATH on an unlinked
    file gives -ENOENT, as it did before when the directory was still there.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 fs/tmpfs/fs_tmpfs.c | 31 ++++++++++++++++++++++++++++---
 1 file changed, 28 insertions(+), 3 deletions(-)

diff --git a/fs/tmpfs/fs_tmpfs.c b/fs/tmpfs/fs_tmpfs.c
index 67da959a79f..a38f5916590 100644
--- a/fs/tmpfs/fs_tmpfs.c
+++ b/fs/tmpfs/fs_tmpfs.c
@@ -104,6 +104,7 @@ static int  tmpfs_realloc_directory(FAR struct 
tmpfs_directory_s *tdo,
               unsigned int nentries);
 static int  tmpfs_realloc_file(FAR struct tmpfs_file_s *tfo,
               size_t newsize);
+static void tmpfs_orphan_file(FAR struct tmpfs_file_s *tfo);
 static void tmpfs_release_lockedobject(FAR struct tmpfs_object_s *to);
 static void tmpfs_release_lockedfile(FAR struct tmpfs_file_s *tfo);
 static int  tmpfs_release_file(FAR struct tmpfs_file_s *tfo);
@@ -449,6 +450,23 @@ static int tmpfs_realloc_file(FAR struct tmpfs_file_s *tfo,
   return OK;
 }
 
+/****************************************************************************
+ * Name: tmpfs_orphan_file
+ *
+ * Description:
+ *   The name of an open file goes: the file stays until its last close,
+ *   but it is in no directory.  Its directory can go before it, so the file
+ *   must not keep a pointer to that directory.  The caller holds the lock
+ *   of the file system.
+ *
+ ****************************************************************************/
+
+static void tmpfs_orphan_file(FAR struct tmpfs_file_s *tfo)
+{
+  tfo->tfo_flags  |= TFO_FLAG_UNLINKED;
+  tfo->tfo_parent  = NULL;
+}
+
 /****************************************************************************
  * Name: tmpfs_release_lockedobject
  ****************************************************************************/
@@ -733,7 +751,7 @@ static int tmpfs_replace_dirent(FAR struct 
tmpfs_directory_s *tdo,
 
       if (oldfile->tfo_refs > 0)
         {
-          oldfile->tfo_flags |= TFO_FLAG_UNLINKED;
+          tmpfs_orphan_file(oldfile);
           tmpfs_unlock_file(oldfile);
         }
       else
@@ -1667,7 +1685,7 @@ static int tmpfs_free_callout(FAR struct 
tmpfs_directory_s *tdo,
         {
           /* Yes.. We cannot delete the file now. Just mark it as unlinked. */
 
-          tfo->tfo_flags |= TFO_FLAG_UNLINKED;
+          tmpfs_orphan_file(tfo);
           return TMPFS_UNLINKED;
         }
 
@@ -2464,6 +2482,13 @@ static int tmpfs_ioctl(FAR struct file *filep, int cmd, 
unsigned long arg)
           return ret;
         }
 
+      /* An unlinked file has no path */
+
+      if ((tfo->tfo_flags & TFO_FLAG_UNLINKED) != 0)
+        {
+          return -ENOENT;
+        }
+
       ret = tmpfs_getpath((FAR struct tmpfs_object_s *)tfo, ptr, PATH_MAX);
       if (ret < 0)
         {
@@ -3098,7 +3123,7 @@ static int tmpfs_unlink(FAR struct inode *mountpt, FAR 
const char *relpath)
     {
       /* Make the file object as unlinked */
 
-      tfo->tfo_flags |= TFO_FLAG_UNLINKED;
+      tmpfs_orphan_file(tfo);
 
       /* Release the reference count on the file object */
 

Reply via email to