fs_inoderemove.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. * Included Files
  37. ****************************************************************************/
  38. #include <nuttx/config.h>
  39. #include <errno.h>
  40. #include <nuttx/kmalloc.h>
  41. #include <nuttx/fs/fs.h>
  42. #include "inode/inode.h"
  43. /****************************************************************************
  44. * Public Functions
  45. ****************************************************************************/
  46. /****************************************************************************
  47. * Name: inode_unlink
  48. *
  49. * Description:
  50. * Given a path, remove a the node from the in-memory, inode tree that the
  51. * path refers to. This is normally done in preparation to removing or
  52. * moving an inode.
  53. *
  54. * In symbolic links in the pseduo file system are enabled, then this
  55. * logic will follow the symbolic links up until the terminal node. Then
  56. * that link in removed. So if this the terminal node is a symbolic link,
  57. * the symbolic link node will be removed, not the target of the link.
  58. *
  59. * Assumptions/Limitations:
  60. * The caller must hold the inode semaphore
  61. *
  62. ****************************************************************************/
  63. FAR struct inode *inode_unlink(FAR const char *path)
  64. {
  65. struct inode_search_s desc;
  66. FAR struct inode *node = NULL;
  67. int ret;
  68. /* Verify parameters. Ignore null paths */
  69. if (path == NULL)
  70. {
  71. return NULL;
  72. }
  73. /* Find the node to unlink */
  74. SETUP_SEARCH(&desc, path, true);
  75. ret = inode_search(&desc);
  76. if (ret >= 0)
  77. {
  78. node = desc.node;
  79. DEBUGASSERT(node != NULL);
  80. /* If peer is non-null, then remove the node from the right of
  81. * of that peer node.
  82. */
  83. if (desc.peer != NULL)
  84. {
  85. desc.peer->i_peer = node->i_peer;
  86. }
  87. /* Then remove the node from head of the list of children. */
  88. else
  89. {
  90. DEBUGASSERT(desc.parent != NULL);
  91. desc.parent->i_child = node->i_peer;
  92. }
  93. node->i_peer = NULL;
  94. }
  95. RELEASE_SEARCH(&desc);
  96. return node;
  97. }
  98. /****************************************************************************
  99. * Name: inode_remove
  100. *
  101. * Description:
  102. * Given a path, remove a the node from the in-memory, inode tree that the
  103. * path refers to and free all resources related to the inode. If the
  104. * inode is in-use, then it will be unlinked, but will not be freed until
  105. * the last reference to the inode is released.
  106. *
  107. * Assumptions/Limitations:
  108. * The caller must hold the inode semaphore
  109. *
  110. ****************************************************************************/
  111. int inode_remove(FAR const char *path)
  112. {
  113. FAR struct inode *node;
  114. /* Find the inode and unlink it from the in-memory inode tree */
  115. node = inode_unlink(path);
  116. if (node)
  117. {
  118. /* Found it! But we cannot delete the inode if there are references
  119. * to it
  120. */
  121. if (node->i_crefs)
  122. {
  123. /* In that case, we will mark it deleted, when the filesystem
  124. * releases the inode, we will then, finally delete the subtree
  125. */
  126. node->i_flags |= FSNODEFLAG_DELETED;
  127. return -EBUSY;
  128. }
  129. else
  130. {
  131. /* And delete it now -- recursively to delete all of its children.
  132. * Since it has been unlinked, then the peer pointer should be
  133. * NULL.
  134. */
  135. DEBUGASSERT(node->i_peer == NULL);
  136. inode_free(node);
  137. return OK;
  138. }
  139. }
  140. /* The node does not exist */
  141. return -ENOENT;
  142. }