From 2bce0f404c37826bf2eae59cfe42fe1c80bd0c22 Mon Sep 17 00:00:00 2001 From: guohao15 Date: Fri, 15 Sep 2023 16:22:13 +0800 Subject: [PATCH] fs:add syncfs api for sync whole fs data Signed-off-by: guohao15 --- fs/vfs/Make.defs | 2 +- fs/vfs/fs_syncfs.c | 102 +++++++++++++++++++++++++++++++++++++++ include/nuttx/cancelpt.h | 16 +++--- include/nuttx/fs/fs.h | 13 +++++ include/unistd.h | 2 +- 5 files changed, 125 insertions(+), 10 deletions(-) create mode 100644 fs/vfs/fs_syncfs.c diff --git a/fs/vfs/Make.defs b/fs/vfs/Make.defs index 4bd008b2c3..339b4aed80 100644 --- a/fs/vfs/Make.defs +++ b/fs/vfs/Make.defs @@ -25,7 +25,7 @@ CSRCS += fs_fchstat.c fs_fstat.c fs_fstatfs.c fs_ioctl.c fs_lseek.c CSRCS += fs_mkdir.c fs_open.c fs_poll.c fs_pread.c fs_pwrite.c fs_read.c CSRCS += fs_rename.c fs_rmdir.c fs_select.c fs_sendfile.c fs_stat.c CSRCS += fs_statfs.c fs_unlink.c fs_write.c fs_dir.c fs_fsync.c -CSRCS += fs_truncate.c +CSRCS += fs_syncfs.c fs_truncate.c # Certain interfaces are not available if there is no mountpoint support diff --git a/fs/vfs/fs_syncfs.c b/fs/vfs/fs_syncfs.c new file mode 100644 index 0000000000..0d4c8e76bd --- /dev/null +++ b/fs/vfs/fs_syncfs.c @@ -0,0 +1,102 @@ +/**************************************************************************** + * fs/vfs/fs_syncfs.c + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include +#include +#include + +#include + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: file_syncfs + * + * Description: + * Equivalent to the standard syncsf() function except that is accepts a + * struct file instance instead of a fd descriptor and it does not set + * the errno variable + * + ****************************************************************************/ + +int file_syncfs(FAR struct file *filep) +{ + FAR struct inode *inode = filep->f_inode; + + if (inode != NULL) + { +#ifndef CONFIG_DISABLE_MOUNTPOINT + if (INODE_IS_MOUNTPT(inode) && inode->u.i_mops && + inode->u.i_mops->syncfs) + { + return inode->u.i_mops->syncfs(inode); + } +#endif /* !CONFIG_DISABLE_MOUNTPOINT */ + } + + return -EBADF; +} + +/**************************************************************************** + * Name: syncfs + * + * Description: + * syncfs() is like sync(), but synchronizes just the filesystem + * containing file referred to by the open file descriptor fd. + * + * Returned Value: + * syncfs() returns 0 on success; on error, it returns -1 and sets + * errno to indicate the error. + * + * Assumptions: + * + ****************************************************************************/ + +int syncfs(int fd) +{ + FAR struct file *filep; + int ret; + + enter_cancellation_point(); + + ret = fs_getfilep(fd, &filep); + if (ret == OK) + { + DEBUGASSERT(filep != NULL); + ret = file_syncfs(filep); + } + + leave_cancellation_point(); + + if (ret < 0) + { + set_errno(-ret); + ret = ERROR; + } + + return ret; +} + diff --git a/include/nuttx/cancelpt.h b/include/nuttx/cancelpt.h index 75d75fcb7a..f76be1885e 100644 --- a/include/nuttx/cancelpt.h +++ b/include/nuttx/cancelpt.h @@ -33,14 +33,14 @@ * clock_nanosleep() msgsnd() read() sigwaitinfo() * close() msync() readv() sleep() * connect() nanosleep() recv() system() - * creat() open() recvfrom() tcdrain() - * fcntl() pause() recvmsg() usleep() - * fdatasync() poll() select() wait() - * fsync() pread() sem_timedwait() waitid() - * getmsg() pselect() sem_wait() waitpid() - * getpmsg() pthread_cond_timedwait() send() write() - * lockf() pthread_cond_wait() sendmsg() writev() - * mq_receive() pthread_join() sendto() + * creat() open() recvfrom() syncfs() + * fcntl() pause() recvmsg() tcdrain() + * fdatasync() poll() select() usleep() + * fsync() pread() sem_timedwait() wait() + * getmsg() pselect() sem_wait() waitid() + * getpmsg() pthread_cond_timedwait() send() waitpid() + * lockf() pthread_cond_wait() sendmsg() write() + * mq_receive() pthread_join() sendto() writev() * mq_send() pthread_testcancel() sigpause() * mq_timedreceive() putmsg() sigsuspend() * diff --git a/include/nuttx/fs/fs.h b/include/nuttx/fs/fs.h index c12d43a765..7c8f0a53e4 100644 --- a/include/nuttx/fs/fs.h +++ b/include/nuttx/fs/fs.h @@ -364,6 +364,7 @@ struct mountpt_operations FAR struct stat *buf); CODE int (*chstat)(FAR struct inode *mountpt, FAR const char *relpath, FAR const struct stat *buf, int flags); + CODE int (*syncfs)(FAR struct inode *mountpt); }; #endif /* CONFIG_DISABLE_MOUNTPOINT */ @@ -1382,6 +1383,18 @@ off_t nx_seek(int fd, off_t offset, int whence); int file_fsync(FAR struct file *filep); +/**************************************************************************** + * Name: file_syncfs + * + * Description: + * Equivalent to the standard syncsf() function except that is accepts a + * struct file instance instead of a fd descriptor and it does not set + * the errno variable + * + ****************************************************************************/ + +int file_syncfs(FAR struct file *filep); + /**************************************************************************** * Name: file_truncate * diff --git a/include/unistd.h b/include/unistd.h index 4d0f739a70..749f936435 100644 --- a/include/unistd.h +++ b/include/unistd.h @@ -258,7 +258,6 @@ /* Helpers and legacy compatibility definitions */ -#define syncfs(f) fsync(f) #define fdatasync(f) fsync(f) #define getdtablesize(f) ((int)sysconf(_SC_OPEN_MAX)) #define getpagesize(f) ((int)sysconf(_SC_PAGESIZE)) @@ -441,6 +440,7 @@ int setregid(gid_t rgid, gid_t egid); int getentropy(FAR void *buffer, size_t length); void sync(void); +int syncfs(int fd); #if CONFIG_FORTIFY_SOURCE > 0 fortify_function(getcwd) FAR char *getcwd(FAR char *buf,