1 /****************************************************************************
2 * fs/inode/fs_inoderemove.c
3 *
4 * Copyright (C) 2007-2009, 2017 Gregory Nutt. All rights reserved.
5 * Author: Gregory Nutt <gnutt@nuttx.org>
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 * 3. Neither the name NuttX nor the names of its contributors may be
18 * used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 *
34 ****************************************************************************/
35
36 /****************************************************************************
37 * Included Files
38 ****************************************************************************/
39
40 #include "vfs_config.h"
41 #include "errno.h"
42 #include "stdlib.h"
43 #include "fs/fs.h"
44 #include "inode/inode.h"
45
46 /****************************************************************************
47 * Public Functions
48 ****************************************************************************/
49
50 /****************************************************************************
51 * Name: inode_unlink
52 *
53 * Description:
54 * Given a path, remove a the node from the in-memory, inode tree that the
55 * path refers to. This is normally done in preparation to removing or
56 * moving an inode.
57 *
58 * In symbolic links in the pseduo file system are enabled, then this
59 * logic will follow the symbolic links up until the terminal node. Then
60 * that link in removed. So if this the terminal node is a symbolic link,
61 * the symbolic link node will be removed, not the target of the link.
62 *
63 * Assumptions/Limitations:
64 * The caller must hold the inode semaphore
65 *
66 ****************************************************************************/
67
inode_unlink(FAR const char * path)68 FAR struct inode *inode_unlink(FAR const char *path)
69 {
70 const char *name = path;
71 FAR struct inode *node;
72 FAR struct inode *peer;
73 FAR struct inode *parent;
74
75 /* Verify parameters. Ignore null paths and relative paths */
76
77 if (path == NULL || *path == '\0' || path[0] != '/')
78 {
79 return NULL;
80 }
81
82 /* Find the node to unlink */
83
84 node = inode_search(&name, &peer, &parent, (const char **)NULL);
85 if (node)
86 {
87 /* If peer is non-null, then remove the node from the right of
88 * of that peer node.
89 */
90
91 if (peer)
92 {
93 peer->i_peer = node->i_peer;
94 }
95
96 /* If parent is non-null, then remove the node from head of
97 * of the list of children.
98 */
99
100 else if (parent)
101 {
102 parent->i_child = node->i_peer;
103 }
104
105 /* Otherwise, we must be removing the root inode. */
106
107 else
108 {
109 g_root_inode = node->i_peer;
110 }
111
112 node->i_peer = NULL;
113 }
114
115 return node;
116 }
117
118 /****************************************************************************
119 * Name: inode_remove
120 *
121 * Description:
122 * Given a path, remove a the node from the in-memory, inode tree that the
123 * path refers to and free all resources related to the inode. If the
124 * inode is in-use, then it will be unlinked, but will not be freed until
125 * the last reference to the inode is released.
126 *
127 * Assumptions/Limitations:
128 * The caller must hold the inode semaphore
129 *
130 ****************************************************************************/
131
inode_remove(FAR const char * path)132 int inode_remove(FAR const char *path)
133 {
134 FAR struct inode *node;
135
136 /* Find the inode and unlink it from the in-memory inode tree */
137
138 node = inode_unlink(path);
139 if (node)
140 {
141 /* Found it! But we cannot delete the inode if there are references
142 * to it
143 */
144
145 if (node->i_crefs)
146 {
147 /* In that case, we will mark it deleted, when the filesystem
148 * releases the inode, we will then, finally delete the subtree
149 */
150
151 node->i_flags |= FSNODEFLAG_DELETED;
152 return -EBUSY;
153 }
154 else
155 {
156 /* And delete it now -- recursively to delete all of its children.
157 * Since it has been unlinked, then the peer pointer should be NULL.
158 */
159
160 inode_free(node->i_child);
161 (void)LOS_MemFree(m_aucSysMem0, node);
162 return OK;
163 }
164 }
165
166 /* The node does not exist */
167
168 return -ENOENT;
169 }
170