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