Skip to content

Commit 2bce0f4

Browse files
guohao15xiaoxiang781216
authored andcommitted
fs:add syncfs api for sync whole fs data
Signed-off-by: guohao15 <guohao15@xiaomi.com>
1 parent a41f68d commit 2bce0f4

File tree

5 files changed

+125
-10
lines changed

5 files changed

+125
-10
lines changed

fs/vfs/Make.defs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ CSRCS += fs_fchstat.c fs_fstat.c fs_fstatfs.c fs_ioctl.c fs_lseek.c
2525
CSRCS += fs_mkdir.c fs_open.c fs_poll.c fs_pread.c fs_pwrite.c fs_read.c
2626
CSRCS += fs_rename.c fs_rmdir.c fs_select.c fs_sendfile.c fs_stat.c
2727
CSRCS += fs_statfs.c fs_unlink.c fs_write.c fs_dir.c fs_fsync.c
28-
CSRCS += fs_truncate.c
28+
CSRCS += fs_syncfs.c fs_truncate.c
2929

3030
# Certain interfaces are not available if there is no mountpoint support
3131

fs/vfs/fs_syncfs.c

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/****************************************************************************
2+
* fs/vfs/fs_syncfs.c
3+
*
4+
* Licensed to the Apache Software Foundation (ASF) under one or more
5+
* contributor license agreements. See the NOTICE file distributed with
6+
* this work for additional information regarding copyright ownership. The
7+
* ASF licenses this file to you under the Apache License, Version 2.0 (the
8+
* "License"); you may not use this file except in compliance with the
9+
* License. You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16+
* License for the specific language governing permissions and limitations
17+
* under the License.
18+
*
19+
****************************************************************************/
20+
21+
/****************************************************************************
22+
* Included Files
23+
****************************************************************************/
24+
25+
#include <nuttx/config.h>
26+
#include <nuttx/cancelpt.h>
27+
#include <nuttx/fs/fs.h>
28+
29+
#include <errno.h>
30+
31+
/****************************************************************************
32+
* Public Functions
33+
****************************************************************************/
34+
35+
/****************************************************************************
36+
* Name: file_syncfs
37+
*
38+
* Description:
39+
* Equivalent to the standard syncsf() function except that is accepts a
40+
* struct file instance instead of a fd descriptor and it does not set
41+
* the errno variable
42+
*
43+
****************************************************************************/
44+
45+
int file_syncfs(FAR struct file *filep)
46+
{
47+
FAR struct inode *inode = filep->f_inode;
48+
49+
if (inode != NULL)
50+
{
51+
#ifndef CONFIG_DISABLE_MOUNTPOINT
52+
if (INODE_IS_MOUNTPT(inode) && inode->u.i_mops &&
53+
inode->u.i_mops->syncfs)
54+
{
55+
return inode->u.i_mops->syncfs(inode);
56+
}
57+
#endif /* !CONFIG_DISABLE_MOUNTPOINT */
58+
}
59+
60+
return -EBADF;
61+
}
62+
63+
/****************************************************************************
64+
* Name: syncfs
65+
*
66+
* Description:
67+
* syncfs() is like sync(), but synchronizes just the filesystem
68+
* containing file referred to by the open file descriptor fd.
69+
*
70+
* Returned Value:
71+
* syncfs() returns 0 on success; on error, it returns -1 and sets
72+
* errno to indicate the error.
73+
*
74+
* Assumptions:
75+
*
76+
****************************************************************************/
77+
78+
int syncfs(int fd)
79+
{
80+
FAR struct file *filep;
81+
int ret;
82+
83+
enter_cancellation_point();
84+
85+
ret = fs_getfilep(fd, &filep);
86+
if (ret == OK)
87+
{
88+
DEBUGASSERT(filep != NULL);
89+
ret = file_syncfs(filep);
90+
}
91+
92+
leave_cancellation_point();
93+
94+
if (ret < 0)
95+
{
96+
set_errno(-ret);
97+
ret = ERROR;
98+
}
99+
100+
return ret;
101+
}
102+

include/nuttx/cancelpt.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@
3333
* clock_nanosleep() msgsnd() read() sigwaitinfo()
3434
* close() msync() readv() sleep()
3535
* connect() nanosleep() recv() system()
36-
* creat() open() recvfrom() tcdrain()
37-
* fcntl() pause() recvmsg() usleep()
38-
* fdatasync() poll() select() wait()
39-
* fsync() pread() sem_timedwait() waitid()
40-
* getmsg() pselect() sem_wait() waitpid()
41-
* getpmsg() pthread_cond_timedwait() send() write()
42-
* lockf() pthread_cond_wait() sendmsg() writev()
43-
* mq_receive() pthread_join() sendto()
36+
* creat() open() recvfrom() syncfs()
37+
* fcntl() pause() recvmsg() tcdrain()
38+
* fdatasync() poll() select() usleep()
39+
* fsync() pread() sem_timedwait() wait()
40+
* getmsg() pselect() sem_wait() waitid()
41+
* getpmsg() pthread_cond_timedwait() send() waitpid()
42+
* lockf() pthread_cond_wait() sendmsg() write()
43+
* mq_receive() pthread_join() sendto() writev()
4444
* mq_send() pthread_testcancel() sigpause()
4545
* mq_timedreceive() putmsg() sigsuspend()
4646
*

include/nuttx/fs/fs.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,7 @@ struct mountpt_operations
364364
FAR struct stat *buf);
365365
CODE int (*chstat)(FAR struct inode *mountpt, FAR const char *relpath,
366366
FAR const struct stat *buf, int flags);
367+
CODE int (*syncfs)(FAR struct inode *mountpt);
367368
};
368369
#endif /* CONFIG_DISABLE_MOUNTPOINT */
369370

@@ -1382,6 +1383,18 @@ off_t nx_seek(int fd, off_t offset, int whence);
13821383

13831384
int file_fsync(FAR struct file *filep);
13841385

1386+
/****************************************************************************
1387+
* Name: file_syncfs
1388+
*
1389+
* Description:
1390+
* Equivalent to the standard syncsf() function except that is accepts a
1391+
* struct file instance instead of a fd descriptor and it does not set
1392+
* the errno variable
1393+
*
1394+
****************************************************************************/
1395+
1396+
int file_syncfs(FAR struct file *filep);
1397+
13851398
/****************************************************************************
13861399
* Name: file_truncate
13871400
*

include/unistd.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,6 @@
258258

259259
/* Helpers and legacy compatibility definitions */
260260

261-
#define syncfs(f) fsync(f)
262261
#define fdatasync(f) fsync(f)
263262
#define getdtablesize(f) ((int)sysconf(_SC_OPEN_MAX))
264263
#define getpagesize(f) ((int)sysconf(_SC_PAGESIZE))
@@ -441,6 +440,7 @@ int setregid(gid_t rgid, gid_t egid);
441440
int getentropy(FAR void *buffer, size_t length);
442441

443442
void sync(void);
443+
int syncfs(int fd);
444444

445445
#if CONFIG_FORTIFY_SOURCE > 0
446446
fortify_function(getcwd) FAR char *getcwd(FAR char *buf,

0 commit comments

Comments
 (0)