nuttx/fs/vfs/fs_open.c
zhengyu16 ebfe22bfb9 fs: rename PSEUDOFS_SOFTLINKS to FS_LINKS
The link support is no longer limited to the pseudo file system and now
covers both soft (symbolic) links and hard links across the VFS.  Rename
the configuration option PSEUDOFS_SOFTLINKS to the more accurate FS_LINKS
and update all references in the source, headers, Kconfig, documentation
and board defconfigs accordingly.

This is a configuration rename; any out-of-tree defconfig that still
selects PSEUDOFS_SOFTLINKS must be updated to FS_LINKS.

Signed-off-by: zhengyu16 <zhengyu16@xiaomi.com>
2026-08-27 01:12:33 +08:00

489 lines
12 KiB
C

/****************************************************************************
* fs/vfs/fs_open.c
*
* SPDX-License-Identifier: Apache-2.0
*
* 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 <nuttx/config.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdbool.h>
#include <fcntl.h>
#include <sched.h>
#include <errno.h>
#include <assert.h>
#include <stdarg.h>
#include <nuttx/sched.h>
#include <nuttx/cancelpt.h>
#include <nuttx/fs/fs.h>
#include "sched/sched.h"
#include "inode/inode.h"
#include "driver/driver.h"
#include "vfs.h"
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: file_vopen
*
* Description:
* file_vopen() is similar to the standard 'open' interface except that it
* populates an instance of 'struct file' rather than return a file
* descriptor. It also is not a cancellation point and does not modify
* the errno variable.
*
* Input Parameters:
* filep - The caller provided location in which to return the 'struct
* file' instance.
* path - The full path to the file to be opened.
* oflags - open flags.
* umask - File mode creation mask. Overrides the open flags.
* ap - Variable argument list, may include 'mode_t mode'
*
* Returned Value:
* Zero (OK) is returned on success. On failure, a negated errno value is
* returned.
*
****************************************************************************/
static int file_vopen(FAR struct file *filep, FAR const char *path,
int oflags, mode_t umask, va_list ap)
{
struct inode_search_s desc;
FAR struct inode *inode;
#if !defined(CONFIG_DISABLE_MOUNTPOINT) || defined(CONFIG_PSEUDOFS_FILE)
mode_t mode = 0666;
#endif
int ret;
if (path == NULL)
{
return -EINVAL;
}
#ifndef CONFIG_DISABLE_MOUNTPOINT
/* If the file is opened for creation, then get the mode bits */
if ((oflags & O_CREAT) != 0)
{
mode = va_arg(ap, mode_t);
}
mode &= ~umask;
#endif
/* Get an inode for this file */
SETUP_SEARCH(&desc, path, (oflags & O_NOFOLLOW) != 0);
ret = inode_find(&desc);
if (ret < 0)
{
#ifdef CONFIG_PSEUDOFS_FILE
if ((oflags & O_CREAT) != 0)
{
ret = pseudofile_create(&desc.node, path, mode);
}
#endif
if (ret < 0)
{
/* "O_CREAT is not set and the named file does not exist. Or, a
* directory component in pathname does not exist or is a dangling
* symbolic link."
*/
goto errout_with_search;
}
}
/* Get the search results */
inode = desc.node;
DEBUGASSERT(inode != NULL);
#ifdef CONFIG_FS_LINKS
if (INODE_IS_HARDLINK(inode))
{
/* The inode is a hard link. The actual inode is referenced
* by the i_private field.
*/
DEBUGASSERT(inode->i_private != NULL);
inode = inode->i_private;
}
if (desc.nofollow && INODE_IS_SOFTLINK(inode))
{
return -ELOOP;
}
#endif
#if defined(CONFIG_BCH) && \
!defined(CONFIG_DISABLE_MOUNTPOINT) && \
!defined(CONFIG_DISABLE_PSEUDOFS_OPERATIONS)
/* If the inode is block driver, then we may return a character driver
* proxy for the block driver. block_proxy() will instantiate a BCH
* character driver wrapper around the block driver, open(), then
* unlink() the character driver.
*
* NOTE: This will recurse to open the character driver proxy.
*/
if (INODE_IS_BLOCK(inode) || INODE_IS_MTD(inode))
{
/* Release the inode reference */
inode_release(inode);
RELEASE_SEARCH(&desc);
/* Get the file structure of the opened character driver proxy */
#ifdef CONFIG_BCH_DEVICE_READONLY
oflags &= ~O_RDWR;
oflags |= O_RDONLY;
#endif
ret = block_proxy(filep, path, oflags);
#ifdef CONFIG_FS_NOTIFY
if (ret >= 0)
{
notify_open(path, filep->f_oflags);
}
#endif
return ret;
}
#endif
/* Enforce directory search (X_OK) on ancestors / mount gates, then
* validate open modes. inode_checkpathperm() takes the tree read lock
* for the path walk; for non-mountpoints, hold it again around openperm
* so i_mode cannot race with concurrent chmod.
*/
ret = inode_checkpathperm(inode, 0, 0);
if (ret < 0)
{
goto errout_with_inode;
}
#ifndef CONFIG_DISABLE_MOUNTPOINT
if (INODE_IS_MOUNTPT(inode))
{
ret = inode_checkopenperm(inode, oflags);
}
else
#endif
{
inode_rlock();
ret = inode_checkopenperm(inode, oflags);
inode_runlock();
}
if (ret < 0)
{
goto errout_with_inode;
}
/* Associate the inode with a file structure */
filep->f_oflags = oflags;
filep->f_inode = inode;
/* Perform the driver open operation. NOTE that the open method may be
* called many times. The driver/mountpoint logic should handle this
* because it may also be closed that many times.
*/
clock_t start_time;
FS_PROFILE_START(start_time);
if (oflags & O_DIRECTORY)
{
ret = dir_allocate(filep, desc.relpath);
}
#ifndef CONFIG_DISABLE_MOUNTPOINT
else if (INODE_IS_MOUNTPT(inode))
{
if (inode->u.i_mops->open != NULL)
{
ret = inode->u.i_mops->open(filep, desc.relpath, oflags, mode);
}
}
#endif
else if (INODE_IS_DRIVER(inode) || INODE_IS_PIPE(inode))
{
if (inode->u.i_ops->open != NULL)
{
ret = inode->u.i_ops->open(filep);
}
}
else
{
ret = -ENXIO;
}
FS_PROFILE_STOP(start_time, g_fs_profile.total_open_time,
g_fs_profile.opens);
if (ret == -EISDIR && (oflags & O_ACCMODE) == O_RDONLY)
{
ret = dir_allocate(filep, desc.relpath);
}
if (ret < 0)
{
goto errout_with_inode;
}
RELEASE_SEARCH(&desc);
#ifdef CONFIG_FS_NOTIFY
notify_open(path, filep->f_oflags);
#endif
return OK;
errout_with_inode:
filep->f_inode = NULL;
inode_release(inode);
errout_with_search:
RELEASE_SEARCH(&desc);
return ret;
}
/****************************************************************************
* Name: nx_vopen
*
* Description:
* nx_vopen() is similar to the standard 'open' interface except that it
* is not a cancellation point and it does not modify the errno variable.
*
* nx_open() is an internal NuttX interface and should not be called from
* applications.
*
* Input Parameters:
* tcb - Address of the task's TCB
* path - The full path to the file to be opened.
* oflags - open flags.
* ap - Variable argument list, may include 'mode_t mode'
*
* Returned Value:
* The new file descriptor is returned on success; a negated errno value is
* returned on any failure.
*
****************************************************************************/
static int nx_vopen(FAR struct fdlist *list,
FAR const char *path, int oflags, va_list ap)
{
FAR struct file *filep;
int ret;
int fd;
filep = file_allocate();
if (filep == NULL)
{
return -ENOMEM;
}
ret = file_vopen(filep, path, oflags, getumask(), ap);
if (ret < 0)
{
file_deallocate(filep);
return ret;
}
fd = fdlist_dupfile(list, oflags, 0, filep);
if (fd < 0)
{
file_close(filep);
file_deallocate(filep);
}
return fd;
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: file_open
*
* Description:
* file_open() is similar to the standard 'open' interface except that it
* populates an instance of 'struct file' rather than return a file
* descriptor. It also is not a cancellation point and does not modify
* the errno variable.
*
* Input Parameters:
* filep - The caller provided location in which to return the 'struct
* file' instance.
* path - The full path to the file to be opened.
* oflags - open flags.
* ... - Variable number of arguments, may include 'mode_t mode'
*
* Returned Value:
* Zero (OK) is returned on success. On failure, a negated errno value is
* returned.
*
****************************************************************************/
int file_open(FAR struct file *filep, FAR const char *path, int oflags, ...)
{
va_list ap;
int ret;
memset(filep, 0, sizeof(*filep));
va_start(ap, oflags);
ret = file_vopen(filep, path, oflags, 0, ap);
va_end(ap);
if (ret >= OK)
{
atomic_add(&filep->f_refs, 1);
}
return ret;
}
/****************************************************************************
* Name: fdlist_open
*
* Description:
* fdlist_open() is similar to the standard 'open' interface except
* that it is not a cancellation point and it does not modify the errno
* variable.
*
* fdlist_open() is an internal NuttX interface and should not be
* called from applications.
*
* Input Parameters:
* tcb - Address of the task's TCB
* path - The full path to the file to be opened.
* oflags - open flags.
* ... - Variable number of arguments, may include 'mode_t mode'
*
* Returned Value:
* The new file descriptor is returned on success; a negated errno value is
* returned on any failure.
*
****************************************************************************/
int fdlist_open(FAR struct fdlist *list,
FAR const char *path, int oflags, ...)
{
va_list ap;
int fd;
/* Let nx_vopen() do all of the work */
va_start(ap, oflags);
fd = nx_vopen(list, path, oflags, ap);
va_end(ap);
return fd;
}
/****************************************************************************
* Name: nx_open
*
* Description:
* nx_open() is similar to the standard 'open' interface except that it is
* not a cancellation point and it does not modify the errno variable.
*
* nx_open() is an internal NuttX interface and should not be called from
* applications.
*
* Input Parameters:
* path - The full path to the file to be opened.
* oflags - open flags.
* ... - Variable number of arguments, may include 'mode_t mode'
*
* Returned Value:
* The new file descriptor is returned on success; a negated errno value is
* returned on any failure.
*
****************************************************************************/
int nx_open(FAR const char *path, int oflags, ...)
{
va_list ap;
int fd;
/* Let nx_vopen() do all of the work */
va_start(ap, oflags);
fd = nx_vopen(nxsched_get_fdlist_from_tcb(this_task()), path, oflags, ap);
va_end(ap);
return fd;
}
/****************************************************************************
* Name: open
*
* Description:
* Standard 'open' interface
*
* Returned Value:
* The new file descriptor is returned on success; -1 (ERROR) is returned
* on any failure with the errno value set appropriately.
*
****************************************************************************/
int open(FAR const char *path, int oflags, ...)
{
va_list ap;
int fd;
/* open() is a cancellation point */
enter_cancellation_point();
/* Let nx_vopen() do most of the work */
va_start(ap, oflags);
fd = nx_vopen(nxsched_get_fdlist_from_tcb(this_task()), path, oflags, ap);
va_end(ap);
/* Set the errno value if any errors were reported by nx_open() */
if (fd < 0)
{
set_errno(-fd);
fd = ERROR;
}
leave_cancellation_point();
return fd;
}