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