1 | /*
|
---|
2 | * Copyright (c) 2008 Jakub Jermar
|
---|
3 | * Copyright (c) 2011 Oleg Romanenko
|
---|
4 | * All rights reserved.
|
---|
5 | *
|
---|
6 | * Redistribution and use in source and binary forms, with or without
|
---|
7 | * modification, are permitted provided that the following conditions
|
---|
8 | * are met:
|
---|
9 | *
|
---|
10 | * - Redistributions of source code must retain the above copyright
|
---|
11 | * notice, this list of conditions and the following disclaimer.
|
---|
12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
13 | * notice, this list of conditions and the following disclaimer in the
|
---|
14 | * documentation and/or other materials provided with the distribution.
|
---|
15 | * - The name of the author may not be used to endorse or promote products
|
---|
16 | * derived from this software without specific prior written permission.
|
---|
17 | *
|
---|
18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
28 | */
|
---|
29 |
|
---|
30 | /** @addtogroup fat
|
---|
31 | * @{
|
---|
32 | */
|
---|
33 |
|
---|
34 | #ifndef FAT_FAT_H_
|
---|
35 | #define FAT_FAT_H_
|
---|
36 |
|
---|
37 | #include "fat_fat.h"
|
---|
38 | #include <fibril_synch.h>
|
---|
39 | #include <libfs.h>
|
---|
40 | #include <stdint.h>
|
---|
41 | #include <stdbool.h>
|
---|
42 | #include <macros.h>
|
---|
43 | #include "../../vfs/vfs.h"
|
---|
44 |
|
---|
45 | #ifndef dprintf
|
---|
46 | #define dprintf(...) printf(__VA_ARGS__)
|
---|
47 | #endif
|
---|
48 |
|
---|
49 | /*
|
---|
50 | * Convenience macros for accessing some frequently used boot sector members.
|
---|
51 | */
|
---|
52 | #define BPS(bs) uint16_t_le2host((bs)->bps)
|
---|
53 | #define SPC(bs) (bs)->spc
|
---|
54 | #define RSCNT(bs) uint16_t_le2host((bs)->rscnt)
|
---|
55 | #define FATCNT(bs) (bs)->fatcnt
|
---|
56 |
|
---|
57 | #define SF(bs) (uint16_t_le2host((bs)->sec_per_fat) ? \
|
---|
58 | uint16_t_le2host((bs)->sec_per_fat) : \
|
---|
59 | uint32_t_le2host(bs->fat32.sectors_per_fat))
|
---|
60 |
|
---|
61 | #define RDE(bs) uint16_t_le2host((bs)->root_ent_max)
|
---|
62 |
|
---|
63 | #define TS(bs) (uint16_t_le2host((bs)->totsec16) ? \
|
---|
64 | uint16_t_le2host((bs)->totsec16) : \
|
---|
65 | uint32_t_le2host(bs->totsec32))
|
---|
66 |
|
---|
67 | #define BS_BLOCK 0
|
---|
68 | #define BS_SIZE 512
|
---|
69 |
|
---|
70 | typedef struct fat_bs {
|
---|
71 | /** Jump instruction */
|
---|
72 | uint8_t ji[3];
|
---|
73 | uint8_t oem_name[8];
|
---|
74 |
|
---|
75 | /* BIOS Parameter Block */
|
---|
76 |
|
---|
77 | /** Bytes per sector */
|
---|
78 | uint16_t bps;
|
---|
79 | /** Sectors per cluster */
|
---|
80 | uint8_t spc;
|
---|
81 | /** Reserved sector count */
|
---|
82 | uint16_t rscnt;
|
---|
83 | /** Number of FATs */
|
---|
84 | uint8_t fatcnt;
|
---|
85 | /** Maximum number of root directory entries */
|
---|
86 | uint16_t root_ent_max;
|
---|
87 | /** Total sectors. 16-bit version */
|
---|
88 | uint16_t totsec16;
|
---|
89 | /** Media descriptor */
|
---|
90 | uint8_t mdesc;
|
---|
91 | /** Sectors per FAT12/FAT16 */
|
---|
92 | uint16_t sec_per_fat;
|
---|
93 | /** Sectors per track */
|
---|
94 | uint16_t sec_per_track;
|
---|
95 | /** Number of heads */
|
---|
96 | uint16_t headcnt;
|
---|
97 | /** Hidden sectors */
|
---|
98 | uint32_t hidden_sec;
|
---|
99 | /** Total sectors. 32-bit version */
|
---|
100 | uint32_t totsec32;
|
---|
101 |
|
---|
102 | union {
|
---|
103 | struct {
|
---|
104 | /* FAT12/FAT16 only: Extended BIOS Parameter Block */
|
---|
105 | /** Physical drive number. */
|
---|
106 | uint8_t pdn;
|
---|
107 | uint8_t reserved;
|
---|
108 | /** Extended boot signature. */
|
---|
109 | uint8_t ebs;
|
---|
110 | /** Serial number. */
|
---|
111 | uint32_t id;
|
---|
112 | /** Volume label. */
|
---|
113 | uint8_t label[11];
|
---|
114 | /** FAT type. */
|
---|
115 | uint8_t type[8];
|
---|
116 | /** Boot code. */
|
---|
117 | uint8_t boot_code[448];
|
---|
118 | /** Boot sector signature. */
|
---|
119 | uint16_t signature;
|
---|
120 | } __attribute__((packed));
|
---|
121 | struct {
|
---|
122 | /* FAT32 only */
|
---|
123 | /** Sectors per FAT. */
|
---|
124 | uint32_t sectors_per_fat;
|
---|
125 | /** FAT flags. */
|
---|
126 | uint16_t flags;
|
---|
127 | /** Version. */
|
---|
128 | uint16_t version;
|
---|
129 | /** Cluster number of root directory. */
|
---|
130 | uint32_t root_cluster;
|
---|
131 | /** Sector number of file system information sector. */
|
---|
132 | uint16_t fsinfo_sec;
|
---|
133 | /** Sector number of boot sector copy. */
|
---|
134 | uint16_t bscopy_sec;
|
---|
135 | uint8_t reserved1[12];
|
---|
136 | /** Physical drive number. */
|
---|
137 | uint8_t pdn;
|
---|
138 | uint8_t reserved2;
|
---|
139 | /** Extended boot signature. */
|
---|
140 | uint8_t ebs;
|
---|
141 | /** Serial number. */
|
---|
142 | uint32_t id;
|
---|
143 | /** Volume label. */
|
---|
144 | uint8_t label[11];
|
---|
145 | /** FAT type. */
|
---|
146 | uint8_t type[8];
|
---|
147 | /** Boot code. */
|
---|
148 | uint8_t boot_code[420];
|
---|
149 | /** Signature. */
|
---|
150 | uint16_t signature;
|
---|
151 | } __attribute__((packed)) fat32;
|
---|
152 | };
|
---|
153 | } __attribute__((packed)) fat_bs_t;
|
---|
154 |
|
---|
155 | #define FAT32_FSINFO_SIG1 "RRaA"
|
---|
156 | #define FAT32_FSINFO_SIG2 "rrAa"
|
---|
157 | #define FAT32_FSINFO_SIG3 "\x00\x00\x55\xaa"
|
---|
158 |
|
---|
159 | typedef struct {
|
---|
160 | uint8_t sig1[4];
|
---|
161 | uint8_t res1[480];
|
---|
162 | uint8_t sig2[4];
|
---|
163 | uint32_t free_clusters;
|
---|
164 | uint32_t last_allocated_cluster;
|
---|
165 | uint8_t res2[12];
|
---|
166 | uint8_t sig3[4];
|
---|
167 | } __attribute__((packed)) fat32_fsinfo_t;
|
---|
168 |
|
---|
169 | typedef enum {
|
---|
170 | FAT_INVALID,
|
---|
171 | FAT_DIRECTORY,
|
---|
172 | FAT_FILE
|
---|
173 | } fat_node_type_t;
|
---|
174 |
|
---|
175 | struct fat_node;
|
---|
176 |
|
---|
177 | /** FAT index structure.
|
---|
178 | *
|
---|
179 | * This structure exists to help us to overcome certain limitations of the FAT
|
---|
180 | * file system design. The problem with FAT is that it is hard to find
|
---|
181 | * an entity which could represent a VFS index. There are two candidates:
|
---|
182 | *
|
---|
183 | * a) number of the node's first cluster
|
---|
184 | * b) the pair of the parent directory's first cluster and the dentry index
|
---|
185 | * within the parent directory
|
---|
186 | *
|
---|
187 | * We need VFS indices to be:
|
---|
188 | * A) unique
|
---|
189 | * B) stable in time, at least until the next mount
|
---|
190 | *
|
---|
191 | * Unfortunately a) does not meet the A) criterion because zero-length files
|
---|
192 | * will have the first cluster field cleared. And b) does not meet the B)
|
---|
193 | * criterion because unlink() and rename() will both free up the original
|
---|
194 | * dentry, which contains all the essential info about the file.
|
---|
195 | *
|
---|
196 | * Therefore, a completely opaque indices are used and the FAT server maintains
|
---|
197 | * a mapping between them and otherwise nice b) variant. On rename(), the VFS
|
---|
198 | * index stays unaltered, while the internal FAT "physical tree address"
|
---|
199 | * changes. The unlink case is also handled this way thanks to an in-core node
|
---|
200 | * pointer embedded in the index structure.
|
---|
201 | */
|
---|
202 | typedef struct {
|
---|
203 | /** Used indices (position) hash table link. */
|
---|
204 | ht_link_t uph_link;
|
---|
205 | /** Used indices (index) hash table link. */
|
---|
206 | ht_link_t uih_link;
|
---|
207 |
|
---|
208 | fibril_mutex_t lock;
|
---|
209 | service_id_t service_id;
|
---|
210 | fs_index_t index;
|
---|
211 | /**
|
---|
212 | * Parent node's first cluster.
|
---|
213 | * Zero is used if this node is not linked, in which case nodep must
|
---|
214 | * contain a pointer to the in-core node structure.
|
---|
215 | * One is used when the parent is the root directory.
|
---|
216 | */
|
---|
217 | fat_cluster_t pfc;
|
---|
218 | /** Directory entry index within the parent node. */
|
---|
219 | unsigned pdi;
|
---|
220 | /** Pointer to in-core node instance. */
|
---|
221 | struct fat_node *nodep;
|
---|
222 | } fat_idx_t;
|
---|
223 |
|
---|
224 | /** FAT in-core node. */
|
---|
225 | typedef struct fat_node {
|
---|
226 | /** Back pointer to the FS node. */
|
---|
227 | fs_node_t *bp;
|
---|
228 |
|
---|
229 | fibril_mutex_t lock;
|
---|
230 | fat_node_type_t type;
|
---|
231 | fat_idx_t *idx;
|
---|
232 | /**
|
---|
233 | * Node's first cluster.
|
---|
234 | * Zero is used for zero-length nodes.
|
---|
235 | * One is used to mark root directory.
|
---|
236 | */
|
---|
237 | fat_cluster_t firstc;
|
---|
238 | /** FAT in-core node free list link. */
|
---|
239 | link_t ffn_link;
|
---|
240 | aoff64_t size;
|
---|
241 | unsigned lnkcnt;
|
---|
242 | unsigned refcnt;
|
---|
243 | bool dirty;
|
---|
244 |
|
---|
245 | /*
|
---|
246 | * Cache of the node's last and "current" cluster to avoid some
|
---|
247 | * unnecessary FAT walks.
|
---|
248 | */
|
---|
249 | /* Node's last cluster in FAT. */
|
---|
250 | bool lastc_cached_valid;
|
---|
251 | fat_cluster_t lastc_cached_value;
|
---|
252 | /* Node's "current" cluster, i.e. where the last I/O took place. */
|
---|
253 | bool currc_cached_valid;
|
---|
254 | aoff64_t currc_cached_bn;
|
---|
255 | fat_cluster_t currc_cached_value;
|
---|
256 | } fat_node_t;
|
---|
257 |
|
---|
258 | typedef struct {
|
---|
259 | bool lfn_enabled;
|
---|
260 | } fat_instance_t;
|
---|
261 |
|
---|
262 | extern vfs_out_ops_t fat_ops;
|
---|
263 | extern libfs_ops_t fat_libfs_ops;
|
---|
264 |
|
---|
265 | extern errno_t fat_idx_get_new(fat_idx_t **, service_id_t);
|
---|
266 | extern fat_idx_t *fat_idx_get_by_pos(service_id_t, fat_cluster_t, unsigned);
|
---|
267 | extern fat_idx_t *fat_idx_get_by_index(service_id_t, fs_index_t);
|
---|
268 | extern void fat_idx_destroy(fat_idx_t *);
|
---|
269 | extern void fat_idx_hashin(fat_idx_t *);
|
---|
270 | extern void fat_idx_hashout(fat_idx_t *);
|
---|
271 |
|
---|
272 | extern errno_t fat_idx_init(void);
|
---|
273 | extern void fat_idx_fini(void);
|
---|
274 | extern errno_t fat_idx_init_by_service_id(service_id_t);
|
---|
275 | extern void fat_idx_fini_by_service_id(service_id_t);
|
---|
276 |
|
---|
277 | #endif
|
---|
278 |
|
---|
279 | /**
|
---|
280 | * @}
|
---|
281 | */
|
---|