Rev 3111 | Rev 3386 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 3111 | Rev 3307 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (c) 2008 Jakub Jermar |
2 | * Copyright (c) 2008 Jakub Jermar |
3 | * All rights reserved. |
3 | * All rights reserved. |
4 | * |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
7 | * are met: |
8 | * |
8 | * |
9 | * - Redistributions of source code must retain the above copyright |
9 | * - Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * - Redistributions in binary form must reproduce the above copyright |
11 | * - Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
13 | * documentation and/or other materials provided with the distribution. |
14 | * - The name of the author may not be used to endorse or promote products |
14 | * - The name of the author may not be used to endorse or promote products |
15 | * derived from this software without specific prior written permission. |
15 | * derived from this software without specific prior written permission. |
16 | * |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
27 | */ |
28 | 28 | ||
29 | /** @addtogroup fs |
29 | /** @addtogroup fs |
30 | * @{ |
30 | * @{ |
31 | */ |
31 | */ |
32 | 32 | ||
33 | #ifndef FAT_FAT_H_ |
33 | #ifndef FAT_FAT_H_ |
34 | #define FAT_FAT_H_ |
34 | #define FAT_FAT_H_ |
35 | 35 | ||
36 | #include <ipc/ipc.h> |
36 | #include <ipc/ipc.h> |
37 | #include <libfs.h> |
37 | #include <libfs.h> |
38 | #include <atomic.h> |
38 | #include <atomic.h> |
39 | #include <sys/types.h> |
39 | #include <sys/types.h> |
40 | #include <bool.h> |
40 | #include <bool.h> |
41 | #include "../../vfs/vfs.h" |
41 | #include "../../vfs/vfs.h" |
42 | 42 | ||
43 | #ifndef dprintf |
43 | #ifndef dprintf |
44 | #define dprintf(...) printf(__VA_ARGS__) |
44 | #define dprintf(...) printf(__VA_ARGS__) |
45 | #endif |
45 | #endif |
46 | 46 | ||
47 | typedef struct { |
47 | typedef struct { |
48 | uint8_t ji[3]; /**< Jump instruction. */ |
48 | uint8_t ji[3]; /**< Jump instruction. */ |
49 | uint8_t oem_name[8]; |
49 | uint8_t oem_name[8]; |
50 | /* BIOS Parameter Block */ |
50 | /* BIOS Parameter Block */ |
51 | uint16_t bps; /**< Bytes per sector. */ |
51 | uint16_t bps; /**< Bytes per sector. */ |
52 | uint8_t spc; /**< Sectors per cluster. */ |
52 | uint8_t spc; /**< Sectors per cluster. */ |
53 | uint16_t rscnt; /**< Reserved sector count. */ |
53 | uint16_t rscnt; /**< Reserved sector count. */ |
54 | uint8_t fatcnt; /**< Number of FATs. */ |
54 | uint8_t fatcnt; /**< Number of FATs. */ |
55 | uint16_t root_ent_max; /**< Maximum number of root directory |
55 | uint16_t root_ent_max; /**< Maximum number of root directory |
56 | entries. */ |
56 | entries. */ |
57 | uint16_t totsec16; /**< Total sectors. 16-bit version. */ |
57 | uint16_t totsec16; /**< Total sectors. 16-bit version. */ |
58 | uint8_t mdesc; /**< Media descriptor. */ |
58 | uint8_t mdesc; /**< Media descriptor. */ |
59 | uint16_t sec_per_fat; /**< Sectors per FAT12/FAT16. */ |
59 | uint16_t sec_per_fat; /**< Sectors per FAT12/FAT16. */ |
60 | uint16_t sec_per_track; /**< Sectors per track. */ |
60 | uint16_t sec_per_track; /**< Sectors per track. */ |
61 | uint16_t headcnt; /**< Number of heads. */ |
61 | uint16_t headcnt; /**< Number of heads. */ |
62 | uint32_t hidden_sec; /**< Hidden sectors. */ |
62 | uint32_t hidden_sec; /**< Hidden sectors. */ |
63 | uint32_t totsec32; /**< Total sectors. 32-bit version. */ |
63 | uint32_t totsec32; /**< Total sectors. 32-bit version. */ |
64 | 64 | ||
65 | union { |
65 | union { |
66 | struct { |
66 | struct { |
67 | /* FAT12/FAT16 only: Extended BIOS Parameter Block */ |
67 | /* FAT12/FAT16 only: Extended BIOS Parameter Block */ |
68 | /** Physical drive number. */ |
68 | /** Physical drive number. */ |
69 | uint8_t pdn; |
69 | uint8_t pdn; |
70 | uint8_t reserved; |
70 | uint8_t reserved; |
71 | /** Extended boot signature. */ |
71 | /** Extended boot signature. */ |
72 | uint8_t ebs; |
72 | uint8_t ebs; |
73 | /** Serial number. */ |
73 | /** Serial number. */ |
74 | uint32_t id; |
74 | uint32_t id; |
75 | /** Volume label. */ |
75 | /** Volume label. */ |
76 | uint8_t label[11]; |
76 | uint8_t label[11]; |
77 | /** FAT type. */ |
77 | /** FAT type. */ |
78 | uint8_t type[8]; |
78 | uint8_t type[8]; |
79 | /** Boot code. */ |
79 | /** Boot code. */ |
80 | uint8_t boot_code[448]; |
80 | uint8_t boot_code[448]; |
81 | /** Boot sector signature. */ |
81 | /** Boot sector signature. */ |
82 | uint16_t signature; |
82 | uint16_t signature; |
83 | } __attribute__ ((packed)); |
83 | } __attribute__ ((packed)); |
84 | struct { |
84 | struct { |
85 | /* FAT32 only */ |
85 | /* FAT32 only */ |
86 | /** Sectors per FAT. */ |
86 | /** Sectors per FAT. */ |
87 | uint32_t sectors_per_fat; |
87 | uint32_t sectors_per_fat; |
88 | /** FAT flags. */ |
88 | /** FAT flags. */ |
89 | uint16_t flags; |
89 | uint16_t flags; |
90 | /** Version. */ |
90 | /** Version. */ |
91 | uint16_t version; |
91 | uint16_t version; |
92 | /** Cluster number of root directory. */ |
92 | /** Cluster number of root directory. */ |
93 | uint32_t root_cluster; |
93 | uint32_t root_cluster; |
94 | /** Sector number of file system information sector. */ |
94 | /** Sector number of file system information sector. */ |
95 | uint16_t fsinfo_sec; |
95 | uint16_t fsinfo_sec; |
96 | /** Sector number of boot sector copy. */ |
96 | /** Sector number of boot sector copy. */ |
97 | uint16_t bscopy_sec; |
97 | uint16_t bscopy_sec; |
98 | uint8_t reserved1[12]; |
98 | uint8_t reserved1[12]; |
99 | /** Physical drive number. */ |
99 | /** Physical drive number. */ |
100 | uint8_t pdn; |
100 | uint8_t pdn; |
101 | uint8_t reserved2; |
101 | uint8_t reserved2; |
102 | /** Extended boot signature. */ |
102 | /** Extended boot signature. */ |
103 | uint8_t ebs; |
103 | uint8_t ebs; |
104 | /** Serial number. */ |
104 | /** Serial number. */ |
105 | uint32_t id; |
105 | uint32_t id; |
106 | /** Volume label. */ |
106 | /** Volume label. */ |
107 | uint8_t label[11]; |
107 | uint8_t label[11]; |
108 | /** FAT type. */ |
108 | /** FAT type. */ |
109 | uint8_t type[8]; |
109 | uint8_t type[8]; |
110 | /** Boot code. */ |
110 | /** Boot code. */ |
111 | uint8_t boot_code[420]; |
111 | uint8_t boot_code[420]; |
112 | /** Signature. */ |
112 | /** Signature. */ |
113 | uint16_t signature; |
113 | uint16_t signature; |
114 | } __attribute__ ((packed)); |
114 | } __attribute__ ((packed)); |
115 | }; |
115 | }; |
116 | } __attribute__ ((packed)) fat_bs_t; |
116 | } __attribute__ ((packed)) fat_bs_t; |
117 | 117 | ||
118 | #define FAT_ATTR_RDONLY (1 << 0) |
118 | #define FAT_ATTR_RDONLY (1 << 0) |
119 | #define FAT_ATTR_VOLLABEL (1 << 3) |
119 | #define FAT_ATTR_VOLLABEL (1 << 3) |
120 | #define FAT_ATTR_SUBDIR (1 << 4) |
120 | #define FAT_ATTR_SUBDIR (1 << 4) |
121 | 121 | ||
122 | typedef struct { |
122 | typedef struct { |
123 | uint8_t name[8]; |
123 | uint8_t name[8]; |
124 | uint8_t ext[3]; |
124 | uint8_t ext[3]; |
125 | uint8_t attr; |
125 | uint8_t attr; |
126 | uint8_t reserved; |
126 | uint8_t reserved; |
127 | uint8_t ctime_fine; |
127 | uint8_t ctime_fine; |
128 | uint16_t ctime; |
128 | uint16_t ctime; |
129 | uint16_t cdate; |
129 | uint16_t cdate; |
130 | uint16_t adate; |
130 | uint16_t adate; |
131 | union { |
131 | union { |
132 | uint16_t eaidx; /* FAT12/FAT16 */ |
132 | uint16_t eaidx; /* FAT12/FAT16 */ |
133 | uint16_t firstc_hi; /* FAT32 */ |
133 | uint16_t firstc_hi; /* FAT32 */ |
134 | }; |
134 | }; |
135 | uint16_t mtime; |
135 | uint16_t mtime; |
136 | uint16_t mdate; |
136 | uint16_t mdate; |
137 | union { |
137 | union { |
138 | uint16_t firstc; /* FAT12/FAT16 */ |
138 | uint16_t firstc; /* FAT12/FAT16 */ |
139 | uint16_t firstc_lo; /* FAT32 */ |
139 | uint16_t firstc_lo; /* FAT32 */ |
140 | }; |
140 | }; |
141 | uint32_t size; |
141 | uint32_t size; |
142 | } __attribute__ ((packed)) fat_dentry_t; |
142 | } __attribute__ ((packed)) fat_dentry_t; |
143 | 143 | ||
144 | typedef uint16_t fat_cluster_t; |
144 | typedef uint16_t fat_cluster_t; |
145 | 145 | ||
146 | typedef enum { |
146 | typedef enum { |
147 | FAT_INVALID, |
147 | FAT_INVALID, |
148 | FAT_DIRECTORY, |
148 | FAT_DIRECTORY, |
149 | FAT_FILE |
149 | FAT_FILE |
150 | } fat_node_type_t; |
150 | } fat_node_type_t; |
151 | 151 | ||
152 | struct fat_node; |
152 | struct fat_node; |
153 | 153 | ||
154 | /** FAT index structure. |
154 | /** FAT index structure. |
155 | * |
155 | * |
156 | * This structure exists to help us to overcome certain limitations of the FAT |
156 | * This structure exists to help us to overcome certain limitations of the FAT |
157 | * file system design. The problem with FAT is that it is hard to find |
157 | * file system design. The problem with FAT is that it is hard to find |
158 | * an entity which could represent a VFS index. There are two candidates: |
158 | * an entity which could represent a VFS index. There are two candidates: |
159 | * |
159 | * |
160 | * a) number of the node's first cluster |
160 | * a) number of the node's first cluster |
161 | * b) the pair of the parent directory's first cluster and the dentry index |
161 | * b) the pair of the parent directory's first cluster and the dentry index |
162 | * within the parent directory |
162 | * within the parent directory |
163 | * |
163 | * |
164 | * We need VFS indices to be: |
164 | * We need VFS indices to be: |
165 | * A) unique |
165 | * A) unique |
166 | * B) stable in time, at least until the next mount |
166 | * B) stable in time, at least until the next mount |
167 | * |
167 | * |
168 | * Unfortunately a) does not meet the A) criterion because zero-length files |
168 | * Unfortunately a) does not meet the A) criterion because zero-length files |
169 | * will have the first cluster field cleared. And b) does not meet the B) |
169 | * will have the first cluster field cleared. And b) does not meet the B) |
170 | * criterion because unlink() and rename() will both free up the original |
170 | * criterion because unlink() and rename() will both free up the original |
171 | * dentry, which contains all the essential info about the file. |
171 | * dentry, which contains all the essential info about the file. |
172 | * |
172 | * |
173 | * Therefore, a completely opaque indices are used and the FAT server maintains |
173 | * Therefore, a completely opaque indices are used and the FAT server maintains |
174 | * a mapping between them and otherwise nice b) variant. On rename(), the VFS |
174 | * a mapping between them and otherwise nice b) variant. On rename(), the VFS |
175 | * index stays unaltered, while the internal FAT "physical tree address" |
175 | * index stays unaltered, while the internal FAT "physical tree address" |
176 | * changes. The unlink case is also handled this way thanks to an in-core node |
176 | * changes. The unlink case is also handled this way thanks to an in-core node |
177 | * pointer embedded in the index structure. |
177 | * pointer embedded in the index structure. |
178 | */ |
178 | */ |
179 | typedef struct { |
179 | typedef struct { |
180 | /** Used indices (position) hash table link. */ |
180 | /** Used indices (position) hash table link. */ |
181 | link_t uph_link; |
181 | link_t uph_link; |
182 | /** Used indices (index) hash table link. */ |
182 | /** Used indices (index) hash table link. */ |
183 | link_t uih_link; |
183 | link_t uih_link; |
184 | 184 | ||
185 | futex_t lock; |
185 | futex_t lock; |
186 | dev_handle_t dev_handle; |
186 | dev_handle_t dev_handle; |
187 | fs_index_t index; |
187 | fs_index_t index; |
188 | /** |
188 | /** |
189 | * Parent node's first cluster. |
189 | * Parent node's first cluster. |
190 | * Zero is used if this node is not linked, in which case nodep must |
190 | * Zero is used if this node is not linked, in which case nodep must |
191 | * contain a pointer to the in-core node structure. |
191 | * contain a pointer to the in-core node structure. |
192 | * One is used when the parent is the root directory. |
192 | * One is used when the parent is the root directory. |
193 | */ |
193 | */ |
194 | fat_cluster_t pfc; |
194 | fat_cluster_t pfc; |
195 | /** Directory entry index within the parent node. */ |
195 | /** Directory entry index within the parent node. */ |
196 | unsigned pdi; |
196 | unsigned pdi; |
197 | /** Pointer to in-core node instance. */ |
197 | /** Pointer to in-core node instance. */ |
198 | struct fat_node *nodep; |
198 | struct fat_node *nodep; |
199 | } fat_idx_t; |
199 | } fat_idx_t; |
200 | 200 | ||
201 | /** FAT in-core node. */ |
201 | /** FAT in-core node. */ |
202 | typedef struct fat_node { |
202 | typedef struct fat_node { |
203 | futex_t lock; |
203 | futex_t lock; |
204 | fat_node_type_t type; |
204 | fat_node_type_t type; |
205 | fat_idx_t *idx; |
205 | fat_idx_t *idx; |
206 | /** |
206 | /** |
207 | * Node's first cluster. |
207 | * Node's first cluster. |
208 | * Zero is used for zero-length nodes. |
208 | * Zero is used for zero-length nodes. |
209 | * One is used to mark root directory. |
209 | * One is used to mark root directory. |
210 | */ |
210 | */ |
211 | fat_cluster_t firstc; |
211 | fat_cluster_t firstc; |
212 | /** FAT in-core node free list link. */ |
212 | /** FAT in-core node free list link. */ |
213 | link_t ffn_link; |
213 | link_t ffn_link; |
214 | size_t size; |
214 | size_t size; |
215 | unsigned lnkcnt; |
215 | unsigned lnkcnt; |
216 | unsigned refcnt; |
216 | unsigned refcnt; |
217 | bool dirty; |
217 | bool dirty; |
218 | } fat_node_t; |
218 | } fat_node_t; |
219 | 219 | ||
220 | extern fs_reg_t fat_reg; |
220 | extern fs_reg_t fat_reg; |
221 | 221 | ||
222 | extern void fat_mounted(ipc_callid_t, ipc_call_t *); |
222 | extern void fat_mounted(ipc_callid_t, ipc_call_t *); |
223 | extern void fat_mount(ipc_callid_t, ipc_call_t *); |
223 | extern void fat_mount(ipc_callid_t, ipc_call_t *); |
224 | extern void fat_lookup(ipc_callid_t, ipc_call_t *); |
224 | extern void fat_lookup(ipc_callid_t, ipc_call_t *); |
- | 225 | extern void fat_read(ipc_callid_t, ipc_call_t *); |
|
225 | 226 | ||
226 | extern fat_idx_t *fat_idx_get_by_pos(dev_handle_t, fat_cluster_t, unsigned); |
227 | extern fat_idx_t *fat_idx_get_by_pos(dev_handle_t, fat_cluster_t, unsigned); |
227 | extern fat_idx_t *fat_idx_get_by_index(dev_handle_t, fs_index_t); |
228 | extern fat_idx_t *fat_idx_get_by_index(dev_handle_t, fs_index_t); |
228 | 229 | ||
229 | extern int fat_idx_init(void); |
230 | extern int fat_idx_init(void); |
230 | extern void fat_idx_fini(void); |
231 | extern void fat_idx_fini(void); |
231 | extern int fat_idx_init_by_dev_handle(dev_handle_t); |
232 | extern int fat_idx_init_by_dev_handle(dev_handle_t); |
232 | extern void fat_idx_fini_by_dev_handle(dev_handle_t); |
233 | extern void fat_idx_fini_by_dev_handle(dev_handle_t); |
233 | 234 | ||
234 | #endif |
235 | #endif |
235 | 236 | ||
236 | /** |
237 | /** |
237 | * @} |
238 | * @} |
238 | */ |
239 | */ |
239 | 240 |