fs_inoderelease.c 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /****************************************************************************
  2. * fs/inode/fs_inoderelease.c
  3. *
  4. * Licensed to the Apache Software Foundation (ASF) under one or more
  5. * contributor license agreements. See the NOTICE file distributed with
  6. * this work for additional information regarding copyright ownership. The
  7. * ASF licenses this file to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance with the
  9. * License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  16. * License for the specific language governing permissions and limitations
  17. * under the License.
  18. *
  19. ****************************************************************************/
  20. /****************************************************************************
  21. * Included Files
  22. ****************************************************************************/
  23. #include <nuttx/config.h>
  24. #include <debug.h>
  25. #include <errno.h>
  26. #include <nuttx/kmalloc.h>
  27. #include <nuttx/fs/fs.h>
  28. #include "inode/inode.h"
  29. /****************************************************************************
  30. * Public Functions
  31. ****************************************************************************/
  32. /****************************************************************************
  33. * Name: inode_release
  34. *
  35. * Description:
  36. * This is called from close() logic when it no longer refers to the inode.
  37. *
  38. ****************************************************************************/
  39. void inode_release(FAR struct inode *node)
  40. {
  41. int ret;
  42. if (node)
  43. {
  44. /* Decrement the references of the inode */
  45. do
  46. {
  47. ret = inode_semtake();
  48. /* This only possible error is due to cancellation of the thread.
  49. * We need to try again anyway in this case, otherwise the
  50. * reference count would be wrong.
  51. */
  52. DEBUGASSERT(ret == OK || ret == -ECANCELED);
  53. }
  54. while (ret < 0);
  55. if (node->i_crefs)
  56. {
  57. node->i_crefs--;
  58. }
  59. /* If the subtree was previously deleted and the reference
  60. * count has decrement to zero, then delete the inode
  61. * now.
  62. */
  63. if (node->i_crefs <= 0 && (node->i_flags & FSNODEFLAG_DELETED) != 0)
  64. {
  65. /* If the inode has been properly unlinked, then the peer pointer
  66. * should be NULL.
  67. */
  68. inode_semgive();
  69. DEBUGASSERT(node->i_peer == NULL);
  70. inode_free(node);
  71. }
  72. else
  73. {
  74. inode_semgive();
  75. }
  76. }
  77. }