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