1 /****************************************************************************
2 * fs/vfs/fs_fsync.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 "vfs_config.h"
26
27 #include "unistd.h"
28 #include "fcntl.h"
29 #include "errno.h"
30 #include "assert.h"
31
32
33
34 #include "vnode.h"
35
36 /****************************************************************************
37 * Public Functions
38 ****************************************************************************/
39
40 /****************************************************************************
41 * Name: file_fsync
42 *
43 * Description:
44 * Equivalent to the standard fsync() function except that is accepts a
45 * struct file instance instead of a file descriptor and it does not set
46 * the errno variable.
47 *
48 ****************************************************************************/
49
file_fsync(struct file * filep)50 int file_fsync(struct file *filep)
51 {
52 int ret;
53
54 /* Was this file opened for write access? */
55
56 if ((filep->f_oflags & O_ACCMODE) == 0)
57 {
58 ret = EBADF;
59 goto errout;
60 }
61
62 /* Is this vnode a registered mountpoint? Does it support the
63 * sync operations may be relevant to device drivers but only
64 * the mountpoint operations vtable contains a sync method.
65 */
66
67 if (!filep || !filep->ops || !filep->ops->fsync)
68 {
69 ret = EINVAL;
70 goto errout;
71 }
72
73 /* Yes, then tell the mountpoint to sync this file */
74
75 ret = filep->ops->fsync(filep);
76 if (ret >= 0)
77 {
78 return OK;
79 }
80
81 ret = -ret;
82
83 errout:
84 set_errno(ret);
85 return VFS_ERROR;
86 }
87
88 /****************************************************************************
89 * Name: fsync
90 *
91 * Description:
92 * This func simply binds vnode sync methods to the sync system call.
93 *
94 ****************************************************************************/
95
fsync(int fd)96 int fsync(int fd)
97 {
98 struct file *filep = NULL;
99
100 /* Get the file structure corresponding to the file descriptor. */
101
102 int ret = fs_getfilep(fd, &filep);
103 if (ret < 0)
104 {
105 /* The errno value has already been set */
106 return VFS_ERROR;
107 }
108
109 /* Perform the fsync operation */
110
111 return file_fsync(filep);
112 }
113
114