• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /****************************************************************************
2  * fs/vfs/fs_fsync.c
3  *
4  *   Copyright (C) 2007-2009, 2013-2014, 2016-2017 Gregory Nutt. All rights
5  *     reserved.
6  *   Author: Gregory Nutt <gnutt@nuttx.org>
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in
16  *    the documentation and/or other materials provided with the
17  *    distribution.
18  * 3. Neither the name NuttX nor the names of its contributors may be
19  *    used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
29  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  *
35  ****************************************************************************/
36 
37 /****************************************************************************
38  * Included Files
39  ****************************************************************************/
40 
41 #include "vfs_config.h"
42 
43 #include "unistd.h"
44 #include "fcntl.h"
45 #include "errno.h"
46 #include "assert.h"
47 
48 
49 
50 #include "vnode.h"
51 
52 /****************************************************************************
53  * Public Functions
54  ****************************************************************************/
55 
56 /****************************************************************************
57  * Name: file_fsync
58  *
59  * Description:
60  *   Equivalent to the standard fsync() function except that is accepts a
61  *   struct file instance instead of a file descriptor and it does not set
62  *   the errno variable.
63  *
64  ****************************************************************************/
65 
file_fsync(struct file * filep)66 int file_fsync(struct file *filep)
67 {
68   int ret;
69 
70   /* Was this file opened for write access? */
71 
72   if ((filep->f_oflags & O_ACCMODE) == 0)
73     {
74       ret = EBADF;
75       goto errout;
76     }
77 
78   /* Is this vnode a registered mountpoint? Does it support the
79    * sync operations may be relevant to device drivers but only
80    * the mountpoint operations vtable contains a sync method.
81    */
82 
83   if (!filep || !filep->ops || !filep->ops->fsync)
84     {
85       ret = EINVAL;
86       goto errout;
87     }
88 
89   /* Yes, then tell the mountpoint to sync this file */
90 
91   ret = filep->ops->fsync(filep);
92   if (ret >= 0)
93     {
94       return OK;
95     }
96 
97   ret = -ret;
98 
99 errout:
100   set_errno(ret);
101   return VFS_ERROR;
102 }
103 
104 /****************************************************************************
105  * Name: fsync
106  *
107  * Description:
108  *   This func simply binds vnode sync methods to the sync system call.
109  *
110  ****************************************************************************/
111 
fsync(int fd)112 int fsync(int fd)
113 {
114   struct file *filep = NULL;
115 
116   /* Get the file structure corresponding to the file descriptor. */
117 
118   int ret = fs_getfilep(fd, &filep);
119   if (ret < 0)
120     {
121       /* The errno value has already been set */
122       return VFS_ERROR;
123     }
124 
125   /* Perform the fsync operation */
126 
127   return file_fsync(filep);
128 }
129 
130