2007-05-20 21:01:57 +02:00
|
|
|
/****************************************************************************
|
2014-09-28 19:46:11 +02:00
|
|
|
* fs/vfs/fs_fsync.c
|
2007-05-20 21:01:57 +02:00
|
|
|
*
|
2021-02-03 14:47:23 +01:00
|
|
|
* 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
|
2007-05-20 21:01:57 +02:00
|
|
|
*
|
2021-02-03 14:47:23 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2007-05-20 21:01:57 +02:00
|
|
|
*
|
2021-02-03 14:47:23 +01:00
|
|
|
* 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.
|
2007-05-20 21:01:57 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
2009-12-15 01:18:32 +01:00
|
|
|
|
2007-05-20 21:01:57 +02:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
2013-09-28 22:47:49 +02:00
|
|
|
#include <assert.h>
|
|
|
|
|
2007-05-20 21:01:57 +02:00
|
|
|
#include <nuttx/sched.h>
|
2016-12-10 16:08:26 +01:00
|
|
|
#include <nuttx/cancelpt.h>
|
2016-12-09 20:49:36 +01:00
|
|
|
#include <nuttx/fs/fs.h>
|
2022-07-07 16:28:04 +02:00
|
|
|
#include <nuttx/fs/ioctl.h>
|
2007-05-20 21:01:57 +02:00
|
|
|
|
2014-09-29 15:14:38 +02:00
|
|
|
#include "inode/inode.h"
|
2007-05-20 21:01:57 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2014-10-06 18:53:25 +02:00
|
|
|
* Name: file_fsync
|
2007-05-20 21:01:57 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2014-10-06 18:53:25 +02:00
|
|
|
* Equivalent to the standard fsync() function except that is accepts a
|
2017-10-11 16:39:19 +02:00
|
|
|
* struct file instance instead of a file descriptor and it does not set
|
|
|
|
* the errno variable.
|
2007-05-20 21:01:57 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-10-06 18:53:25 +02:00
|
|
|
int file_fsync(FAR struct file *filep)
|
2007-05-20 21:01:57 +02:00
|
|
|
{
|
2022-07-07 16:28:04 +02:00
|
|
|
FAR struct inode *inode;
|
|
|
|
int ret;
|
2007-05-20 21:01:57 +02:00
|
|
|
|
|
|
|
/* Is this inode a registered mountpoint? Does it support the
|
|
|
|
* sync operations may be relevant to device drivers but only
|
|
|
|
* the mountpoint operations vtable contains a sync method.
|
|
|
|
*/
|
|
|
|
|
2013-09-28 22:47:49 +02:00
|
|
|
inode = filep->f_inode;
|
2022-07-07 16:28:04 +02:00
|
|
|
if (inode != NULL)
|
2007-05-20 21:01:57 +02:00
|
|
|
{
|
2023-02-10 07:54:23 +01:00
|
|
|
#ifndef CONFIG_DISABLE_MOUNTPOINT
|
2022-07-07 16:28:04 +02:00
|
|
|
if (INODE_IS_MOUNTPT(inode) && inode->u.i_mops &&
|
|
|
|
inode->u.i_mops->sync)
|
|
|
|
{
|
|
|
|
/* Yes, then tell the mountpoint to sync this file */
|
|
|
|
|
|
|
|
return inode->u.i_mops->sync(filep);
|
|
|
|
}
|
2023-02-10 07:54:23 +01:00
|
|
|
else
|
|
|
|
#endif
|
|
|
|
if (inode->u.i_ops && inode->u.i_ops->ioctl)
|
2022-07-07 16:28:04 +02:00
|
|
|
{
|
|
|
|
ret = inode->u.i_ops->ioctl(filep, BIOC_FLUSH, 0);
|
|
|
|
return ret >= 0 ? 0 : ret;
|
|
|
|
}
|
2007-05-20 21:01:57 +02:00
|
|
|
}
|
|
|
|
|
2022-07-07 16:28:04 +02:00
|
|
|
return -EINVAL;
|
2007-05-20 21:01:57 +02:00
|
|
|
}
|
|
|
|
|
2014-10-06 18:53:25 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: fsync
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This func simply binds inode sync methods to the sync system call.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int fsync(int fd)
|
|
|
|
{
|
|
|
|
FAR struct file *filep;
|
2016-12-09 20:49:36 +01:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* fsync() is a cancellation point */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
enter_cancellation_point();
|
2014-10-06 18:53:25 +02:00
|
|
|
|
|
|
|
/* Get the file structure corresponding to the file descriptor. */
|
|
|
|
|
2017-10-11 16:39:19 +02:00
|
|
|
ret = fs_getfilep(fd, &filep);
|
|
|
|
if (ret < 0)
|
2014-10-06 18:53:25 +02:00
|
|
|
{
|
2017-10-11 16:39:19 +02:00
|
|
|
goto errout;
|
2014-10-06 18:53:25 +02:00
|
|
|
}
|
|
|
|
|
2017-10-11 16:39:19 +02:00
|
|
|
DEBUGASSERT(filep != NULL);
|
|
|
|
|
2014-10-06 18:53:25 +02:00
|
|
|
/* Perform the fsync operation */
|
|
|
|
|
2016-12-09 20:49:36 +01:00
|
|
|
ret = file_fsync(filep);
|
2017-10-11 16:39:19 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
2016-12-09 20:49:36 +01:00
|
|
|
leave_cancellation_point();
|
|
|
|
return ret;
|
2017-10-11 16:39:19 +02:00
|
|
|
|
|
|
|
errout:
|
|
|
|
leave_cancellation_point();
|
|
|
|
set_errno(-ret);
|
|
|
|
return ERROR;
|
2014-10-06 18:53:25 +02:00
|
|
|
}
|