Rev 4153 | Rev 4581 | 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" |
||
4153 | mejdrech | 39 | #include "fat_dentry.h" |
40 | #include "fat_fat.h" |
||
2638 | jermar | 41 | #include "../../vfs/vfs.h" |
2793 | jermar | 42 | #include <libfs.h> |
4153 | mejdrech | 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> |
4153 | mejdrech | 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 | |||
4153 | mejdrech | 76 | static void fat_node_sync(fat_node_t *node) |
2843 | jermar | 77 | { |
4153 | mejdrech | 78 | block_t *b; |
79 | fat_bs_t *bs; |
||
80 | fat_dentry_t *d; |
||
2843 | jermar | 81 | uint16_t bps; |
4153 | mejdrech | 82 | unsigned dps; |
2843 | jermar | 83 | |
4153 | mejdrech | 84 | assert(node->dirty); |
2843 | jermar | 85 | |
4153 | mejdrech | 86 | bs = block_bb_get(node->idx->dev_handle); |
87 | bps = uint16_t_le2host(bs->bps); |
||
88 | dps = bps / sizeof(fat_dentry_t); |
||
89 | |||
90 | /* Read the block that contains the dentry of interest. */ |
||
91 | b = _fat_block_get(bs, node->idx->dev_handle, node->idx->pfc, |
||
92 | (node->idx->pdi * sizeof(fat_dentry_t)) / bps, BLOCK_FLAGS_NONE); |
||
2843 | jermar | 93 | |
4153 | mejdrech | 94 | d = ((fat_dentry_t *)b->data) + (node->idx->pdi % dps); |
2845 | jermar | 95 | |
4153 | mejdrech | 96 | d->firstc = host2uint16_t_le(node->firstc); |
97 | if (node->type == FAT_FILE) { |
||
98 | d->size = host2uint32_t_le(node->size); |
||
99 | } else if (node->type == FAT_DIRECTORY) { |
||
100 | d->attr = FAT_ATTR_SUBDIR; |
||
2845 | jermar | 101 | } |
4153 | mejdrech | 102 | |
103 | /* TODO: update other fields? (e.g time fields) */ |
||
104 | |||
105 | b->dirty = true; /* need to sync block */ |
||
106 | block_put(b); |
||
2845 | jermar | 107 | } |
108 | |||
4153 | mejdrech | 109 | static fat_node_t *fat_node_get_new(void) |
2831 | jermar | 110 | { |
4153 | mejdrech | 111 | fat_node_t *nodep; |
112 | |||
113 | futex_down(&ffn_futex); |
||
114 | if (!list_empty(&ffn_head)) { |
||
115 | /* Try to use a cached free node structure. */ |
||
116 | fat_idx_t *idxp_tmp; |
||
117 | nodep = list_get_instance(ffn_head.next, fat_node_t, ffn_link); |
||
118 | if (futex_trydown(&nodep->lock) == ESYNCH_WOULD_BLOCK) |
||
119 | goto skip_cache; |
||
120 | idxp_tmp = nodep->idx; |
||
121 | if (futex_trydown(&idxp_tmp->lock) == ESYNCH_WOULD_BLOCK) { |
||
122 | futex_up(&nodep->lock); |
||
123 | goto skip_cache; |
||
124 | } |
||
125 | list_remove(&nodep->ffn_link); |
||
126 | futex_up(&ffn_futex); |
||
127 | if (nodep->dirty) |
||
128 | fat_node_sync(nodep); |
||
129 | idxp_tmp->nodep = NULL; |
||
130 | futex_up(&nodep->lock); |
||
131 | futex_up(&idxp_tmp->lock); |
||
132 | } else { |
||
133 | skip_cache: |
||
134 | /* Try to allocate a new node structure. */ |
||
135 | futex_up(&ffn_futex); |
||
136 | nodep = (fat_node_t *)malloc(sizeof(fat_node_t)); |
||
137 | if (!nodep) |
||
138 | return NULL; |
||
139 | } |
||
140 | fat_node_initialize(nodep); |
||
141 | |||
142 | return nodep; |
||
2831 | jermar | 143 | } |
144 | |||
2951 | jermar | 145 | /** Internal version of fat_node_get(). |
146 | * |
||
147 | * @param idxp Locked index structure. |
||
148 | */ |
||
149 | static void *fat_node_get_core(fat_idx_t *idxp) |
||
2831 | jermar | 150 | { |
2891 | jermar | 151 | block_t *b; |
4153 | mejdrech | 152 | fat_bs_t *bs; |
2891 | jermar | 153 | fat_dentry_t *d; |
3312 | jermar | 154 | fat_node_t *nodep = NULL; |
2891 | jermar | 155 | unsigned bps; |
4153 | mejdrech | 156 | unsigned spc; |
2891 | jermar | 157 | unsigned dps; |
158 | |||
2951 | jermar | 159 | if (idxp->nodep) { |
2891 | jermar | 160 | /* |
161 | * We are lucky. |
||
162 | * The node is already instantiated in memory. |
||
163 | */ |
||
2951 | jermar | 164 | futex_down(&idxp->nodep->lock); |
165 | if (!idxp->nodep->refcnt++) |
||
3312 | jermar | 166 | list_remove(&idxp->nodep->ffn_link); |
2951 | jermar | 167 | futex_up(&idxp->nodep->lock); |
168 | return idxp->nodep; |
||
2891 | jermar | 169 | } |
170 | |||
171 | /* |
||
172 | * We must instantiate the node from the file system. |
||
173 | */ |
||
174 | |||
2951 | jermar | 175 | assert(idxp->pfc); |
2891 | jermar | 176 | |
4153 | mejdrech | 177 | nodep = fat_node_get_new(); |
178 | if (!nodep) |
||
179 | return NULL; |
||
2891 | jermar | 180 | |
4153 | mejdrech | 181 | bs = block_bb_get(idxp->dev_handle); |
182 | bps = uint16_t_le2host(bs->bps); |
||
183 | spc = bs->spc; |
||
2891 | jermar | 184 | dps = bps / sizeof(fat_dentry_t); |
185 | |||
2893 | jermar | 186 | /* Read the block that contains the dentry of interest. */ |
4153 | mejdrech | 187 | b = _fat_block_get(bs, idxp->dev_handle, idxp->pfc, |
188 | (idxp->pdi * sizeof(fat_dentry_t)) / bps, BLOCK_FLAGS_NONE); |
||
2891 | jermar | 189 | assert(b); |
190 | |||
2951 | jermar | 191 | d = ((fat_dentry_t *)b->data) + (idxp->pdi % dps); |
2893 | jermar | 192 | if (d->attr & FAT_ATTR_SUBDIR) { |
193 | /* |
||
194 | * The only directory which does not have this bit set is the |
||
195 | * root directory itself. The root directory node is handled |
||
196 | * and initialized elsewhere. |
||
197 | */ |
||
198 | nodep->type = FAT_DIRECTORY; |
||
3282 | jermar | 199 | /* |
3325 | jermar | 200 | * Unfortunately, the 'size' field of the FAT dentry is not |
201 | * defined for the directory entry type. We must determine the |
||
202 | * size of the directory by walking the FAT. |
||
3282 | jermar | 203 | */ |
4153 | mejdrech | 204 | nodep->size = bps * spc * fat_clusters_get(bs, idxp->dev_handle, |
3325 | jermar | 205 | uint16_t_le2host(d->firstc)); |
2893 | jermar | 206 | } else { |
207 | nodep->type = FAT_FILE; |
||
3282 | jermar | 208 | nodep->size = uint32_t_le2host(d->size); |
2893 | jermar | 209 | } |
210 | nodep->firstc = uint16_t_le2host(d->firstc); |
||
211 | nodep->lnkcnt = 1; |
||
212 | nodep->refcnt = 1; |
||
213 | |||
214 | block_put(b); |
||
215 | |||
216 | /* Link the idx structure with the node structure. */ |
||
2951 | jermar | 217 | nodep->idx = idxp; |
218 | idxp->nodep = nodep; |
||
2893 | jermar | 219 | |
220 | return nodep; |
||
2831 | jermar | 221 | } |
222 | |||
4153 | mejdrech | 223 | /* |
224 | * Forward declarations of FAT libfs operations. |
||
225 | */ |
||
226 | static void *fat_node_get(dev_handle_t, fs_index_t); |
||
227 | static void fat_node_put(void *); |
||
228 | static void *fat_create_node(dev_handle_t, int); |
||
229 | static int fat_destroy_node(void *); |
||
230 | static int fat_link(void *, void *, const char *); |
||
231 | static int fat_unlink(void *, void *); |
||
232 | static void *fat_match(void *, const char *); |
||
233 | static fs_index_t fat_index_get(void *); |
||
234 | static size_t fat_size_get(void *); |
||
235 | static unsigned fat_lnkcnt_get(void *); |
||
236 | static bool fat_has_children(void *); |
||
237 | static void *fat_root_get(dev_handle_t); |
||
238 | static char fat_plb_get_char(unsigned); |
||
239 | static bool fat_is_directory(void *); |
||
240 | static bool fat_is_file(void *node); |
||
241 | |||
242 | /* |
||
243 | * FAT libfs operations. |
||
244 | */ |
||
245 | |||
2951 | jermar | 246 | /** Instantiate a FAT in-core node. */ |
4153 | mejdrech | 247 | void *fat_node_get(dev_handle_t dev_handle, fs_index_t index) |
2951 | jermar | 248 | { |
249 | void *node; |
||
250 | fat_idx_t *idxp; |
||
251 | |||
252 | idxp = fat_idx_get_by_index(dev_handle, index); |
||
253 | if (!idxp) |
||
254 | return NULL; |
||
255 | /* idxp->lock held */ |
||
256 | node = fat_node_get_core(idxp); |
||
257 | futex_up(&idxp->lock); |
||
258 | return node; |
||
259 | } |
||
260 | |||
4153 | mejdrech | 261 | void fat_node_put(void *node) |
2852 | jermar | 262 | { |
2910 | jermar | 263 | fat_node_t *nodep = (fat_node_t *)node; |
4153 | mejdrech | 264 | bool destroy = false; |
2910 | jermar | 265 | |
2951 | jermar | 266 | futex_down(&nodep->lock); |
2910 | jermar | 267 | if (!--nodep->refcnt) { |
4153 | mejdrech | 268 | if (nodep->idx) { |
269 | futex_down(&ffn_futex); |
||
270 | list_append(&nodep->ffn_link, &ffn_head); |
||
271 | futex_up(&ffn_futex); |
||
272 | } else { |
||
273 | /* |
||
274 | * The node does not have any index structure associated |
||
275 | * with itself. This can only mean that we are releasing |
||
276 | * the node after a failed attempt to allocate the index |
||
277 | * structure for it. |
||
278 | */ |
||
279 | destroy = true; |
||
280 | } |
||
2910 | jermar | 281 | } |
2951 | jermar | 282 | futex_up(&nodep->lock); |
4153 | mejdrech | 283 | if (destroy) |
284 | free(node); |
||
2852 | jermar | 285 | } |
286 | |||
4153 | mejdrech | 287 | void *fat_create_node(dev_handle_t dev_handle, int flags) |
2857 | jermar | 288 | { |
4153 | mejdrech | 289 | fat_idx_t *idxp; |
290 | fat_node_t *nodep; |
||
291 | fat_bs_t *bs; |
||
292 | fat_cluster_t mcl, lcl; |
||
293 | uint16_t bps; |
||
294 | int rc; |
||
295 | |||
296 | bs = block_bb_get(dev_handle); |
||
297 | bps = uint16_t_le2host(bs->bps); |
||
298 | if (flags & L_DIRECTORY) { |
||
299 | /* allocate a cluster */ |
||
300 | rc = fat_alloc_clusters(bs, dev_handle, 1, &mcl, &lcl); |
||
301 | if (rc != EOK) |
||
302 | return NULL; |
||
303 | } |
||
304 | |||
305 | nodep = fat_node_get_new(); |
||
306 | if (!nodep) { |
||
307 | fat_free_clusters(bs, dev_handle, mcl); |
||
308 | return NULL; |
||
309 | } |
||
310 | idxp = fat_idx_get_new(dev_handle); |
||
311 | if (!idxp) { |
||
312 | fat_free_clusters(bs, dev_handle, mcl); |
||
313 | fat_node_put(nodep); |
||
314 | return NULL; |
||
315 | } |
||
316 | /* idxp->lock held */ |
||
317 | if (flags & L_DIRECTORY) { |
||
318 | int i; |
||
319 | block_t *b; |
||
320 | |||
321 | /* |
||
322 | * Populate the new cluster with unused dentries. |
||
323 | */ |
||
324 | for (i = 0; i < bs->spc; i++) { |
||
325 | b = _fat_block_get(bs, dev_handle, mcl, i, |
||
326 | BLOCK_FLAGS_NOREAD); |
||
327 | /* mark all dentries as never-used */ |
||
328 | memset(b->data, 0, bps); |
||
329 | b->dirty = false; |
||
330 | block_put(b); |
||
331 | } |
||
332 | nodep->type = FAT_DIRECTORY; |
||
333 | nodep->firstc = mcl; |
||
334 | nodep->size = bps * bs->spc; |
||
335 | } else { |
||
336 | nodep->type = FAT_FILE; |
||
337 | nodep->firstc = FAT_CLST_RES0; |
||
338 | nodep->size = 0; |
||
339 | } |
||
340 | nodep->lnkcnt = 0; /* not linked anywhere */ |
||
341 | nodep->refcnt = 1; |
||
342 | nodep->dirty = true; |
||
343 | |||
344 | nodep->idx = idxp; |
||
345 | idxp->nodep = nodep; |
||
346 | |||
347 | futex_up(&idxp->lock); |
||
348 | return nodep; |
||
2857 | jermar | 349 | } |
350 | |||
4153 | mejdrech | 351 | int fat_destroy_node(void *node) |
2857 | jermar | 352 | { |
4153 | mejdrech | 353 | fat_node_t *nodep = (fat_node_t *)node; |
354 | fat_bs_t *bs; |
||
355 | |||
356 | /* |
||
357 | * The node is not reachable from the file system. This means that the |
||
358 | * link count should be zero and that the index structure cannot be |
||
359 | * found in the position hash. Obviously, we don't need to lock the node |
||
360 | * nor its index structure. |
||
361 | */ |
||
362 | assert(nodep->lnkcnt == 0); |
||
363 | |||
364 | /* |
||
365 | * The node may not have any children. |
||
366 | */ |
||
367 | assert(fat_has_children(node) == false); |
||
368 | |||
369 | bs = block_bb_get(nodep->idx->dev_handle); |
||
370 | if (nodep->firstc != FAT_CLST_RES0) { |
||
371 | assert(nodep->size); |
||
372 | /* Free all clusters allocated to the node. */ |
||
373 | fat_free_clusters(bs, nodep->idx->dev_handle, nodep->firstc); |
||
374 | } |
||
375 | |||
376 | fat_idx_destroy(nodep->idx); |
||
377 | free(nodep); |
||
378 | return EOK; |
||
2857 | jermar | 379 | } |
380 | |||
4153 | mejdrech | 381 | int fat_link(void *prnt, void *chld, const char *name) |
2857 | jermar | 382 | { |
4153 | mejdrech | 383 | fat_node_t *parentp = (fat_node_t *)prnt; |
384 | fat_node_t *childp = (fat_node_t *)chld; |
||
385 | fat_dentry_t *d; |
||
386 | fat_bs_t *bs; |
||
387 | block_t *b; |
||
388 | int i, j; |
||
389 | uint16_t bps; |
||
390 | unsigned dps; |
||
391 | unsigned blocks; |
||
392 | fat_cluster_t mcl, lcl; |
||
393 | int rc; |
||
394 | |||
395 | futex_down(&childp->lock); |
||
396 | if (childp->lnkcnt == 1) { |
||
397 | /* |
||
398 | * On FAT, we don't support multiple hard links. |
||
399 | */ |
||
400 | futex_up(&childp->lock); |
||
401 | return EMLINK; |
||
402 | } |
||
403 | assert(childp->lnkcnt == 0); |
||
404 | futex_up(&childp->lock); |
||
405 | |||
406 | if (!fat_dentry_name_verify(name)) { |
||
407 | /* |
||
408 | * Attempt to create unsupported name. |
||
409 | */ |
||
410 | return ENOTSUP; |
||
411 | } |
||
412 | |||
413 | /* |
||
414 | * Get us an unused parent node's dentry or grow the parent and allocate |
||
415 | * a new one. |
||
416 | */ |
||
417 | |||
418 | futex_down(&parentp->idx->lock); |
||
419 | bs = block_bb_get(parentp->idx->dev_handle); |
||
420 | bps = uint16_t_le2host(bs->bps); |
||
421 | dps = bps / sizeof(fat_dentry_t); |
||
422 | |||
423 | blocks = parentp->size / bps; |
||
424 | |||
425 | for (i = 0; i < blocks; i++) { |
||
426 | b = fat_block_get(bs, parentp, i, BLOCK_FLAGS_NONE); |
||
427 | for (j = 0; j < dps; j++) { |
||
428 | d = ((fat_dentry_t *)b->data) + j; |
||
429 | switch (fat_classify_dentry(d)) { |
||
430 | case FAT_DENTRY_SKIP: |
||
431 | case FAT_DENTRY_VALID: |
||
432 | /* skipping used and meta entries */ |
||
433 | continue; |
||
434 | case FAT_DENTRY_FREE: |
||
435 | case FAT_DENTRY_LAST: |
||
436 | /* found an empty slot */ |
||
437 | goto hit; |
||
438 | } |
||
439 | } |
||
440 | block_put(b); |
||
441 | } |
||
442 | j = 0; |
||
443 | |||
444 | /* |
||
445 | * We need to grow the parent in order to create a new unused dentry. |
||
446 | */ |
||
447 | if (parentp->idx->pfc == FAT_CLST_ROOT) { |
||
448 | /* Can't grow the root directory. */ |
||
449 | futex_up(&parentp->idx->lock); |
||
450 | return ENOSPC; |
||
451 | } |
||
452 | rc = fat_alloc_clusters(bs, parentp->idx->dev_handle, 1, &mcl, &lcl); |
||
453 | if (rc != EOK) { |
||
454 | futex_up(&parentp->idx->lock); |
||
455 | return rc; |
||
456 | } |
||
457 | fat_append_clusters(bs, parentp, mcl); |
||
458 | b = fat_block_get(bs, parentp, i, BLOCK_FLAGS_NOREAD); |
||
459 | d = (fat_dentry_t *)b->data; |
||
460 | /* |
||
461 | * Clear all dentries in the block except for the first one (the first |
||
462 | * dentry will be cleared in the next step). |
||
463 | */ |
||
464 | memset(d + 1, 0, bps - sizeof(fat_dentry_t)); |
||
465 | |||
466 | hit: |
||
467 | /* |
||
468 | * At this point we only establish the link between the parent and the |
||
469 | * child. The dentry, except of the name and the extension, will remain |
||
470 | * uninitialized until the corresponding node is synced. Thus the valid |
||
471 | * dentry data is kept in the child node structure. |
||
472 | */ |
||
473 | memset(d, 0, sizeof(fat_dentry_t)); |
||
474 | fat_dentry_name_set(d, name); |
||
475 | b->dirty = true; /* need to sync block */ |
||
476 | block_put(b); |
||
477 | futex_up(&parentp->idx->lock); |
||
478 | |||
479 | futex_down(&childp->idx->lock); |
||
480 | |||
481 | /* |
||
482 | * If possible, create the Sub-directory Identifier Entry and the |
||
483 | * Sub-directory Parent Pointer Entry (i.e. "." and ".."). These entries |
||
484 | * are not mandatory according to Standard ECMA-107 and HelenOS VFS does |
||
485 | * not use them anyway, so this is rather a sign of our good will. |
||
486 | */ |
||
487 | b = fat_block_get(bs, childp, 0, BLOCK_FLAGS_NONE); |
||
488 | d = (fat_dentry_t *)b->data; |
||
489 | if (fat_classify_dentry(d) == FAT_DENTRY_LAST || |
||
4327 | mejdrech | 490 | str_cmp(d->name, FAT_NAME_DOT) == 0) { |
4153 | mejdrech | 491 | memset(d, 0, sizeof(fat_dentry_t)); |
4327 | mejdrech | 492 | str_cpy(d->name, 8, FAT_NAME_DOT); |
493 | str_cpy(d->ext, 3, FAT_EXT_PAD); |
||
4153 | mejdrech | 494 | d->attr = FAT_ATTR_SUBDIR; |
495 | d->firstc = host2uint16_t_le(childp->firstc); |
||
496 | /* TODO: initialize also the date/time members. */ |
||
497 | } |
||
498 | d++; |
||
499 | if (fat_classify_dentry(d) == FAT_DENTRY_LAST || |
||
4327 | mejdrech | 500 | str_cmp(d->name, FAT_NAME_DOT_DOT) == 0) { |
4153 | mejdrech | 501 | memset(d, 0, sizeof(fat_dentry_t)); |
4327 | mejdrech | 502 | str_cpy(d->name, 8, FAT_NAME_DOT_DOT); |
503 | str_cpy(d->ext, 3, FAT_EXT_PAD); |
||
4153 | mejdrech | 504 | d->attr = FAT_ATTR_SUBDIR; |
505 | d->firstc = (parentp->firstc == FAT_CLST_ROOT) ? |
||
506 | host2uint16_t_le(FAT_CLST_RES0) : |
||
507 | host2uint16_t_le(parentp->firstc); |
||
508 | /* TODO: initialize also the date/time members. */ |
||
509 | } |
||
510 | b->dirty = true; /* need to sync block */ |
||
511 | block_put(b); |
||
512 | |||
513 | childp->idx->pfc = parentp->firstc; |
||
514 | childp->idx->pdi = i * dps + j; |
||
515 | futex_up(&childp->idx->lock); |
||
516 | |||
517 | futex_down(&childp->lock); |
||
518 | childp->lnkcnt = 1; |
||
519 | childp->dirty = true; /* need to sync node */ |
||
520 | futex_up(&childp->lock); |
||
521 | |||
522 | /* |
||
523 | * Hash in the index structure into the position hash. |
||
524 | */ |
||
525 | fat_idx_hashin(childp->idx); |
||
526 | |||
527 | return EOK; |
||
2857 | jermar | 528 | } |
529 | |||
4153 | mejdrech | 530 | int fat_unlink(void *prnt, void *chld) |
2857 | jermar | 531 | { |
4153 | mejdrech | 532 | fat_node_t *parentp = (fat_node_t *)prnt; |
533 | fat_node_t *childp = (fat_node_t *)chld; |
||
534 | fat_bs_t *bs; |
||
535 | fat_dentry_t *d; |
||
536 | uint16_t bps; |
||
537 | block_t *b; |
||
538 | |||
539 | futex_down(&parentp->lock); |
||
540 | futex_down(&childp->lock); |
||
541 | assert(childp->lnkcnt == 1); |
||
542 | futex_down(&childp->idx->lock); |
||
543 | bs = block_bb_get(childp->idx->dev_handle); |
||
544 | bps = uint16_t_le2host(bs->bps); |
||
545 | |||
546 | b = _fat_block_get(bs, childp->idx->dev_handle, childp->idx->pfc, |
||
547 | (childp->idx->pdi * sizeof(fat_dentry_t)) / bps, |
||
548 | BLOCK_FLAGS_NONE); |
||
549 | d = (fat_dentry_t *)b->data + |
||
550 | (childp->idx->pdi % (bps / sizeof(fat_dentry_t))); |
||
551 | /* mark the dentry as not-currently-used */ |
||
552 | d->name[0] = FAT_DENTRY_ERASED; |
||
553 | b->dirty = true; /* need to sync block */ |
||
554 | block_put(b); |
||
555 | |||
556 | /* remove the index structure from the position hash */ |
||
557 | fat_idx_hashout(childp->idx); |
||
558 | /* clear position information */ |
||
559 | childp->idx->pfc = FAT_CLST_RES0; |
||
560 | childp->idx->pdi = 0; |
||
561 | futex_up(&childp->idx->lock); |
||
562 | childp->lnkcnt = 0; |
||
563 | childp->dirty = true; |
||
564 | futex_up(&childp->lock); |
||
565 | futex_up(&parentp->lock); |
||
566 | |||
567 | return EOK; |
||
2857 | jermar | 568 | } |
569 | |||
4153 | mejdrech | 570 | void *fat_match(void *prnt, const char *component) |
2793 | jermar | 571 | { |
4153 | mejdrech | 572 | fat_bs_t *bs; |
2793 | jermar | 573 | fat_node_t *parentp = (fat_node_t *)prnt; |
574 | char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1]; |
||
2822 | jermar | 575 | unsigned i, j; |
2828 | jermar | 576 | unsigned bps; /* bytes per sector */ |
2822 | jermar | 577 | unsigned dps; /* dentries per sector */ |
578 | unsigned blocks; |
||
2793 | jermar | 579 | fat_dentry_t *d; |
2822 | jermar | 580 | block_t *b; |
2793 | jermar | 581 | |
2953 | jermar | 582 | futex_down(&parentp->idx->lock); |
4153 | mejdrech | 583 | bs = block_bb_get(parentp->idx->dev_handle); |
584 | bps = uint16_t_le2host(bs->bps); |
||
2828 | jermar | 585 | dps = bps / sizeof(fat_dentry_t); |
4153 | mejdrech | 586 | blocks = parentp->size / bps; |
2822 | jermar | 587 | for (i = 0; i < blocks; i++) { |
4153 | mejdrech | 588 | b = fat_block_get(bs, parentp, i, BLOCK_FLAGS_NONE); |
589 | for (j = 0; j < dps; j++) { |
||
2822 | jermar | 590 | d = ((fat_dentry_t *)b->data) + j; |
2845 | jermar | 591 | switch (fat_classify_dentry(d)) { |
592 | case FAT_DENTRY_SKIP: |
||
4153 | mejdrech | 593 | case FAT_DENTRY_FREE: |
2822 | jermar | 594 | continue; |
2845 | jermar | 595 | case FAT_DENTRY_LAST: |
2822 | jermar | 596 | block_put(b); |
2953 | jermar | 597 | futex_up(&parentp->idx->lock); |
2822 | jermar | 598 | return NULL; |
2845 | jermar | 599 | default: |
600 | case FAT_DENTRY_VALID: |
||
4153 | mejdrech | 601 | fat_dentry_name_get(d, name); |
2845 | jermar | 602 | break; |
2822 | jermar | 603 | } |
4153 | mejdrech | 604 | if (fat_dentry_namecmp(name, component) == 0) { |
2822 | jermar | 605 | /* hit */ |
2951 | jermar | 606 | void *node; |
2953 | jermar | 607 | /* |
608 | * Assume tree hierarchy for locking. We |
||
609 | * already have the parent and now we are going |
||
610 | * to lock the child. Never lock in the oposite |
||
611 | * order. |
||
612 | */ |
||
2890 | jermar | 613 | fat_idx_t *idx = fat_idx_get_by_pos( |
2881 | jermar | 614 | parentp->idx->dev_handle, parentp->firstc, |
2864 | jermar | 615 | i * dps + j); |
2953 | jermar | 616 | futex_up(&parentp->idx->lock); |
2890 | jermar | 617 | if (!idx) { |
618 | /* |
||
619 | * Can happen if memory is low or if we |
||
620 | * run out of 32-bit indices. |
||
621 | */ |
||
622 | block_put(b); |
||
623 | return NULL; |
||
624 | } |
||
2951 | jermar | 625 | node = fat_node_get_core(idx); |
626 | futex_up(&idx->lock); |
||
2822 | jermar | 627 | block_put(b); |
628 | return node; |
||
629 | } |
||
2793 | jermar | 630 | } |
2822 | jermar | 631 | block_put(b); |
2639 | jermar | 632 | } |
4153 | mejdrech | 633 | |
2953 | jermar | 634 | futex_up(&parentp->idx->lock); |
2793 | jermar | 635 | return NULL; |
2638 | jermar | 636 | } |
637 | |||
4153 | mejdrech | 638 | fs_index_t fat_index_get(void *node) |
2831 | jermar | 639 | { |
640 | fat_node_t *fnodep = (fat_node_t *)node; |
||
641 | if (!fnodep) |
||
642 | return 0; |
||
2864 | jermar | 643 | return fnodep->idx->index; |
2831 | jermar | 644 | } |
645 | |||
4153 | mejdrech | 646 | size_t fat_size_get(void *node) |
2831 | jermar | 647 | { |
648 | return ((fat_node_t *)node)->size; |
||
649 | } |
||
650 | |||
4153 | mejdrech | 651 | unsigned fat_lnkcnt_get(void *node) |
2831 | jermar | 652 | { |
653 | return ((fat_node_t *)node)->lnkcnt; |
||
654 | } |
||
655 | |||
4153 | mejdrech | 656 | bool fat_has_children(void *node) |
2845 | jermar | 657 | { |
4153 | mejdrech | 658 | fat_bs_t *bs; |
2845 | jermar | 659 | fat_node_t *nodep = (fat_node_t *)node; |
660 | unsigned bps; |
||
661 | unsigned dps; |
||
662 | unsigned blocks; |
||
663 | block_t *b; |
||
664 | unsigned i, j; |
||
665 | |||
666 | if (nodep->type != FAT_DIRECTORY) |
||
667 | return false; |
||
4153 | mejdrech | 668 | |
2951 | jermar | 669 | futex_down(&nodep->idx->lock); |
4153 | mejdrech | 670 | bs = block_bb_get(nodep->idx->dev_handle); |
671 | bps = uint16_t_le2host(bs->bps); |
||
2845 | jermar | 672 | dps = bps / sizeof(fat_dentry_t); |
673 | |||
4153 | mejdrech | 674 | blocks = nodep->size / bps; |
2845 | jermar | 675 | |
676 | for (i = 0; i < blocks; i++) { |
||
677 | fat_dentry_t *d; |
||
678 | |||
4153 | mejdrech | 679 | b = fat_block_get(bs, nodep, i, BLOCK_FLAGS_NONE); |
680 | for (j = 0; j < dps; j++) { |
||
2845 | jermar | 681 | d = ((fat_dentry_t *)b->data) + j; |
682 | switch (fat_classify_dentry(d)) { |
||
683 | case FAT_DENTRY_SKIP: |
||
4153 | mejdrech | 684 | case FAT_DENTRY_FREE: |
2845 | jermar | 685 | continue; |
686 | case FAT_DENTRY_LAST: |
||
687 | block_put(b); |
||
2951 | jermar | 688 | futex_up(&nodep->idx->lock); |
2845 | jermar | 689 | return false; |
690 | default: |
||
691 | case FAT_DENTRY_VALID: |
||
692 | block_put(b); |
||
2951 | jermar | 693 | futex_up(&nodep->idx->lock); |
2845 | jermar | 694 | return true; |
695 | } |
||
696 | block_put(b); |
||
2951 | jermar | 697 | futex_up(&nodep->idx->lock); |
2845 | jermar | 698 | return true; |
699 | } |
||
700 | block_put(b); |
||
701 | } |
||
702 | |||
2951 | jermar | 703 | futex_up(&nodep->idx->lock); |
2845 | jermar | 704 | return false; |
705 | } |
||
706 | |||
4153 | mejdrech | 707 | void *fat_root_get(dev_handle_t dev_handle) |
2844 | jermar | 708 | { |
3119 | jermar | 709 | return fat_node_get(dev_handle, 0); |
2844 | jermar | 710 | } |
711 | |||
4153 | mejdrech | 712 | char fat_plb_get_char(unsigned pos) |
2844 | jermar | 713 | { |
714 | return fat_reg.plb_ro[pos % PLB_SIZE]; |
||
715 | } |
||
716 | |||
4153 | mejdrech | 717 | bool fat_is_directory(void *node) |
2831 | jermar | 718 | { |
719 | return ((fat_node_t *)node)->type == FAT_DIRECTORY; |
||
720 | } |
||
721 | |||
4153 | mejdrech | 722 | bool fat_is_file(void *node) |
2831 | jermar | 723 | { |
724 | return ((fat_node_t *)node)->type == FAT_FILE; |
||
725 | } |
||
726 | |||
2793 | jermar | 727 | /** libfs operations */ |
728 | libfs_ops_t fat_libfs_ops = { |
||
729 | .match = fat_match, |
||
730 | .node_get = fat_node_get, |
||
2852 | jermar | 731 | .node_put = fat_node_put, |
4153 | mejdrech | 732 | .create = fat_create_node, |
733 | .destroy = fat_destroy_node, |
||
2857 | jermar | 734 | .link = fat_link, |
735 | .unlink = fat_unlink, |
||
2831 | jermar | 736 | .index_get = fat_index_get, |
737 | .size_get = fat_size_get, |
||
738 | .lnkcnt_get = fat_lnkcnt_get, |
||
2845 | jermar | 739 | .has_children = fat_has_children, |
2844 | jermar | 740 | .root_get = fat_root_get, |
741 | .plb_get_char = fat_plb_get_char, |
||
2831 | jermar | 742 | .is_directory = fat_is_directory, |
743 | .is_file = fat_is_file |
||
2793 | jermar | 744 | }; |
745 | |||
4153 | mejdrech | 746 | /* |
747 | * VFS operations. |
||
748 | */ |
||
749 | |||
3110 | jermar | 750 | void fat_mounted(ipc_callid_t rid, ipc_call_t *request) |
751 | { |
||
752 | dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request); |
||
4153 | mejdrech | 753 | fat_bs_t *bs; |
3257 | jermar | 754 | uint16_t bps; |
3119 | jermar | 755 | uint16_t rde; |
3110 | jermar | 756 | int rc; |
757 | |||
4327 | mejdrech | 758 | /* accept the mount options */ |
759 | ipc_callid_t callid; |
||
760 | size_t size; |
||
761 | if (!ipc_data_write_receive(&callid, &size)) { |
||
762 | ipc_answer_0(callid, EINVAL); |
||
763 | ipc_answer_0(rid, EINVAL); |
||
764 | return; |
||
765 | } |
||
766 | char *opts = malloc(size + 1); |
||
767 | if (!opts) { |
||
768 | ipc_answer_0(callid, ENOMEM); |
||
769 | ipc_answer_0(rid, ENOMEM); |
||
770 | return; |
||
771 | } |
||
772 | ipcarg_t retval = ipc_data_write_finalize(callid, opts, size); |
||
773 | if (retval != EOK) { |
||
774 | ipc_answer_0(rid, retval); |
||
775 | free(opts); |
||
776 | return; |
||
777 | } |
||
778 | opts[size] = '\0'; |
||
779 | |||
4153 | mejdrech | 780 | /* initialize libblock */ |
781 | rc = block_init(dev_handle, BS_SIZE); |
||
782 | if (rc != EOK) { |
||
783 | ipc_answer_0(rid, rc); |
||
3257 | jermar | 784 | return; |
785 | } |
||
786 | |||
4153 | mejdrech | 787 | /* prepare the boot block */ |
788 | rc = block_bb_read(dev_handle, BS_BLOCK * BS_SIZE, BS_SIZE); |
||
3257 | jermar | 789 | if (rc != EOK) { |
4153 | mejdrech | 790 | block_fini(dev_handle); |
3257 | jermar | 791 | ipc_answer_0(rid, rc); |
792 | return; |
||
793 | } |
||
794 | |||
4153 | mejdrech | 795 | /* get the buffer with the boot sector */ |
796 | bs = block_bb_get(dev_handle); |
||
797 | |||
3119 | jermar | 798 | /* Read the number of root directory entries. */ |
4153 | mejdrech | 799 | bps = uint16_t_le2host(bs->bps); |
800 | rde = uint16_t_le2host(bs->root_ent_max); |
||
3119 | jermar | 801 | |
3257 | jermar | 802 | if (bps != BS_SIZE) { |
4153 | mejdrech | 803 | block_fini(dev_handle); |
3257 | jermar | 804 | ipc_answer_0(rid, ENOTSUP); |
805 | return; |
||
806 | } |
||
807 | |||
4153 | mejdrech | 808 | /* Initialize the block cache */ |
809 | rc = block_cache_init(dev_handle, bps, 0 /* XXX */); |
||
810 | if (rc != EOK) { |
||
811 | block_fini(dev_handle); |
||
812 | ipc_answer_0(rid, rc); |
||
813 | return; |
||
814 | } |
||
815 | |||
3110 | jermar | 816 | rc = fat_idx_init_by_dev_handle(dev_handle); |
817 | if (rc != EOK) { |
||
4153 | mejdrech | 818 | block_fini(dev_handle); |
3110 | jermar | 819 | ipc_answer_0(rid, rc); |
820 | return; |
||
821 | } |
||
822 | |||
3119 | jermar | 823 | /* Initialize the root node. */ |
824 | fat_node_t *rootp = (fat_node_t *)malloc(sizeof(fat_node_t)); |
||
825 | if (!rootp) { |
||
4153 | mejdrech | 826 | block_fini(dev_handle); |
3119 | jermar | 827 | fat_idx_fini_by_dev_handle(dev_handle); |
828 | ipc_answer_0(rid, ENOMEM); |
||
829 | return; |
||
830 | } |
||
831 | fat_node_initialize(rootp); |
||
832 | |||
833 | fat_idx_t *ridxp = fat_idx_get_by_pos(dev_handle, FAT_CLST_ROOTPAR, 0); |
||
834 | if (!ridxp) { |
||
4153 | mejdrech | 835 | block_fini(dev_handle); |
3119 | jermar | 836 | free(rootp); |
837 | fat_idx_fini_by_dev_handle(dev_handle); |
||
838 | ipc_answer_0(rid, ENOMEM); |
||
839 | return; |
||
840 | } |
||
841 | assert(ridxp->index == 0); |
||
842 | /* ridxp->lock held */ |
||
843 | |||
844 | rootp->type = FAT_DIRECTORY; |
||
845 | rootp->firstc = FAT_CLST_ROOT; |
||
846 | rootp->refcnt = 1; |
||
3352 | jermar | 847 | rootp->lnkcnt = 0; /* FS root is not linked */ |
3119 | jermar | 848 | rootp->size = rde * sizeof(fat_dentry_t); |
849 | rootp->idx = ridxp; |
||
850 | ridxp->nodep = rootp; |
||
851 | |||
852 | futex_up(&ridxp->lock); |
||
853 | |||
3352 | jermar | 854 | ipc_answer_3(rid, EOK, ridxp->index, rootp->size, rootp->lnkcnt); |
3110 | jermar | 855 | } |
856 | |||
857 | void fat_mount(ipc_callid_t rid, ipc_call_t *request) |
||
858 | { |
||
859 | ipc_answer_0(rid, ENOTSUP); |
||
860 | } |
||
861 | |||
2627 | jermar | 862 | void fat_lookup(ipc_callid_t rid, ipc_call_t *request) |
863 | { |
||
2793 | jermar | 864 | libfs_lookup(&fat_libfs_ops, fat_reg.fs_handle, rid, request); |
2627 | jermar | 865 | } |
866 | |||
3307 | jermar | 867 | void fat_read(ipc_callid_t rid, ipc_call_t *request) |
868 | { |
||
869 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
||
870 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); |
||
871 | off_t pos = (off_t)IPC_GET_ARG3(*request); |
||
872 | fat_node_t *nodep = (fat_node_t *)fat_node_get(dev_handle, index); |
||
4153 | mejdrech | 873 | fat_bs_t *bs; |
874 | uint16_t bps; |
||
3308 | jermar | 875 | size_t bytes; |
3335 | jermar | 876 | block_t *b; |
3308 | jermar | 877 | |
3307 | jermar | 878 | if (!nodep) { |
879 | ipc_answer_0(rid, ENOENT); |
||
880 | return; |
||
881 | } |
||
882 | |||
883 | ipc_callid_t callid; |
||
884 | size_t len; |
||
3314 | jermar | 885 | if (!ipc_data_read_receive(&callid, &len)) { |
3307 | jermar | 886 | fat_node_put(nodep); |
887 | ipc_answer_0(callid, EINVAL); |
||
888 | ipc_answer_0(rid, EINVAL); |
||
889 | return; |
||
890 | } |
||
891 | |||
4153 | mejdrech | 892 | bs = block_bb_get(dev_handle); |
893 | bps = uint16_t_le2host(bs->bps); |
||
894 | |||
3307 | jermar | 895 | if (nodep->type == FAT_FILE) { |
3335 | jermar | 896 | /* |
897 | * Our strategy for regular file reads is to read one block at |
||
898 | * most and make use of the possibility to return less data than |
||
899 | * requested. This keeps the code very simple. |
||
900 | */ |
||
4153 | mejdrech | 901 | if (pos >= nodep->size) { |
902 | /* reading beyond the EOF */ |
||
903 | bytes = 0; |
||
904 | (void) ipc_data_read_finalize(callid, NULL, 0); |
||
905 | } else { |
||
906 | bytes = min(len, bps - pos % bps); |
||
907 | bytes = min(bytes, nodep->size - pos); |
||
908 | b = fat_block_get(bs, nodep, pos / bps, |
||
909 | BLOCK_FLAGS_NONE); |
||
910 | (void) ipc_data_read_finalize(callid, b->data + pos % bps, |
||
911 | bytes); |
||
912 | block_put(b); |
||
913 | } |
||
3307 | jermar | 914 | } else { |
3335 | jermar | 915 | unsigned bnum; |
916 | off_t spos = pos; |
||
917 | char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1]; |
||
918 | fat_dentry_t *d; |
||
919 | |||
3307 | jermar | 920 | assert(nodep->type == FAT_DIRECTORY); |
3335 | jermar | 921 | assert(nodep->size % bps == 0); |
922 | assert(bps % sizeof(fat_dentry_t) == 0); |
||
923 | |||
924 | /* |
||
925 | * Our strategy for readdir() is to use the position pointer as |
||
926 | * an index into the array of all dentries. On entry, it points |
||
927 | * to the first unread dentry. If we skip any dentries, we bump |
||
928 | * the position pointer accordingly. |
||
929 | */ |
||
930 | bnum = (pos * sizeof(fat_dentry_t)) / bps; |
||
931 | while (bnum < nodep->size / bps) { |
||
932 | off_t o; |
||
933 | |||
4153 | mejdrech | 934 | b = fat_block_get(bs, nodep, bnum, BLOCK_FLAGS_NONE); |
3335 | jermar | 935 | for (o = pos % (bps / sizeof(fat_dentry_t)); |
936 | o < bps / sizeof(fat_dentry_t); |
||
937 | o++, pos++) { |
||
938 | d = ((fat_dentry_t *)b->data) + o; |
||
939 | switch (fat_classify_dentry(d)) { |
||
940 | case FAT_DENTRY_SKIP: |
||
4153 | mejdrech | 941 | case FAT_DENTRY_FREE: |
3335 | jermar | 942 | continue; |
943 | case FAT_DENTRY_LAST: |
||
944 | block_put(b); |
||
945 | goto miss; |
||
946 | default: |
||
947 | case FAT_DENTRY_VALID: |
||
4153 | mejdrech | 948 | fat_dentry_name_get(d, name); |
3335 | jermar | 949 | block_put(b); |
950 | goto hit; |
||
951 | } |
||
952 | } |
||
953 | block_put(b); |
||
954 | bnum++; |
||
955 | } |
||
956 | miss: |
||
3307 | jermar | 957 | fat_node_put(nodep); |
3335 | jermar | 958 | ipc_answer_0(callid, ENOENT); |
959 | ipc_answer_1(rid, ENOENT, 0); |
||
3307 | jermar | 960 | return; |
3335 | jermar | 961 | hit: |
4327 | mejdrech | 962 | (void) ipc_data_read_finalize(callid, name, str_size(name) + 1); |
3335 | jermar | 963 | bytes = (pos - spos) + 1; |
3307 | jermar | 964 | } |
965 | |||
966 | fat_node_put(nodep); |
||
3308 | jermar | 967 | ipc_answer_1(rid, EOK, (ipcarg_t)bytes); |
3307 | jermar | 968 | } |
969 | |||
4153 | mejdrech | 970 | void fat_write(ipc_callid_t rid, ipc_call_t *request) |
971 | { |
||
972 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
||
973 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); |
||
974 | off_t pos = (off_t)IPC_GET_ARG3(*request); |
||
975 | fat_node_t *nodep = (fat_node_t *)fat_node_get(dev_handle, index); |
||
976 | fat_bs_t *bs; |
||
977 | size_t bytes; |
||
978 | block_t *b; |
||
979 | uint16_t bps; |
||
980 | unsigned spc; |
||
981 | unsigned bpc; /* bytes per cluster */ |
||
982 | off_t boundary; |
||
983 | int flags = BLOCK_FLAGS_NONE; |
||
984 | |||
985 | if (!nodep) { |
||
986 | ipc_answer_0(rid, ENOENT); |
||
987 | return; |
||
988 | } |
||
989 | |||
990 | ipc_callid_t callid; |
||
991 | size_t len; |
||
992 | if (!ipc_data_write_receive(&callid, &len)) { |
||
993 | fat_node_put(nodep); |
||
994 | ipc_answer_0(callid, EINVAL); |
||
995 | ipc_answer_0(rid, EINVAL); |
||
996 | return; |
||
997 | } |
||
998 | |||
999 | bs = block_bb_get(dev_handle); |
||
1000 | bps = uint16_t_le2host(bs->bps); |
||
1001 | spc = bs->spc; |
||
1002 | bpc = bps * spc; |
||
1003 | |||
1004 | /* |
||
1005 | * In all scenarios, we will attempt to write out only one block worth |
||
1006 | * of data at maximum. There might be some more efficient approaches, |
||
1007 | * but this one greatly simplifies fat_write(). Note that we can afford |
||
1008 | * to do this because the client must be ready to handle the return |
||
1009 | * value signalizing a smaller number of bytes written. |
||
1010 | */ |
||
1011 | bytes = min(len, bps - pos % bps); |
||
1012 | if (bytes == bps) |
||
1013 | flags |= BLOCK_FLAGS_NOREAD; |
||
1014 | |||
1015 | boundary = ROUND_UP(nodep->size, bpc); |
||
1016 | if (pos < boundary) { |
||
1017 | /* |
||
1018 | * This is the easier case - we are either overwriting already |
||
1019 | * existing contents or writing behind the EOF, but still within |
||
1020 | * the limits of the last cluster. The node size may grow to the |
||
1021 | * next block size boundary. |
||
1022 | */ |
||
1023 | fat_fill_gap(bs, nodep, FAT_CLST_RES0, pos); |
||
1024 | b = fat_block_get(bs, nodep, pos / bps, flags); |
||
1025 | (void) ipc_data_write_finalize(callid, b->data + pos % bps, |
||
1026 | bytes); |
||
1027 | b->dirty = true; /* need to sync block */ |
||
1028 | block_put(b); |
||
1029 | if (pos + bytes > nodep->size) { |
||
1030 | nodep->size = pos + bytes; |
||
1031 | nodep->dirty = true; /* need to sync node */ |
||
1032 | } |
||
1033 | ipc_answer_2(rid, EOK, bytes, nodep->size); |
||
1034 | fat_node_put(nodep); |
||
1035 | return; |
||
1036 | } else { |
||
1037 | /* |
||
1038 | * This is the more difficult case. We must allocate new |
||
1039 | * clusters for the node and zero them out. |
||
1040 | */ |
||
1041 | int status; |
||
1042 | unsigned nclsts; |
||
1043 | fat_cluster_t mcl, lcl; |
||
1044 | |||
1045 | nclsts = (ROUND_UP(pos + bytes, bpc) - boundary) / bpc; |
||
1046 | /* create an independent chain of nclsts clusters in all FATs */ |
||
1047 | status = fat_alloc_clusters(bs, dev_handle, nclsts, &mcl, &lcl); |
||
1048 | if (status != EOK) { |
||
1049 | /* could not allocate a chain of nclsts clusters */ |
||
1050 | fat_node_put(nodep); |
||
1051 | ipc_answer_0(callid, status); |
||
1052 | ipc_answer_0(rid, status); |
||
1053 | return; |
||
1054 | } |
||
1055 | /* zero fill any gaps */ |
||
1056 | fat_fill_gap(bs, nodep, mcl, pos); |
||
1057 | b = _fat_block_get(bs, dev_handle, lcl, (pos / bps) % spc, |
||
1058 | flags); |
||
1059 | (void) ipc_data_write_finalize(callid, b->data + pos % bps, |
||
1060 | bytes); |
||
1061 | b->dirty = true; /* need to sync block */ |
||
1062 | block_put(b); |
||
1063 | /* |
||
1064 | * Append the cluster chain starting in mcl to the end of the |
||
1065 | * node's cluster chain. |
||
1066 | */ |
||
1067 | fat_append_clusters(bs, nodep, mcl); |
||
1068 | nodep->size = pos + bytes; |
||
1069 | nodep->dirty = true; /* need to sync node */ |
||
1070 | ipc_answer_2(rid, EOK, bytes, nodep->size); |
||
1071 | fat_node_put(nodep); |
||
1072 | return; |
||
1073 | } |
||
1074 | } |
||
1075 | |||
1076 | void fat_truncate(ipc_callid_t rid, ipc_call_t *request) |
||
1077 | { |
||
1078 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
||
1079 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); |
||
1080 | size_t size = (off_t)IPC_GET_ARG3(*request); |
||
1081 | fat_node_t *nodep = (fat_node_t *)fat_node_get(dev_handle, index); |
||
1082 | fat_bs_t *bs; |
||
1083 | uint16_t bps; |
||
1084 | uint8_t spc; |
||
1085 | unsigned bpc; /* bytes per cluster */ |
||
1086 | int rc; |
||
1087 | |||
1088 | if (!nodep) { |
||
1089 | ipc_answer_0(rid, ENOENT); |
||
1090 | return; |
||
1091 | } |
||
1092 | |||
1093 | bs = block_bb_get(dev_handle); |
||
1094 | bps = uint16_t_le2host(bs->bps); |
||
1095 | spc = bs->spc; |
||
1096 | bpc = bps * spc; |
||
1097 | |||
1098 | if (nodep->size == size) { |
||
1099 | rc = EOK; |
||
1100 | } else if (nodep->size < size) { |
||
1101 | /* |
||
1102 | * The standard says we have the freedom to grow the node. |
||
1103 | * For now, we simply return an error. |
||
1104 | */ |
||
1105 | rc = EINVAL; |
||
1106 | } else if (ROUND_UP(nodep->size, bpc) == ROUND_UP(size, bpc)) { |
||
1107 | /* |
||
1108 | * The node will be shrunk, but no clusters will be deallocated. |
||
1109 | */ |
||
1110 | nodep->size = size; |
||
1111 | nodep->dirty = true; /* need to sync node */ |
||
1112 | rc = EOK; |
||
1113 | } else { |
||
1114 | /* |
||
1115 | * The node will be shrunk, clusters will be deallocated. |
||
1116 | */ |
||
1117 | if (size == 0) { |
||
1118 | fat_chop_clusters(bs, nodep, FAT_CLST_RES0); |
||
1119 | } else { |
||
1120 | fat_cluster_t lastc; |
||
1121 | (void) fat_cluster_walk(bs, dev_handle, nodep->firstc, |
||
1122 | &lastc, (size - 1) / bpc); |
||
1123 | fat_chop_clusters(bs, nodep, lastc); |
||
1124 | } |
||
1125 | nodep->size = size; |
||
1126 | nodep->dirty = true; /* need to sync node */ |
||
1127 | rc = EOK; |
||
1128 | } |
||
1129 | fat_node_put(nodep); |
||
1130 | ipc_answer_0(rid, rc); |
||
1131 | return; |
||
1132 | } |
||
1133 | |||
1134 | void fat_destroy(ipc_callid_t rid, ipc_call_t *request) |
||
1135 | { |
||
1136 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
||
1137 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); |
||
1138 | int rc; |
||
1139 | |||
1140 | fat_node_t *nodep = fat_node_get(dev_handle, index); |
||
1141 | if (!nodep) { |
||
1142 | ipc_answer_0(rid, ENOENT); |
||
1143 | return; |
||
1144 | } |
||
1145 | |||
1146 | rc = fat_destroy_node(nodep); |
||
1147 | ipc_answer_0(rid, rc); |
||
1148 | } |
||
1149 | |||
2627 | jermar | 1150 | /** |
1151 | * @} |
||
1152 | */ |