Rev 2858 | Rev 2863 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 2858 | Rev 2859 | ||
---|---|---|---|
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 | /** |
33 | /** |
34 | * @file fat_ops.c |
34 | * @file fat_ops.c |
35 | * @brief Implementation of VFS operations for the FAT file system server. |
35 | * @brief Implementation of VFS operations for the FAT file system server. |
36 | */ |
36 | */ |
37 | 37 | ||
38 | #include "fat.h" |
38 | #include "fat.h" |
39 | #include "../../vfs/vfs.h" |
39 | #include "../../vfs/vfs.h" |
40 | #include <libfs.h> |
40 | #include <libfs.h> |
41 | #include <ipc/ipc.h> |
41 | #include <ipc/ipc.h> |
42 | #include <async.h> |
42 | #include <async.h> |
43 | #include <errno.h> |
43 | #include <errno.h> |
44 | #include <string.h> |
44 | #include <string.h> |
45 | #include <byteorder.h> |
45 | #include <byteorder.h> |
46 | #include <libadt/hash_table.h> |
46 | #include <libadt/hash_table.h> |
47 | #include <libadt/list.h> |
47 | #include <libadt/list.h> |
48 | #include <assert.h> |
48 | #include <assert.h> |
49 | #include <futex.h> |
49 | #include <futex.h> |
50 | 50 | ||
51 | #define BS_BLOCK 0 |
51 | #define BS_BLOCK 0 |
52 | 52 | ||
53 | #define FIN_KEY_DEV_HANDLE 0 |
53 | #define FIN_KEY_DEV_HANDLE 0 |
54 | #define FIN_KEY_INDEX 1 |
54 | #define FIN_KEY_INDEX 1 |
55 | 55 | ||
56 | /** Futex protecting both fin_hash and ffn_head. */ |
56 | /** Futex protecting both fin_hash and ffn_head. */ |
57 | futex_t fin_futex = FUTEX_INITIALIZER; |
57 | futex_t fin_futex = FUTEX_INITIALIZER; |
58 | 58 | ||
59 | /** Hash table of FAT in-core nodes. */ |
59 | /** Hash table of FAT in-core nodes. */ |
60 | hash_table_t fin_hash; |
60 | hash_table_t fin_hash; |
61 | 61 | ||
62 | /** List of free FAT in-core nodes. */ |
62 | /** List of free FAT in-core nodes. */ |
63 | link_t ffn_head; |
63 | link_t ffn_head; |
64 | 64 | ||
65 | #define FAT_NAME_LEN 8 |
65 | #define FAT_NAME_LEN 8 |
66 | #define FAT_EXT_LEN 3 |
66 | #define FAT_EXT_LEN 3 |
67 | 67 | ||
68 | #define FAT_PAD ' ' |
68 | #define FAT_PAD ' ' |
69 | 69 | ||
70 | #define FAT_DENTRY_UNUSED 0x00 |
70 | #define FAT_DENTRY_UNUSED 0x00 |
71 | #define FAT_DENTRY_E5_ESC 0x05 |
71 | #define FAT_DENTRY_E5_ESC 0x05 |
72 | #define FAT_DENTRY_DOT 0x2e |
72 | #define FAT_DENTRY_DOT 0x2e |
73 | #define FAT_DENTRY_ERASED 0xe5 |
73 | #define FAT_DENTRY_ERASED 0xe5 |
74 | 74 | ||
75 | static void dentry_name_canonify(fat_dentry_t *d, char *buf) |
75 | static void dentry_name_canonify(fat_dentry_t *d, char *buf) |
76 | { |
76 | { |
77 | int i; |
77 | int i; |
78 | 78 | ||
79 | for (i = 0; i < FAT_NAME_LEN; i++) { |
79 | for (i = 0; i < FAT_NAME_LEN; i++) { |
80 | if (d->name[i] == FAT_PAD) { |
80 | if (d->name[i] == FAT_PAD) { |
81 | buf++; |
81 | buf++; |
82 | break; |
82 | break; |
83 | } |
83 | } |
84 | if (d->name[i] == FAT_DENTRY_E5_ESC) |
84 | if (d->name[i] == FAT_DENTRY_E5_ESC) |
85 | *buf++ = 0xe5; |
85 | *buf++ = 0xe5; |
86 | else |
86 | else |
87 | *buf++ = d->name[i]; |
87 | *buf++ = d->name[i]; |
88 | } |
88 | } |
89 | if (d->ext[0] != FAT_PAD) |
89 | if (d->ext[0] != FAT_PAD) |
90 | *buf++ = '.'; |
90 | *buf++ = '.'; |
91 | for (i = 0; i < FAT_EXT_LEN; i++) { |
91 | for (i = 0; i < FAT_EXT_LEN; i++) { |
92 | if (d->ext[i] == FAT_PAD) { |
92 | if (d->ext[i] == FAT_PAD) { |
93 | *buf = '\0'; |
93 | *buf = '\0'; |
94 | return; |
94 | return; |
95 | } |
95 | } |
96 | if (d->ext[i] == FAT_DENTRY_E5_ESC) |
96 | if (d->ext[i] == FAT_DENTRY_E5_ESC) |
97 | *buf++ = 0xe5; |
97 | *buf++ = 0xe5; |
98 | else |
98 | else |
99 | *buf++ = d->ext[i]; |
99 | *buf++ = d->ext[i]; |
100 | } |
100 | } |
101 | } |
101 | } |
102 | 102 | ||
103 | /* TODO and also move somewhere else */ |
103 | /* TODO and also move somewhere else */ |
104 | typedef struct { |
104 | typedef struct { |
105 | void *data; |
105 | void *data; |
106 | } block_t; |
106 | } block_t; |
107 | 107 | ||
108 | static block_t *block_get(dev_handle_t dev_handle, off_t offset) |
108 | static block_t *block_get(dev_handle_t dev_handle, off_t offset) |
109 | { |
109 | { |
110 | return NULL; /* TODO */ |
110 | return NULL; /* TODO */ |
111 | } |
111 | } |
112 | 112 | ||
113 | static block_t *fat_block_get(dev_handle_t dev_handle, fs_index_t index, |
- | |
114 | off_t offset) { |
- | |
115 | return NULL; /* TODO */ |
- | |
116 | } |
- | |
117 | - | ||
118 | static void block_put(block_t *block) |
113 | static void block_put(block_t *block) |
119 | { |
114 | { |
120 | /* TODO */ |
115 | /* TODO */ |
121 | } |
116 | } |
122 | 117 | ||
- | 118 | ||
- | 119 | #define FAT_BS(b) ((fat_bs_t *)((b)->data)) |
|
- | 120 | ||
- | 121 | #define FAT_CLST_FIRST 0x0002 |
|
- | 122 | #define FAT_CLST_BAD 0xfff7 |
|
- | 123 | #define FAT_CLST_LAST1 0xfff8 |
|
- | 124 | #define FAT_CLST_LAST8 0xffff |
|
- | 125 | ||
- | 126 | /** Convert cluster number to an index within a FAT. |
|
- | 127 | * |
|
- | 128 | * Format Identifier and cluster numbering is considered. |
|
- | 129 | */ |
|
- | 130 | #define C2FAT_IDX(c) (1 + (c) - FAT_CLST_FIRST) |
|
- | 131 | ||
- | 132 | static block_t *fat_block_get(dev_handle_t dev_handle, fs_index_t index, |
|
- | 133 | off_t offset) |
|
- | 134 | { |
|
- | 135 | block_t *bb; |
|
- | 136 | block_t *b; |
|
- | 137 | unsigned bps; |
|
- | 138 | unsigned spc; |
|
- | 139 | unsigned rscnt; /* block address of the first FAT */ |
|
- | 140 | unsigned fatcnt; |
|
- | 141 | unsigned rde; |
|
- | 142 | unsigned rds; /* root directory size */ |
|
- | 143 | unsigned sf; |
|
- | 144 | unsigned ssa; /* size of the system area */ |
|
- | 145 | unsigned clusters; |
|
- | 146 | unsigned clst = index; |
|
- | 147 | unsigned i; |
|
- | 148 | ||
- | 149 | bb = block_get(dev_handle, BS_BLOCK); |
|
- | 150 | bps = uint16_t_le2host(FAT_BS(bb)->bps); |
|
- | 151 | spc = FAT_BS(bb)->spc; |
|
- | 152 | rscnt = uint16_t_le2host(FAT_BS(bb)->rscnt); |
|
- | 153 | fatcnt = FAT_BS(bb)->fatcnt; |
|
- | 154 | rde = uint16_t_le2host(FAT_BS(bb)->root_ent_max); |
|
- | 155 | sf = uint16_t_le2host(FAT_BS(bb)->sec_per_fat); |
|
- | 156 | block_put(bb); |
|
- | 157 | ||
- | 158 | rds = (sizeof(fat_dentry_t) * rde) / bps; |
|
- | 159 | rds += ((sizeof(fat_dentry_t) * rde) % bps != 0); |
|
- | 160 | ssa = rscnt + fatcnt * sf + rds; |
|
- | 161 | ||
- | 162 | if (!index) { |
|
- | 163 | /* root directory special case */ |
|
- | 164 | assert(offset < rds); |
|
- | 165 | b = block_get(dev_handle, rscnt + fatcnt * sf + offset); |
|
- | 166 | return b; |
|
- | 167 | } |
|
- | 168 | ||
- | 169 | clusters = offset / spc; |
|
- | 170 | for (i = 0; i < clusters; i++) { |
|
- | 171 | unsigned fsec; /* sector offset relative to FAT1 */ |
|
- | 172 | unsigned fidx; /* FAT1 entry index */ |
|
- | 173 | ||
- | 174 | assert(clst >= FAT_CLST_FIRST && clst < FAT_CLST_BAD); |
|
- | 175 | fsec = (C2FAT_IDX(clst) * sizeof(uint16_t)) / bps; |
|
- | 176 | fidx = C2FAT_IDX(clst) % (bps / sizeof(uint16_t)); |
|
- | 177 | /* read FAT1 */ |
|
- | 178 | b = block_get(dev_handle, rscnt + fsec); |
|
- | 179 | clst = uint16_t_le2host(((uint16_t *)b->data)[fidx]); |
|
- | 180 | assert(clst != FAT_CLST_BAD); |
|
- | 181 | assert(clst < FAT_CLST_LAST1); |
|
- | 182 | block_put(b); |
|
- | 183 | } |
|
- | 184 | ||
- | 185 | b = block_get(dev_handle, ssa + (clst - FAT_CLST_FIRST) * spc + |
|
- | 186 | offset % spc); |
|
- | 187 | ||
- | 188 | return b; |
|
- | 189 | } |
|
- | 190 | ||
123 | static void fat_node_initialize(fat_node_t *node) |
191 | static void fat_node_initialize(fat_node_t *node) |
124 | { |
192 | { |
125 | futex_initialize(&node->lock, 1); |
193 | futex_initialize(&node->lock, 1); |
126 | node->type = 0; |
194 | node->type = 0; |
127 | node->index = 0; |
195 | node->index = 0; |
128 | node->pindex = 0; |
196 | node->pindex = 0; |
129 | node->dev_handle = 0; |
197 | node->dev_handle = 0; |
130 | link_initialize(&node->fin_link); |
198 | link_initialize(&node->fin_link); |
131 | link_initialize(&node->ffn_link); |
199 | link_initialize(&node->ffn_link); |
132 | node->size = 0; |
200 | node->size = 0; |
133 | node->lnkcnt = 0; |
201 | node->lnkcnt = 0; |
134 | node->refcnt = 0; |
202 | node->refcnt = 0; |
135 | node->dirty = false; |
203 | node->dirty = false; |
136 | } |
204 | } |
137 | 205 | ||
138 | static uint16_t fat_bps_get(dev_handle_t dev_handle) |
206 | static uint16_t fat_bps_get(dev_handle_t dev_handle) |
139 | { |
207 | { |
140 | block_t *bb; |
208 | block_t *bb; |
141 | uint16_t bps; |
209 | uint16_t bps; |
142 | 210 | ||
143 | bb = block_get(dev_handle, BS_BLOCK); |
211 | bb = block_get(dev_handle, BS_BLOCK); |
144 | assert(bb != NULL); |
212 | assert(bb != NULL); |
145 | bps = uint16_t_le2host(((fat_bs_t *)bb->data)->bps); |
213 | bps = uint16_t_le2host(FAT_BS(bb)->bps); |
146 | block_put(bb); |
214 | block_put(bb); |
147 | 215 | ||
148 | return bps; |
216 | return bps; |
149 | } |
217 | } |
150 | 218 | ||
151 | typedef enum { |
219 | typedef enum { |
152 | FAT_DENTRY_SKIP, |
220 | FAT_DENTRY_SKIP, |
153 | FAT_DENTRY_LAST, |
221 | FAT_DENTRY_LAST, |
154 | FAT_DENTRY_VALID |
222 | FAT_DENTRY_VALID |
155 | } fat_dentry_clsf_t; |
223 | } fat_dentry_clsf_t; |
156 | 224 | ||
157 | static fat_dentry_clsf_t fat_classify_dentry(fat_dentry_t *d) |
225 | static fat_dentry_clsf_t fat_classify_dentry(fat_dentry_t *d) |
158 | { |
226 | { |
159 | if (d->attr & FAT_ATTR_VOLLABEL) { |
227 | if (d->attr & FAT_ATTR_VOLLABEL) { |
160 | /* volume label entry */ |
228 | /* volume label entry */ |
161 | return FAT_DENTRY_SKIP; |
229 | return FAT_DENTRY_SKIP; |
162 | } |
230 | } |
163 | if (d->name[0] == FAT_DENTRY_ERASED) { |
231 | if (d->name[0] == FAT_DENTRY_ERASED) { |
164 | /* not-currently-used entry */ |
232 | /* not-currently-used entry */ |
165 | return FAT_DENTRY_SKIP; |
233 | return FAT_DENTRY_SKIP; |
166 | } |
234 | } |
167 | if (d->name[0] == FAT_DENTRY_UNUSED) { |
235 | if (d->name[0] == FAT_DENTRY_UNUSED) { |
168 | /* never used entry */ |
236 | /* never used entry */ |
169 | return FAT_DENTRY_LAST; |
237 | return FAT_DENTRY_LAST; |
170 | } |
238 | } |
171 | if (d->name[0] == FAT_DENTRY_DOT) { |
239 | if (d->name[0] == FAT_DENTRY_DOT) { |
172 | /* |
240 | /* |
173 | * Most likely '.' or '..'. |
241 | * Most likely '.' or '..'. |
174 | * It cannot occur in a regular file name. |
242 | * It cannot occur in a regular file name. |
175 | */ |
243 | */ |
176 | return FAT_DENTRY_SKIP; |
244 | return FAT_DENTRY_SKIP; |
177 | } |
245 | } |
178 | return FAT_DENTRY_VALID; |
246 | return FAT_DENTRY_VALID; |
179 | } |
247 | } |
180 | 248 | ||
181 | static void fat_sync_node(fat_node_t *node) |
249 | static void fat_sync_node(fat_node_t *node) |
182 | { |
250 | { |
183 | /* TODO */ |
251 | /* TODO */ |
184 | } |
252 | } |
185 | 253 | ||
186 | /** Instantiate a FAT in-core node. |
254 | /** Instantiate a FAT in-core node. |
187 | * |
255 | * |
188 | * FAT stores the info necessary for instantiation of a node in the parent of |
256 | * FAT stores the info necessary for instantiation of a node in the parent of |
189 | * that node. This design necessitated the addition of the parent node index |
257 | * that node. This design necessitated the addition of the parent node index |
190 | * parameter to this otherwise generic libfs API. |
258 | * parameter to this otherwise generic libfs API. |
191 | */ |
259 | */ |
192 | static void * |
260 | static void * |
193 | fat_node_get(dev_handle_t dev_handle, fs_index_t index, fs_index_t pindex) |
261 | fat_node_get(dev_handle_t dev_handle, fs_index_t index, fs_index_t pindex) |
194 | { |
262 | { |
195 | link_t *lnk; |
263 | link_t *lnk; |
196 | fat_node_t *node = NULL; |
264 | fat_node_t *node = NULL; |
197 | block_t *b; |
265 | block_t *b; |
198 | unsigned bps; |
266 | unsigned bps; |
199 | unsigned dps; |
267 | unsigned dps; |
200 | fat_dentry_t *d; |
268 | fat_dentry_t *d; |
201 | unsigned i, j; |
269 | unsigned i, j; |
202 | 270 | ||
203 | unsigned long key[] = { |
271 | unsigned long key[] = { |
204 | [FIN_KEY_DEV_HANDLE] = dev_handle, |
272 | [FIN_KEY_DEV_HANDLE] = dev_handle, |
205 | [FIN_KEY_INDEX] = index |
273 | [FIN_KEY_INDEX] = index |
206 | }; |
274 | }; |
207 | 275 | ||
208 | futex_down(&fin_futex); |
276 | futex_down(&fin_futex); |
209 | lnk = hash_table_find(&fin_hash, key); |
277 | lnk = hash_table_find(&fin_hash, key); |
210 | if (lnk) { |
278 | if (lnk) { |
211 | /* |
279 | /* |
212 | * The in-core node was found in the hash table. |
280 | * The in-core node was found in the hash table. |
213 | */ |
281 | */ |
214 | node = hash_table_get_instance(lnk, fat_node_t, fin_link); |
282 | node = hash_table_get_instance(lnk, fat_node_t, fin_link); |
215 | if (!node->refcnt++) |
283 | if (!node->refcnt++) |
216 | list_remove(&node->ffn_link); |
284 | list_remove(&node->ffn_link); |
217 | futex_up(&fin_futex); |
285 | futex_up(&fin_futex); |
218 | 286 | ||
219 | /* Make sure that the node is fully instantiated. */ |
287 | /* Make sure that the node is fully instantiated. */ |
220 | futex_down(&node->lock); |
288 | futex_down(&node->lock); |
221 | futex_up(&node->lock); |
289 | futex_up(&node->lock); |
222 | 290 | ||
223 | return (void *) node; |
291 | return (void *) node; |
224 | } |
292 | } |
225 | 293 | ||
226 | bps = fat_bps_get(dev_handle); |
294 | bps = fat_bps_get(dev_handle); |
227 | dps = bps / sizeof(fat_dentry_t); |
295 | dps = bps / sizeof(fat_dentry_t); |
228 | 296 | ||
229 | if (!list_empty(&ffn_head)) { |
297 | if (!list_empty(&ffn_head)) { |
230 | /* |
298 | /* |
231 | * We are going to reuse a node from the free list. |
299 | * We are going to reuse a node from the free list. |
232 | */ |
300 | */ |
233 | lnk = ffn_head.next; |
301 | lnk = ffn_head.next; |
234 | list_remove(lnk); |
302 | list_remove(lnk); |
235 | node = list_get_instance(lnk, fat_node_t, ffn_link); |
303 | node = list_get_instance(lnk, fat_node_t, ffn_link); |
236 | assert(!node->refcnt); |
304 | assert(!node->refcnt); |
237 | if (node->dirty) |
305 | if (node->dirty) |
238 | fat_sync_node(node); |
306 | fat_sync_node(node); |
239 | key[FIN_KEY_DEV_HANDLE] = node->dev_handle; |
307 | key[FIN_KEY_DEV_HANDLE] = node->dev_handle; |
240 | key[FIN_KEY_INDEX] = node->index; |
308 | key[FIN_KEY_INDEX] = node->index; |
241 | hash_table_remove(&fin_hash, key, sizeof(key)/sizeof(*key)); |
309 | hash_table_remove(&fin_hash, key, sizeof(key)/sizeof(*key)); |
242 | } else { |
310 | } else { |
243 | /* |
311 | /* |
244 | * We need to allocate a new node. |
312 | * We need to allocate a new node. |
245 | */ |
313 | */ |
246 | node = malloc(sizeof(fat_node_t)); |
314 | node = malloc(sizeof(fat_node_t)); |
247 | if (!node) |
315 | if (!node) |
248 | return NULL; |
316 | return NULL; |
249 | } |
317 | } |
250 | fat_node_initialize(node); |
318 | fat_node_initialize(node); |
251 | node->refcnt++; |
319 | node->refcnt++; |
252 | node->lnkcnt++; |
320 | node->lnkcnt++; |
253 | node->dev_handle = dev_handle; |
321 | node->dev_handle = dev_handle; |
254 | node->index = index; |
322 | node->index = index; |
255 | node->pindex = pindex; |
323 | node->pindex = pindex; |
256 | key[FIN_KEY_DEV_HANDLE] = node->dev_handle; |
324 | key[FIN_KEY_DEV_HANDLE] = node->dev_handle; |
257 | key[FIN_KEY_INDEX] = node->index; |
325 | key[FIN_KEY_INDEX] = node->index; |
258 | hash_table_insert(&fin_hash, key, &node->fin_link); |
326 | hash_table_insert(&fin_hash, key, &node->fin_link); |
259 | 327 | ||
260 | /* |
328 | /* |
261 | * We have already put the node back to fin_hash. |
329 | * We have already put the node back to fin_hash. |
262 | * The node is not yet fully instantiated so we lock it prior to |
330 | * The node is not yet fully instantiated so we lock it prior to |
263 | * unlocking fin_hash. |
331 | * unlocking fin_hash. |
264 | */ |
332 | */ |
265 | futex_down(&node->lock); |
333 | futex_down(&node->lock); |
266 | futex_up(&fin_futex); |
334 | futex_up(&fin_futex); |
267 | 335 | ||
268 | /* |
336 | /* |
269 | * Because of the design of the FAT file system, we have no clue about |
337 | * Because of the design of the FAT file system, we have no clue about |
270 | * how big (i.e. how many directory entries it contains) is the parent |
338 | * how big (i.e. how many directory entries it contains) is the parent |
271 | * of the node we are trying to instantiate. However, we know that it |
339 | * of the node we are trying to instantiate. However, we know that it |
272 | * must contain a directory entry for our node of interest. We simply |
340 | * must contain a directory entry for our node of interest. We simply |
273 | * scan the parent until we find it. |
341 | * scan the parent until we find it. |
274 | */ |
342 | */ |
275 | for (i = 0; ; i++) { |
343 | for (i = 0; ; i++) { |
276 | b = fat_block_get(node->dev_handle, node->pindex, i); |
344 | b = fat_block_get(node->dev_handle, node->pindex, i); |
277 | for (j = 0; j < dps; j++) { |
345 | for (j = 0; j < dps; j++) { |
278 | d = ((fat_dentry_t *)b->data) + j; |
346 | d = ((fat_dentry_t *)b->data) + j; |
279 | if (d->firstc == node->index) |
347 | if (d->firstc == node->index) |
280 | goto found; |
348 | goto found; |
281 | } |
349 | } |
282 | block_put(b); |
350 | block_put(b); |
283 | } |
351 | } |
284 | 352 | ||
285 | found: |
353 | found: |
286 | if (!(d->attr & (FAT_ATTR_SUBDIR | FAT_ATTR_VOLLABEL))) |
354 | if (!(d->attr & (FAT_ATTR_SUBDIR | FAT_ATTR_VOLLABEL))) |
287 | node->type = FAT_FILE; |
355 | node->type = FAT_FILE; |
288 | if ((d->attr & FAT_ATTR_SUBDIR) || !index) |
356 | if ((d->attr & FAT_ATTR_SUBDIR) || !index) |
289 | node->type = FAT_DIRECTORY; |
357 | node->type = FAT_DIRECTORY; |
290 | assert((node->type == FAT_FILE) || (node->type == FAT_DIRECTORY)); |
358 | assert((node->type == FAT_FILE) || (node->type == FAT_DIRECTORY)); |
291 | 359 | ||
292 | node->size = uint32_t_le2host(d->size); |
360 | node->size = uint32_t_le2host(d->size); |
293 | block_put(b); |
361 | block_put(b); |
294 | 362 | ||
295 | futex_up(&node->lock); |
363 | futex_up(&node->lock); |
296 | return node; |
364 | return node; |
297 | } |
365 | } |
298 | 366 | ||
299 | static void fat_node_put(void *node) |
367 | static void fat_node_put(void *node) |
300 | { |
368 | { |
301 | fat_node_t *nodep = (fat_node_t *)node; |
369 | fat_node_t *nodep = (fat_node_t *)node; |
302 | 370 | ||
303 | futex_down(&fin_futex); |
371 | futex_down(&fin_futex); |
304 | if (!--nodep->refcnt) |
372 | if (!--nodep->refcnt) |
305 | list_append(&nodep->ffn_link, &ffn_head); |
373 | list_append(&nodep->ffn_link, &ffn_head); |
306 | futex_up(&fin_futex); |
374 | futex_up(&fin_futex); |
307 | } |
375 | } |
308 | 376 | ||
309 | static void *fat_create(int flags) |
377 | static void *fat_create(int flags) |
310 | { |
378 | { |
311 | return NULL; /* not supported at the moment */ |
379 | return NULL; /* not supported at the moment */ |
312 | } |
380 | } |
313 | 381 | ||
314 | static int fat_destroy(void *node) |
382 | static int fat_destroy(void *node) |
315 | { |
383 | { |
316 | return ENOTSUP; /* not supported at the moment */ |
384 | return ENOTSUP; /* not supported at the moment */ |
317 | } |
385 | } |
318 | 386 | ||
319 | static bool fat_link(void *prnt, void *chld, const char *name) |
387 | static bool fat_link(void *prnt, void *chld, const char *name) |
320 | { |
388 | { |
321 | return false; /* not supported at the moment */ |
389 | return false; /* not supported at the moment */ |
322 | } |
390 | } |
323 | 391 | ||
324 | static int fat_unlink(void *prnt, void *chld) |
392 | static int fat_unlink(void *prnt, void *chld) |
325 | { |
393 | { |
326 | return ENOTSUP; /* not supported at the moment */ |
394 | return ENOTSUP; /* not supported at the moment */ |
327 | } |
395 | } |
328 | 396 | ||
329 | static void *fat_match(void *prnt, const char *component) |
397 | static void *fat_match(void *prnt, const char *component) |
330 | { |
398 | { |
331 | fat_node_t *parentp = (fat_node_t *)prnt; |
399 | fat_node_t *parentp = (fat_node_t *)prnt; |
332 | char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1]; |
400 | char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1]; |
333 | unsigned i, j; |
401 | unsigned i, j; |
334 | unsigned bps; /* bytes per sector */ |
402 | unsigned bps; /* bytes per sector */ |
335 | unsigned dps; /* dentries per sector */ |
403 | unsigned dps; /* dentries per sector */ |
336 | unsigned blocks; |
404 | unsigned blocks; |
337 | fat_dentry_t *d; |
405 | fat_dentry_t *d; |
338 | block_t *b; |
406 | block_t *b; |
339 | 407 | ||
340 | bps = fat_bps_get(parentp->dev_handle); |
408 | bps = fat_bps_get(parentp->dev_handle); |
341 | dps = bps / sizeof(fat_dentry_t); |
409 | dps = bps / sizeof(fat_dentry_t); |
342 | blocks = parentp->size / bps + (parentp->size % bps != 0); |
410 | blocks = parentp->size / bps + (parentp->size % bps != 0); |
343 | for (i = 0; i < blocks; i++) { |
411 | for (i = 0; i < blocks; i++) { |
344 | unsigned dentries; |
412 | unsigned dentries; |
345 | 413 | ||
346 | b = fat_block_get(parentp->dev_handle, parentp->index, i); |
414 | b = fat_block_get(parentp->dev_handle, parentp->index, i); |
347 | dentries = (i == blocks - 1) ? |
415 | dentries = (i == blocks - 1) ? |
348 | parentp->size % sizeof(fat_dentry_t) : |
416 | parentp->size % sizeof(fat_dentry_t) : |
349 | dps; |
417 | dps; |
350 | for (j = 0; j < dentries; j++) { |
418 | for (j = 0; j < dentries; j++) { |
351 | d = ((fat_dentry_t *)b->data) + j; |
419 | d = ((fat_dentry_t *)b->data) + j; |
352 | switch (fat_classify_dentry(d)) { |
420 | switch (fat_classify_dentry(d)) { |
353 | case FAT_DENTRY_SKIP: |
421 | case FAT_DENTRY_SKIP: |
354 | continue; |
422 | continue; |
355 | case FAT_DENTRY_LAST: |
423 | case FAT_DENTRY_LAST: |
356 | block_put(b); |
424 | block_put(b); |
357 | return NULL; |
425 | return NULL; |
358 | default: |
426 | default: |
359 | case FAT_DENTRY_VALID: |
427 | case FAT_DENTRY_VALID: |
360 | dentry_name_canonify(d, name); |
428 | dentry_name_canonify(d, name); |
361 | break; |
429 | break; |
362 | } |
430 | } |
363 | if (strcmp(name, component) == 0) { |
431 | if (strcmp(name, component) == 0) { |
364 | /* hit */ |
432 | /* hit */ |
365 | void *node = fat_node_get(parentp->dev_handle, |
433 | void *node = fat_node_get(parentp->dev_handle, |
366 | (fs_index_t)uint16_t_le2host(d->firstc), |
434 | (fs_index_t)uint16_t_le2host(d->firstc), |
367 | parentp->index); |
435 | parentp->index); |
368 | block_put(b); |
436 | block_put(b); |
369 | return node; |
437 | return node; |
370 | } |
438 | } |
371 | } |
439 | } |
372 | block_put(b); |
440 | block_put(b); |
373 | } |
441 | } |
374 | 442 | ||
375 | return NULL; |
443 | return NULL; |
376 | } |
444 | } |
377 | 445 | ||
378 | static fs_index_t fat_index_get(void *node) |
446 | static fs_index_t fat_index_get(void *node) |
379 | { |
447 | { |
380 | fat_node_t *fnodep = (fat_node_t *)node; |
448 | fat_node_t *fnodep = (fat_node_t *)node; |
381 | if (!fnodep) |
449 | if (!fnodep) |
382 | return 0; |
450 | return 0; |
383 | return fnodep->index; |
451 | return fnodep->index; |
384 | } |
452 | } |
385 | 453 | ||
386 | static size_t fat_size_get(void *node) |
454 | static size_t fat_size_get(void *node) |
387 | { |
455 | { |
388 | return ((fat_node_t *)node)->size; |
456 | return ((fat_node_t *)node)->size; |
389 | } |
457 | } |
390 | 458 | ||
391 | static unsigned fat_lnkcnt_get(void *node) |
459 | static unsigned fat_lnkcnt_get(void *node) |
392 | { |
460 | { |
393 | return ((fat_node_t *)node)->lnkcnt; |
461 | return ((fat_node_t *)node)->lnkcnt; |
394 | } |
462 | } |
395 | 463 | ||
396 | static bool fat_has_children(void *node) |
464 | static bool fat_has_children(void *node) |
397 | { |
465 | { |
398 | fat_node_t *nodep = (fat_node_t *)node; |
466 | fat_node_t *nodep = (fat_node_t *)node; |
399 | unsigned bps; |
467 | unsigned bps; |
400 | unsigned dps; |
468 | unsigned dps; |
401 | unsigned blocks; |
469 | unsigned blocks; |
402 | block_t *b; |
470 | block_t *b; |
403 | unsigned i, j; |
471 | unsigned i, j; |
404 | 472 | ||
405 | if (nodep->type != FAT_DIRECTORY) |
473 | if (nodep->type != FAT_DIRECTORY) |
406 | return false; |
474 | return false; |
407 | 475 | ||
408 | bps = fat_bps_get(nodep->dev_handle); |
476 | bps = fat_bps_get(nodep->dev_handle); |
409 | dps = bps / sizeof(fat_dentry_t); |
477 | dps = bps / sizeof(fat_dentry_t); |
410 | 478 | ||
411 | blocks = nodep->size / bps + (nodep->size % bps != 0); |
479 | blocks = nodep->size / bps + (nodep->size % bps != 0); |
412 | 480 | ||
413 | for (i = 0; i < blocks; i++) { |
481 | for (i = 0; i < blocks; i++) { |
414 | unsigned dentries; |
482 | unsigned dentries; |
415 | fat_dentry_t *d; |
483 | fat_dentry_t *d; |
416 | 484 | ||
417 | b = fat_block_get(nodep->dev_handle, nodep->index, i); |
485 | b = fat_block_get(nodep->dev_handle, nodep->index, i); |
418 | dentries = (i == blocks - 1) ? |
486 | dentries = (i == blocks - 1) ? |
419 | nodep->size % sizeof(fat_dentry_t) : |
487 | nodep->size % sizeof(fat_dentry_t) : |
420 | dps; |
488 | dps; |
421 | for (j = 0; j < dentries; j++) { |
489 | for (j = 0; j < dentries; j++) { |
422 | d = ((fat_dentry_t *)b->data) + j; |
490 | d = ((fat_dentry_t *)b->data) + j; |
423 | switch (fat_classify_dentry(d)) { |
491 | switch (fat_classify_dentry(d)) { |
424 | case FAT_DENTRY_SKIP: |
492 | case FAT_DENTRY_SKIP: |
425 | continue; |
493 | continue; |
426 | case FAT_DENTRY_LAST: |
494 | case FAT_DENTRY_LAST: |
427 | block_put(b); |
495 | block_put(b); |
428 | return false; |
496 | return false; |
429 | default: |
497 | default: |
430 | case FAT_DENTRY_VALID: |
498 | case FAT_DENTRY_VALID: |
431 | block_put(b); |
499 | block_put(b); |
432 | return true; |
500 | return true; |
433 | } |
501 | } |
434 | block_put(b); |
502 | block_put(b); |
435 | return true; |
503 | return true; |
436 | } |
504 | } |
437 | block_put(b); |
505 | block_put(b); |
438 | } |
506 | } |
439 | 507 | ||
440 | return false; |
508 | return false; |
441 | } |
509 | } |
442 | 510 | ||
443 | static void *fat_root_get(dev_handle_t dev_handle) |
511 | static void *fat_root_get(dev_handle_t dev_handle) |
444 | { |
512 | { |
445 | return fat_node_get(dev_handle, 0, 0); |
513 | return fat_node_get(dev_handle, 0, 0); |
446 | } |
514 | } |
447 | 515 | ||
448 | static char fat_plb_get_char(unsigned pos) |
516 | static char fat_plb_get_char(unsigned pos) |
449 | { |
517 | { |
450 | return fat_reg.plb_ro[pos % PLB_SIZE]; |
518 | return fat_reg.plb_ro[pos % PLB_SIZE]; |
451 | } |
519 | } |
452 | 520 | ||
453 | static bool fat_is_directory(void *node) |
521 | static bool fat_is_directory(void *node) |
454 | { |
522 | { |
455 | return ((fat_node_t *)node)->type == FAT_DIRECTORY; |
523 | return ((fat_node_t *)node)->type == FAT_DIRECTORY; |
456 | } |
524 | } |
457 | 525 | ||
458 | static bool fat_is_file(void *node) |
526 | static bool fat_is_file(void *node) |
459 | { |
527 | { |
460 | return ((fat_node_t *)node)->type == FAT_FILE; |
528 | return ((fat_node_t *)node)->type == FAT_FILE; |
461 | } |
529 | } |
462 | 530 | ||
463 | /** libfs operations */ |
531 | /** libfs operations */ |
464 | libfs_ops_t fat_libfs_ops = { |
532 | libfs_ops_t fat_libfs_ops = { |
465 | .match = fat_match, |
533 | .match = fat_match, |
466 | .node_get = fat_node_get, |
534 | .node_get = fat_node_get, |
467 | .node_put = fat_node_put, |
535 | .node_put = fat_node_put, |
468 | .create = fat_create, |
536 | .create = fat_create, |
469 | .destroy = fat_destroy, |
537 | .destroy = fat_destroy, |
470 | .link = fat_link, |
538 | .link = fat_link, |
471 | .unlink = fat_unlink, |
539 | .unlink = fat_unlink, |
472 | .index_get = fat_index_get, |
540 | .index_get = fat_index_get, |
473 | .size_get = fat_size_get, |
541 | .size_get = fat_size_get, |
474 | .lnkcnt_get = fat_lnkcnt_get, |
542 | .lnkcnt_get = fat_lnkcnt_get, |
475 | .has_children = fat_has_children, |
543 | .has_children = fat_has_children, |
476 | .root_get = fat_root_get, |
544 | .root_get = fat_root_get, |
477 | .plb_get_char = fat_plb_get_char, |
545 | .plb_get_char = fat_plb_get_char, |
478 | .is_directory = fat_is_directory, |
546 | .is_directory = fat_is_directory, |
479 | .is_file = fat_is_file |
547 | .is_file = fat_is_file |
480 | }; |
548 | }; |
481 | 549 | ||
482 | void fat_lookup(ipc_callid_t rid, ipc_call_t *request) |
550 | void fat_lookup(ipc_callid_t rid, ipc_call_t *request) |
483 | { |
551 | { |
484 | libfs_lookup(&fat_libfs_ops, fat_reg.fs_handle, rid, request); |
552 | libfs_lookup(&fat_libfs_ops, fat_reg.fs_handle, rid, request); |
485 | } |
553 | } |
486 | 554 | ||
487 | /** |
555 | /** |
488 | * @} |
556 | * @} |
489 | */ |
557 | */ |
490 | 558 |