2007-05-09 02:04:03 +02:00
|
|
|
/****************************************************************************
|
2014-09-28 19:06:21 +02:00
|
|
|
* fs/inode/fs_inoderemove.c
|
2007-03-14 23:41:09 +01: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-03-14 23:41:09 +01:00
|
|
|
*
|
2021-02-03 14:47:23 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2007-03-14 23:41:09 +01: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-03-14 23:41:09 +01:00
|
|
|
*
|
2007-05-09 02:04:03 +02:00
|
|
|
****************************************************************************/
|
2007-03-14 23:41:09 +01:00
|
|
|
|
2007-05-09 02:04:03 +02:00
|
|
|
/****************************************************************************
|
2007-03-14 23:41:09 +01:00
|
|
|
* Included Files
|
2007-05-09 02:04:03 +02:00
|
|
|
****************************************************************************/
|
2007-03-14 23:41:09 +01:00
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
2009-12-15 01:18:32 +01:00
|
|
|
|
2021-05-18 08:59:14 +02:00
|
|
|
#include <assert.h>
|
2007-03-14 23:41:09 +01:00
|
|
|
#include <errno.h>
|
2011-04-05 22:54:00 +02:00
|
|
|
|
|
|
|
#include <nuttx/kmalloc.h>
|
2012-03-21 19:01:07 +01:00
|
|
|
#include <nuttx/fs/fs.h>
|
2009-12-15 01:18:32 +01:00
|
|
|
|
2014-09-29 15:14:38 +02:00
|
|
|
#include "inode/inode.h"
|
2007-03-14 23:41:09 +01:00
|
|
|
|
2014-02-19 20:14:39 +01:00
|
|
|
/****************************************************************************
|
2023-10-29 16:54:44 +01:00
|
|
|
* Private Functions
|
2014-02-19 20:14:39 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
2007-05-09 02:04:03 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: inode_unlink
|
2014-02-19 20:14:39 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Given a path, remove a the node from the in-memory, inode tree that the
|
|
|
|
* path refers to. This is normally done in preparation to removing or
|
|
|
|
* moving an inode.
|
|
|
|
*
|
2017-02-03 02:37:58 +01:00
|
|
|
* In symbolic links in the pseduo file system are enabled, then this
|
|
|
|
* logic will follow the symbolic links up until the terminal node. Then
|
|
|
|
* that link in removed. So if this the terminal node is a symbolic link,
|
|
|
|
* the symbolic link node will be removed, not the target of the link.
|
|
|
|
*
|
2014-02-19 20:14:39 +01:00
|
|
|
* Assumptions/Limitations:
|
|
|
|
* The caller must hold the inode semaphore
|
|
|
|
*
|
2007-05-09 02:04:03 +02:00
|
|
|
****************************************************************************/
|
2007-03-14 23:41:09 +01:00
|
|
|
|
2023-10-29 16:54:44 +01:00
|
|
|
static FAR struct inode *inode_unlink(FAR const char *path)
|
2007-03-14 23:41:09 +01:00
|
|
|
{
|
2017-02-04 18:21:44 +01:00
|
|
|
struct inode_search_s desc;
|
2017-02-07 22:41:02 +01:00
|
|
|
FAR struct inode *node = NULL;
|
2017-02-04 18:21:44 +01:00
|
|
|
int ret;
|
2014-02-19 20:14:39 +01:00
|
|
|
|
2020-09-13 19:15:05 +02:00
|
|
|
/* Verify parameters. Ignore null paths */
|
2007-03-14 23:41:09 +01:00
|
|
|
|
2020-09-13 19:15:05 +02:00
|
|
|
if (path == NULL)
|
2012-07-14 23:05:40 +02:00
|
|
|
{
|
2014-02-19 20:14:39 +01:00
|
|
|
return NULL;
|
2012-07-14 23:05:40 +02:00
|
|
|
}
|
2007-03-14 23:41:09 +01:00
|
|
|
|
2014-02-19 20:14:39 +01:00
|
|
|
/* Find the node to unlink */
|
2007-03-14 23:41:09 +01:00
|
|
|
|
2017-02-05 21:25:45 +01:00
|
|
|
SETUP_SEARCH(&desc, path, true);
|
2017-02-04 18:21:44 +01:00
|
|
|
|
2017-02-04 19:51:14 +01:00
|
|
|
ret = inode_search(&desc);
|
2017-02-04 18:21:44 +01:00
|
|
|
if (ret >= 0)
|
2012-07-14 23:05:40 +02:00
|
|
|
{
|
2017-02-04 18:21:44 +01:00
|
|
|
node = desc.node;
|
|
|
|
DEBUGASSERT(node != NULL);
|
|
|
|
|
2014-02-19 20:14:39 +01:00
|
|
|
/* If peer is non-null, then remove the node from the right of
|
|
|
|
* of that peer node.
|
|
|
|
*/
|
2007-03-14 23:41:09 +01:00
|
|
|
|
2017-02-04 18:21:44 +01:00
|
|
|
if (desc.peer != NULL)
|
2014-02-19 20:14:39 +01:00
|
|
|
{
|
2017-02-04 18:21:44 +01:00
|
|
|
desc.peer->i_peer = node->i_peer;
|
2014-02-19 20:14:39 +01:00
|
|
|
}
|
2007-03-14 23:41:09 +01:00
|
|
|
|
2020-09-15 11:42:42 +02:00
|
|
|
/* Then remove the node from head of the list of children. */
|
2014-02-19 20:14:39 +01:00
|
|
|
|
|
|
|
else
|
|
|
|
{
|
2020-09-15 11:42:42 +02:00
|
|
|
DEBUGASSERT(desc.parent != NULL);
|
|
|
|
desc.parent->i_child = node->i_peer;
|
2014-02-19 20:14:39 +01:00
|
|
|
}
|
|
|
|
|
2021-07-08 11:01:15 +02:00
|
|
|
node->i_peer = NULL;
|
|
|
|
node->i_parent = NULL;
|
2024-07-23 13:30:19 +02:00
|
|
|
atomic_fetch_sub(&node->i_crefs, 1);
|
2012-07-14 23:05:40 +02:00
|
|
|
}
|
|
|
|
|
2017-02-05 21:25:45 +01:00
|
|
|
RELEASE_SEARCH(&desc);
|
2014-02-19 20:14:39 +01:00
|
|
|
return node;
|
2007-03-14 23:41:09 +01:00
|
|
|
}
|
|
|
|
|
2023-10-29 16:54:44 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2007-05-09 02:04:03 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: inode_remove
|
2007-05-12 22:07:51 +02:00
|
|
|
*
|
2012-08-04 00:04:14 +02:00
|
|
|
* Description:
|
2014-02-19 20:14:39 +01:00
|
|
|
* Given a path, remove a the node from the in-memory, inode tree that the
|
|
|
|
* path refers to and free all resources related to the inode. If the
|
|
|
|
* inode is in-use, then it will be unlinked, but will not be freed until
|
|
|
|
* the last reference to the inode is released.
|
2012-08-04 00:04:14 +02:00
|
|
|
*
|
2014-02-19 20:14:39 +01:00
|
|
|
* Assumptions/Limitations:
|
|
|
|
* The caller must hold the inode semaphore
|
2012-07-14 23:05:40 +02:00
|
|
|
*
|
2007-05-09 02:04:03 +02:00
|
|
|
****************************************************************************/
|
2007-03-14 23:41:09 +01:00
|
|
|
|
2012-08-04 00:04:14 +02:00
|
|
|
int inode_remove(FAR const char *path)
|
2007-03-14 23:41:09 +01:00
|
|
|
{
|
|
|
|
FAR struct inode *node;
|
|
|
|
|
2014-02-19 20:14:39 +01:00
|
|
|
/* Find the inode and unlink it from the in-memory inode tree */
|
2007-03-14 23:41:09 +01:00
|
|
|
|
2014-02-19 20:14:39 +01:00
|
|
|
node = inode_unlink(path);
|
2007-03-14 23:41:09 +01:00
|
|
|
if (node)
|
|
|
|
{
|
2014-02-19 20:14:39 +01:00
|
|
|
/* Found it! But we cannot delete the inode if there are references
|
|
|
|
* to it
|
|
|
|
*/
|
2007-03-14 23:41:09 +01:00
|
|
|
|
2024-07-23 13:30:19 +02:00
|
|
|
if (atomic_load(&node->i_crefs))
|
2007-03-14 23:41:09 +01:00
|
|
|
{
|
2019-10-27 18:48:14 +01:00
|
|
|
return -EBUSY;
|
2012-07-14 23:05:40 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-06-11 19:24:20 +02:00
|
|
|
/* And delete it now -- recursively to delete all of its children.
|
2020-09-15 11:42:42 +02:00
|
|
|
* Since it has been unlinked, then the peer pointer should be
|
|
|
|
* NULL.
|
2015-06-11 19:24:20 +02:00
|
|
|
*/
|
2007-03-14 23:41:09 +01:00
|
|
|
|
2015-06-11 19:24:20 +02:00
|
|
|
DEBUGASSERT(node->i_peer == NULL);
|
|
|
|
inode_free(node);
|
2007-03-14 23:41:09 +01:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-19 20:14:39 +01:00
|
|
|
/* The node does not exist */
|
2007-03-14 23:41:09 +01:00
|
|
|
|
2007-05-27 21:10:40 +02:00
|
|
|
return -ENOENT;
|
2007-03-14 23:41:09 +01:00
|
|
|
}
|