Rev 2891 | Rev 2910 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 2891 | Rev 2893 | ||
---|---|---|---|
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 |
- | |
54 | #define FIN_KEY_INDEX 1 |
- | |
55 | - | ||
56 | /** Hash table of FAT in-core nodes. */ |
- | |
57 | hash_table_t fin_hash; |
- | |
58 | - | ||
59 | /** List of free FAT in-core nodes. */ |
53 | /** List of free FAT nodes that still contain valid data. */ |
60 | link_t ffn_head; |
54 | LIST_INITIALIZE(ffn_head); |
61 | 55 | ||
62 | #define FAT_NAME_LEN 8 |
56 | #define FAT_NAME_LEN 8 |
63 | #define FAT_EXT_LEN 3 |
57 | #define FAT_EXT_LEN 3 |
64 | 58 | ||
65 | #define FAT_PAD ' ' |
59 | #define FAT_PAD ' ' |
66 | 60 | ||
67 | #define FAT_DENTRY_UNUSED 0x00 |
61 | #define FAT_DENTRY_UNUSED 0x00 |
68 | #define FAT_DENTRY_E5_ESC 0x05 |
62 | #define FAT_DENTRY_E5_ESC 0x05 |
69 | #define FAT_DENTRY_DOT 0x2e |
63 | #define FAT_DENTRY_DOT 0x2e |
70 | #define FAT_DENTRY_ERASED 0xe5 |
64 | #define FAT_DENTRY_ERASED 0xe5 |
71 | 65 | ||
72 | static void dentry_name_canonify(fat_dentry_t *d, char *buf) |
66 | static void dentry_name_canonify(fat_dentry_t *d, char *buf) |
73 | { |
67 | { |
74 | int i; |
68 | int i; |
75 | 69 | ||
76 | for (i = 0; i < FAT_NAME_LEN; i++) { |
70 | for (i = 0; i < FAT_NAME_LEN; i++) { |
77 | if (d->name[i] == FAT_PAD) { |
71 | if (d->name[i] == FAT_PAD) { |
78 | buf++; |
72 | buf++; |
79 | break; |
73 | break; |
80 | } |
74 | } |
81 | if (d->name[i] == FAT_DENTRY_E5_ESC) |
75 | if (d->name[i] == FAT_DENTRY_E5_ESC) |
82 | *buf++ = 0xe5; |
76 | *buf++ = 0xe5; |
83 | else |
77 | else |
84 | *buf++ = d->name[i]; |
78 | *buf++ = d->name[i]; |
85 | } |
79 | } |
86 | if (d->ext[0] != FAT_PAD) |
80 | if (d->ext[0] != FAT_PAD) |
87 | *buf++ = '.'; |
81 | *buf++ = '.'; |
88 | for (i = 0; i < FAT_EXT_LEN; i++) { |
82 | for (i = 0; i < FAT_EXT_LEN; i++) { |
89 | if (d->ext[i] == FAT_PAD) { |
83 | if (d->ext[i] == FAT_PAD) { |
90 | *buf = '\0'; |
84 | *buf = '\0'; |
91 | return; |
85 | return; |
92 | } |
86 | } |
93 | if (d->ext[i] == FAT_DENTRY_E5_ESC) |
87 | if (d->ext[i] == FAT_DENTRY_E5_ESC) |
94 | *buf++ = 0xe5; |
88 | *buf++ = 0xe5; |
95 | else |
89 | else |
96 | *buf++ = d->ext[i]; |
90 | *buf++ = d->ext[i]; |
97 | } |
91 | } |
98 | } |
92 | } |
99 | 93 | ||
100 | /* TODO and also move somewhere else */ |
94 | /* TODO and also move somewhere else */ |
101 | typedef struct { |
95 | typedef struct { |
102 | void *data; |
96 | void *data; |
103 | } block_t; |
97 | } block_t; |
104 | 98 | ||
105 | static block_t *block_get(dev_handle_t dev_handle, off_t offset) |
99 | static block_t *block_get(dev_handle_t dev_handle, off_t offset) |
106 | { |
100 | { |
107 | return NULL; /* TODO */ |
101 | return NULL; /* TODO */ |
108 | } |
102 | } |
109 | 103 | ||
110 | static void block_put(block_t *block) |
104 | static void block_put(block_t *block) |
111 | { |
105 | { |
112 | /* TODO */ |
106 | /* TODO */ |
113 | } |
107 | } |
114 | 108 | ||
115 | #define FAT_BS(b) ((fat_bs_t *)((b)->data)) |
109 | #define FAT_BS(b) ((fat_bs_t *)((b)->data)) |
116 | 110 | ||
117 | #define FAT_CLST_RES0 0x0000 |
111 | #define FAT_CLST_RES0 0x0000 |
118 | #define FAT_CLST_RES1 0x0001 /* internally used to mark root directory */ |
112 | #define FAT_CLST_RES1 0x0001 /* internally used to mark root directory */ |
119 | #define FAT_CLST_FIRST 0x0002 |
113 | #define FAT_CLST_FIRST 0x0002 |
120 | #define FAT_CLST_BAD 0xfff7 |
114 | #define FAT_CLST_BAD 0xfff7 |
121 | #define FAT_CLST_LAST1 0xfff8 |
115 | #define FAT_CLST_LAST1 0xfff8 |
122 | #define FAT_CLST_LAST8 0xffff |
116 | #define FAT_CLST_LAST8 0xffff |
123 | 117 | ||
124 | #define fat_block_get(np, off) \ |
118 | #define fat_block_get(np, off) \ |
125 | _fat_block_get((np)->idx->dev_handle, (np)->firstc, (off)) |
119 | _fat_block_get((np)->idx->dev_handle, (np)->firstc, (off)) |
126 | 120 | ||
127 | static block_t * |
121 | static block_t * |
128 | _fat_block_get(dev_handle_t dev_handle, fat_cluster_t fc, off_t offset) |
122 | _fat_block_get(dev_handle_t dev_handle, fat_cluster_t firstc, off_t offset) |
129 | { |
123 | { |
130 | block_t *bb; |
124 | block_t *bb; |
131 | block_t *b; |
125 | block_t *b; |
132 | unsigned bps; |
126 | unsigned bps; |
133 | unsigned spc; |
127 | unsigned spc; |
134 | unsigned rscnt; /* block address of the first FAT */ |
128 | unsigned rscnt; /* block address of the first FAT */ |
135 | unsigned fatcnt; |
129 | unsigned fatcnt; |
136 | unsigned rde; |
130 | unsigned rde; |
137 | unsigned rds; /* root directory size */ |
131 | unsigned rds; /* root directory size */ |
138 | unsigned sf; |
132 | unsigned sf; |
139 | unsigned ssa; /* size of the system area */ |
133 | unsigned ssa; /* size of the system area */ |
140 | unsigned clusters; |
134 | unsigned clusters; |
141 | fat_cluster_t clst = fc; |
135 | fat_cluster_t clst = firstc; |
142 | unsigned i; |
136 | unsigned i; |
143 | 137 | ||
144 | bb = block_get(dev_handle, BS_BLOCK); |
138 | bb = block_get(dev_handle, BS_BLOCK); |
145 | bps = uint16_t_le2host(FAT_BS(bb)->bps); |
139 | bps = uint16_t_le2host(FAT_BS(bb)->bps); |
146 | spc = FAT_BS(bb)->spc; |
140 | spc = FAT_BS(bb)->spc; |
147 | rscnt = uint16_t_le2host(FAT_BS(bb)->rscnt); |
141 | rscnt = uint16_t_le2host(FAT_BS(bb)->rscnt); |
148 | fatcnt = FAT_BS(bb)->fatcnt; |
142 | fatcnt = FAT_BS(bb)->fatcnt; |
149 | rde = uint16_t_le2host(FAT_BS(bb)->root_ent_max); |
143 | rde = uint16_t_le2host(FAT_BS(bb)->root_ent_max); |
150 | sf = uint16_t_le2host(FAT_BS(bb)->sec_per_fat); |
144 | sf = uint16_t_le2host(FAT_BS(bb)->sec_per_fat); |
151 | block_put(bb); |
145 | block_put(bb); |
152 | 146 | ||
153 | rds = (sizeof(fat_dentry_t) * rde) / bps; |
147 | rds = (sizeof(fat_dentry_t) * rde) / bps; |
154 | rds += ((sizeof(fat_dentry_t) * rde) % bps != 0); |
148 | rds += ((sizeof(fat_dentry_t) * rde) % bps != 0); |
155 | ssa = rscnt + fatcnt * sf + rds; |
149 | ssa = rscnt + fatcnt * sf + rds; |
156 | 150 | ||
157 | if (fc == FAT_CLST_RES1) { |
151 | if (firstc == FAT_CLST_RES1) { |
158 | /* root directory special case */ |
152 | /* root directory special case */ |
159 | assert(offset < rds); |
153 | assert(offset < rds); |
160 | b = block_get(dev_handle, rscnt + fatcnt * sf + offset); |
154 | b = block_get(dev_handle, rscnt + fatcnt * sf + offset); |
161 | return b; |
155 | return b; |
162 | } |
156 | } |
163 | 157 | ||
164 | clusters = offset / spc; |
158 | clusters = offset / spc; |
165 | for (i = 0; i < clusters; i++) { |
159 | for (i = 0; i < clusters; i++) { |
166 | unsigned fsec; /* sector offset relative to FAT1 */ |
160 | unsigned fsec; /* sector offset relative to FAT1 */ |
167 | unsigned fidx; /* FAT1 entry index */ |
161 | unsigned fidx; /* FAT1 entry index */ |
168 | 162 | ||
169 | assert(clst >= FAT_CLST_FIRST && clst < FAT_CLST_BAD); |
163 | assert(clst >= FAT_CLST_FIRST && clst < FAT_CLST_BAD); |
170 | fsec = (clst * sizeof(fat_cluster_t)) / bps; |
164 | fsec = (clst * sizeof(fat_cluster_t)) / bps; |
171 | fidx = clst % (bps / sizeof(fat_cluster_t)); |
165 | fidx = clst % (bps / sizeof(fat_cluster_t)); |
172 | /* read FAT1 */ |
166 | /* read FAT1 */ |
173 | b = block_get(dev_handle, rscnt + fsec); |
167 | b = block_get(dev_handle, rscnt + fsec); |
174 | clst = uint16_t_le2host(((fat_cluster_t *)b->data)[fidx]); |
168 | clst = uint16_t_le2host(((fat_cluster_t *)b->data)[fidx]); |
175 | assert(clst != FAT_CLST_BAD); |
169 | assert(clst != FAT_CLST_BAD); |
176 | assert(clst < FAT_CLST_LAST1); |
170 | assert(clst < FAT_CLST_LAST1); |
177 | block_put(b); |
171 | block_put(b); |
178 | } |
172 | } |
179 | 173 | ||
180 | b = block_get(dev_handle, ssa + (clst - FAT_CLST_FIRST) * spc + |
174 | b = block_get(dev_handle, ssa + (clst - FAT_CLST_FIRST) * spc + |
181 | offset % spc); |
175 | offset % spc); |
182 | 176 | ||
183 | return b; |
177 | return b; |
184 | } |
178 | } |
185 | 179 | ||
186 | static void fat_node_initialize(fat_node_t *node) |
180 | static void fat_node_initialize(fat_node_t *node) |
187 | { |
181 | { |
188 | node->idx = NULL; |
182 | node->idx = NULL; |
189 | node->type = 0; |
183 | node->type = 0; |
190 | link_initialize(&node->fin_link); |
- | |
191 | link_initialize(&node->ffn_link); |
184 | link_initialize(&node->ffn_link); |
192 | node->size = 0; |
185 | node->size = 0; |
193 | node->lnkcnt = 0; |
186 | node->lnkcnt = 0; |
194 | node->refcnt = 0; |
187 | node->refcnt = 0; |
195 | node->dirty = false; |
188 | node->dirty = false; |
196 | } |
189 | } |
197 | 190 | ||
198 | static uint16_t fat_bps_get(dev_handle_t dev_handle) |
191 | static uint16_t fat_bps_get(dev_handle_t dev_handle) |
199 | { |
192 | { |
200 | block_t *bb; |
193 | block_t *bb; |
201 | uint16_t bps; |
194 | uint16_t bps; |
202 | 195 | ||
203 | bb = block_get(dev_handle, BS_BLOCK); |
196 | bb = block_get(dev_handle, BS_BLOCK); |
204 | assert(bb != NULL); |
197 | assert(bb != NULL); |
205 | bps = uint16_t_le2host(FAT_BS(bb)->bps); |
198 | bps = uint16_t_le2host(FAT_BS(bb)->bps); |
206 | block_put(bb); |
199 | block_put(bb); |
207 | 200 | ||
208 | return bps; |
201 | return bps; |
209 | } |
202 | } |
210 | 203 | ||
211 | typedef enum { |
204 | typedef enum { |
212 | FAT_DENTRY_SKIP, |
205 | FAT_DENTRY_SKIP, |
213 | FAT_DENTRY_LAST, |
206 | FAT_DENTRY_LAST, |
214 | FAT_DENTRY_VALID |
207 | FAT_DENTRY_VALID |
215 | } fat_dentry_clsf_t; |
208 | } fat_dentry_clsf_t; |
216 | 209 | ||
217 | static fat_dentry_clsf_t fat_classify_dentry(fat_dentry_t *d) |
210 | static fat_dentry_clsf_t fat_classify_dentry(fat_dentry_t *d) |
218 | { |
211 | { |
219 | if (d->attr & FAT_ATTR_VOLLABEL) { |
212 | if (d->attr & FAT_ATTR_VOLLABEL) { |
220 | /* volume label entry */ |
213 | /* volume label entry */ |
221 | return FAT_DENTRY_SKIP; |
214 | return FAT_DENTRY_SKIP; |
222 | } |
215 | } |
223 | if (d->name[0] == FAT_DENTRY_ERASED) { |
216 | if (d->name[0] == FAT_DENTRY_ERASED) { |
224 | /* not-currently-used entry */ |
217 | /* not-currently-used entry */ |
225 | return FAT_DENTRY_SKIP; |
218 | return FAT_DENTRY_SKIP; |
226 | } |
219 | } |
227 | if (d->name[0] == FAT_DENTRY_UNUSED) { |
220 | if (d->name[0] == FAT_DENTRY_UNUSED) { |
228 | /* never used entry */ |
221 | /* never used entry */ |
229 | return FAT_DENTRY_LAST; |
222 | return FAT_DENTRY_LAST; |
230 | } |
223 | } |
231 | if (d->name[0] == FAT_DENTRY_DOT) { |
224 | if (d->name[0] == FAT_DENTRY_DOT) { |
232 | /* |
225 | /* |
233 | * Most likely '.' or '..'. |
226 | * Most likely '.' or '..'. |
234 | * It cannot occur in a regular file name. |
227 | * It cannot occur in a regular file name. |
235 | */ |
228 | */ |
236 | return FAT_DENTRY_SKIP; |
229 | return FAT_DENTRY_SKIP; |
237 | } |
230 | } |
238 | return FAT_DENTRY_VALID; |
231 | return FAT_DENTRY_VALID; |
239 | } |
232 | } |
240 | 233 | ||
241 | static void fat_sync_node(fat_node_t *node) |
234 | static void fat_node_sync(fat_node_t *node) |
242 | { |
235 | { |
243 | /* TODO */ |
236 | /* TODO */ |
244 | } |
237 | } |
245 | 238 | ||
246 | /** Instantiate a FAT in-core node. */ |
239 | /** Instantiate a FAT in-core node. */ |
247 | static void *fat_node_get(dev_handle_t dev_handle, fs_index_t index) |
240 | static void *fat_node_get(dev_handle_t dev_handle, fs_index_t index) |
248 | { |
241 | { |
249 | fat_idx_t *idx; |
242 | fat_idx_t *idx; |
250 | block_t *b; |
243 | block_t *b; |
251 | fat_dentry_t *d; |
244 | fat_dentry_t *d; |
252 | fat_node_t *nodep; |
245 | fat_node_t *nodep; |
253 | unsigned bps; |
246 | unsigned bps; |
254 | unsigned dps; |
247 | unsigned dps; |
255 | 248 | ||
256 | idx = fat_idx_get_by_index(dev_handle, index); |
249 | idx = fat_idx_get_by_index(dev_handle, index); |
257 | if (!idx) |
250 | if (!idx) |
258 | return NULL; |
251 | return NULL; |
259 | 252 | ||
260 | if (idx->nodep) { |
253 | if (idx->nodep) { |
261 | /* |
254 | /* |
262 | * We are lucky. |
255 | * We are lucky. |
263 | * The node is already instantiated in memory. |
256 | * The node is already instantiated in memory. |
264 | */ |
257 | */ |
265 | idx->nodep->refcnt++; |
258 | idx->nodep->refcnt++; |
266 | return idx->nodep; |
259 | return idx->nodep; |
267 | } |
260 | } |
268 | 261 | ||
269 | /* |
262 | /* |
270 | * We must instantiate the node from the file system. |
263 | * We must instantiate the node from the file system. |
271 | */ |
264 | */ |
272 | 265 | ||
273 | assert(idx->pfc); |
266 | assert(idx->pfc); |
274 | 267 | ||
- | 268 | if (!list_empty(&ffn_head)) { |
|
- | 269 | /* Try to use a cached unused node structure. */ |
|
- | 270 | nodep = list_get_instance(ffn_head.next, fat_node_t, ffn_link); |
|
- | 271 | if (nodep->dirty) |
|
- | 272 | fat_node_sync(nodep); |
|
- | 273 | list_remove(&nodep->ffn_link); |
|
- | 274 | nodep->idx->nodep = NULL; |
|
- | 275 | } else { |
|
- | 276 | /* Try to allocate a new node structure. */ |
|
275 | nodep = (fat_node_t *)malloc(sizeof(fat_node_t)); |
277 | nodep = (fat_node_t *)malloc(sizeof(fat_node_t)); |
276 | if (!nodep) |
278 | if (!nodep) |
277 | return NULL; |
279 | return NULL; |
- | 280 | } |
|
278 | fat_node_initialize(nodep); |
281 | fat_node_initialize(nodep); |
279 | 282 | ||
280 | bps = fat_bps_get(dev_handle); |
283 | bps = fat_bps_get(dev_handle); |
281 | dps = bps / sizeof(fat_dentry_t); |
284 | dps = bps / sizeof(fat_dentry_t); |
282 | 285 | ||
- | 286 | /* Read the block that contains the dentry of interest. */ |
|
283 | b = _fat_block_get(dev_handle, idx->pfc, |
287 | b = _fat_block_get(dev_handle, idx->pfc, |
284 | (idx->pdi * sizeof(fat_dentry_t)) / bps); |
288 | (idx->pdi * sizeof(fat_dentry_t)) / bps); |
285 | - | ||
286 | assert(b); |
289 | assert(b); |
287 | 290 | ||
288 | d = ((fat_dentry_t *)b->data) + (idx->pdi % dps); |
291 | d = ((fat_dentry_t *)b->data) + (idx->pdi % dps); |
- | 292 | if (d->attr & FAT_ATTR_SUBDIR) { |
|
- | 293 | /* |
|
- | 294 | * The only directory which does not have this bit set is the |
|
- | 295 | * root directory itself. The root directory node is handled |
|
- | 296 | * and initialized elsewhere. |
|
289 | /* XXX */ |
297 | */ |
- | 298 | nodep->type = FAT_DIRECTORY; |
|
- | 299 | } else { |
|
- | 300 | nodep->type = FAT_FILE; |
|
- | 301 | } |
|
- | 302 | nodep->firstc = uint16_t_le2host(d->firstc); |
|
- | 303 | nodep->size = uint32_t_le2host(d->size); |
|
- | 304 | nodep->lnkcnt = 1; |
|
- | 305 | nodep->refcnt = 1; |
|
- | 306 | ||
- | 307 | block_put(b); |
|
- | 308 | ||
- | 309 | /* Link the idx structure with the node structure. */ |
|
- | 310 | nodep->idx = idx; |
|
- | 311 | idx->nodep = nodep; |
|
- | 312 | ||
- | 313 | return nodep; |
|
290 | } |
314 | } |
291 | 315 | ||
292 | static void fat_node_put(void *node) |
316 | static void fat_node_put(void *node) |
293 | { |
317 | { |
294 | /* TODO */ |
318 | /* TODO */ |
295 | } |
319 | } |
296 | 320 | ||
297 | static void *fat_create(int flags) |
321 | static void *fat_create(int flags) |
298 | { |
322 | { |
299 | return NULL; /* not supported at the moment */ |
323 | return NULL; /* not supported at the moment */ |
300 | } |
324 | } |
301 | 325 | ||
302 | static int fat_destroy(void *node) |
326 | static int fat_destroy(void *node) |
303 | { |
327 | { |
304 | return ENOTSUP; /* not supported at the moment */ |
328 | return ENOTSUP; /* not supported at the moment */ |
305 | } |
329 | } |
306 | 330 | ||
307 | static bool fat_link(void *prnt, void *chld, const char *name) |
331 | static bool fat_link(void *prnt, void *chld, const char *name) |
308 | { |
332 | { |
309 | return false; /* not supported at the moment */ |
333 | return false; /* not supported at the moment */ |
310 | } |
334 | } |
311 | 335 | ||
312 | static int fat_unlink(void *prnt, void *chld) |
336 | static int fat_unlink(void *prnt, void *chld) |
313 | { |
337 | { |
314 | return ENOTSUP; /* not supported at the moment */ |
338 | return ENOTSUP; /* not supported at the moment */ |
315 | } |
339 | } |
316 | 340 | ||
317 | static void *fat_match(void *prnt, const char *component) |
341 | static void *fat_match(void *prnt, const char *component) |
318 | { |
342 | { |
319 | fat_node_t *parentp = (fat_node_t *)prnt; |
343 | fat_node_t *parentp = (fat_node_t *)prnt; |
320 | char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1]; |
344 | char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1]; |
321 | unsigned i, j; |
345 | unsigned i, j; |
322 | unsigned bps; /* bytes per sector */ |
346 | unsigned bps; /* bytes per sector */ |
323 | unsigned dps; /* dentries per sector */ |
347 | unsigned dps; /* dentries per sector */ |
324 | unsigned blocks; |
348 | unsigned blocks; |
325 | fat_dentry_t *d; |
349 | fat_dentry_t *d; |
326 | block_t *b; |
350 | block_t *b; |
327 | 351 | ||
328 | bps = fat_bps_get(parentp->idx->dev_handle); |
352 | bps = fat_bps_get(parentp->idx->dev_handle); |
329 | dps = bps / sizeof(fat_dentry_t); |
353 | dps = bps / sizeof(fat_dentry_t); |
330 | blocks = parentp->size / bps + (parentp->size % bps != 0); |
354 | blocks = parentp->size / bps + (parentp->size % bps != 0); |
331 | for (i = 0; i < blocks; i++) { |
355 | for (i = 0; i < blocks; i++) { |
332 | unsigned dentries; |
356 | unsigned dentries; |
333 | 357 | ||
334 | b = fat_block_get(parentp, i); |
358 | b = fat_block_get(parentp, i); |
335 | dentries = (i == blocks - 1) ? |
359 | dentries = (i == blocks - 1) ? |
336 | parentp->size % sizeof(fat_dentry_t) : |
360 | parentp->size % sizeof(fat_dentry_t) : |
337 | dps; |
361 | dps; |
338 | for (j = 0; j < dentries; j++) { |
362 | for (j = 0; j < dentries; j++) { |
339 | d = ((fat_dentry_t *)b->data) + j; |
363 | d = ((fat_dentry_t *)b->data) + j; |
340 | switch (fat_classify_dentry(d)) { |
364 | switch (fat_classify_dentry(d)) { |
341 | case FAT_DENTRY_SKIP: |
365 | case FAT_DENTRY_SKIP: |
342 | continue; |
366 | continue; |
343 | case FAT_DENTRY_LAST: |
367 | case FAT_DENTRY_LAST: |
344 | block_put(b); |
368 | block_put(b); |
345 | return NULL; |
369 | return NULL; |
346 | default: |
370 | default: |
347 | case FAT_DENTRY_VALID: |
371 | case FAT_DENTRY_VALID: |
348 | dentry_name_canonify(d, name); |
372 | dentry_name_canonify(d, name); |
349 | break; |
373 | break; |
350 | } |
374 | } |
351 | if (strcmp(name, component) == 0) { |
375 | if (strcmp(name, component) == 0) { |
352 | /* hit */ |
376 | /* hit */ |
353 | fat_idx_t *idx = fat_idx_get_by_pos( |
377 | fat_idx_t *idx = fat_idx_get_by_pos( |
354 | parentp->idx->dev_handle, parentp->firstc, |
378 | parentp->idx->dev_handle, parentp->firstc, |
355 | i * dps + j); |
379 | i * dps + j); |
356 | if (!idx) { |
380 | if (!idx) { |
357 | /* |
381 | /* |
358 | * Can happen if memory is low or if we |
382 | * Can happen if memory is low or if we |
359 | * run out of 32-bit indices. |
383 | * run out of 32-bit indices. |
360 | */ |
384 | */ |
361 | block_put(b); |
385 | block_put(b); |
362 | return NULL; |
386 | return NULL; |
363 | } |
387 | } |
364 | void *node = fat_node_get(idx->dev_handle, |
388 | void *node = fat_node_get(idx->dev_handle, |
365 | idx->index); |
389 | idx->index); |
366 | block_put(b); |
390 | block_put(b); |
367 | return node; |
391 | return node; |
368 | } |
392 | } |
369 | } |
393 | } |
370 | block_put(b); |
394 | block_put(b); |
371 | } |
395 | } |
372 | 396 | ||
373 | return NULL; |
397 | return NULL; |
374 | } |
398 | } |
375 | 399 | ||
376 | static fs_index_t fat_index_get(void *node) |
400 | static fs_index_t fat_index_get(void *node) |
377 | { |
401 | { |
378 | fat_node_t *fnodep = (fat_node_t *)node; |
402 | fat_node_t *fnodep = (fat_node_t *)node; |
379 | if (!fnodep) |
403 | if (!fnodep) |
380 | return 0; |
404 | return 0; |
381 | return fnodep->idx->index; |
405 | return fnodep->idx->index; |
382 | } |
406 | } |
383 | 407 | ||
384 | static size_t fat_size_get(void *node) |
408 | static size_t fat_size_get(void *node) |
385 | { |
409 | { |
386 | return ((fat_node_t *)node)->size; |
410 | return ((fat_node_t *)node)->size; |
387 | } |
411 | } |
388 | 412 | ||
389 | static unsigned fat_lnkcnt_get(void *node) |
413 | static unsigned fat_lnkcnt_get(void *node) |
390 | { |
414 | { |
391 | return ((fat_node_t *)node)->lnkcnt; |
415 | return ((fat_node_t *)node)->lnkcnt; |
392 | } |
416 | } |
393 | 417 | ||
394 | static bool fat_has_children(void *node) |
418 | static bool fat_has_children(void *node) |
395 | { |
419 | { |
396 | fat_node_t *nodep = (fat_node_t *)node; |
420 | fat_node_t *nodep = (fat_node_t *)node; |
397 | unsigned bps; |
421 | unsigned bps; |
398 | unsigned dps; |
422 | unsigned dps; |
399 | unsigned blocks; |
423 | unsigned blocks; |
400 | block_t *b; |
424 | block_t *b; |
401 | unsigned i, j; |
425 | unsigned i, j; |
402 | 426 | ||
403 | if (nodep->type != FAT_DIRECTORY) |
427 | if (nodep->type != FAT_DIRECTORY) |
404 | return false; |
428 | return false; |
405 | 429 | ||
406 | bps = fat_bps_get(nodep->idx->dev_handle); |
430 | bps = fat_bps_get(nodep->idx->dev_handle); |
407 | dps = bps / sizeof(fat_dentry_t); |
431 | dps = bps / sizeof(fat_dentry_t); |
408 | 432 | ||
409 | blocks = nodep->size / bps + (nodep->size % bps != 0); |
433 | blocks = nodep->size / bps + (nodep->size % bps != 0); |
410 | 434 | ||
411 | for (i = 0; i < blocks; i++) { |
435 | for (i = 0; i < blocks; i++) { |
412 | unsigned dentries; |
436 | unsigned dentries; |
413 | fat_dentry_t *d; |
437 | fat_dentry_t *d; |
414 | 438 | ||
415 | b = fat_block_get(nodep, i); |
439 | b = fat_block_get(nodep, i); |
416 | dentries = (i == blocks - 1) ? |
440 | dentries = (i == blocks - 1) ? |
417 | nodep->size % sizeof(fat_dentry_t) : |
441 | nodep->size % sizeof(fat_dentry_t) : |
418 | dps; |
442 | dps; |
419 | for (j = 0; j < dentries; j++) { |
443 | for (j = 0; j < dentries; j++) { |
420 | d = ((fat_dentry_t *)b->data) + j; |
444 | d = ((fat_dentry_t *)b->data) + j; |
421 | switch (fat_classify_dentry(d)) { |
445 | switch (fat_classify_dentry(d)) { |
422 | case FAT_DENTRY_SKIP: |
446 | case FAT_DENTRY_SKIP: |
423 | continue; |
447 | continue; |
424 | case FAT_DENTRY_LAST: |
448 | case FAT_DENTRY_LAST: |
425 | block_put(b); |
449 | block_put(b); |
426 | return false; |
450 | return false; |
427 | default: |
451 | default: |
428 | case FAT_DENTRY_VALID: |
452 | case FAT_DENTRY_VALID: |
429 | block_put(b); |
453 | block_put(b); |
430 | return true; |
454 | return true; |
431 | } |
455 | } |
432 | block_put(b); |
456 | block_put(b); |
433 | return true; |
457 | return true; |
434 | } |
458 | } |
435 | block_put(b); |
459 | block_put(b); |
436 | } |
460 | } |
437 | 461 | ||
438 | return false; |
462 | return false; |
439 | } |
463 | } |
440 | 464 | ||
441 | static void *fat_root_get(dev_handle_t dev_handle) |
465 | static void *fat_root_get(dev_handle_t dev_handle) |
442 | { |
466 | { |
443 | return fat_node_get(dev_handle, 0); /* TODO */ |
467 | return NULL; /* TODO */ |
444 | } |
468 | } |
445 | 469 | ||
446 | static char fat_plb_get_char(unsigned pos) |
470 | static char fat_plb_get_char(unsigned pos) |
447 | { |
471 | { |
448 | return fat_reg.plb_ro[pos % PLB_SIZE]; |
472 | return fat_reg.plb_ro[pos % PLB_SIZE]; |
449 | } |
473 | } |
450 | 474 | ||
451 | static bool fat_is_directory(void *node) |
475 | static bool fat_is_directory(void *node) |
452 | { |
476 | { |
453 | return ((fat_node_t *)node)->type == FAT_DIRECTORY; |
477 | return ((fat_node_t *)node)->type == FAT_DIRECTORY; |
454 | } |
478 | } |
455 | 479 | ||
456 | static bool fat_is_file(void *node) |
480 | static bool fat_is_file(void *node) |
457 | { |
481 | { |
458 | return ((fat_node_t *)node)->type == FAT_FILE; |
482 | return ((fat_node_t *)node)->type == FAT_FILE; |
459 | } |
483 | } |
460 | 484 | ||
461 | /** libfs operations */ |
485 | /** libfs operations */ |
462 | libfs_ops_t fat_libfs_ops = { |
486 | libfs_ops_t fat_libfs_ops = { |
463 | .match = fat_match, |
487 | .match = fat_match, |
464 | .node_get = fat_node_get, |
488 | .node_get = fat_node_get, |
465 | .node_put = fat_node_put, |
489 | .node_put = fat_node_put, |
466 | .create = fat_create, |
490 | .create = fat_create, |
467 | .destroy = fat_destroy, |
491 | .destroy = fat_destroy, |
468 | .link = fat_link, |
492 | .link = fat_link, |
469 | .unlink = fat_unlink, |
493 | .unlink = fat_unlink, |
470 | .index_get = fat_index_get, |
494 | .index_get = fat_index_get, |
471 | .size_get = fat_size_get, |
495 | .size_get = fat_size_get, |
472 | .lnkcnt_get = fat_lnkcnt_get, |
496 | .lnkcnt_get = fat_lnkcnt_get, |
473 | .has_children = fat_has_children, |
497 | .has_children = fat_has_children, |
474 | .root_get = fat_root_get, |
498 | .root_get = fat_root_get, |
475 | .plb_get_char = fat_plb_get_char, |
499 | .plb_get_char = fat_plb_get_char, |
476 | .is_directory = fat_is_directory, |
500 | .is_directory = fat_is_directory, |
477 | .is_file = fat_is_file |
501 | .is_file = fat_is_file |
478 | }; |
502 | }; |
479 | 503 | ||
480 | void fat_lookup(ipc_callid_t rid, ipc_call_t *request) |
504 | void fat_lookup(ipc_callid_t rid, ipc_call_t *request) |
481 | { |
505 | { |
482 | libfs_lookup(&fat_libfs_ops, fat_reg.fs_handle, rid, request); |
506 | libfs_lookup(&fat_libfs_ops, fat_reg.fs_handle, rid, request); |
483 | } |
507 | } |
484 | 508 | ||
485 | /** |
509 | /** |
486 | * @} |
510 | * @} |
487 | */ |
511 | */ |
488 | 512 |