Rev 3519 | Rev 3526 | 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" |
||
3505 | jermar | 39 | #include "fat_dentry.h" |
40 | #include "fat_fat.h" |
||
2638 | jermar | 41 | #include "../../vfs/vfs.h" |
2793 | jermar | 42 | #include <libfs.h> |
3521 | jermar | 43 | #include <libblock.h> |
2627 | jermar | 44 | #include <ipc/ipc.h> |
3257 | jermar | 45 | #include <ipc/services.h> |
46 | #include <ipc/devmap.h> |
||
2627 | jermar | 47 | #include <async.h> |
48 | #include <errno.h> |
||
2793 | jermar | 49 | #include <string.h> |
2798 | jermar | 50 | #include <byteorder.h> |
2831 | jermar | 51 | #include <libadt/hash_table.h> |
52 | #include <libadt/list.h> |
||
53 | #include <assert.h> |
||
2856 | jermar | 54 | #include <futex.h> |
3257 | jermar | 55 | #include <sys/mman.h> |
3499 | jermar | 56 | #include <align.h> |
2627 | jermar | 57 | |
2951 | jermar | 58 | /** Futex protecting the list of cached free FAT nodes. */ |
59 | static futex_t ffn_futex = FUTEX_INITIALIZER; |
||
2843 | jermar | 60 | |
2951 | jermar | 61 | /** List of cached free FAT nodes. */ |
62 | static LIST_INITIALIZE(ffn_head); |
||
63 | |||
2831 | jermar | 64 | static void fat_node_initialize(fat_node_t *node) |
2793 | jermar | 65 | { |
2951 | jermar | 66 | futex_initialize(&node->lock, 1); |
2864 | jermar | 67 | node->idx = NULL; |
2831 | jermar | 68 | node->type = 0; |
69 | link_initialize(&node->ffn_link); |
||
70 | node->size = 0; |
||
71 | node->lnkcnt = 0; |
||
72 | node->refcnt = 0; |
||
73 | node->dirty = false; |
||
2793 | jermar | 74 | } |
75 | |||
2893 | jermar | 76 | static void fat_node_sync(fat_node_t *node) |
2831 | jermar | 77 | { |
3519 | jermar | 78 | block_t *bb, *b; |
79 | fat_dentry_t *d; |
||
80 | uint16_t bps; |
||
81 | unsigned dps; |
||
82 | |||
83 | assert(node->dirty); |
||
84 | |||
85 | bb = block_get(node->idx->dev_handle, BS_BLOCK, BS_SIZE); |
||
86 | bps = uint16_t_le2host(FAT_BS(bb)->bps); |
||
87 | dps = bps / sizeof(fat_dentry_t); |
||
88 | |||
89 | /* Read the block that contains the dentry of interest. */ |
||
90 | b = _fat_block_get(bb->data, node->idx->dev_handle, node->idx->pfc, |
||
91 | (node->idx->pdi * sizeof(fat_dentry_t)) / bps); |
||
92 | |||
93 | d = ((fat_dentry_t *)b->data) + (node->idx->pdi % dps); |
||
94 | |||
95 | d->firstc = host2uint16_t_le(node->firstc); |
||
96 | if (node->type == FAT_FILE) |
||
97 | d->size = host2uint32_t_le(node->size); |
||
98 | /* TODO: update other fields? (e.g time fields, attr field) */ |
||
99 | |||
100 | b->dirty = true; /* need to sync block */ |
||
101 | block_put(b); |
||
102 | block_put(bb); |
||
2831 | jermar | 103 | } |
104 | |||
2951 | jermar | 105 | /** Internal version of fat_node_get(). |
106 | * |
||
107 | * @param idxp Locked index structure. |
||
108 | */ |
||
109 | static void *fat_node_get_core(fat_idx_t *idxp) |
||
2831 | jermar | 110 | { |
3516 | jermar | 111 | block_t *bb, *b; |
2891 | jermar | 112 | fat_dentry_t *d; |
3312 | jermar | 113 | fat_node_t *nodep = NULL; |
2891 | jermar | 114 | unsigned bps; |
115 | unsigned dps; |
||
116 | |||
2951 | jermar | 117 | if (idxp->nodep) { |
2891 | jermar | 118 | /* |
119 | * We are lucky. |
||
120 | * The node is already instantiated in memory. |
||
121 | */ |
||
2951 | jermar | 122 | futex_down(&idxp->nodep->lock); |
123 | if (!idxp->nodep->refcnt++) |
||
3312 | jermar | 124 | list_remove(&idxp->nodep->ffn_link); |
2951 | jermar | 125 | futex_up(&idxp->nodep->lock); |
126 | return idxp->nodep; |
||
2891 | jermar | 127 | } |
128 | |||
129 | /* |
||
130 | * We must instantiate the node from the file system. |
||
131 | */ |
||
132 | |||
2951 | jermar | 133 | assert(idxp->pfc); |
2891 | jermar | 134 | |
2951 | jermar | 135 | futex_down(&ffn_futex); |
2893 | jermar | 136 | if (!list_empty(&ffn_head)) { |
2951 | jermar | 137 | /* Try to use a cached free node structure. */ |
138 | fat_idx_t *idxp_tmp; |
||
2893 | jermar | 139 | nodep = list_get_instance(ffn_head.next, fat_node_t, ffn_link); |
2951 | jermar | 140 | if (futex_trydown(&nodep->lock) == ESYNCH_WOULD_BLOCK) |
141 | goto skip_cache; |
||
142 | idxp_tmp = nodep->idx; |
||
143 | if (futex_trydown(&idxp_tmp->lock) == ESYNCH_WOULD_BLOCK) { |
||
144 | futex_up(&nodep->lock); |
||
145 | goto skip_cache; |
||
146 | } |
||
147 | list_remove(&nodep->ffn_link); |
||
148 | futex_up(&ffn_futex); |
||
2893 | jermar | 149 | if (nodep->dirty) |
150 | fat_node_sync(nodep); |
||
2951 | jermar | 151 | idxp_tmp->nodep = NULL; |
152 | futex_up(&nodep->lock); |
||
153 | futex_up(&idxp_tmp->lock); |
||
2893 | jermar | 154 | } else { |
2951 | jermar | 155 | skip_cache: |
2893 | jermar | 156 | /* Try to allocate a new node structure. */ |
2951 | jermar | 157 | futex_up(&ffn_futex); |
2893 | jermar | 158 | nodep = (fat_node_t *)malloc(sizeof(fat_node_t)); |
159 | if (!nodep) |
||
160 | return NULL; |
||
161 | } |
||
2891 | jermar | 162 | fat_node_initialize(nodep); |
163 | |||
3516 | jermar | 164 | bb = block_get(idxp->dev_handle, BS_BLOCK, BS_SIZE); |
165 | bps = uint16_t_le2host(FAT_BS(bb)->bps); |
||
2891 | jermar | 166 | dps = bps / sizeof(fat_dentry_t); |
167 | |||
2893 | jermar | 168 | /* Read the block that contains the dentry of interest. */ |
3516 | jermar | 169 | b = _fat_block_get(bb->data, idxp->dev_handle, idxp->pfc, |
2951 | jermar | 170 | (idxp->pdi * sizeof(fat_dentry_t)) / bps); |
2891 | jermar | 171 | assert(b); |
172 | |||
2951 | jermar | 173 | d = ((fat_dentry_t *)b->data) + (idxp->pdi % dps); |
2893 | jermar | 174 | if (d->attr & FAT_ATTR_SUBDIR) { |
175 | /* |
||
176 | * The only directory which does not have this bit set is the |
||
177 | * root directory itself. The root directory node is handled |
||
178 | * and initialized elsewhere. |
||
179 | */ |
||
180 | nodep->type = FAT_DIRECTORY; |
||
3282 | jermar | 181 | /* |
3325 | jermar | 182 | * Unfortunately, the 'size' field of the FAT dentry is not |
183 | * defined for the directory entry type. We must determine the |
||
184 | * size of the directory by walking the FAT. |
||
3282 | jermar | 185 | */ |
3516 | jermar | 186 | nodep->size = bps * _fat_blcks_get(bb->data, idxp->dev_handle, |
3513 | jermar | 187 | uint16_t_le2host(d->firstc), NULL); |
2893 | jermar | 188 | } else { |
189 | nodep->type = FAT_FILE; |
||
3282 | jermar | 190 | nodep->size = uint32_t_le2host(d->size); |
2893 | jermar | 191 | } |
192 | nodep->firstc = uint16_t_le2host(d->firstc); |
||
193 | nodep->lnkcnt = 1; |
||
194 | nodep->refcnt = 1; |
||
195 | |||
196 | block_put(b); |
||
3516 | jermar | 197 | block_put(bb); |
2893 | jermar | 198 | |
199 | /* Link the idx structure with the node structure. */ |
||
2951 | jermar | 200 | nodep->idx = idxp; |
201 | idxp->nodep = nodep; |
||
2893 | jermar | 202 | |
203 | return nodep; |
||
2831 | jermar | 204 | } |
205 | |||
2951 | jermar | 206 | /** Instantiate a FAT in-core node. */ |
207 | static void *fat_node_get(dev_handle_t dev_handle, fs_index_t index) |
||
208 | { |
||
209 | void *node; |
||
210 | fat_idx_t *idxp; |
||
211 | |||
212 | idxp = fat_idx_get_by_index(dev_handle, index); |
||
213 | if (!idxp) |
||
214 | return NULL; |
||
215 | /* idxp->lock held */ |
||
216 | node = fat_node_get_core(idxp); |
||
217 | futex_up(&idxp->lock); |
||
218 | return node; |
||
219 | } |
||
220 | |||
2852 | jermar | 221 | static void fat_node_put(void *node) |
222 | { |
||
2910 | jermar | 223 | fat_node_t *nodep = (fat_node_t *)node; |
224 | |||
2951 | jermar | 225 | futex_down(&nodep->lock); |
2910 | jermar | 226 | if (!--nodep->refcnt) { |
2951 | jermar | 227 | futex_down(&ffn_futex); |
2910 | jermar | 228 | list_append(&nodep->ffn_link, &ffn_head); |
2951 | jermar | 229 | futex_up(&ffn_futex); |
2910 | jermar | 230 | } |
2951 | jermar | 231 | futex_up(&nodep->lock); |
2852 | jermar | 232 | } |
233 | |||
2857 | jermar | 234 | static void *fat_create(int flags) |
235 | { |
||
236 | return NULL; /* not supported at the moment */ |
||
237 | } |
||
238 | |||
2858 | jermar | 239 | static int fat_destroy(void *node) |
2857 | jermar | 240 | { |
2858 | jermar | 241 | return ENOTSUP; /* not supported at the moment */ |
2857 | jermar | 242 | } |
243 | |||
244 | static bool fat_link(void *prnt, void *chld, const char *name) |
||
245 | { |
||
246 | return false; /* not supported at the moment */ |
||
247 | } |
||
248 | |||
249 | static int fat_unlink(void *prnt, void *chld) |
||
250 | { |
||
251 | return ENOTSUP; /* not supported at the moment */ |
||
252 | } |
||
253 | |||
2793 | jermar | 254 | static void *fat_match(void *prnt, const char *component) |
255 | { |
||
256 | fat_node_t *parentp = (fat_node_t *)prnt; |
||
257 | char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1]; |
||
2822 | jermar | 258 | unsigned i, j; |
2828 | jermar | 259 | unsigned bps; /* bytes per sector */ |
2822 | jermar | 260 | unsigned dps; /* dentries per sector */ |
261 | unsigned blocks; |
||
2793 | jermar | 262 | fat_dentry_t *d; |
3516 | jermar | 263 | block_t *bb, *b; |
2793 | jermar | 264 | |
2953 | jermar | 265 | futex_down(&parentp->idx->lock); |
3516 | jermar | 266 | bb = block_get(parentp->idx->dev_handle, BS_BLOCK, BS_SIZE); |
267 | bps = uint16_t_le2host(FAT_BS(bb)->bps); |
||
2828 | jermar | 268 | dps = bps / sizeof(fat_dentry_t); |
269 | blocks = parentp->size / bps + (parentp->size % bps != 0); |
||
2822 | jermar | 270 | for (i = 0; i < blocks; i++) { |
271 | unsigned dentries; |
||
2793 | jermar | 272 | |
3516 | jermar | 273 | b = fat_block_get(bb->data, parentp, i); |
2822 | jermar | 274 | dentries = (i == blocks - 1) ? |
275 | parentp->size % sizeof(fat_dentry_t) : |
||
276 | dps; |
||
277 | for (j = 0; j < dentries; j++) { |
||
278 | d = ((fat_dentry_t *)b->data) + j; |
||
2845 | jermar | 279 | switch (fat_classify_dentry(d)) { |
280 | case FAT_DENTRY_SKIP: |
||
2822 | jermar | 281 | continue; |
2845 | jermar | 282 | case FAT_DENTRY_LAST: |
2822 | jermar | 283 | block_put(b); |
3516 | jermar | 284 | block_put(bb); |
2953 | jermar | 285 | futex_up(&parentp->idx->lock); |
2822 | jermar | 286 | return NULL; |
2845 | jermar | 287 | default: |
288 | case FAT_DENTRY_VALID: |
||
289 | dentry_name_canonify(d, name); |
||
290 | break; |
||
2822 | jermar | 291 | } |
3272 | jermar | 292 | if (stricmp(name, component) == 0) { |
2822 | jermar | 293 | /* hit */ |
2951 | jermar | 294 | void *node; |
2953 | jermar | 295 | /* |
296 | * Assume tree hierarchy for locking. We |
||
297 | * already have the parent and now we are going |
||
298 | * to lock the child. Never lock in the oposite |
||
299 | * order. |
||
300 | */ |
||
2890 | jermar | 301 | fat_idx_t *idx = fat_idx_get_by_pos( |
2881 | jermar | 302 | parentp->idx->dev_handle, parentp->firstc, |
2864 | jermar | 303 | i * dps + j); |
2953 | jermar | 304 | futex_up(&parentp->idx->lock); |
2890 | jermar | 305 | if (!idx) { |
306 | /* |
||
307 | * Can happen if memory is low or if we |
||
308 | * run out of 32-bit indices. |
||
309 | */ |
||
310 | block_put(b); |
||
3516 | jermar | 311 | block_put(bb); |
2890 | jermar | 312 | return NULL; |
313 | } |
||
2951 | jermar | 314 | node = fat_node_get_core(idx); |
315 | futex_up(&idx->lock); |
||
2822 | jermar | 316 | block_put(b); |
3516 | jermar | 317 | block_put(bb); |
2822 | jermar | 318 | return node; |
319 | } |
||
2793 | jermar | 320 | } |
2822 | jermar | 321 | block_put(b); |
2639 | jermar | 322 | } |
3516 | jermar | 323 | block_put(bb); |
324 | |||
2953 | jermar | 325 | futex_up(&parentp->idx->lock); |
2793 | jermar | 326 | return NULL; |
2638 | jermar | 327 | } |
328 | |||
2831 | jermar | 329 | static fs_index_t fat_index_get(void *node) |
330 | { |
||
331 | fat_node_t *fnodep = (fat_node_t *)node; |
||
332 | if (!fnodep) |
||
333 | return 0; |
||
2864 | jermar | 334 | return fnodep->idx->index; |
2831 | jermar | 335 | } |
336 | |||
337 | static size_t fat_size_get(void *node) |
||
338 | { |
||
339 | return ((fat_node_t *)node)->size; |
||
340 | } |
||
341 | |||
342 | static unsigned fat_lnkcnt_get(void *node) |
||
343 | { |
||
344 | return ((fat_node_t *)node)->lnkcnt; |
||
345 | } |
||
346 | |||
2845 | jermar | 347 | static bool fat_has_children(void *node) |
348 | { |
||
349 | fat_node_t *nodep = (fat_node_t *)node; |
||
350 | unsigned bps; |
||
351 | unsigned dps; |
||
352 | unsigned blocks; |
||
3516 | jermar | 353 | block_t *bb, *b; |
2845 | jermar | 354 | unsigned i, j; |
355 | |||
356 | if (nodep->type != FAT_DIRECTORY) |
||
357 | return false; |
||
358 | |||
2951 | jermar | 359 | futex_down(&nodep->idx->lock); |
3516 | jermar | 360 | bb = block_get(nodep->idx->dev_handle, BS_BLOCK, BS_SIZE); |
361 | bps = uint16_t_le2host(FAT_BS(bb)->bps); |
||
2845 | jermar | 362 | dps = bps / sizeof(fat_dentry_t); |
363 | |||
364 | blocks = nodep->size / bps + (nodep->size % bps != 0); |
||
365 | |||
366 | for (i = 0; i < blocks; i++) { |
||
367 | unsigned dentries; |
||
368 | fat_dentry_t *d; |
||
369 | |||
3516 | jermar | 370 | b = fat_block_get(bb->data, nodep, i); |
2845 | jermar | 371 | dentries = (i == blocks - 1) ? |
372 | nodep->size % sizeof(fat_dentry_t) : |
||
373 | dps; |
||
374 | for (j = 0; j < dentries; j++) { |
||
375 | d = ((fat_dentry_t *)b->data) + j; |
||
376 | switch (fat_classify_dentry(d)) { |
||
377 | case FAT_DENTRY_SKIP: |
||
378 | continue; |
||
379 | case FAT_DENTRY_LAST: |
||
380 | block_put(b); |
||
3516 | jermar | 381 | block_put(bb); |
2951 | jermar | 382 | futex_up(&nodep->idx->lock); |
2845 | jermar | 383 | return false; |
384 | default: |
||
385 | case FAT_DENTRY_VALID: |
||
386 | block_put(b); |
||
3516 | jermar | 387 | block_put(bb); |
2951 | jermar | 388 | futex_up(&nodep->idx->lock); |
2845 | jermar | 389 | return true; |
390 | } |
||
391 | block_put(b); |
||
3516 | jermar | 392 | block_put(bb); |
2951 | jermar | 393 | futex_up(&nodep->idx->lock); |
2845 | jermar | 394 | return true; |
395 | } |
||
396 | block_put(b); |
||
397 | } |
||
3516 | jermar | 398 | block_put(bb); |
2845 | jermar | 399 | |
2951 | jermar | 400 | futex_up(&nodep->idx->lock); |
2845 | jermar | 401 | return false; |
402 | } |
||
403 | |||
2844 | jermar | 404 | static void *fat_root_get(dev_handle_t dev_handle) |
405 | { |
||
3119 | jermar | 406 | return fat_node_get(dev_handle, 0); |
2844 | jermar | 407 | } |
408 | |||
409 | static char fat_plb_get_char(unsigned pos) |
||
410 | { |
||
411 | return fat_reg.plb_ro[pos % PLB_SIZE]; |
||
412 | } |
||
413 | |||
2831 | jermar | 414 | static bool fat_is_directory(void *node) |
415 | { |
||
416 | return ((fat_node_t *)node)->type == FAT_DIRECTORY; |
||
417 | } |
||
418 | |||
419 | static bool fat_is_file(void *node) |
||
420 | { |
||
421 | return ((fat_node_t *)node)->type == FAT_FILE; |
||
422 | } |
||
423 | |||
2793 | jermar | 424 | /** libfs operations */ |
425 | libfs_ops_t fat_libfs_ops = { |
||
426 | .match = fat_match, |
||
427 | .node_get = fat_node_get, |
||
2852 | jermar | 428 | .node_put = fat_node_put, |
2857 | jermar | 429 | .create = fat_create, |
430 | .destroy = fat_destroy, |
||
431 | .link = fat_link, |
||
432 | .unlink = fat_unlink, |
||
2831 | jermar | 433 | .index_get = fat_index_get, |
434 | .size_get = fat_size_get, |
||
435 | .lnkcnt_get = fat_lnkcnt_get, |
||
2845 | jermar | 436 | .has_children = fat_has_children, |
2844 | jermar | 437 | .root_get = fat_root_get, |
438 | .plb_get_char = fat_plb_get_char, |
||
2831 | jermar | 439 | .is_directory = fat_is_directory, |
440 | .is_file = fat_is_file |
||
2793 | jermar | 441 | }; |
442 | |||
3110 | jermar | 443 | void fat_mounted(ipc_callid_t rid, ipc_call_t *request) |
444 | { |
||
445 | dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request); |
||
3119 | jermar | 446 | block_t *bb; |
3257 | jermar | 447 | uint16_t bps; |
3119 | jermar | 448 | uint16_t rde; |
3110 | jermar | 449 | int rc; |
450 | |||
3257 | jermar | 451 | /* |
452 | * For now, we don't bother to remember dev_handle, dev_phone or |
||
453 | * dev_buffer in some data structure. We use global variables because we |
||
454 | * know there will be at most one mount on this file system. |
||
455 | * Of course, this is a huge TODO item. |
||
456 | */ |
||
457 | dev_buffer = mmap(NULL, BS_SIZE, PROTO_READ | PROTO_WRITE, |
||
458 | MAP_ANONYMOUS | MAP_PRIVATE, 0, 0); |
||
459 | |||
460 | if (!dev_buffer) { |
||
461 | ipc_answer_0(rid, ENOMEM); |
||
462 | return; |
||
463 | } |
||
464 | |||
465 | dev_phone = ipc_connect_me_to(PHONE_NS, SERVICE_DEVMAP, |
||
466 | DEVMAP_CONNECT_TO_DEVICE, dev_handle); |
||
467 | |||
468 | if (dev_phone < 0) { |
||
469 | munmap(dev_buffer, BS_SIZE); |
||
470 | ipc_answer_0(rid, dev_phone); |
||
471 | return; |
||
472 | } |
||
473 | |||
474 | rc = ipc_share_out_start(dev_phone, dev_buffer, |
||
475 | AS_AREA_READ | AS_AREA_WRITE); |
||
476 | if (rc != EOK) { |
||
477 | munmap(dev_buffer, BS_SIZE); |
||
478 | ipc_answer_0(rid, rc); |
||
479 | return; |
||
480 | } |
||
481 | |||
3119 | jermar | 482 | /* Read the number of root directory entries. */ |
3253 | jermar | 483 | bb = block_get(dev_handle, BS_BLOCK, BS_SIZE); |
3257 | jermar | 484 | bps = uint16_t_le2host(FAT_BS(bb)->bps); |
3119 | jermar | 485 | rde = uint16_t_le2host(FAT_BS(bb)->root_ent_max); |
486 | block_put(bb); |
||
487 | |||
3257 | jermar | 488 | if (bps != BS_SIZE) { |
489 | munmap(dev_buffer, BS_SIZE); |
||
490 | ipc_answer_0(rid, ENOTSUP); |
||
491 | return; |
||
492 | } |
||
493 | |||
3110 | jermar | 494 | rc = fat_idx_init_by_dev_handle(dev_handle); |
495 | if (rc != EOK) { |
||
3257 | jermar | 496 | munmap(dev_buffer, BS_SIZE); |
3110 | jermar | 497 | ipc_answer_0(rid, rc); |
498 | return; |
||
499 | } |
||
500 | |||
3119 | jermar | 501 | /* Initialize the root node. */ |
502 | fat_node_t *rootp = (fat_node_t *)malloc(sizeof(fat_node_t)); |
||
503 | if (!rootp) { |
||
3257 | jermar | 504 | munmap(dev_buffer, BS_SIZE); |
3119 | jermar | 505 | fat_idx_fini_by_dev_handle(dev_handle); |
506 | ipc_answer_0(rid, ENOMEM); |
||
507 | return; |
||
508 | } |
||
509 | fat_node_initialize(rootp); |
||
510 | |||
511 | fat_idx_t *ridxp = fat_idx_get_by_pos(dev_handle, FAT_CLST_ROOTPAR, 0); |
||
512 | if (!ridxp) { |
||
3257 | jermar | 513 | munmap(dev_buffer, BS_SIZE); |
3119 | jermar | 514 | free(rootp); |
515 | fat_idx_fini_by_dev_handle(dev_handle); |
||
516 | ipc_answer_0(rid, ENOMEM); |
||
517 | return; |
||
518 | } |
||
519 | assert(ridxp->index == 0); |
||
520 | /* ridxp->lock held */ |
||
521 | |||
522 | rootp->type = FAT_DIRECTORY; |
||
523 | rootp->firstc = FAT_CLST_ROOT; |
||
524 | rootp->refcnt = 1; |
||
3352 | jermar | 525 | rootp->lnkcnt = 0; /* FS root is not linked */ |
3119 | jermar | 526 | rootp->size = rde * sizeof(fat_dentry_t); |
527 | rootp->idx = ridxp; |
||
528 | ridxp->nodep = rootp; |
||
529 | |||
530 | futex_up(&ridxp->lock); |
||
531 | |||
3352 | jermar | 532 | ipc_answer_3(rid, EOK, ridxp->index, rootp->size, rootp->lnkcnt); |
3110 | jermar | 533 | } |
534 | |||
535 | void fat_mount(ipc_callid_t rid, ipc_call_t *request) |
||
536 | { |
||
537 | ipc_answer_0(rid, ENOTSUP); |
||
538 | } |
||
539 | |||
2627 | jermar | 540 | void fat_lookup(ipc_callid_t rid, ipc_call_t *request) |
541 | { |
||
2793 | jermar | 542 | libfs_lookup(&fat_libfs_ops, fat_reg.fs_handle, rid, request); |
2627 | jermar | 543 | } |
544 | |||
3307 | jermar | 545 | void fat_read(ipc_callid_t rid, ipc_call_t *request) |
546 | { |
||
547 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
||
548 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); |
||
549 | off_t pos = (off_t)IPC_GET_ARG3(*request); |
||
550 | fat_node_t *nodep = (fat_node_t *)fat_node_get(dev_handle, index); |
||
3516 | jermar | 551 | uint16_t bps; |
3308 | jermar | 552 | size_t bytes; |
3516 | jermar | 553 | block_t *bb, *b; |
3308 | jermar | 554 | |
3307 | jermar | 555 | if (!nodep) { |
556 | ipc_answer_0(rid, ENOENT); |
||
557 | return; |
||
558 | } |
||
559 | |||
560 | ipc_callid_t callid; |
||
561 | size_t len; |
||
3314 | jermar | 562 | if (!ipc_data_read_receive(&callid, &len)) { |
3307 | jermar | 563 | fat_node_put(nodep); |
564 | ipc_answer_0(callid, EINVAL); |
||
565 | ipc_answer_0(rid, EINVAL); |
||
566 | return; |
||
567 | } |
||
568 | |||
3516 | jermar | 569 | bb = block_get(dev_handle, BS_BLOCK, BS_SIZE); |
570 | bps = uint16_t_le2host(FAT_BS(bb)->bps); |
||
571 | |||
3307 | jermar | 572 | if (nodep->type == FAT_FILE) { |
3335 | jermar | 573 | /* |
574 | * Our strategy for regular file reads is to read one block at |
||
575 | * most and make use of the possibility to return less data than |
||
576 | * requested. This keeps the code very simple. |
||
577 | */ |
||
3308 | jermar | 578 | bytes = min(len, bps - pos % bps); |
3516 | jermar | 579 | b = fat_block_get(bb->data, nodep, pos / bps); |
3308 | jermar | 580 | (void) ipc_data_read_finalize(callid, b->data + pos % bps, |
581 | bytes); |
||
582 | block_put(b); |
||
3307 | jermar | 583 | } else { |
3335 | jermar | 584 | unsigned bnum; |
585 | off_t spos = pos; |
||
586 | char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1]; |
||
587 | fat_dentry_t *d; |
||
588 | |||
3307 | jermar | 589 | assert(nodep->type == FAT_DIRECTORY); |
3335 | jermar | 590 | assert(nodep->size % bps == 0); |
591 | assert(bps % sizeof(fat_dentry_t) == 0); |
||
592 | |||
593 | /* |
||
594 | * Our strategy for readdir() is to use the position pointer as |
||
595 | * an index into the array of all dentries. On entry, it points |
||
596 | * to the first unread dentry. If we skip any dentries, we bump |
||
597 | * the position pointer accordingly. |
||
598 | */ |
||
599 | bnum = (pos * sizeof(fat_dentry_t)) / bps; |
||
600 | while (bnum < nodep->size / bps) { |
||
601 | off_t o; |
||
602 | |||
3516 | jermar | 603 | b = fat_block_get(bb->data, nodep, bnum); |
3335 | jermar | 604 | for (o = pos % (bps / sizeof(fat_dentry_t)); |
605 | o < bps / sizeof(fat_dentry_t); |
||
606 | o++, pos++) { |
||
607 | d = ((fat_dentry_t *)b->data) + o; |
||
608 | switch (fat_classify_dentry(d)) { |
||
609 | case FAT_DENTRY_SKIP: |
||
610 | continue; |
||
611 | case FAT_DENTRY_LAST: |
||
612 | block_put(b); |
||
613 | goto miss; |
||
614 | default: |
||
615 | case FAT_DENTRY_VALID: |
||
616 | dentry_name_canonify(d, name); |
||
617 | block_put(b); |
||
618 | goto hit; |
||
619 | } |
||
620 | } |
||
621 | block_put(b); |
||
622 | bnum++; |
||
623 | } |
||
624 | miss: |
||
3307 | jermar | 625 | fat_node_put(nodep); |
3516 | jermar | 626 | block_put(bb); |
3335 | jermar | 627 | ipc_answer_0(callid, ENOENT); |
628 | ipc_answer_1(rid, ENOENT, 0); |
||
3307 | jermar | 629 | return; |
3335 | jermar | 630 | hit: |
631 | (void) ipc_data_read_finalize(callid, name, strlen(name) + 1); |
||
632 | bytes = (pos - spos) + 1; |
||
3307 | jermar | 633 | } |
634 | |||
635 | fat_node_put(nodep); |
||
3516 | jermar | 636 | block_put(bb); |
3308 | jermar | 637 | ipc_answer_1(rid, EOK, (ipcarg_t)bytes); |
3307 | jermar | 638 | } |
639 | |||
3497 | jermar | 640 | void fat_write(ipc_callid_t rid, ipc_call_t *request) |
641 | { |
||
3499 | jermar | 642 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
643 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); |
||
644 | off_t pos = (off_t)IPC_GET_ARG3(*request); |
||
645 | fat_node_t *nodep = (fat_node_t *)fat_node_get(dev_handle, index); |
||
646 | size_t bytes; |
||
647 | block_t *b, *bb; |
||
648 | uint16_t bps; |
||
649 | unsigned spc; |
||
3501 | jermar | 650 | off_t boundary; |
3499 | jermar | 651 | |
652 | if (!nodep) { |
||
653 | ipc_answer_0(rid, ENOENT); |
||
654 | return; |
||
655 | } |
||
656 | |||
657 | /* XXX remove me when you are ready */ |
||
658 | { |
||
659 | ipc_answer_0(rid, ENOTSUP); |
||
660 | fat_node_put(nodep); |
||
661 | return; |
||
662 | } |
||
663 | |||
664 | ipc_callid_t callid; |
||
665 | size_t len; |
||
666 | if (!ipc_data_write_receive(&callid, &len)) { |
||
667 | fat_node_put(nodep); |
||
668 | ipc_answer_0(callid, EINVAL); |
||
669 | ipc_answer_0(rid, EINVAL); |
||
670 | return; |
||
671 | } |
||
672 | |||
673 | /* |
||
674 | * In all scenarios, we will attempt to write out only one block worth |
||
675 | * of data at maximum. There might be some more efficient approaches, |
||
676 | * but this one greatly simplifies fat_write(). Note that we can afford |
||
677 | * to do this because the client must be ready to handle the return |
||
678 | * value signalizing a smaller number of bytes written. |
||
679 | */ |
||
680 | bytes = min(len, bps - pos % bps); |
||
681 | |||
682 | bb = block_get(dev_handle, BS_BLOCK, BS_SIZE); |
||
683 | bps = uint16_t_le2host(FAT_BS(bb)->bps); |
||
684 | spc = FAT_BS(bb)->spc; |
||
685 | |||
3501 | jermar | 686 | boundary = ROUND_UP(nodep->size, bps * spc); |
687 | if (pos < boundary) { |
||
3499 | jermar | 688 | /* |
689 | * This is the easier case - we are either overwriting already |
||
690 | * existing contents or writing behind the EOF, but still within |
||
691 | * the limits of the last cluster. The node size may grow to the |
||
692 | * next block size boundary. |
||
693 | */ |
||
3516 | jermar | 694 | fat_fill_gap(bb->data, nodep, FAT_CLST_RES0, pos); |
695 | b = fat_block_get(bb->data, nodep, pos / bps); |
||
3499 | jermar | 696 | (void) ipc_data_write_finalize(callid, b->data + pos % bps, |
697 | bytes); |
||
698 | b->dirty = true; /* need to sync block */ |
||
3500 | jermar | 699 | block_put(b); |
3499 | jermar | 700 | if (pos + bytes > nodep->size) { |
701 | nodep->size = pos + bytes; |
||
702 | nodep->dirty = true; /* need to sync node */ |
||
703 | } |
||
704 | fat_node_put(nodep); |
||
3516 | jermar | 705 | block_put(bb); |
3499 | jermar | 706 | ipc_answer_1(rid, EOK, bytes); |
707 | return; |
||
708 | } else { |
||
709 | /* |
||
710 | * This is the more difficult case. We must allocate new |
||
711 | * clusters for the node and zero them out. |
||
712 | */ |
||
3500 | jermar | 713 | int status; |
3499 | jermar | 714 | unsigned nclsts; |
3500 | jermar | 715 | fat_cluster_t mcl, lcl; |
716 | |||
3501 | jermar | 717 | nclsts = (ROUND_UP(pos + bytes, bps * spc) - boundary) / |
3500 | jermar | 718 | bps * spc; |
719 | /* create an independent chain of nclsts clusters in all FATs */ |
||
3516 | jermar | 720 | status = fat_alloc_clusters(bb->data, dev_handle, nclsts, &mcl, |
721 | &lcl); |
||
3500 | jermar | 722 | if (status != EOK) { |
723 | /* could not allocate a chain of nclsts clusters */ |
||
724 | fat_node_put(nodep); |
||
3516 | jermar | 725 | block_put(bb); |
3500 | jermar | 726 | ipc_answer_0(callid, status); |
727 | ipc_answer_0(rid, status); |
||
728 | return; |
||
729 | } |
||
730 | /* zero fill any gaps */ |
||
3516 | jermar | 731 | fat_fill_gap(bb->data, nodep, mcl, pos); |
732 | b = _fat_block_get(bb->data, dev_handle, lcl, |
||
733 | (pos / bps) % spc); |
||
3500 | jermar | 734 | (void) ipc_data_write_finalize(callid, b->data + pos % bps, |
735 | bytes); |
||
3501 | jermar | 736 | b->dirty = true; /* need to sync block */ |
3500 | jermar | 737 | block_put(b); |
738 | /* |
||
739 | * Append the cluster chain starting in mcl to the end of the |
||
740 | * node's cluster chain. |
||
741 | */ |
||
3516 | jermar | 742 | fat_append_clusters(bb->data, nodep, mcl); |
3500 | jermar | 743 | nodep->size = pos + bytes; |
3501 | jermar | 744 | nodep->dirty = true; /* need to sync node */ |
3500 | jermar | 745 | fat_node_put(nodep); |
3516 | jermar | 746 | block_put(bb); |
3500 | jermar | 747 | ipc_answer_1(rid, EOK, bytes); |
748 | return; |
||
3499 | jermar | 749 | } |
3497 | jermar | 750 | } |
751 | |||
2627 | jermar | 752 | /** |
753 | * @} |
||
754 | */ |