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