• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /****************************************************************************
2  * fs/tmpfs/fs_tmpfs.h
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 #ifndef __FS_TMPFS_FS_TMPFS_H
22 #define __FS_TMPFS_FS_TMPFS_H
23 
24 /****************************************************************************
25  * Included Files
26  ****************************************************************************/
27 
28 #include <semaphore.h>
29 
30 #ifdef __cplusplus
31 #if __cplusplus
32 extern "C" {
33 #endif /* __cplusplus */
34 #endif /* __cplusplus */
35 
36 #ifdef LOSCFG_FS_RAMFS
37 
38 /****************************************************************************
39  * Pre-processor Definitions
40  ****************************************************************************/
41 /* Indicates that there is no holder of the re-entrant semaphore */
42 
43 #define TMPFS_NO_HOLDER   -1
44 
45 /* Bit definitions for file object flags */
46 
47 #define TFO_FLAG_UNLINKED (1 << 0)  /* Bit 0: File is unlinked */
48 
49 /****************************************************************************
50  * Public Types
51  ****************************************************************************/
52 
53 /* TMPFS memory object types */
54 
55 enum tmpfs_objtype_e
56 {
57   TMPFS_DIRECTORY = 0,  /* Directory */
58   TMPFS_REGULAR         /* Regular file */
59 };
60 
61 /* Values returned by tmpfs_foreach() */
62 
63 enum tmpfs_foreach_e
64 {
65   TMPFS_CONTINUE = 0,    /* Continue enumeration */
66   TMPFS_HALT,            /* Stop enumeration */
67   TMPFS_DELETED,         /* Object and directory entry deleted */
68   TMPFS_UNLINKED         /* Only the directory entry was deleted */
69 };
70 
71 /* Re-entrant semaphore */
72 
73 struct tmpfs_sem_s
74 {
75   sem_t    ts_sem;       /* The actual semaphore */
76   pid_t    ts_holder;    /* Current older (-1 if not held) */
77   uint16_t ts_count;     /* Number of counts held */
78 };
79 
80 /* The form of one directory entry */
81 
82 struct tmpfs_dirent_s
83 {
84   LOS_DL_LIST tde_node;
85   struct tmpfs_object_s *tde_object;
86   char *tde_name;
87   bool tde_inuse;
88 };
89 
90 /* The generic form of a TMPFS memory object */
91 
92 struct tmpfs_object_s
93 {
94   struct tmpfs_dirent_s *to_dirent;
95   struct tmpfs_sem_s to_exclsem;
96 
97   uint8_t  to_type;      /* See enum tmpfs_objtype_e */
98   uint8_t  to_refs;      /* Reference count */
99   time_t   to_ctime;     /* last changed status time */
100   time_t   to_mtime;     /* last modified time */
101   time_t   to_atime;     /* last access time */
102 
103   mode_t   mode;
104   uint     gid;
105   uint     uid;
106 };
107 
108 /* The form of a directory memory object */
109 
110 struct tmpfs_directory_s
111 {
112   /* First fields must match common TMPFS object layout */
113 
114   struct tmpfs_dirent_s *tdo_dirent;
115   struct tmpfs_sem_s tdo_exclsem;
116 
117   uint8_t  tdo_type;     /* See enum tmpfs_objtype_e */
118   uint8_t  tdo_refs;     /* Reference count */
119   time_t   tdo_ctime;    /* last changed status time */
120   time_t   tdo_mtime;    /* last modified time */
121   time_t   tdo_atime;    /* last access time */
122 
123   mode_t   mode;
124   uint     gid;
125   uint     uid;
126 
127   /* Remaining fields are unique to a directory object */
128 
129   uint8_t  tdo_count;    /* Number of times the directory was opened */
130   uint16_t tdo_nentries; /* Number of directory entries */
131   LOS_DL_LIST tdo_entry;
132 };
133 
134 #define SIZEOF_TMPFS_DIRECTORY(n) \
135   (sizeof(struct tmpfs_directory_s) + ((n) - 1) * sizeof(struct tmpfs_dirent_s))
136 
137 /* The form of a regular file memory object
138  *
139  * NOTE that in this very simplified implementation, there is no per-open
140  * state.  The file memory object also serves as the open file object,
141  * saving an allocation.  This has the negative side effect that no per-
142  * open state can be retained (such as open flags).
143  */
144 
145 struct tmpfs_file_s
146 {
147   /* First fields must match common TMPFS object layout */
148 
149   struct tmpfs_dirent_s *tfo_dirent;
150   struct tmpfs_sem_s tfo_exclsem;
151 
152   uint8_t  tfo_type;     /* See enum tmpfs_objtype_e */
153   uint8_t  tfo_refs;     /* Reference count */
154   time_t   tfo_ctime;    /* last changed status time */
155   time_t   tfo_mtime;    /* last modified time */
156   time_t   tfo_atime;    /* last access time */
157 
158   mode_t   mode;
159   uint     gid;
160   uint     uid;
161 
162   /* Remaining fields are unique to a directory object */
163 
164   uint8_t  tfo_flags;    /* See TFO_FLAG_* definitions */
165   size_t   tfo_size;     /* Valid file size */
166   char     *tfo_data;    /* File data pointer */
167 };
168 
169 #define SIZEOF_TMPFS_FILE(n) (sizeof(struct tmpfs_file_s) + (n) - 1)
170 
171 /* This structure represents one instance of a TMPFS file system */
172 
173 struct tmpfs_s
174 {
175   /* The root directory */
176   struct tmpfs_dirent_s tfs_root;
177   struct tmpfs_sem_s tfs_exclsem;
178 };
179 
180 /* This is the type used the tmpfs_statfs_callout to accumulate memory usage */
181 
182 struct tmpfs_statfs_s
183 {
184   size_t tsf_alloc;      /* Total memory allocated */
185   size_t tsf_inuse;      /* Total memory in use */
186   off_t  tsf_files;      /* Total file nodes in the file system */
187   off_t  tsf_ffree;      /* Free directory nodes in the file system */
188 };
189 
190 /* This is the type of the for tmpfs_foreach callback */
191 
192 typedef int (*tmpfs_foreach_t)(struct tmpfs_directory_s *tdo,
193                                unsigned int index, void *arg);
194 
195 /****************************************************************************
196  * Public Data
197  ****************************************************************************/
198 
199 extern void los_set_ramfs_unit(off_t size);
200 
201 #endif
202 
203 #ifdef __cplusplus
204 #if __cplusplus
205 }
206 #endif /* __cplusplus */
207 #endif /* __cplusplus */
208 
209 #endif /* __FS_TMPFS_FS_TMPFS_H */
210