Rev 3009 | Rev 3403 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2627 | jermar | 1 | /* |
2793 | jermar | 2 | * Copyright (c) 2008 Jakub Jermar |
2627 | jermar | 3 | * All rights reserved. |
4 | * |
||
5 | * Redistribution and use in source and binary forms, with or without |
||
6 | * modification, are permitted provided that the following conditions |
||
7 | * are met: |
||
8 | * |
||
9 | * - Redistributions of source code must retain the above copyright |
||
10 | * notice, this list of conditions and the following disclaimer. |
||
11 | * - Redistributions in binary form must reproduce the above copyright |
||
12 | * notice, this list of conditions and the following disclaimer in the |
||
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 |
||
15 | * derived from this software without specific prior written permission. |
||
16 | * |
||
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 |
||
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
||
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
||
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
||
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 |
||
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 |
||
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
27 | */ |
||
28 | |||
29 | /** @addtogroup fs |
||
30 | * @{ |
||
31 | */ |
||
32 | |||
33 | /** |
||
34 | * @file fat_ops.c |
||
35 | * @brief Implementation of VFS operations for the FAT file system server. |
||
36 | */ |
||
37 | |||
38 | #include "fat.h" |
||
2638 | jermar | 39 | #include "../../vfs/vfs.h" |
2793 | jermar | 40 | #include <libfs.h> |
2627 | jermar | 41 | #include <ipc/ipc.h> |
42 | #include <async.h> |
||
43 | #include <errno.h> |
||
2793 | jermar | 44 | #include <string.h> |
2798 | jermar | 45 | #include <byteorder.h> |
2831 | jermar | 46 | #include <libadt/hash_table.h> |
47 | #include <libadt/list.h> |
||
48 | #include <assert.h> |
||
2856 | jermar | 49 | #include <futex.h> |
2627 | jermar | 50 | |
2843 | jermar | 51 | #define BS_BLOCK 0 |
52 | |||
3009 | svoboda | 53 | /** Futex protecting the list of cached free FAT nodes. */ |
54 | static futex_t ffn_futex = FUTEX_INITIALIZER; |
||
2843 | jermar | 55 | |
3009 | svoboda | 56 | /** List of cached free FAT nodes. */ |
57 | static LIST_INITIALIZE(ffn_head); |
||
58 | |||
2638 | jermar | 59 | #define FAT_NAME_LEN 8 |
60 | #define FAT_EXT_LEN 3 |
||
61 | |||
62 | #define FAT_PAD ' ' |
||
63 | |||
64 | #define FAT_DENTRY_UNUSED 0x00 |
||
65 | #define FAT_DENTRY_E5_ESC 0x05 |
||
66 | #define FAT_DENTRY_DOT 0x2e |
||
67 | #define FAT_DENTRY_ERASED 0xe5 |
||
68 | |||
2793 | jermar | 69 | static void dentry_name_canonify(fat_dentry_t *d, char *buf) |
2638 | jermar | 70 | { |
2793 | jermar | 71 | int i; |
2639 | jermar | 72 | |
2793 | jermar | 73 | for (i = 0; i < FAT_NAME_LEN; i++) { |
74 | if (d->name[i] == FAT_PAD) { |
||
75 | buf++; |
||
76 | break; |
||
2639 | jermar | 77 | } |
2793 | jermar | 78 | if (d->name[i] == FAT_DENTRY_E5_ESC) |
79 | *buf++ = 0xe5; |
||
80 | else |
||
81 | *buf++ = d->name[i]; |
||
82 | } |
||
83 | if (d->ext[0] != FAT_PAD) |
||
84 | *buf++ = '.'; |
||
85 | for (i = 0; i < FAT_EXT_LEN; i++) { |
||
86 | if (d->ext[i] == FAT_PAD) { |
||
87 | *buf = '\0'; |
||
88 | return; |
||
89 | } |
||
90 | if (d->ext[i] == FAT_DENTRY_E5_ESC) |
||
91 | *buf++ = 0xe5; |
||
92 | else |
||
93 | *buf++ = d->ext[i]; |
||
94 | } |
||
95 | } |
||
96 | |||
2910 | jermar | 97 | /* TODO move somewhere else */ |
2822 | jermar | 98 | typedef struct { |
99 | void *data; |
||
100 | } block_t; |
||
101 | |||
102 | static block_t *block_get(dev_handle_t dev_handle, off_t offset) |
||
2793 | jermar | 103 | { |
104 | return NULL; /* TODO */ |
||
105 | } |
||
106 | |||
2822 | jermar | 107 | static void block_put(block_t *block) |
2793 | jermar | 108 | { |
109 | /* TODO */ |
||
110 | } |
||
111 | |||
2859 | jermar | 112 | #define FAT_BS(b) ((fat_bs_t *)((b)->data)) |
113 | |||
2864 | jermar | 114 | #define FAT_CLST_RES0 0x0000 |
3153 | svoboda | 115 | #define FAT_CLST_RES1 0x0001 |
2859 | jermar | 116 | #define FAT_CLST_FIRST 0x0002 |
117 | #define FAT_CLST_BAD 0xfff7 |
||
118 | #define FAT_CLST_LAST1 0xfff8 |
||
119 | #define FAT_CLST_LAST8 0xffff |
||
120 | |||
3153 | svoboda | 121 | /* internally used to mark root directory's parent */ |
122 | #define FAT_CLST_ROOTPAR FAT_CLST_RES0 |
||
123 | /* internally used to mark root directory */ |
||
124 | #define FAT_CLST_ROOT FAT_CLST_RES1 |
||
125 | |||
2891 | jermar | 126 | #define fat_block_get(np, off) \ |
127 | _fat_block_get((np)->idx->dev_handle, (np)->firstc, (off)) |
||
128 | |||
129 | static block_t * |
||
2893 | jermar | 130 | _fat_block_get(dev_handle_t dev_handle, fat_cluster_t firstc, off_t offset) |
2859 | jermar | 131 | { |
132 | block_t *bb; |
||
133 | block_t *b; |
||
134 | unsigned bps; |
||
135 | unsigned spc; |
||
136 | unsigned rscnt; /* block address of the first FAT */ |
||
137 | unsigned fatcnt; |
||
138 | unsigned rde; |
||
139 | unsigned rds; /* root directory size */ |
||
140 | unsigned sf; |
||
141 | unsigned ssa; /* size of the system area */ |
||
142 | unsigned clusters; |
||
2893 | jermar | 143 | fat_cluster_t clst = firstc; |
2859 | jermar | 144 | unsigned i; |
145 | |||
2891 | jermar | 146 | bb = block_get(dev_handle, BS_BLOCK); |
2859 | jermar | 147 | bps = uint16_t_le2host(FAT_BS(bb)->bps); |
148 | spc = FAT_BS(bb)->spc; |
||
149 | rscnt = uint16_t_le2host(FAT_BS(bb)->rscnt); |
||
150 | fatcnt = FAT_BS(bb)->fatcnt; |
||
151 | rde = uint16_t_le2host(FAT_BS(bb)->root_ent_max); |
||
152 | sf = uint16_t_le2host(FAT_BS(bb)->sec_per_fat); |
||
153 | block_put(bb); |
||
154 | |||
155 | rds = (sizeof(fat_dentry_t) * rde) / bps; |
||
156 | rds += ((sizeof(fat_dentry_t) * rde) % bps != 0); |
||
157 | ssa = rscnt + fatcnt * sf + rds; |
||
158 | |||
3153 | svoboda | 159 | if (firstc == FAT_CLST_ROOT) { |
2859 | jermar | 160 | /* root directory special case */ |
161 | assert(offset < rds); |
||
2891 | jermar | 162 | b = block_get(dev_handle, rscnt + fatcnt * sf + offset); |
2859 | jermar | 163 | return b; |
164 | } |
||
165 | |||
166 | clusters = offset / spc; |
||
167 | for (i = 0; i < clusters; i++) { |
||
168 | unsigned fsec; /* sector offset relative to FAT1 */ |
||
169 | unsigned fidx; /* FAT1 entry index */ |
||
170 | |||
171 | assert(clst >= FAT_CLST_FIRST && clst < FAT_CLST_BAD); |
||
2864 | jermar | 172 | fsec = (clst * sizeof(fat_cluster_t)) / bps; |
173 | fidx = clst % (bps / sizeof(fat_cluster_t)); |
||
2859 | jermar | 174 | /* read FAT1 */ |
2891 | jermar | 175 | b = block_get(dev_handle, rscnt + fsec); |
2864 | jermar | 176 | clst = uint16_t_le2host(((fat_cluster_t *)b->data)[fidx]); |
2859 | jermar | 177 | assert(clst != FAT_CLST_BAD); |
178 | assert(clst < FAT_CLST_LAST1); |
||
179 | block_put(b); |
||
180 | } |
||
181 | |||
2891 | jermar | 182 | b = block_get(dev_handle, ssa + (clst - FAT_CLST_FIRST) * spc + |
183 | offset % spc); |
||
2859 | jermar | 184 | |
185 | return b; |
||
186 | } |
||
187 | |||
2831 | jermar | 188 | static void fat_node_initialize(fat_node_t *node) |
2793 | jermar | 189 | { |
3009 | svoboda | 190 | futex_initialize(&node->lock, 1); |
2864 | jermar | 191 | node->idx = NULL; |
2831 | jermar | 192 | node->type = 0; |
193 | link_initialize(&node->ffn_link); |
||
194 | node->size = 0; |
||
195 | node->lnkcnt = 0; |
||
196 | node->refcnt = 0; |
||
197 | node->dirty = false; |
||
2793 | jermar | 198 | } |
199 | |||
2843 | jermar | 200 | static uint16_t fat_bps_get(dev_handle_t dev_handle) |
201 | { |
||
202 | block_t *bb; |
||
203 | uint16_t bps; |
||
204 | |||
205 | bb = block_get(dev_handle, BS_BLOCK); |
||
206 | assert(bb != NULL); |
||
2859 | jermar | 207 | bps = uint16_t_le2host(FAT_BS(bb)->bps); |
2843 | jermar | 208 | block_put(bb); |
209 | |||
210 | return bps; |
||
211 | } |
||
212 | |||
2845 | jermar | 213 | typedef enum { |
214 | FAT_DENTRY_SKIP, |
||
215 | FAT_DENTRY_LAST, |
||
216 | FAT_DENTRY_VALID |
||
217 | } fat_dentry_clsf_t; |
||
218 | |||
219 | static fat_dentry_clsf_t fat_classify_dentry(fat_dentry_t *d) |
||
220 | { |
||
221 | if (d->attr & FAT_ATTR_VOLLABEL) { |
||
222 | /* volume label entry */ |
||
223 | return FAT_DENTRY_SKIP; |
||
224 | } |
||
225 | if (d->name[0] == FAT_DENTRY_ERASED) { |
||
226 | /* not-currently-used entry */ |
||
227 | return FAT_DENTRY_SKIP; |
||
228 | } |
||
229 | if (d->name[0] == FAT_DENTRY_UNUSED) { |
||
230 | /* never used entry */ |
||
231 | return FAT_DENTRY_LAST; |
||
232 | } |
||
233 | if (d->name[0] == FAT_DENTRY_DOT) { |
||
234 | /* |
||
235 | * Most likely '.' or '..'. |
||
236 | * It cannot occur in a regular file name. |
||
237 | */ |
||
238 | return FAT_DENTRY_SKIP; |
||
239 | } |
||
240 | return FAT_DENTRY_VALID; |
||
241 | } |
||
242 | |||
2893 | jermar | 243 | static void fat_node_sync(fat_node_t *node) |
2831 | jermar | 244 | { |
245 | /* TODO */ |
||
246 | } |
||
247 | |||
3009 | svoboda | 248 | /** Internal version of fat_node_get(). |
249 | * |
||
250 | * @param idxp Locked index structure. |
||
251 | */ |
||
252 | static void *fat_node_get_core(fat_idx_t *idxp) |
||
2831 | jermar | 253 | { |
2891 | jermar | 254 | block_t *b; |
255 | fat_dentry_t *d; |
||
256 | fat_node_t *nodep; |
||
257 | unsigned bps; |
||
258 | unsigned dps; |
||
259 | |||
3009 | svoboda | 260 | if (idxp->nodep) { |
2891 | jermar | 261 | /* |
262 | * We are lucky. |
||
263 | * The node is already instantiated in memory. |
||
264 | */ |
||
3009 | svoboda | 265 | futex_down(&idxp->nodep->lock); |
266 | if (!idxp->nodep->refcnt++) |
||
2910 | jermar | 267 | list_remove(&nodep->ffn_link); |
3009 | svoboda | 268 | futex_up(&idxp->nodep->lock); |
269 | return idxp->nodep; |
||
2891 | jermar | 270 | } |
271 | |||
272 | /* |
||
273 | * We must instantiate the node from the file system. |
||
274 | */ |
||
275 | |||
3009 | svoboda | 276 | assert(idxp->pfc); |
2891 | jermar | 277 | |
3009 | svoboda | 278 | futex_down(&ffn_futex); |
2893 | jermar | 279 | if (!list_empty(&ffn_head)) { |
3009 | svoboda | 280 | /* Try to use a cached free node structure. */ |
281 | fat_idx_t *idxp_tmp; |
||
2893 | jermar | 282 | nodep = list_get_instance(ffn_head.next, fat_node_t, ffn_link); |
3009 | svoboda | 283 | if (futex_trydown(&nodep->lock) == ESYNCH_WOULD_BLOCK) |
284 | goto skip_cache; |
||
285 | idxp_tmp = nodep->idx; |
||
286 | if (futex_trydown(&idxp_tmp->lock) == ESYNCH_WOULD_BLOCK) { |
||
287 | futex_up(&nodep->lock); |
||
288 | goto skip_cache; |
||
289 | } |
||
290 | list_remove(&nodep->ffn_link); |
||
291 | futex_up(&ffn_futex); |
||
2893 | jermar | 292 | if (nodep->dirty) |
293 | fat_node_sync(nodep); |
||
3009 | svoboda | 294 | idxp_tmp->nodep = NULL; |
295 | futex_up(&nodep->lock); |
||
296 | futex_up(&idxp_tmp->lock); |
||
2893 | jermar | 297 | } else { |
3009 | svoboda | 298 | skip_cache: |
2893 | jermar | 299 | /* Try to allocate a new node structure. */ |
3009 | svoboda | 300 | futex_up(&ffn_futex); |
2893 | jermar | 301 | nodep = (fat_node_t *)malloc(sizeof(fat_node_t)); |
302 | if (!nodep) |
||
303 | return NULL; |
||
304 | } |
||
2891 | jermar | 305 | fat_node_initialize(nodep); |
306 | |||
3009 | svoboda | 307 | bps = fat_bps_get(idxp->dev_handle); |
2891 | jermar | 308 | dps = bps / sizeof(fat_dentry_t); |
309 | |||
2893 | jermar | 310 | /* Read the block that contains the dentry of interest. */ |
3009 | svoboda | 311 | b = _fat_block_get(idxp->dev_handle, idxp->pfc, |
312 | (idxp->pdi * sizeof(fat_dentry_t)) / bps); |
||
2891 | jermar | 313 | assert(b); |
314 | |||
3009 | svoboda | 315 | d = ((fat_dentry_t *)b->data) + (idxp->pdi % dps); |
2893 | jermar | 316 | if (d->attr & FAT_ATTR_SUBDIR) { |
317 | /* |
||
318 | * The only directory which does not have this bit set is the |
||
319 | * root directory itself. The root directory node is handled |
||
320 | * and initialized elsewhere. |
||
321 | */ |
||
322 | nodep->type = FAT_DIRECTORY; |
||
323 | } else { |
||
324 | nodep->type = FAT_FILE; |
||
325 | } |
||
326 | nodep->firstc = uint16_t_le2host(d->firstc); |
||
327 | nodep->size = uint32_t_le2host(d->size); |
||
328 | nodep->lnkcnt = 1; |
||
329 | nodep->refcnt = 1; |
||
330 | |||
331 | block_put(b); |
||
332 | |||
333 | /* Link the idx structure with the node structure. */ |
||
3009 | svoboda | 334 | nodep->idx = idxp; |
335 | idxp->nodep = nodep; |
||
2893 | jermar | 336 | |
337 | return nodep; |
||
2831 | jermar | 338 | } |
339 | |||
3009 | svoboda | 340 | /** Instantiate a FAT in-core node. */ |
341 | static void *fat_node_get(dev_handle_t dev_handle, fs_index_t index) |
||
342 | { |
||
343 | void *node; |
||
344 | fat_idx_t *idxp; |
||
345 | |||
346 | idxp = fat_idx_get_by_index(dev_handle, index); |
||
347 | if (!idxp) |
||
348 | return NULL; |
||
349 | /* idxp->lock held */ |
||
350 | node = fat_node_get_core(idxp); |
||
351 | futex_up(&idxp->lock); |
||
352 | return node; |
||
353 | } |
||
354 | |||
2852 | jermar | 355 | static void fat_node_put(void *node) |
356 | { |
||
2910 | jermar | 357 | fat_node_t *nodep = (fat_node_t *)node; |
358 | |||
3009 | svoboda | 359 | futex_down(&nodep->lock); |
2910 | jermar | 360 | if (!--nodep->refcnt) { |
3009 | svoboda | 361 | futex_down(&ffn_futex); |
2910 | jermar | 362 | list_append(&nodep->ffn_link, &ffn_head); |
3009 | svoboda | 363 | futex_up(&ffn_futex); |
2910 | jermar | 364 | } |
3009 | svoboda | 365 | futex_up(&nodep->lock); |
2852 | jermar | 366 | } |
367 | |||
2857 | jermar | 368 | static void *fat_create(int flags) |
369 | { |
||
370 | return NULL; /* not supported at the moment */ |
||
371 | } |
||
372 | |||
2858 | jermar | 373 | static int fat_destroy(void *node) |
2857 | jermar | 374 | { |
2858 | jermar | 375 | return ENOTSUP; /* not supported at the moment */ |
2857 | jermar | 376 | } |
377 | |||
378 | static bool fat_link(void *prnt, void *chld, const char *name) |
||
379 | { |
||
380 | return false; /* not supported at the moment */ |
||
381 | } |
||
382 | |||
383 | static int fat_unlink(void *prnt, void *chld) |
||
384 | { |
||
385 | return ENOTSUP; /* not supported at the moment */ |
||
386 | } |
||
387 | |||
2793 | jermar | 388 | static void *fat_match(void *prnt, const char *component) |
389 | { |
||
390 | fat_node_t *parentp = (fat_node_t *)prnt; |
||
391 | char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1]; |
||
2822 | jermar | 392 | unsigned i, j; |
2828 | jermar | 393 | unsigned bps; /* bytes per sector */ |
2822 | jermar | 394 | unsigned dps; /* dentries per sector */ |
395 | unsigned blocks; |
||
2793 | jermar | 396 | fat_dentry_t *d; |
2822 | jermar | 397 | block_t *b; |
2793 | jermar | 398 | |
3009 | svoboda | 399 | futex_down(&parentp->idx->lock); |
2864 | jermar | 400 | bps = fat_bps_get(parentp->idx->dev_handle); |
2828 | jermar | 401 | dps = bps / sizeof(fat_dentry_t); |
402 | blocks = parentp->size / bps + (parentp->size % bps != 0); |
||
2822 | jermar | 403 | for (i = 0; i < blocks; i++) { |
404 | unsigned dentries; |
||
2793 | jermar | 405 | |
2864 | jermar | 406 | b = fat_block_get(parentp, i); |
2822 | jermar | 407 | dentries = (i == blocks - 1) ? |
408 | parentp->size % sizeof(fat_dentry_t) : |
||
409 | dps; |
||
410 | for (j = 0; j < dentries; j++) { |
||
411 | d = ((fat_dentry_t *)b->data) + j; |
||
2845 | jermar | 412 | switch (fat_classify_dentry(d)) { |
413 | case FAT_DENTRY_SKIP: |
||
2822 | jermar | 414 | continue; |
2845 | jermar | 415 | case FAT_DENTRY_LAST: |
2822 | jermar | 416 | block_put(b); |
3009 | svoboda | 417 | futex_up(&parentp->idx->lock); |
2822 | jermar | 418 | return NULL; |
2845 | jermar | 419 | default: |
420 | case FAT_DENTRY_VALID: |
||
421 | dentry_name_canonify(d, name); |
||
422 | break; |
||
2822 | jermar | 423 | } |
424 | if (strcmp(name, component) == 0) { |
||
425 | /* hit */ |
||
3009 | svoboda | 426 | void *node; |
427 | /* |
||
428 | * Assume tree hierarchy for locking. We |
||
429 | * already have the parent and now we are going |
||
430 | * to lock the child. Never lock in the oposite |
||
431 | * order. |
||
432 | */ |
||
2890 | jermar | 433 | fat_idx_t *idx = fat_idx_get_by_pos( |
2881 | jermar | 434 | parentp->idx->dev_handle, parentp->firstc, |
2864 | jermar | 435 | i * dps + j); |
3009 | svoboda | 436 | futex_up(&parentp->idx->lock); |
2890 | jermar | 437 | if (!idx) { |
438 | /* |
||
439 | * Can happen if memory is low or if we |
||
440 | * run out of 32-bit indices. |
||
441 | */ |
||
442 | block_put(b); |
||
443 | return NULL; |
||
444 | } |
||
3009 | svoboda | 445 | node = fat_node_get_core(idx); |
446 | futex_up(&idx->lock); |
||
2822 | jermar | 447 | block_put(b); |
448 | return node; |
||
449 | } |
||
2793 | jermar | 450 | } |
2822 | jermar | 451 | block_put(b); |
2639 | jermar | 452 | } |
3009 | svoboda | 453 | futex_up(&parentp->idx->lock); |
2793 | jermar | 454 | return NULL; |
2638 | jermar | 455 | } |
456 | |||
2831 | jermar | 457 | static fs_index_t fat_index_get(void *node) |
458 | { |
||
459 | fat_node_t *fnodep = (fat_node_t *)node; |
||
460 | if (!fnodep) |
||
461 | return 0; |
||
2864 | jermar | 462 | return fnodep->idx->index; |
2831 | jermar | 463 | } |
464 | |||
465 | static size_t fat_size_get(void *node) |
||
466 | { |
||
467 | return ((fat_node_t *)node)->size; |
||
468 | } |
||
469 | |||
470 | static unsigned fat_lnkcnt_get(void *node) |
||
471 | { |
||
472 | return ((fat_node_t *)node)->lnkcnt; |
||
473 | } |
||
474 | |||
2845 | jermar | 475 | static bool fat_has_children(void *node) |
476 | { |
||
477 | fat_node_t *nodep = (fat_node_t *)node; |
||
478 | unsigned bps; |
||
479 | unsigned dps; |
||
480 | unsigned blocks; |
||
481 | block_t *b; |
||
482 | unsigned i, j; |
||
483 | |||
484 | if (nodep->type != FAT_DIRECTORY) |
||
485 | return false; |
||
486 | |||
3009 | svoboda | 487 | futex_down(&nodep->idx->lock); |
2864 | jermar | 488 | bps = fat_bps_get(nodep->idx->dev_handle); |
2845 | jermar | 489 | dps = bps / sizeof(fat_dentry_t); |
490 | |||
491 | blocks = nodep->size / bps + (nodep->size % bps != 0); |
||
492 | |||
493 | for (i = 0; i < blocks; i++) { |
||
494 | unsigned dentries; |
||
495 | fat_dentry_t *d; |
||
496 | |||
2864 | jermar | 497 | b = fat_block_get(nodep, i); |
2845 | jermar | 498 | dentries = (i == blocks - 1) ? |
499 | nodep->size % sizeof(fat_dentry_t) : |
||
500 | dps; |
||
501 | for (j = 0; j < dentries; j++) { |
||
502 | d = ((fat_dentry_t *)b->data) + j; |
||
503 | switch (fat_classify_dentry(d)) { |
||
504 | case FAT_DENTRY_SKIP: |
||
505 | continue; |
||
506 | case FAT_DENTRY_LAST: |
||
507 | block_put(b); |
||
3009 | svoboda | 508 | futex_up(&nodep->idx->lock); |
2845 | jermar | 509 | return false; |
510 | default: |
||
511 | case FAT_DENTRY_VALID: |
||
512 | block_put(b); |
||
3009 | svoboda | 513 | futex_up(&nodep->idx->lock); |
2845 | jermar | 514 | return true; |
515 | } |
||
516 | block_put(b); |
||
3009 | svoboda | 517 | futex_up(&nodep->idx->lock); |
2845 | jermar | 518 | return true; |
519 | } |
||
520 | block_put(b); |
||
521 | } |
||
522 | |||
3009 | svoboda | 523 | futex_up(&nodep->idx->lock); |
2845 | jermar | 524 | return false; |
525 | } |
||
526 | |||
2844 | jermar | 527 | static void *fat_root_get(dev_handle_t dev_handle) |
528 | { |
||
3153 | svoboda | 529 | return fat_node_get(dev_handle, 0); |
2844 | jermar | 530 | } |
531 | |||
532 | static char fat_plb_get_char(unsigned pos) |
||
533 | { |
||
534 | return fat_reg.plb_ro[pos % PLB_SIZE]; |
||
535 | } |
||
536 | |||
2831 | jermar | 537 | static bool fat_is_directory(void *node) |
538 | { |
||
539 | return ((fat_node_t *)node)->type == FAT_DIRECTORY; |
||
540 | } |
||
541 | |||
542 | static bool fat_is_file(void *node) |
||
543 | { |
||
544 | return ((fat_node_t *)node)->type == FAT_FILE; |
||
545 | } |
||
546 | |||
2793 | jermar | 547 | /** libfs operations */ |
548 | libfs_ops_t fat_libfs_ops = { |
||
549 | .match = fat_match, |
||
550 | .node_get = fat_node_get, |
||
2852 | jermar | 551 | .node_put = fat_node_put, |
2857 | jermar | 552 | .create = fat_create, |
553 | .destroy = fat_destroy, |
||
554 | .link = fat_link, |
||
555 | .unlink = fat_unlink, |
||
2831 | jermar | 556 | .index_get = fat_index_get, |
557 | .size_get = fat_size_get, |
||
558 | .lnkcnt_get = fat_lnkcnt_get, |
||
2845 | jermar | 559 | .has_children = fat_has_children, |
2844 | jermar | 560 | .root_get = fat_root_get, |
561 | .plb_get_char = fat_plb_get_char, |
||
2831 | jermar | 562 | .is_directory = fat_is_directory, |
563 | .is_file = fat_is_file |
||
2793 | jermar | 564 | }; |
565 | |||
3153 | svoboda | 566 | void fat_mounted(ipc_callid_t rid, ipc_call_t *request) |
567 | { |
||
568 | dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request); |
||
569 | block_t *bb; |
||
570 | uint16_t rde; |
||
571 | int rc; |
||
572 | |||
573 | /* Read the number of root directory entries. */ |
||
574 | bb = block_get(dev_handle, BS_BLOCK); |
||
575 | rde = uint16_t_le2host(FAT_BS(bb)->root_ent_max); |
||
576 | block_put(bb); |
||
577 | |||
578 | rc = fat_idx_init_by_dev_handle(dev_handle); |
||
579 | if (rc != EOK) { |
||
580 | ipc_answer_0(rid, rc); |
||
581 | return; |
||
582 | } |
||
583 | |||
584 | /* Initialize the root node. */ |
||
585 | fat_node_t *rootp = (fat_node_t *)malloc(sizeof(fat_node_t)); |
||
586 | if (!rootp) { |
||
587 | fat_idx_fini_by_dev_handle(dev_handle); |
||
588 | ipc_answer_0(rid, ENOMEM); |
||
589 | return; |
||
590 | } |
||
591 | fat_node_initialize(rootp); |
||
592 | |||
593 | fat_idx_t *ridxp = fat_idx_get_by_pos(dev_handle, FAT_CLST_ROOTPAR, 0); |
||
594 | if (!ridxp) { |
||
595 | free(rootp); |
||
596 | fat_idx_fini_by_dev_handle(dev_handle); |
||
597 | ipc_answer_0(rid, ENOMEM); |
||
598 | return; |
||
599 | } |
||
600 | assert(ridxp->index == 0); |
||
601 | /* ridxp->lock held */ |
||
602 | |||
603 | rootp->type = FAT_DIRECTORY; |
||
604 | rootp->firstc = FAT_CLST_ROOT; |
||
605 | rootp->refcnt = 1; |
||
606 | rootp->size = rde * sizeof(fat_dentry_t); |
||
607 | rootp->idx = ridxp; |
||
608 | ridxp->nodep = rootp; |
||
609 | |||
610 | futex_up(&ridxp->lock); |
||
611 | |||
612 | ipc_answer_0(rid, EOK); |
||
613 | } |
||
614 | |||
615 | void fat_mount(ipc_callid_t rid, ipc_call_t *request) |
||
616 | { |
||
617 | ipc_answer_0(rid, ENOTSUP); |
||
618 | } |
||
619 | |||
2627 | jermar | 620 | void fat_lookup(ipc_callid_t rid, ipc_call_t *request) |
621 | { |
||
2793 | jermar | 622 | libfs_lookup(&fat_libfs_ops, fat_reg.fs_handle, rid, request); |
2627 | jermar | 623 | } |
624 | |||
625 | /** |
||
626 | * @} |
||
627 | */ |