Sarthak-Pandey opened a new issue, #22528:
URL: https://github.com/apache/nuttx/issues/22528

   ### Description / Steps to reproduce the issue
   
   ### Description
   In `fs/vfs/fs_close.c`, when a file or device is closed via `file_close()`, 
if the underlying driver/filesystem's `close` callback 
(`inode->u.i_ops->close`) returns an error (`ret < 0`, e.g. I/O error `-EIO` or 
network timeout), the inode reference is not released because 
`inode_release(inode)` is guarded by `if (ret >= 0)`.
   
   However, `filep->f_inode = NULL;` is executed unconditionally right after:
   
   ```c
         if (ret >= 0)
           {
   #ifdef CONFIG_FS_NOTIFY
             if (path != NULL)
               {
                 notify_close(path, filep->f_oflags);
                 lib_put_pathbuffer(path);
               }
   #endif
   
             inode_release(inode);
           }
   #ifdef CONFIG_FS_NOTIFY
         else if (path != NULL)
           {
             lib_put_pathbuffer(path);
           }
   #endif
   
         filep->f_inode = NULL;
   ```
   
   ### Impact & Root Cause
   1. When `ret < 0`, `inode_release(inode)` is skipped, leaving 
`inode->i_crefs` permanently incremented.
   2. Because `filep->f_inode = NULL;` is executed anyway, the caller loses the 
handle and cannot retry the close.
   3. Subsequent `umount()` calls fail indefinitely with `-EBUSY` because the 
inode reference count never drops back to zero.
   4. According to POSIX `close()` semantics, file descriptor resources and 
inode associations must be released regardless of whether the driver 
encountered an error during flush/close.
   
   ### Steps to Reproduce
   1. Open a file or character device whose driver `close` callback can return 
a negative error code (e.g. simulate `-EIO`).
   2. Record `inode->i_crefs`.
   3. Call `close(fd)` on the descriptor.
   4. Observe that `inode->i_crefs` is not decremented, leaking the inode 
reference permanently.
   5. Attempt to unmount the filesystem containing the file — unmount fails 
with `-EBUSY`.
   
   ### Expected Behavior
   `inode_release(inode)` must be called unconditionally in `file_close()` so 
that inode references are always cleaned up even if the driver's close 
operation fails.
   
   ### Proposed Fix
   Move `inode_release(inode)` outside the `if (ret >= 0)` check:
   ```diff
   --- a/fs/vfs/fs_close.c
   +++ b/fs/vfs/fs_close.c
   @@ -140,8 +140,6 @@ int file_close(FAR struct file *filep)
                  notify_close(path, filep->f_oflags);
                  lib_put_pathbuffer(path);
                }
   -#endif
   -
              inode_release(inode);
            }
    #ifdef CONFIG_FS_NOTIFY
   @@ -150,6 +148,8 @@ int file_close(FAR struct file *filep)
              lib_put_pathbuffer(path);
            }
    #endif
   +
   +      inode_release(inode);
          filep->f_inode = NULL;
        }
   ```
   
   ### On which OS does this issue occur?
   
   [OS: Windows]
   
   ### What is the version of your OS?
   
   All (Architecture and OS independent logic defect)
   
   ### NuttX Version
   
   master
   
   ### Issue Architecture
   
   [Arch: all]
   
   ### Issue Area
   
   [Area: File System]
   
   ### Host information
   
   N/A (Generic VFS logic issue in fs/vfs/fs_close.c)
   
   ### Verification
   
   - [x] I have verified before submitting the report.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to