1 // Copyright 2018 The Fuchsia Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #pragma once
6 
7 #ifdef __cplusplus
8 extern "C" {
9 #endif
10 
11 #include <stddef.h>    // for size_t definition
12 #include <stdint.h>    // for fixed width types
13 #include <time.h>      // for time_t definition
14 #include <stdio_tfs.h> // for FILENAME_MAX
15 
16 /***********************************************************************/
17 /* Symbol Definitions                                                  */
18 /***********************************************************************/
19 #define dev_t uint32_t
20 #define gid_t uint16_t
21 #define ino_t uint32_t
22 #define nlink_t uint32_t
23 #define off_t int32_t
24 #define off64_t int64_t
25 #define uid_t uint16_t
26 #define pid_t uint32_t
27 #define mode_t uint16_t
28 
29 // vstat() file system types - also used for NDM partitions, so existing
30 // values cannot be modified, only new values added or old ones removed
31 
32 #define FAT_VOL 2
33 #define XFS_VOL 6
34 #define USR_VOL 7
35 
36 // vstat() FTL types
37 
38 #define FTL_NONE 1
39 #define FTL_NDM 2
40 
41 // Types of Flash
42 
43 #define FFS_NAND_SLC (1 << 0)
44 #define FFS_NAND_MLC (1 << 1)
45 
46 /***********************************************************************/
47 /* Type definitions                                                    */
48 /***********************************************************************/
49 
50 // Driver count statistics for TargetFTL-NDM volumes
51 typedef struct {
52     uint32_t write_page;
53     uint32_t read_page;
54     uint32_t read_spare;
55     uint32_t page_check;
56     uint32_t page_erased;
57     uint32_t transfer_page;
58     uint32_t erase_block;
59     uint32_t ram_used;
60     uint32_t wear_count;
61 } ftl_ndm_stats;
62 
63 // Driver count statistics for TargetFTL volumes
64 typedef union {
65     ftl_ndm_stats ndm;
66 } ftl_drvr_stats;
67 
68 // Driver count statistics for TargetFAT/TargetFTL volumes
69 typedef struct {
70     uint32_t write_sectors;
71     uint32_t read_sectors;
72     ftl_drvr_stats ftl;
73 } fat_drvr_stats;
74 
75 // Driver count statistics for TargetXFS/TargetFTL volumes
76 typedef struct {
77     uint32_t write_pages;
78     uint32_t read_pages;
79     ftl_drvr_stats ftl;
80 } xfs_drvr_stats;
81 
82 typedef struct {
83     uint32_t vol_type;      // set to FAT_VOL
84     uint32_t sect_size;     // sector size in bytes
85     uint32_t garbage_level; // garbage level as percentage 0 to 100
86     uint32_t ftl_type;
87     fat_drvr_stats drvr_stats; // driver count statistics
88     uint32_t clust_size;       // cluster size in bytes
89     uint32_t num_clusts;       // number of clusters in volume
90     uint32_t num_sects;        // number of FAT sectors in volume
91     uint32_t used_clusts;      // number of used clusters
92     uint32_t free_clusts;      // number of free clusters
93     uint32_t fat_type;         // FAT12, FAT16, or FAT32
94     uint32_t root_dir_sects;   // number of root directory sectors
95     uint32_t sects_per_fat;    // FAT (1 table) size in sectors
96     uint32_t num_fats;         // number of FAT tables
97     uint32_t ram_used;         // amount of RAM in bytes used by volume
98     uint32_t cached_clusts;    // size of data cache in clusters
99     int cache_hits;            // percentage of data cache hits
100 } vstat_fat;
101 
102 typedef struct // Must be the same as vstat_fat up to driver stats
103 {
104     uint32_t vol_type;      // set to XFS_VOL
105     uint32_t sect_size;     // sector size in bytes
106     uint32_t garbage_level; // garbage level as percentage 0 to 100
107     uint32_t ftl_type;
108     xfs_drvr_stats drvr_stats; // driver count statistics
109     uint32_t num_sects;        // number of sectors on volume
110     uint32_t free_sects;       // number of free sectors on volume
111     uint32_t free_2_sync;      // # of free sectors before forced sync
112     uint32_t pages_per_stbl;   // number of pages per sector table
113     uint32_t ram_used;         // amount of RAM in bytes used by volume
114     uint32_t cached_pages;     // size of data cache in pages
115     int cache_hits;            // percentage of data cache hits
116 } vstat_xfs;
117 
118 union vstat {
119     uint32_t vol_type; // FS volume type
120     vstat_fat fat;
121     vstat_xfs xfs;
122 };
123 
124 typedef struct file DIR_TFS;
125 
126 #define D_NAME_PTR FALSE // set to TRUE to use char * for d_name
127 struct dirent_TFS {
128     long d_ino;
129 #if D_NAME_PTR
130     char* d_name;
131 #else
132     char d_name[FILENAME_MAX + 1];
133 #endif
134 };
135 
136 struct utimbuf {
137     time_t actime;  // access time
138     time_t modtime; // modification time
139 };
140 
141 struct stat_TFS {
142     dev_t st_dev;     // ID of device containing this file
143     ino_t st_ino;     // file serial number
144     nlink_t st_nlink; // number of links
145     dev_t st_rdev;    // device ID (if inode device)
146     off_t st_size;    // the file size in bytes
147     time_t st_atime;  // time of last access
148     time_t st_mtime;  // time of last data modification
149     time_t st_ctime;  // time of last status change
150     mode_t st_mode;   // file mode
151     uid_t st_uid;     // user ID of file's owner
152     gid_t st_gid;     // group ID of file's owner
153 };
154 
155 // Structure containing file/dir info for sortdir() comparisons
156 typedef struct {
157     const char* st_name; // file name
158     ino_t st_ino;        // file serial number
159     nlink_t st_nlink;    // number of links
160     off_t st_size;       // file size in bytes
161     time_t st_atime;     // time of last access
162     time_t st_mtime;     // time of last data modification
163     time_t st_ctime;     // time of last status change
164     mode_t st_mode;      // file mode
165     uid_t st_uid;        // user ID of file's owner
166     gid_t st_gid;        // group ID of file's owner
167 } DirEntry;
168 
169 int XfsDelVol(const char* name);
170 int FtlNdmDelVol(const char* name);
171 
172 #ifdef __cplusplus
173 }
174 #endif
175