From b99e7617aa2fa70f8724a2a7db4b08e723a09bb4 Mon Sep 17 00:00:00 2001 From: chao an Date: Tue, 22 Oct 2024 13:26:41 +0800 Subject: [PATCH] fs/inode: refresh tcb after each file sync() is completed After tcb is destroyed, it is very dangerous to back reference tcb through file. This commit will perform file operations while ensuring the validity of tcb during fsync, with will avoid tcb check in each subsystem. Signed-off-by: chao an --- fs/inode/fs_files.c | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/fs/inode/fs_files.c b/fs/inode/fs_files.c index 42cd054759a..8374ad06d30 100644 --- a/fs/inode/fs_files.c +++ b/fs/inode/fs_files.c @@ -208,32 +208,43 @@ static int files_extend(FAR struct filelist *list, size_t row) static void task_fssync(FAR struct tcb_s *tcb, FAR void *arg) { - FAR struct filelist *list; + FAR struct tcb_s *ctcb; + FAR struct file *filep; + int pid = tcb->pid; + uint8_t rows; int i; int j; - list = files_getlist(tcb); - if (list == NULL) + if (tcb->group == NULL) { return; } - for (i = 0; i < list->fl_rows; i++) + rows = tcb->group->tg_filelist.fl_rows; + + for (i = 0; i < rows; i++) { for (j = 0; j < CONFIG_NFILE_DESCRIPTORS_PER_BLOCK; j++) { - FAR struct file *filep; + ctcb = nxsched_get_tcb(pid); + if (ctcb == NULL || ctcb->group == NULL || ctcb != tcb) + { + return; + } - filep = files_fget_by_index(list, i, j, NULL); + filep = files_fget_by_index(&ctcb->group->tg_filelist, + i, j, NULL); if (filep != NULL) { file_fsync(filep); - fs_putfilep(filep); + ctcb = nxsched_get_tcb(pid); + if (ctcb != NULL && ctcb->group != NULL && ctcb == tcb) + { + fs_putfilep(filep); + } } } } - - files_putlist(list); } /****************************************************************************