fs/vfs: Separate file descriptors from file descriptions

This patch is a rework of the NuttX file descriptor implementation. The
goal is two-fold:

1. Improve POSIX compliance. The old implementation tied file description
to inode only, not the file struct. POSIX however dictates otherwise.
2. Fix a bug with descriptor duplication (dup2() and dup3()). There is
an existing race condition with this POSIX API that currently results
in a kernel side crash.

The crash occurs when a partially open / closed file descriptor is
duplicated. The reason for the crash is that even if the descriptor is
closed, the file might still be in use by the kernel (due to e.g. ongoing
write to file). The open file data is changed by file_dup3() and this
causes a crash in the device / drivers themselves as they lose access to
the inode and private data.

The fix is done by separating struct file into file and file descriptor
structs. The file struct can live on even if the descriptor is closed,
fixing the crash. This also fixes the POSIX issue, as two descriptors
can now point to the same file.

Signed-off-by: Ville Juven <ville.juven@unikie.com>
Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
This commit is contained in:
Ville Juven
2025-06-12 18:12:42 +08:00
committed by Xiang Xiao
parent a12d21e830
commit b8e30b54ec
29 changed files with 837 additions and 753 deletions
+4 -4
View File
@@ -456,10 +456,10 @@ static void dump_backtrace(FAR struct tcb_s *tcb, FAR void *arg)
****************************************************************************/
#ifdef CONFIG_SCHED_DUMP_ON_EXIT
static void dump_filelist(FAR struct tcb_s *tcb, FAR void *arg)
static void dump_fdlist(FAR struct tcb_s *tcb, FAR void *arg)
{
FAR struct filelist *filelist = &tcb->group->tg_filelist;
files_dumplist(filelist);
FAR struct fdlist *list = &tcb->group->tg_fdlist;
fdlist_dump(list);
}
#endif
@@ -548,7 +548,7 @@ static void dump_tasks(void)
#endif
#ifdef CONFIG_SCHED_DUMP_ON_EXIT
nxsched_foreach(dump_filelist, NULL);
nxsched_foreach(dump_fdlist, NULL);
#endif
}