fs_inode.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /****************************************************************************
  2. * fs/inode/fs_inode.c
  3. *
  4. * Copyright (C) 2007-2009, 2011-2012, 2016-2017 Gregory Nutt. All rights
  5. * reserved.
  6. * Author: Gregory Nutt <gnutt@nuttx.org>
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. * 3. Neither the name NuttX nor the names of its contributors may be
  19. * used to endorse or promote products derived from this software
  20. * without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  24. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  25. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  26. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  27. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  28. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  29. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  30. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  31. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  32. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  33. * POSSIBILITY OF SUCH DAMAGE.
  34. *
  35. ****************************************************************************/
  36. /****************************************************************************
  37. * Included Files
  38. ****************************************************************************/
  39. #include <nuttx/config.h>
  40. #include <unistd.h>
  41. #include <semaphore.h>
  42. #include <assert.h>
  43. #include <errno.h>
  44. #include <nuttx/fs/fs.h>
  45. #include "inode/inode.h"
  46. /****************************************************************************
  47. * Pre-processor Definitions
  48. ****************************************************************************/
  49. #define NO_HOLDER ((pid_t)-1)
  50. /****************************************************************************
  51. * Private Types
  52. ****************************************************************************/
  53. /* Implements a re-entrant mutex for inode access. This must be re-entrant
  54. * because there can be cycles. For example, it may be necessary to destroy
  55. * a block driver inode on umount() after a removable block device has been
  56. * removed. In that case umount() holds the inode semaphore, but the block
  57. * driver may callback to unregister_blockdriver() after the un-mount,
  58. * requiring the semaphore again.
  59. */
  60. struct inode_sem_s
  61. {
  62. sem_t sem; /* The semaphore */
  63. pid_t holder; /* The current holder of the semaphore */
  64. int16_t count; /* Number of counts held */
  65. };
  66. /****************************************************************************
  67. * Private Data
  68. ****************************************************************************/
  69. static struct inode_sem_s g_inode_sem;
  70. /****************************************************************************
  71. * Public Functions
  72. ****************************************************************************/
  73. /****************************************************************************
  74. * Name: inode_initialize
  75. *
  76. * Description:
  77. * This is called from the OS initialization logic to configure the file
  78. * system.
  79. *
  80. ****************************************************************************/
  81. void inode_initialize(void)
  82. {
  83. /* Initialize the semaphore to one (to support one-at-a-time access to the
  84. * inode tree).
  85. */
  86. (void)nxsem_init(&g_inode_sem.sem, 0, 1);
  87. g_inode_sem.holder = NO_HOLDER;
  88. g_inode_sem.count = 0;
  89. /* Initialize files array (if it is used) */
  90. #ifdef CONFIG_HAVE_WEAKFUNCTIONS
  91. if (files_initialize != NULL)
  92. #endif
  93. {
  94. files_initialize();
  95. }
  96. }
  97. /****************************************************************************
  98. * Name: inode_semtake
  99. *
  100. * Description:
  101. * Get exclusive access to the in-memory inode tree (g_inode_sem).
  102. *
  103. ****************************************************************************/
  104. void inode_semtake(void)
  105. {
  106. pid_t me;
  107. /* Do we already hold the semaphore? */
  108. me = getpid();
  109. if (me == g_inode_sem.holder)
  110. {
  111. /* Yes... just increment the count */
  112. g_inode_sem.count++;
  113. DEBUGASSERT(g_inode_sem.count > 0);
  114. }
  115. /* Take the semaphore (perhaps waiting) */
  116. else
  117. {
  118. int ret;
  119. do
  120. {
  121. ret = nxsem_wait(&g_inode_sem.sem);
  122. /* The only case that an error should occur here is if the wait
  123. * was awakened by a signal.
  124. */
  125. DEBUGASSERT(ret == OK || ret == -EINTR);
  126. }
  127. while (ret == -EINTR);
  128. /* No we hold the semaphore */
  129. g_inode_sem.holder = me;
  130. g_inode_sem.count = 1;
  131. }
  132. }
  133. /****************************************************************************
  134. * Name: inode_semgive
  135. *
  136. * Description:
  137. * Relinquish exclusive access to the in-memory inode tree (g_inode_sem).
  138. *
  139. ****************************************************************************/
  140. void inode_semgive(void)
  141. {
  142. DEBUGASSERT(g_inode_sem.holder == getpid());
  143. /* Is this our last count on the semaphore? */
  144. if (g_inode_sem.count > 1)
  145. {
  146. /* No.. just decrement the count */
  147. g_inode_sem.count--;
  148. }
  149. /* Yes.. then we can really release the semaphore */
  150. else
  151. {
  152. g_inode_sem.holder = NO_HOLDER;
  153. g_inode_sem.count = 0;
  154. nxsem_post(&g_inode_sem.sem);
  155. }
  156. }