Rev 3609 | Rev 3621 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 3609 | Rev 3615 | ||
---|---|---|---|
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 "fat_fat.h" |
36 | #include "fat_fat.h" |
37 | #include <ipc/ipc.h> |
37 | #include <ipc/ipc.h> |
38 | #include <libfs.h> |
38 | #include <libfs.h> |
39 | #include <atomic.h> |
39 | #include <atomic.h> |
40 | #include <sys/types.h> |
40 | #include <sys/types.h> |
41 | #include <bool.h> |
41 | #include <bool.h> |
42 | #include "../../vfs/vfs.h" |
42 | #include "../../vfs/vfs.h" |
43 | 43 | ||
44 | #ifndef dprintf |
44 | #ifndef dprintf |
45 | #define dprintf(...) printf(__VA_ARGS__) |
45 | #define dprintf(...) printf(__VA_ARGS__) |
46 | #endif |
46 | #endif |
47 | 47 | ||
48 | #define min(a, b) ((a) < (b) ? (a) : (b)) |
48 | #define min(a, b) ((a) < (b) ? (a) : (b)) |
49 | 49 | ||
50 | #define BS_BLOCK 0 |
50 | #define BS_BLOCK 0 |
51 | #define BS_SIZE 512 |
51 | #define BS_SIZE 512 |
52 | 52 | ||
53 | typedef struct fat_bs { |
53 | typedef struct fat_bs { |
54 | uint8_t ji[3]; /**< Jump instruction. */ |
54 | uint8_t ji[3]; /**< Jump instruction. */ |
55 | uint8_t oem_name[8]; |
55 | uint8_t oem_name[8]; |
56 | /* BIOS Parameter Block */ |
56 | /* BIOS Parameter Block */ |
57 | uint16_t bps; /**< Bytes per sector. */ |
57 | uint16_t bps; /**< Bytes per sector. */ |
58 | uint8_t spc; /**< Sectors per cluster. */ |
58 | uint8_t spc; /**< Sectors per cluster. */ |
59 | uint16_t rscnt; /**< Reserved sector count. */ |
59 | uint16_t rscnt; /**< Reserved sector count. */ |
60 | uint8_t fatcnt; /**< Number of FATs. */ |
60 | uint8_t fatcnt; /**< Number of FATs. */ |
61 | uint16_t root_ent_max; /**< Maximum number of root directory |
61 | uint16_t root_ent_max; /**< Maximum number of root directory |
62 | entries. */ |
62 | entries. */ |
63 | uint16_t totsec16; /**< Total sectors. 16-bit version. */ |
63 | uint16_t totsec16; /**< Total sectors. 16-bit version. */ |
64 | uint8_t mdesc; /**< Media descriptor. */ |
64 | uint8_t mdesc; /**< Media descriptor. */ |
65 | uint16_t sec_per_fat; /**< Sectors per FAT12/FAT16. */ |
65 | uint16_t sec_per_fat; /**< Sectors per FAT12/FAT16. */ |
66 | uint16_t sec_per_track; /**< Sectors per track. */ |
66 | uint16_t sec_per_track; /**< Sectors per track. */ |
67 | uint16_t headcnt; /**< Number of heads. */ |
67 | uint16_t headcnt; /**< Number of heads. */ |
68 | uint32_t hidden_sec; /**< Hidden sectors. */ |
68 | uint32_t hidden_sec; /**< Hidden sectors. */ |
69 | uint32_t totsec32; /**< Total sectors. 32-bit version. */ |
69 | uint32_t totsec32; /**< Total sectors. 32-bit version. */ |
70 | 70 | ||
71 | union { |
71 | union { |
72 | struct { |
72 | struct { |
73 | /* FAT12/FAT16 only: Extended BIOS Parameter Block */ |
73 | /* FAT12/FAT16 only: Extended BIOS Parameter Block */ |
74 | /** Physical drive number. */ |
74 | /** Physical drive number. */ |
75 | uint8_t pdn; |
75 | uint8_t pdn; |
76 | uint8_t reserved; |
76 | uint8_t reserved; |
77 | /** Extended boot signature. */ |
77 | /** Extended boot signature. */ |
78 | uint8_t ebs; |
78 | uint8_t ebs; |
79 | /** Serial number. */ |
79 | /** Serial number. */ |
80 | uint32_t id; |
80 | uint32_t id; |
81 | /** Volume label. */ |
81 | /** Volume label. */ |
82 | uint8_t label[11]; |
82 | uint8_t label[11]; |
83 | /** FAT type. */ |
83 | /** FAT type. */ |
84 | uint8_t type[8]; |
84 | uint8_t type[8]; |
85 | /** Boot code. */ |
85 | /** Boot code. */ |
86 | uint8_t boot_code[448]; |
86 | uint8_t boot_code[448]; |
87 | /** Boot sector signature. */ |
87 | /** Boot sector signature. */ |
88 | uint16_t signature; |
88 | uint16_t signature; |
89 | } __attribute__ ((packed)); |
89 | } __attribute__ ((packed)); |
90 | struct { |
90 | struct { |
91 | /* FAT32 only */ |
91 | /* FAT32 only */ |
92 | /** Sectors per FAT. */ |
92 | /** Sectors per FAT. */ |
93 | uint32_t sectors_per_fat; |
93 | uint32_t sectors_per_fat; |
94 | /** FAT flags. */ |
94 | /** FAT flags. */ |
95 | uint16_t flags; |
95 | uint16_t flags; |
96 | /** Version. */ |
96 | /** Version. */ |
97 | uint16_t version; |
97 | uint16_t version; |
98 | /** Cluster number of root directory. */ |
98 | /** Cluster number of root directory. */ |
99 | uint32_t root_cluster; |
99 | uint32_t root_cluster; |
100 | /** Sector number of file system information sector. */ |
100 | /** Sector number of file system information sector. */ |
101 | uint16_t fsinfo_sec; |
101 | uint16_t fsinfo_sec; |
102 | /** Sector number of boot sector copy. */ |
102 | /** Sector number of boot sector copy. */ |
103 | uint16_t bscopy_sec; |
103 | uint16_t bscopy_sec; |
104 | uint8_t reserved1[12]; |
104 | uint8_t reserved1[12]; |
105 | /** Physical drive number. */ |
105 | /** Physical drive number. */ |
106 | uint8_t pdn; |
106 | uint8_t pdn; |
107 | uint8_t reserved2; |
107 | uint8_t reserved2; |
108 | /** Extended boot signature. */ |
108 | /** Extended boot signature. */ |
109 | uint8_t ebs; |
109 | uint8_t ebs; |
110 | /** Serial number. */ |
110 | /** Serial number. */ |
111 | uint32_t id; |
111 | uint32_t id; |
112 | /** Volume label. */ |
112 | /** Volume label. */ |
113 | uint8_t label[11]; |
113 | uint8_t label[11]; |
114 | /** FAT type. */ |
114 | /** FAT type. */ |
115 | uint8_t type[8]; |
115 | uint8_t type[8]; |
116 | /** Boot code. */ |
116 | /** Boot code. */ |
117 | uint8_t boot_code[420]; |
117 | uint8_t boot_code[420]; |
118 | /** Signature. */ |
118 | /** Signature. */ |
119 | uint16_t signature; |
119 | uint16_t signature; |
120 | } __attribute__ ((packed)); |
120 | } __attribute__ ((packed)); |
121 | }; |
121 | }; |
122 | } __attribute__ ((packed)) fat_bs_t; |
122 | } __attribute__ ((packed)) fat_bs_t; |
123 | 123 | ||
124 | typedef enum { |
124 | typedef enum { |
125 | FAT_INVALID, |
125 | FAT_INVALID, |
126 | FAT_DIRECTORY, |
126 | FAT_DIRECTORY, |
127 | FAT_FILE |
127 | FAT_FILE |
128 | } fat_node_type_t; |
128 | } fat_node_type_t; |
129 | 129 | ||
130 | struct fat_node; |
130 | struct fat_node; |
131 | 131 | ||
132 | /** FAT index structure. |
132 | /** FAT index structure. |
133 | * |
133 | * |
134 | * This structure exists to help us to overcome certain limitations of the FAT |
134 | * This structure exists to help us to overcome certain limitations of the FAT |
135 | * file system design. The problem with FAT is that it is hard to find |
135 | * file system design. The problem with FAT is that it is hard to find |
136 | * an entity which could represent a VFS index. There are two candidates: |
136 | * an entity which could represent a VFS index. There are two candidates: |
137 | * |
137 | * |
138 | * a) number of the node's first cluster |
138 | * a) number of the node's first cluster |
139 | * b) the pair of the parent directory's first cluster and the dentry index |
139 | * b) the pair of the parent directory's first cluster and the dentry index |
140 | * within the parent directory |
140 | * within the parent directory |
141 | * |
141 | * |
142 | * We need VFS indices to be: |
142 | * We need VFS indices to be: |
143 | * A) unique |
143 | * A) unique |
144 | * B) stable in time, at least until the next mount |
144 | * B) stable in time, at least until the next mount |
145 | * |
145 | * |
146 | * Unfortunately a) does not meet the A) criterion because zero-length files |
146 | * Unfortunately a) does not meet the A) criterion because zero-length files |
147 | * will have the first cluster field cleared. And b) does not meet the B) |
147 | * will have the first cluster field cleared. And b) does not meet the B) |
148 | * criterion because unlink() and rename() will both free up the original |
148 | * criterion because unlink() and rename() will both free up the original |
149 | * dentry, which contains all the essential info about the file. |
149 | * dentry, which contains all the essential info about the file. |
150 | * |
150 | * |
151 | * Therefore, a completely opaque indices are used and the FAT server maintains |
151 | * Therefore, a completely opaque indices are used and the FAT server maintains |
152 | * a mapping between them and otherwise nice b) variant. On rename(), the VFS |
152 | * a mapping between them and otherwise nice b) variant. On rename(), the VFS |
153 | * index stays unaltered, while the internal FAT "physical tree address" |
153 | * index stays unaltered, while the internal FAT "physical tree address" |
154 | * changes. The unlink case is also handled this way thanks to an in-core node |
154 | * changes. The unlink case is also handled this way thanks to an in-core node |
155 | * pointer embedded in the index structure. |
155 | * pointer embedded in the index structure. |
156 | */ |
156 | */ |
157 | typedef struct { |
157 | typedef struct { |
158 | /** Used indices (position) hash table link. */ |
158 | /** Used indices (position) hash table link. */ |
159 | link_t uph_link; |
159 | link_t uph_link; |
160 | /** Used indices (index) hash table link. */ |
160 | /** Used indices (index) hash table link. */ |
161 | link_t uih_link; |
161 | link_t uih_link; |
162 | 162 | ||
163 | futex_t lock; |
163 | futex_t lock; |
164 | dev_handle_t dev_handle; |
164 | dev_handle_t dev_handle; |
165 | fs_index_t index; |
165 | fs_index_t index; |
166 | /** |
166 | /** |
167 | * Parent node's first cluster. |
167 | * Parent node's first cluster. |
168 | * Zero is used if this node is not linked, in which case nodep must |
168 | * Zero is used if this node is not linked, in which case nodep must |
169 | * contain a pointer to the in-core node structure. |
169 | * contain a pointer to the in-core node structure. |
170 | * One is used when the parent is the root directory. |
170 | * One is used when the parent is the root directory. |
171 | */ |
171 | */ |
172 | fat_cluster_t pfc; |
172 | fat_cluster_t pfc; |
173 | /** Directory entry index within the parent node. */ |
173 | /** Directory entry index within the parent node. */ |
174 | unsigned pdi; |
174 | unsigned pdi; |
175 | /** Pointer to in-core node instance. */ |
175 | /** Pointer to in-core node instance. */ |
176 | struct fat_node *nodep; |
176 | struct fat_node *nodep; |
177 | } fat_idx_t; |
177 | } fat_idx_t; |
178 | 178 | ||
179 | /** FAT in-core node. */ |
179 | /** FAT in-core node. */ |
180 | typedef struct fat_node { |
180 | typedef struct fat_node { |
181 | futex_t lock; |
181 | futex_t lock; |
182 | fat_node_type_t type; |
182 | fat_node_type_t type; |
183 | fat_idx_t *idx; |
183 | fat_idx_t *idx; |
184 | /** |
184 | /** |
185 | * Node's first cluster. |
185 | * Node's first cluster. |
186 | * Zero is used for zero-length nodes. |
186 | * Zero is used for zero-length nodes. |
187 | * One is used to mark root directory. |
187 | * One is used to mark root directory. |
188 | */ |
188 | */ |
189 | fat_cluster_t firstc; |
189 | fat_cluster_t firstc; |
190 | /** FAT in-core node free list link. */ |
190 | /** FAT in-core node free list link. */ |
191 | link_t ffn_link; |
191 | link_t ffn_link; |
192 | size_t size; |
192 | size_t size; |
193 | unsigned lnkcnt; |
193 | unsigned lnkcnt; |
194 | unsigned refcnt; |
194 | unsigned refcnt; |
195 | bool dirty; |
195 | bool dirty; |
196 | } fat_node_t; |
196 | } fat_node_t; |
197 | 197 | ||
198 | extern fs_reg_t fat_reg; |
198 | extern fs_reg_t fat_reg; |
199 | 199 | ||
200 | extern void fat_mounted(ipc_callid_t, ipc_call_t *); |
200 | extern void fat_mounted(ipc_callid_t, ipc_call_t *); |
201 | extern void fat_mount(ipc_callid_t, ipc_call_t *); |
201 | extern void fat_mount(ipc_callid_t, ipc_call_t *); |
202 | extern void fat_lookup(ipc_callid_t, ipc_call_t *); |
202 | extern void fat_lookup(ipc_callid_t, ipc_call_t *); |
203 | extern void fat_read(ipc_callid_t, ipc_call_t *); |
203 | extern void fat_read(ipc_callid_t, ipc_call_t *); |
204 | extern void fat_write(ipc_callid_t, ipc_call_t *); |
204 | extern void fat_write(ipc_callid_t, ipc_call_t *); |
205 | extern void fat_truncate(ipc_callid_t, ipc_call_t *); |
205 | extern void fat_truncate(ipc_callid_t, ipc_call_t *); |
206 | 206 | ||
207 | extern fat_idx_t *fat_idx_get_new(dev_handle_t); |
207 | extern fat_idx_t *fat_idx_get_new(dev_handle_t); |
208 | extern fat_idx_t *fat_idx_get_by_pos(dev_handle_t, fat_cluster_t, unsigned); |
208 | extern fat_idx_t *fat_idx_get_by_pos(dev_handle_t, fat_cluster_t, unsigned); |
209 | extern fat_idx_t *fat_idx_get_by_index(dev_handle_t, fs_index_t); |
209 | extern fat_idx_t *fat_idx_get_by_index(dev_handle_t, fs_index_t); |
- | 210 | extern void fat_idx_destroy(fat_idx_t *); |
|
210 | 211 | ||
211 | extern int fat_idx_init(void); |
212 | extern int fat_idx_init(void); |
212 | extern void fat_idx_fini(void); |
213 | extern void fat_idx_fini(void); |
213 | extern int fat_idx_init_by_dev_handle(dev_handle_t); |
214 | extern int fat_idx_init_by_dev_handle(dev_handle_t); |
214 | extern void fat_idx_fini_by_dev_handle(dev_handle_t); |
215 | extern void fat_idx_fini_by_dev_handle(dev_handle_t); |
215 | 216 | ||
216 | #endif |
217 | #endif |
217 | 218 | ||
218 | /** |
219 | /** |
219 | * @} |
220 | * @} |
220 | */ |
221 | */ |
221 | 222 |