fs_tmpfs.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /****************************************************************************
  2. * fs/tmpfs/fs_tmpfs.h
  3. *
  4. * Copyright (C) 2015 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. #ifndef __FS_TMPFS_FS_TMPFS_H
  36. #define __FS_TMPFS_FS_TMPFS_H
  37. /****************************************************************************
  38. * Included Files
  39. ****************************************************************************/
  40. #include <nuttx/config.h>
  41. #include <stdint.h>
  42. #include <nuttx/fs/fs.h>
  43. #include <nuttx/semaphore.h>
  44. /****************************************************************************
  45. * Pre-processor Definitions
  46. ****************************************************************************/
  47. /* Indicates that there is no holder of the re-entrant semaphore */
  48. #define TMPFS_NO_HOLDER -1
  49. /* Bit definitions for file object flags */
  50. #define TFO_FLAG_UNLINKED (1 << 0) /* Bit 0: File is unlinked */
  51. /****************************************************************************
  52. * Public Types
  53. ****************************************************************************/
  54. /* TMPFS memory object types */
  55. enum tmpfs_objtype_e
  56. {
  57. TMPFS_DIRECTORY = 0, /* Directory */
  58. TMPFS_REGULAR /* Regular file */
  59. };
  60. /* Values returned by tmpfs_foreach() */
  61. enum tmpfs_foreach_e
  62. {
  63. TMPFS_CONTINUE = 0, /* Continue enumeration */
  64. TMPFS_HALT, /* Stop enumeration */
  65. TMPFS_DELETED, /* Object and directory entry deleted */
  66. TMPFS_UNLINKED /* Only the directory entry was deleted */
  67. };
  68. /* Re-entrant semaphore */
  69. struct tmpfs_sem_s
  70. {
  71. sem_t ts_sem; /* The actual semaphore */
  72. pid_t ts_holder; /* Current older (-1 if not held) */
  73. uint16_t ts_count; /* Number of counts held */
  74. };
  75. /* The form of one directory entry */
  76. struct tmpfs_dirent_s
  77. {
  78. FAR struct tmpfs_object_s *tde_object;
  79. FAR char *tde_name;
  80. };
  81. /* The generic form of a TMPFS memory object */
  82. struct tmpfs_object_s
  83. {
  84. FAR struct tmpfs_dirent_s *to_dirent;
  85. struct tmpfs_sem_s to_exclsem;
  86. size_t to_alloc; /* Allocated size of the memory object */
  87. uint8_t to_type; /* See enum tmpfs_objtype_e */
  88. uint8_t to_refs; /* Reference count */
  89. };
  90. /* The form of a directory memory object */
  91. struct tmpfs_directory_s
  92. {
  93. /* First fields must match common TMPFS object layout */
  94. FAR struct tmpfs_dirent_s *tdo_dirent;
  95. struct tmpfs_sem_s tdo_exclsem;
  96. size_t tdo_alloc; /* Allocated size of the directory object */
  97. uint8_t tdo_type; /* See enum tmpfs_objtype_e */
  98. uint8_t tdo_refs; /* Reference count */
  99. /* Remaining fields are unique to a directory object */
  100. uint16_t tdo_nentries; /* Number of directory entries */
  101. struct tmpfs_dirent_s tdo_entry[1];
  102. };
  103. #define SIZEOF_TMPFS_DIRECTORY(n) \
  104. (sizeof(struct tmpfs_directory_s) + ((n) - 1) * sizeof(struct tmpfs_dirent_s))
  105. /* The form of a regular file memory object
  106. *
  107. * NOTE that in this very simplified implementation, there is no per-open
  108. * state. The file memory object also serves as the open file object,
  109. * saving an allocation. This has the negative side effect that no per-
  110. * open state can be retained (such as open flags).
  111. */
  112. struct tmpfs_file_s
  113. {
  114. /* First fields must match common TMPFS object layout */
  115. FAR struct tmpfs_dirent_s *tfo_dirent;
  116. struct tmpfs_sem_s tfo_exclsem;
  117. size_t tfo_alloc; /* Allocated size of the file object */
  118. uint8_t tfo_type; /* See enum tmpfs_objtype_e */
  119. uint8_t tfo_refs; /* Reference count */
  120. /* Remaining fields are unique to a directory object */
  121. uint8_t tfo_flags; /* See TFO_FLAG_* definitions */
  122. size_t tfo_size; /* Valid file size */
  123. uint8_t tfo_data[1]; /* File data starts here */
  124. };
  125. #define SIZEOF_TMPFS_FILE(n) (sizeof(struct tmpfs_file_s) + (n) - 1)
  126. /* This structure represents one instance of a TMPFS file system */
  127. struct tmpfs_s
  128. {
  129. /* The root directory */
  130. FAR struct tmpfs_dirent_s tfs_root;
  131. struct tmpfs_sem_s tfs_exclsem;
  132. };
  133. /* This is the type used the tmpfs_statfs_callout to accumulate memory
  134. * usage
  135. */
  136. struct tmpfs_statfs_s
  137. {
  138. size_t tsf_alloc; /* Total memory allocated */
  139. size_t tsf_inuse; /* Total memory in use */
  140. off_t tsf_files; /* Total file nodes in the file system */
  141. off_t tsf_ffree; /* Free directory nodes in the file system */
  142. };
  143. /* This is the type of the for tmpfs_foreach callback */
  144. typedef int (*tmpfs_foreach_t)(FAR struct tmpfs_directory_s *tdo,
  145. unsigned int index, FAR void *arg);
  146. /****************************************************************************
  147. * Public Data
  148. ****************************************************************************/
  149. #undef EXTERN
  150. #if defined(__cplusplus)
  151. #define EXTERN extern "C"
  152. extern "C"
  153. {
  154. #else
  155. #define EXTERN extern
  156. #endif
  157. EXTERN const struct mountpt_operations tmpfs_operations;
  158. /****************************************************************************
  159. * Public Function Prototypes
  160. ****************************************************************************/
  161. #undef EXTERN
  162. #if defined(__cplusplus)
  163. }
  164. #endif
  165. #endif /* __FS_TMPFS_FS_TMPFS_H */