From c6bd160be51057615e13616a7e38bf5f3faa193b Mon Sep 17 00:00:00 2001 From: Jiuzhu Dong Date: Sat, 25 Dec 2021 19:08:17 +0800 Subject: [PATCH] fs/romfs: remove the error single list and using reference Signed-off-by: Jiuzhu Dong --- fs/romfs/fs_romfs.c | 31 +++++++++++++------------------ fs/romfs/fs_romfs.h | 24 +++++++++++------------- 2 files changed, 24 insertions(+), 31 deletions(-) diff --git a/fs/romfs/fs_romfs.c b/fs/romfs/fs_romfs.c index 9430acb513..637166ac64 100644 --- a/fs/romfs/fs_romfs.c +++ b/fs/romfs/fs_romfs.c @@ -271,14 +271,7 @@ static int romfs_open(FAR struct file *filep, FAR const char *relpath, filep->f_priv = rf; - /* Then insert the new instance into the mountpoint structure. - * It needs to be there (1) to handle error conditions that effect - * all files, and (2) to inform the umount logic that we are busy - * (but a simple reference count could have done that). - */ - - rf->rf_next = rm->rm_head; - rm->rm_head = rf->rf_next; + rm->rm_refs++; romfs_semgive(rm); return OK; @@ -298,7 +291,7 @@ static int romfs_close(FAR struct file *filep) { FAR struct romfs_mountpt_s *rm; FAR struct romfs_file_s *rf; - int ret = OK; + int ret; finfo("Closing\n"); @@ -313,6 +306,15 @@ static int romfs_close(FAR struct file *filep) DEBUGASSERT(rm != NULL); + ret = romfs_semtake(rm); + if (ret < 0) + { + return ret; + } + + rm->rm_refs--; + romfs_semgive(rm); + /* Do not check if the mount is healthy. We must support closing of * the file even when there is healthy mount. */ @@ -681,14 +683,7 @@ static int romfs_dup(FAR const struct file *oldp, FAR struct file *newp) newp->f_priv = newrf; - /* Then insert the new instance into the mountpoint structure. - * It needs to be there (1) to handle error conditions that effect - * all files, and (2) to inform the umount logic that we are busy - * (but a simple reference count could have done that). - */ - - newrf->rf_next = rm->rm_head; - rm->rm_head = newrf->rf_next; + rm->rm_refs++; romfs_semgive(rm); return OK; @@ -1091,7 +1086,7 @@ static int romfs_unbind(FAR void *handle, FAR struct inode **blkdriver, return ret; } - if (rm->rm_head) + if (rm->rm_refs) { /* We cannot unmount now.. there are open files */ diff --git a/fs/romfs/fs_romfs.h b/fs/romfs/fs_romfs.h index a038e3aa0a..9b8591dd12 100644 --- a/fs/romfs/fs_romfs.h +++ b/fs/romfs/fs_romfs.h @@ -126,18 +126,17 @@ struct romfs_file_s; struct romfs_mountpt_s { - struct inode *rm_blkdriver; /* The block driver inode that hosts the FAT32 fs */ - struct romfs_file_s *rm_head; /* A list to all files opened on this mountpoint */ - - bool rm_mounted; /* true: The file system is ready */ - uint16_t rm_hwsectorsize; /* HW: Sector size reported by block driver */ - sem_t rm_sem; /* Used to assume thread-safe access */ - uint32_t rm_rootoffset; /* Saved offset to the first root directory entry */ - uint32_t rm_hwnsectors; /* HW: The number of sectors reported by the hardware */ - uint32_t rm_volsize; /* Size of the ROMFS volume */ - uint32_t rm_cachesector; /* Current sector in the rm_buffer */ - uint8_t *rm_xipbase; /* Base address of directly accessible media */ - uint8_t *rm_buffer; /* Device sector buffer, allocated if rm_xipbase==0 */ + struct inode *rm_blkdriver; /* The block driver inode that hosts the FAT32 fs */ + bool rm_mounted; /* true: The file system is ready */ + uint16_t rm_hwsectorsize; /* HW: Sector size reported by block driver */ + sem_t rm_sem; /* Used to assume thread-safe access */ + uint32_t rm_refs; /* The references for all files opened on this mountpoint */ + uint32_t rm_rootoffset; /* Saved offset to the first root directory entry */ + uint32_t rm_hwnsectors; /* HW: The number of sectors reported by the hardware */ + uint32_t rm_volsize; /* Size of the ROMFS volume */ + uint32_t rm_cachesector; /* Current sector in the rm_buffer */ + uint8_t *rm_xipbase; /* Base address of directly accessible media */ + uint8_t *rm_buffer; /* Device sector buffer, allocated if rm_xipbase==0 */ }; /* This structure represents on open file under the mountpoint. An instance @@ -147,7 +146,6 @@ struct romfs_mountpt_s struct romfs_file_s { - FAR struct romfs_file_s *rf_next; /* Retained in a singly linked list */ uint32_t rf_startoffset; /* Offset to the start of the file data */ uint32_t rf_size; /* Size of the file in bytes */ uint32_t rf_cachesector; /* Current sector in the rf_buffer */