source: mainline/uspace/srv/fs/fat/fat_ops.c@ 8786aa5

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 8786aa5 was 1313ee9, checked in by Martin Decky <martin@…>, 16 years ago

introduce device namespaces

  • add support for explicit open in libfs (needed by devfs, but also possibly for other filesystems which need to track some stateful information)
  • extend libfs to be more generic, make proper adjustments to libc, tmpfs and fat
  • various updates to make use of the device namespaces
  • code cleanup
  • Property mode set to 100644
File size: 34.7 KB
Line 
1/*
2 * Copyright (c) 2008 Jakub 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"
39#include "fat_dentry.h"
40#include "fat_fat.h"
41#include "../../vfs/vfs.h"
42#include <libfs.h>
43#include <libblock.h>
44#include <ipc/ipc.h>
45#include <ipc/services.h>
46#include <ipc/devmap.h>
47#include <async.h>
48#include <errno.h>
49#include <string.h>
50#include <byteorder.h>
51#include <adt/hash_table.h>
52#include <adt/list.h>
53#include <assert.h>
54#include <fibril_synch.h>
55#include <sys/mman.h>
56#include <align.h>
57
58#define FAT_NODE(node) ((node) ? (fat_node_t *) (node)->data : NULL)
59#define FS_NODE(node) ((node) ? (node)->bp : NULL)
60
61/** Mutex protecting the list of cached free FAT nodes. */
62static FIBRIL_MUTEX_INITIALIZE(ffn_mutex);
63
64/** List of cached free FAT nodes. */
65static LIST_INITIALIZE(ffn_head);
66
67/*
68 * Forward declarations of FAT libfs operations.
69 */
70static int fat_root_get(fs_node_t **, dev_handle_t);
71static int fat_match(fs_node_t **, fs_node_t *, const char *);
72static int fat_node_get(fs_node_t **, dev_handle_t, fs_index_t);
73static int fat_node_open(fs_node_t *);
74static int fat_node_put(fs_node_t *);
75static int fat_create_node(fs_node_t **, dev_handle_t, int);
76static int fat_destroy_node(fs_node_t *);
77static int fat_link(fs_node_t *, fs_node_t *, const char *);
78static int fat_unlink(fs_node_t *, fs_node_t *, const char *);
79static int fat_has_children(bool *, fs_node_t *);
80static fs_index_t fat_index_get(fs_node_t *);
81static size_t fat_size_get(fs_node_t *);
82static unsigned fat_lnkcnt_get(fs_node_t *);
83static char fat_plb_get_char(unsigned);
84static bool fat_is_directory(fs_node_t *);
85static bool fat_is_file(fs_node_t *node);
86static dev_handle_t fat_device_get(fs_node_t *node);
87
88/*
89 * Helper functions.
90 */
91static void fat_node_initialize(fat_node_t *node)
92{
93 fibril_mutex_initialize(&node->lock);
94 node->bp = NULL;
95 node->idx = NULL;
96 node->type = 0;
97 link_initialize(&node->ffn_link);
98 node->size = 0;
99 node->lnkcnt = 0;
100 node->refcnt = 0;
101 node->dirty = false;
102}
103
104static int fat_node_sync(fat_node_t *node)
105{
106 block_t *b;
107 fat_bs_t *bs;
108 fat_dentry_t *d;
109 uint16_t bps;
110 unsigned dps;
111 int rc;
112
113 assert(node->dirty);
114
115 bs = block_bb_get(node->idx->dev_handle);
116 bps = uint16_t_le2host(bs->bps);
117 dps = bps / sizeof(fat_dentry_t);
118
119 /* Read the block that contains the dentry of interest. */
120 rc = _fat_block_get(&b, bs, node->idx->dev_handle, node->idx->pfc,
121 (node->idx->pdi * sizeof(fat_dentry_t)) / bps, BLOCK_FLAGS_NONE);
122 if (rc != EOK)
123 return rc;
124
125 d = ((fat_dentry_t *)b->data) + (node->idx->pdi % dps);
126
127 d->firstc = host2uint16_t_le(node->firstc);
128 if (node->type == FAT_FILE) {
129 d->size = host2uint32_t_le(node->size);
130 } else if (node->type == FAT_DIRECTORY) {
131 d->attr = FAT_ATTR_SUBDIR;
132 }
133
134 /* TODO: update other fields? (e.g time fields) */
135
136 b->dirty = true; /* need to sync block */
137 rc = block_put(b);
138 return rc;
139}
140
141static int fat_node_get_new(fat_node_t **nodepp)
142{
143 fs_node_t *fn;
144 fat_node_t *nodep;
145 int rc;
146
147 fibril_mutex_lock(&ffn_mutex);
148 if (!list_empty(&ffn_head)) {
149 /* Try to use a cached free node structure. */
150 fat_idx_t *idxp_tmp;
151 nodep = list_get_instance(ffn_head.next, fat_node_t, ffn_link);
152 if (!fibril_mutex_trylock(&nodep->lock))
153 goto skip_cache;
154 idxp_tmp = nodep->idx;
155 if (!fibril_mutex_trylock(&idxp_tmp->lock)) {
156 fibril_mutex_unlock(&nodep->lock);
157 goto skip_cache;
158 }
159 list_remove(&nodep->ffn_link);
160 fibril_mutex_unlock(&ffn_mutex);
161 if (nodep->dirty) {
162 rc = fat_node_sync(nodep);
163 if (rc != EOK) {
164 idxp_tmp->nodep = NULL;
165 fibril_mutex_unlock(&nodep->lock);
166 fibril_mutex_unlock(&idxp_tmp->lock);
167 free(nodep->bp);
168 free(nodep);
169 return rc;
170 }
171 }
172 idxp_tmp->nodep = NULL;
173 fibril_mutex_unlock(&nodep->lock);
174 fibril_mutex_unlock(&idxp_tmp->lock);
175 fn = FS_NODE(nodep);
176 } else {
177skip_cache:
178 /* Try to allocate a new node structure. */
179 fibril_mutex_unlock(&ffn_mutex);
180 fn = (fs_node_t *)malloc(sizeof(fs_node_t));
181 if (!fn)
182 return ENOMEM;
183 nodep = (fat_node_t *)malloc(sizeof(fat_node_t));
184 if (!nodep) {
185 free(fn);
186 return ENOMEM;
187 }
188 }
189 fat_node_initialize(nodep);
190 fs_node_initialize(fn);
191 fn->data = nodep;
192 nodep->bp = fn;
193
194 *nodepp = nodep;
195 return EOK;
196}
197
198/** Internal version of fat_node_get().
199 *
200 * @param idxp Locked index structure.
201 */
202static int fat_node_get_core(fat_node_t **nodepp, fat_idx_t *idxp)
203{
204 block_t *b;
205 fat_bs_t *bs;
206 fat_dentry_t *d;
207 fat_node_t *nodep = NULL;
208 unsigned bps;
209 unsigned spc;
210 unsigned dps;
211 int rc;
212
213 if (idxp->nodep) {
214 /*
215 * We are lucky.
216 * The node is already instantiated in memory.
217 */
218 fibril_mutex_lock(&idxp->nodep->lock);
219 if (!idxp->nodep->refcnt++) {
220 fibril_mutex_lock(&ffn_mutex);
221 list_remove(&idxp->nodep->ffn_link);
222 fibril_mutex_unlock(&ffn_mutex);
223 }
224 fibril_mutex_unlock(&idxp->nodep->lock);
225 *nodepp = idxp->nodep;
226 return EOK;
227 }
228
229 /*
230 * We must instantiate the node from the file system.
231 */
232
233 assert(idxp->pfc);
234
235 rc = fat_node_get_new(&nodep);
236 if (rc != EOK)
237 return rc;
238
239 bs = block_bb_get(idxp->dev_handle);
240 bps = uint16_t_le2host(bs->bps);
241 spc = bs->spc;
242 dps = bps / sizeof(fat_dentry_t);
243
244 /* Read the block that contains the dentry of interest. */
245 rc = _fat_block_get(&b, bs, idxp->dev_handle, idxp->pfc,
246 (idxp->pdi * sizeof(fat_dentry_t)) / bps, BLOCK_FLAGS_NONE);
247 if (rc != EOK) {
248 (void) fat_node_put(FS_NODE(nodep));
249 return rc;
250 }
251
252 d = ((fat_dentry_t *)b->data) + (idxp->pdi % dps);
253 if (d->attr & FAT_ATTR_SUBDIR) {
254 /*
255 * The only directory which does not have this bit set is the
256 * root directory itself. The root directory node is handled
257 * and initialized elsewhere.
258 */
259 nodep->type = FAT_DIRECTORY;
260 /*
261 * Unfortunately, the 'size' field of the FAT dentry is not
262 * defined for the directory entry type. We must determine the
263 * size of the directory by walking the FAT.
264 */
265 uint16_t clusters;
266 rc = fat_clusters_get(&clusters, bs, idxp->dev_handle,
267 uint16_t_le2host(d->firstc));
268 if (rc != EOK) {
269 (void) fat_node_put(FS_NODE(nodep));
270 return rc;
271 }
272 nodep->size = bps * spc * clusters;
273 } else {
274 nodep->type = FAT_FILE;
275 nodep->size = uint32_t_le2host(d->size);
276 }
277 nodep->firstc = uint16_t_le2host(d->firstc);
278 nodep->lnkcnt = 1;
279 nodep->refcnt = 1;
280
281 rc = block_put(b);
282 if (rc != EOK) {
283 (void) fat_node_put(FS_NODE(nodep));
284 return rc;
285 }
286
287 /* Link the idx structure with the node structure. */
288 nodep->idx = idxp;
289 idxp->nodep = nodep;
290
291 *nodepp = nodep;
292 return EOK;
293}
294
295/*
296 * FAT libfs operations.
297 */
298
299int fat_root_get(fs_node_t **rfn, dev_handle_t dev_handle)
300{
301 return fat_node_get(rfn, dev_handle, 0);
302}
303
304int fat_match(fs_node_t **rfn, fs_node_t *pfn, const char *component)
305{
306 fat_bs_t *bs;
307 fat_node_t *parentp = FAT_NODE(pfn);
308 char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1];
309 unsigned i, j;
310 unsigned bps; /* bytes per sector */
311 unsigned dps; /* dentries per sector */
312 unsigned blocks;
313 fat_dentry_t *d;
314 block_t *b;
315 int rc;
316
317 fibril_mutex_lock(&parentp->idx->lock);
318 bs = block_bb_get(parentp->idx->dev_handle);
319 bps = uint16_t_le2host(bs->bps);
320 dps = bps / sizeof(fat_dentry_t);
321 blocks = parentp->size / bps;
322 for (i = 0; i < blocks; i++) {
323 rc = fat_block_get(&b, bs, parentp, i, BLOCK_FLAGS_NONE);
324 if (rc != EOK) {
325 fibril_mutex_unlock(&parentp->idx->lock);
326 return rc;
327 }
328 for (j = 0; j < dps; j++) {
329 d = ((fat_dentry_t *)b->data) + j;
330 switch (fat_classify_dentry(d)) {
331 case FAT_DENTRY_SKIP:
332 case FAT_DENTRY_FREE:
333 continue;
334 case FAT_DENTRY_LAST:
335 /* miss */
336 rc = block_put(b);
337 fibril_mutex_unlock(&parentp->idx->lock);
338 *rfn = NULL;
339 return rc;
340 default:
341 case FAT_DENTRY_VALID:
342 fat_dentry_name_get(d, name);
343 break;
344 }
345 if (fat_dentry_namecmp(name, component) == 0) {
346 /* hit */
347 fat_node_t *nodep;
348 /*
349 * Assume tree hierarchy for locking. We
350 * already have the parent and now we are going
351 * to lock the child. Never lock in the oposite
352 * order.
353 */
354 fat_idx_t *idx = fat_idx_get_by_pos(
355 parentp->idx->dev_handle, parentp->firstc,
356 i * dps + j);
357 fibril_mutex_unlock(&parentp->idx->lock);
358 if (!idx) {
359 /*
360 * Can happen if memory is low or if we
361 * run out of 32-bit indices.
362 */
363 rc = block_put(b);
364 return (rc == EOK) ? ENOMEM : rc;
365 }
366 rc = fat_node_get_core(&nodep, idx);
367 fibril_mutex_unlock(&idx->lock);
368 if (rc != EOK) {
369 (void) block_put(b);
370 return rc;
371 }
372 *rfn = FS_NODE(nodep);
373 rc = block_put(b);
374 if (rc != EOK)
375 (void) fat_node_put(*rfn);
376 return rc;
377 }
378 }
379 rc = block_put(b);
380 if (rc != EOK) {
381 fibril_mutex_unlock(&parentp->idx->lock);
382 return rc;
383 }
384 }
385
386 fibril_mutex_unlock(&parentp->idx->lock);
387 *rfn = NULL;
388 return EOK;
389}
390
391/** Instantiate a FAT in-core node. */
392int fat_node_get(fs_node_t **rfn, dev_handle_t dev_handle, fs_index_t index)
393{
394 fat_node_t *nodep;
395 fat_idx_t *idxp;
396 int rc;
397
398 idxp = fat_idx_get_by_index(dev_handle, index);
399 if (!idxp) {
400 *rfn = NULL;
401 return EOK;
402 }
403 /* idxp->lock held */
404 rc = fat_node_get_core(&nodep, idxp);
405 fibril_mutex_unlock(&idxp->lock);
406 if (rc == EOK)
407 *rfn = FS_NODE(nodep);
408 return rc;
409}
410
411int fat_node_open(fs_node_t *fn)
412{
413 /*
414 * Opening a file is stateless, nothing
415 * to be done here.
416 */
417 return EOK;
418}
419
420int fat_node_put(fs_node_t *fn)
421{
422 fat_node_t *nodep = FAT_NODE(fn);
423 bool destroy = false;
424
425 fibril_mutex_lock(&nodep->lock);
426 if (!--nodep->refcnt) {
427 if (nodep->idx) {
428 fibril_mutex_lock(&ffn_mutex);
429 list_append(&nodep->ffn_link, &ffn_head);
430 fibril_mutex_unlock(&ffn_mutex);
431 } else {
432 /*
433 * The node does not have any index structure associated
434 * with itself. This can only mean that we are releasing
435 * the node after a failed attempt to allocate the index
436 * structure for it.
437 */
438 destroy = true;
439 }
440 }
441 fibril_mutex_unlock(&nodep->lock);
442 if (destroy) {
443 free(nodep->bp);
444 free(nodep);
445 }
446 return EOK;
447}
448
449int fat_create_node(fs_node_t **rfn, dev_handle_t dev_handle, int flags)
450{
451 fat_idx_t *idxp;
452 fat_node_t *nodep;
453 fat_bs_t *bs;
454 fat_cluster_t mcl, lcl;
455 uint16_t bps;
456 int rc;
457
458 bs = block_bb_get(dev_handle);
459 bps = uint16_t_le2host(bs->bps);
460 if (flags & L_DIRECTORY) {
461 /* allocate a cluster */
462 rc = fat_alloc_clusters(bs, dev_handle, 1, &mcl, &lcl);
463 if (rc != EOK)
464 return rc;
465 /* populate the new cluster with unused dentries */
466 rc = fat_zero_cluster(bs, dev_handle, mcl);
467 if (rc != EOK) {
468 (void) fat_free_clusters(bs, dev_handle, mcl);
469 return rc;
470 }
471 }
472
473 rc = fat_node_get_new(&nodep);
474 if (rc != EOK) {
475 (void) fat_free_clusters(bs, dev_handle, mcl);
476 return rc;
477 }
478 rc = fat_idx_get_new(&idxp, dev_handle);
479 if (rc != EOK) {
480 (void) fat_free_clusters(bs, dev_handle, mcl);
481 (void) fat_node_put(FS_NODE(nodep));
482 return rc;
483 }
484 /* idxp->lock held */
485 if (flags & L_DIRECTORY) {
486 nodep->type = FAT_DIRECTORY;
487 nodep->firstc = mcl;
488 nodep->size = bps * bs->spc;
489 } else {
490 nodep->type = FAT_FILE;
491 nodep->firstc = FAT_CLST_RES0;
492 nodep->size = 0;
493 }
494 nodep->lnkcnt = 0; /* not linked anywhere */
495 nodep->refcnt = 1;
496 nodep->dirty = true;
497
498 nodep->idx = idxp;
499 idxp->nodep = nodep;
500
501 fibril_mutex_unlock(&idxp->lock);
502 *rfn = FS_NODE(nodep);
503 return EOK;
504}
505
506int fat_destroy_node(fs_node_t *fn)
507{
508 fat_node_t *nodep = FAT_NODE(fn);
509 fat_bs_t *bs;
510 bool has_children;
511 int rc;
512
513 /*
514 * The node is not reachable from the file system. This means that the
515 * link count should be zero and that the index structure cannot be
516 * found in the position hash. Obviously, we don't need to lock the node
517 * nor its index structure.
518 */
519 assert(nodep->lnkcnt == 0);
520
521 /*
522 * The node may not have any children.
523 */
524 rc = fat_has_children(&has_children, fn);
525 if (rc != EOK)
526 return rc;
527 assert(!has_children);
528
529 bs = block_bb_get(nodep->idx->dev_handle);
530 if (nodep->firstc != FAT_CLST_RES0) {
531 assert(nodep->size);
532 /* Free all clusters allocated to the node. */
533 rc = fat_free_clusters(bs, nodep->idx->dev_handle,
534 nodep->firstc);
535 }
536
537 fat_idx_destroy(nodep->idx);
538 free(nodep->bp);
539 free(nodep);
540 return rc;
541}
542
543int fat_link(fs_node_t *pfn, fs_node_t *cfn, const char *name)
544{
545 fat_node_t *parentp = FAT_NODE(pfn);
546 fat_node_t *childp = FAT_NODE(cfn);
547 fat_dentry_t *d;
548 fat_bs_t *bs;
549 block_t *b;
550 unsigned i, j;
551 uint16_t bps;
552 unsigned dps;
553 unsigned blocks;
554 fat_cluster_t mcl, lcl;
555 int rc;
556
557 fibril_mutex_lock(&childp->lock);
558 if (childp->lnkcnt == 1) {
559 /*
560 * On FAT, we don't support multiple hard links.
561 */
562 fibril_mutex_unlock(&childp->lock);
563 return EMLINK;
564 }
565 assert(childp->lnkcnt == 0);
566 fibril_mutex_unlock(&childp->lock);
567
568 if (!fat_dentry_name_verify(name)) {
569 /*
570 * Attempt to create unsupported name.
571 */
572 return ENOTSUP;
573 }
574
575 /*
576 * Get us an unused parent node's dentry or grow the parent and allocate
577 * a new one.
578 */
579
580 fibril_mutex_lock(&parentp->idx->lock);
581 bs = block_bb_get(parentp->idx->dev_handle);
582 bps = uint16_t_le2host(bs->bps);
583 dps = bps / sizeof(fat_dentry_t);
584
585 blocks = parentp->size / bps;
586
587 for (i = 0; i < blocks; i++) {
588 rc = fat_block_get(&b, bs, parentp, i, BLOCK_FLAGS_NONE);
589 if (rc != EOK) {
590 fibril_mutex_unlock(&parentp->idx->lock);
591 return rc;
592 }
593 for (j = 0; j < dps; j++) {
594 d = ((fat_dentry_t *)b->data) + j;
595 switch (fat_classify_dentry(d)) {
596 case FAT_DENTRY_SKIP:
597 case FAT_DENTRY_VALID:
598 /* skipping used and meta entries */
599 continue;
600 case FAT_DENTRY_FREE:
601 case FAT_DENTRY_LAST:
602 /* found an empty slot */
603 goto hit;
604 }
605 }
606 rc = block_put(b);
607 if (rc != EOK) {
608 fibril_mutex_unlock(&parentp->idx->lock);
609 return rc;
610 }
611 }
612 j = 0;
613
614 /*
615 * We need to grow the parent in order to create a new unused dentry.
616 */
617 if (parentp->firstc == FAT_CLST_ROOT) {
618 /* Can't grow the root directory. */
619 fibril_mutex_unlock(&parentp->idx->lock);
620 return ENOSPC;
621 }
622 rc = fat_alloc_clusters(bs, parentp->idx->dev_handle, 1, &mcl, &lcl);
623 if (rc != EOK) {
624 fibril_mutex_unlock(&parentp->idx->lock);
625 return rc;
626 }
627 rc = fat_zero_cluster(bs, parentp->idx->dev_handle, mcl);
628 if (rc != EOK) {
629 (void) fat_free_clusters(bs, parentp->idx->dev_handle, mcl);
630 fibril_mutex_unlock(&parentp->idx->lock);
631 return rc;
632 }
633 rc = fat_append_clusters(bs, parentp, mcl);
634 if (rc != EOK) {
635 (void) fat_free_clusters(bs, parentp->idx->dev_handle, mcl);
636 fibril_mutex_unlock(&parentp->idx->lock);
637 return rc;
638 }
639 parentp->size += bps * bs->spc;
640 parentp->dirty = true; /* need to sync node */
641 rc = fat_block_get(&b, bs, parentp, i, BLOCK_FLAGS_NONE);
642 if (rc != EOK) {
643 fibril_mutex_unlock(&parentp->idx->lock);
644 return rc;
645 }
646 d = (fat_dentry_t *)b->data;
647
648hit:
649 /*
650 * At this point we only establish the link between the parent and the
651 * child. The dentry, except of the name and the extension, will remain
652 * uninitialized until the corresponding node is synced. Thus the valid
653 * dentry data is kept in the child node structure.
654 */
655 memset(d, 0, sizeof(fat_dentry_t));
656 fat_dentry_name_set(d, name);
657 b->dirty = true; /* need to sync block */
658 rc = block_put(b);
659 fibril_mutex_unlock(&parentp->idx->lock);
660 if (rc != EOK)
661 return rc;
662
663 fibril_mutex_lock(&childp->idx->lock);
664
665 /*
666 * If possible, create the Sub-directory Identifier Entry and the
667 * Sub-directory Parent Pointer Entry (i.e. "." and ".."). These entries
668 * are not mandatory according to Standard ECMA-107 and HelenOS VFS does
669 * not use them anyway, so this is rather a sign of our good will.
670 */
671 rc = fat_block_get(&b, bs, childp, 0, BLOCK_FLAGS_NONE);
672 if (rc != EOK) {
673 /*
674 * Rather than returning an error, simply skip the creation of
675 * these two entries.
676 */
677 goto skip_dots;
678 }
679 d = (fat_dentry_t *)b->data;
680 if (fat_classify_dentry(d) == FAT_DENTRY_LAST ||
681 str_cmp(d->name, FAT_NAME_DOT) == 0) {
682 memset(d, 0, sizeof(fat_dentry_t));
683 str_cpy(d->name, 8, FAT_NAME_DOT);
684 str_cpy(d->ext, 3, FAT_EXT_PAD);
685 d->attr = FAT_ATTR_SUBDIR;
686 d->firstc = host2uint16_t_le(childp->firstc);
687 /* TODO: initialize also the date/time members. */
688 }
689 d++;
690 if (fat_classify_dentry(d) == FAT_DENTRY_LAST ||
691 str_cmp(d->name, FAT_NAME_DOT_DOT) == 0) {
692 memset(d, 0, sizeof(fat_dentry_t));
693 str_cpy(d->name, 8, FAT_NAME_DOT_DOT);
694 str_cpy(d->ext, 3, FAT_EXT_PAD);
695 d->attr = FAT_ATTR_SUBDIR;
696 d->firstc = (parentp->firstc == FAT_CLST_ROOT) ?
697 host2uint16_t_le(FAT_CLST_RES0) :
698 host2uint16_t_le(parentp->firstc);
699 /* TODO: initialize also the date/time members. */
700 }
701 b->dirty = true; /* need to sync block */
702 /*
703 * Ignore the return value as we would have fallen through on error
704 * anyway.
705 */
706 (void) block_put(b);
707skip_dots:
708
709 childp->idx->pfc = parentp->firstc;
710 childp->idx->pdi = i * dps + j;
711 fibril_mutex_unlock(&childp->idx->lock);
712
713 fibril_mutex_lock(&childp->lock);
714 childp->lnkcnt = 1;
715 childp->dirty = true; /* need to sync node */
716 fibril_mutex_unlock(&childp->lock);
717
718 /*
719 * Hash in the index structure into the position hash.
720 */
721 fat_idx_hashin(childp->idx);
722
723 return EOK;
724}
725
726int fat_unlink(fs_node_t *pfn, fs_node_t *cfn, const char *nm)
727{
728 fat_node_t *parentp = FAT_NODE(pfn);
729 fat_node_t *childp = FAT_NODE(cfn);
730 fat_bs_t *bs;
731 fat_dentry_t *d;
732 uint16_t bps;
733 block_t *b;
734 bool has_children;
735 int rc;
736
737 if (!parentp)
738 return EBUSY;
739
740 rc = fat_has_children(&has_children, cfn);
741 if (rc != EOK)
742 return rc;
743 if (has_children)
744 return ENOTEMPTY;
745
746 fibril_mutex_lock(&parentp->lock);
747 fibril_mutex_lock(&childp->lock);
748 assert(childp->lnkcnt == 1);
749 fibril_mutex_lock(&childp->idx->lock);
750 bs = block_bb_get(childp->idx->dev_handle);
751 bps = uint16_t_le2host(bs->bps);
752
753 rc = _fat_block_get(&b, bs, childp->idx->dev_handle, childp->idx->pfc,
754 (childp->idx->pdi * sizeof(fat_dentry_t)) / bps,
755 BLOCK_FLAGS_NONE);
756 if (rc != EOK)
757 goto error;
758 d = (fat_dentry_t *)b->data +
759 (childp->idx->pdi % (bps / sizeof(fat_dentry_t)));
760 /* mark the dentry as not-currently-used */
761 d->name[0] = FAT_DENTRY_ERASED;
762 b->dirty = true; /* need to sync block */
763 rc = block_put(b);
764 if (rc != EOK)
765 goto error;
766
767 /* remove the index structure from the position hash */
768 fat_idx_hashout(childp->idx);
769 /* clear position information */
770 childp->idx->pfc = FAT_CLST_RES0;
771 childp->idx->pdi = 0;
772 fibril_mutex_unlock(&childp->idx->lock);
773 childp->lnkcnt = 0;
774 childp->dirty = true;
775 fibril_mutex_unlock(&childp->lock);
776 fibril_mutex_unlock(&parentp->lock);
777
778 return EOK;
779
780error:
781 fibril_mutex_unlock(&parentp->idx->lock);
782 fibril_mutex_unlock(&childp->lock);
783 fibril_mutex_unlock(&childp->idx->lock);
784 return rc;
785}
786
787int fat_has_children(bool *has_children, fs_node_t *fn)
788{
789 fat_bs_t *bs;
790 fat_node_t *nodep = FAT_NODE(fn);
791 unsigned bps;
792 unsigned dps;
793 unsigned blocks;
794 block_t *b;
795 unsigned i, j;
796 int rc;
797
798 if (nodep->type != FAT_DIRECTORY) {
799 *has_children = false;
800 return EOK;
801 }
802
803 fibril_mutex_lock(&nodep->idx->lock);
804 bs = block_bb_get(nodep->idx->dev_handle);
805 bps = uint16_t_le2host(bs->bps);
806 dps = bps / sizeof(fat_dentry_t);
807
808 blocks = nodep->size / bps;
809
810 for (i = 0; i < blocks; i++) {
811 fat_dentry_t *d;
812
813 rc = fat_block_get(&b, bs, nodep, i, BLOCK_FLAGS_NONE);
814 if (rc != EOK) {
815 fibril_mutex_unlock(&nodep->idx->lock);
816 return rc;
817 }
818 for (j = 0; j < dps; j++) {
819 d = ((fat_dentry_t *)b->data) + j;
820 switch (fat_classify_dentry(d)) {
821 case FAT_DENTRY_SKIP:
822 case FAT_DENTRY_FREE:
823 continue;
824 case FAT_DENTRY_LAST:
825 rc = block_put(b);
826 fibril_mutex_unlock(&nodep->idx->lock);
827 *has_children = false;
828 return rc;
829 default:
830 case FAT_DENTRY_VALID:
831 rc = block_put(b);
832 fibril_mutex_unlock(&nodep->idx->lock);
833 *has_children = true;
834 return rc;
835 }
836 }
837 rc = block_put(b);
838 if (rc != EOK) {
839 fibril_mutex_unlock(&nodep->idx->lock);
840 return rc;
841 }
842 }
843
844 fibril_mutex_unlock(&nodep->idx->lock);
845 *has_children = false;
846 return EOK;
847}
848
849
850fs_index_t fat_index_get(fs_node_t *fn)
851{
852 return FAT_NODE(fn)->idx->index;
853}
854
855size_t fat_size_get(fs_node_t *fn)
856{
857 return FAT_NODE(fn)->size;
858}
859
860unsigned fat_lnkcnt_get(fs_node_t *fn)
861{
862 return FAT_NODE(fn)->lnkcnt;
863}
864
865char fat_plb_get_char(unsigned pos)
866{
867 return fat_reg.plb_ro[pos % PLB_SIZE];
868}
869
870bool fat_is_directory(fs_node_t *fn)
871{
872 return FAT_NODE(fn)->type == FAT_DIRECTORY;
873}
874
875bool fat_is_file(fs_node_t *fn)
876{
877 return FAT_NODE(fn)->type == FAT_FILE;
878}
879
880dev_handle_t fat_device_get(fs_node_t *node)
881{
882 return 0;
883}
884
885/** libfs operations */
886libfs_ops_t fat_libfs_ops = {
887 .root_get = fat_root_get,
888 .match = fat_match,
889 .node_get = fat_node_get,
890 .node_open = fat_node_open,
891 .node_put = fat_node_put,
892 .create = fat_create_node,
893 .destroy = fat_destroy_node,
894 .link = fat_link,
895 .unlink = fat_unlink,
896 .has_children = fat_has_children,
897 .index_get = fat_index_get,
898 .size_get = fat_size_get,
899 .lnkcnt_get = fat_lnkcnt_get,
900 .plb_get_char = fat_plb_get_char,
901 .is_directory = fat_is_directory,
902 .is_file = fat_is_file,
903 .device_get = fat_device_get
904};
905
906/*
907 * VFS operations.
908 */
909
910void fat_mounted(ipc_callid_t rid, ipc_call_t *request)
911{
912 dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request);
913 enum cache_mode cmode;
914 fat_bs_t *bs;
915 uint16_t bps;
916 uint16_t rde;
917 int rc;
918
919 /* accept the mount options */
920 ipc_callid_t callid;
921 size_t size;
922 if (!async_data_write_receive(&callid, &size)) {
923 ipc_answer_0(callid, EINVAL);
924 ipc_answer_0(rid, EINVAL);
925 return;
926 }
927 char *opts = malloc(size + 1);
928 if (!opts) {
929 ipc_answer_0(callid, ENOMEM);
930 ipc_answer_0(rid, ENOMEM);
931 return;
932 }
933 ipcarg_t retval = async_data_write_finalize(callid, opts, size);
934 if (retval != EOK) {
935 ipc_answer_0(rid, retval);
936 free(opts);
937 return;
938 }
939 opts[size] = '\0';
940
941 /* Check for option enabling write through. */
942 if (str_cmp(opts, "wtcache") == 0)
943 cmode = CACHE_MODE_WT;
944 else
945 cmode = CACHE_MODE_WB;
946
947 /* initialize libblock */
948 rc = block_init(dev_handle, BS_SIZE);
949 if (rc != EOK) {
950 ipc_answer_0(rid, rc);
951 return;
952 }
953
954 /* prepare the boot block */
955 rc = block_bb_read(dev_handle, BS_BLOCK);
956 if (rc != EOK) {
957 block_fini(dev_handle);
958 ipc_answer_0(rid, rc);
959 return;
960 }
961
962 /* get the buffer with the boot sector */
963 bs = block_bb_get(dev_handle);
964
965 /* Read the number of root directory entries. */
966 bps = uint16_t_le2host(bs->bps);
967 rde = uint16_t_le2host(bs->root_ent_max);
968
969 if (bps != BS_SIZE) {
970 block_fini(dev_handle);
971 ipc_answer_0(rid, ENOTSUP);
972 return;
973 }
974
975 /* Initialize the block cache */
976 rc = block_cache_init(dev_handle, bps, 0 /* XXX */, cmode);
977 if (rc != EOK) {
978 block_fini(dev_handle);
979 ipc_answer_0(rid, rc);
980 return;
981 }
982
983 rc = fat_idx_init_by_dev_handle(dev_handle);
984 if (rc != EOK) {
985 block_fini(dev_handle);
986 ipc_answer_0(rid, rc);
987 return;
988 }
989
990 /* Initialize the root node. */
991 fs_node_t *rfn = (fs_node_t *)malloc(sizeof(fs_node_t));
992 if (!rfn) {
993 block_fini(dev_handle);
994 fat_idx_fini_by_dev_handle(dev_handle);
995 ipc_answer_0(rid, ENOMEM);
996 return;
997 }
998 fs_node_initialize(rfn);
999 fat_node_t *rootp = (fat_node_t *)malloc(sizeof(fat_node_t));
1000 if (!rootp) {
1001 free(rfn);
1002 block_fini(dev_handle);
1003 fat_idx_fini_by_dev_handle(dev_handle);
1004 ipc_answer_0(rid, ENOMEM);
1005 return;
1006 }
1007 fat_node_initialize(rootp);
1008
1009 fat_idx_t *ridxp = fat_idx_get_by_pos(dev_handle, FAT_CLST_ROOTPAR, 0);
1010 if (!ridxp) {
1011 free(rfn);
1012 free(rootp);
1013 block_fini(dev_handle);
1014 fat_idx_fini_by_dev_handle(dev_handle);
1015 ipc_answer_0(rid, ENOMEM);
1016 return;
1017 }
1018 assert(ridxp->index == 0);
1019 /* ridxp->lock held */
1020
1021 rootp->type = FAT_DIRECTORY;
1022 rootp->firstc = FAT_CLST_ROOT;
1023 rootp->refcnt = 1;
1024 rootp->lnkcnt = 0; /* FS root is not linked */
1025 rootp->size = rde * sizeof(fat_dentry_t);
1026 rootp->idx = ridxp;
1027 ridxp->nodep = rootp;
1028 rootp->bp = rfn;
1029 rfn->data = rootp;
1030
1031 fibril_mutex_unlock(&ridxp->lock);
1032
1033 ipc_answer_3(rid, EOK, ridxp->index, rootp->size, rootp->lnkcnt);
1034}
1035
1036void fat_mount(ipc_callid_t rid, ipc_call_t *request)
1037{
1038 libfs_mount(&fat_libfs_ops, fat_reg.fs_handle, rid, request);
1039}
1040
1041void fat_lookup(ipc_callid_t rid, ipc_call_t *request)
1042{
1043 libfs_lookup(&fat_libfs_ops, fat_reg.fs_handle, rid, request);
1044}
1045
1046void fat_read(ipc_callid_t rid, ipc_call_t *request)
1047{
1048 dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
1049 fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
1050 off_t pos = (off_t)IPC_GET_ARG3(*request);
1051 fs_node_t *fn;
1052 fat_node_t *nodep;
1053 fat_bs_t *bs;
1054 uint16_t bps;
1055 size_t bytes;
1056 block_t *b;
1057 int rc;
1058
1059 rc = fat_node_get(&fn, dev_handle, index);
1060 if (rc != EOK) {
1061 ipc_answer_0(rid, rc);
1062 return;
1063 }
1064 if (!fn) {
1065 ipc_answer_0(rid, ENOENT);
1066 return;
1067 }
1068 nodep = FAT_NODE(fn);
1069
1070 ipc_callid_t callid;
1071 size_t len;
1072 if (!async_data_read_receive(&callid, &len)) {
1073 fat_node_put(fn);
1074 ipc_answer_0(callid, EINVAL);
1075 ipc_answer_0(rid, EINVAL);
1076 return;
1077 }
1078
1079 bs = block_bb_get(dev_handle);
1080 bps = uint16_t_le2host(bs->bps);
1081
1082 if (nodep->type == FAT_FILE) {
1083 /*
1084 * Our strategy for regular file reads is to read one block at
1085 * most and make use of the possibility to return less data than
1086 * requested. This keeps the code very simple.
1087 */
1088 if (pos >= nodep->size) {
1089 /* reading beyond the EOF */
1090 bytes = 0;
1091 (void) async_data_read_finalize(callid, NULL, 0);
1092 } else {
1093 bytes = min(len, bps - pos % bps);
1094 bytes = min(bytes, nodep->size - pos);
1095 rc = fat_block_get(&b, bs, nodep, pos / bps,
1096 BLOCK_FLAGS_NONE);
1097 if (rc != EOK) {
1098 fat_node_put(fn);
1099 ipc_answer_0(callid, rc);
1100 ipc_answer_0(rid, rc);
1101 return;
1102 }
1103 (void) async_data_read_finalize(callid, b->data + pos % bps,
1104 bytes);
1105 rc = block_put(b);
1106 if (rc != EOK) {
1107 fat_node_put(fn);
1108 ipc_answer_0(rid, rc);
1109 return;
1110 }
1111 }
1112 } else {
1113 unsigned bnum;
1114 off_t spos = pos;
1115 char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1];
1116 fat_dentry_t *d;
1117
1118 assert(nodep->type == FAT_DIRECTORY);
1119 assert(nodep->size % bps == 0);
1120 assert(bps % sizeof(fat_dentry_t) == 0);
1121
1122 /*
1123 * Our strategy for readdir() is to use the position pointer as
1124 * an index into the array of all dentries. On entry, it points
1125 * to the first unread dentry. If we skip any dentries, we bump
1126 * the position pointer accordingly.
1127 */
1128 bnum = (pos * sizeof(fat_dentry_t)) / bps;
1129 while (bnum < nodep->size / bps) {
1130 off_t o;
1131
1132 rc = fat_block_get(&b, bs, nodep, bnum,
1133 BLOCK_FLAGS_NONE);
1134 if (rc != EOK)
1135 goto err;
1136 for (o = pos % (bps / sizeof(fat_dentry_t));
1137 o < bps / sizeof(fat_dentry_t);
1138 o++, pos++) {
1139 d = ((fat_dentry_t *)b->data) + o;
1140 switch (fat_classify_dentry(d)) {
1141 case FAT_DENTRY_SKIP:
1142 case FAT_DENTRY_FREE:
1143 continue;
1144 case FAT_DENTRY_LAST:
1145 rc = block_put(b);
1146 if (rc != EOK)
1147 goto err;
1148 goto miss;
1149 default:
1150 case FAT_DENTRY_VALID:
1151 fat_dentry_name_get(d, name);
1152 rc = block_put(b);
1153 if (rc != EOK)
1154 goto err;
1155 goto hit;
1156 }
1157 }
1158 rc = block_put(b);
1159 if (rc != EOK)
1160 goto err;
1161 bnum++;
1162 }
1163miss:
1164 rc = fat_node_put(fn);
1165 ipc_answer_0(callid, rc != EOK ? rc : ENOENT);
1166 ipc_answer_1(rid, rc != EOK ? rc : ENOENT, 0);
1167 return;
1168
1169err:
1170 (void) fat_node_put(fn);
1171 ipc_answer_0(callid, rc);
1172 ipc_answer_0(rid, rc);
1173 return;
1174
1175hit:
1176 (void) async_data_read_finalize(callid, name, str_size(name) + 1);
1177 bytes = (pos - spos) + 1;
1178 }
1179
1180 rc = fat_node_put(fn);
1181 ipc_answer_1(rid, rc, (ipcarg_t)bytes);
1182}
1183
1184void fat_write(ipc_callid_t rid, ipc_call_t *request)
1185{
1186 dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
1187 fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
1188 off_t pos = (off_t)IPC_GET_ARG3(*request);
1189 fs_node_t *fn;
1190 fat_node_t *nodep;
1191 fat_bs_t *bs;
1192 size_t bytes, size;
1193 block_t *b;
1194 uint16_t bps;
1195 unsigned spc;
1196 unsigned bpc; /* bytes per cluster */
1197 off_t boundary;
1198 int flags = BLOCK_FLAGS_NONE;
1199 int rc;
1200
1201 rc = fat_node_get(&fn, dev_handle, index);
1202 if (rc != EOK) {
1203 ipc_answer_0(rid, rc);
1204 return;
1205 }
1206 if (!fn) {
1207 ipc_answer_0(rid, ENOENT);
1208 return;
1209 }
1210 nodep = FAT_NODE(fn);
1211
1212 ipc_callid_t callid;
1213 size_t len;
1214 if (!async_data_write_receive(&callid, &len)) {
1215 (void) fat_node_put(fn);
1216 ipc_answer_0(callid, EINVAL);
1217 ipc_answer_0(rid, EINVAL);
1218 return;
1219 }
1220
1221 bs = block_bb_get(dev_handle);
1222 bps = uint16_t_le2host(bs->bps);
1223 spc = bs->spc;
1224 bpc = bps * spc;
1225
1226 /*
1227 * In all scenarios, we will attempt to write out only one block worth
1228 * of data at maximum. There might be some more efficient approaches,
1229 * but this one greatly simplifies fat_write(). Note that we can afford
1230 * to do this because the client must be ready to handle the return
1231 * value signalizing a smaller number of bytes written.
1232 */
1233 bytes = min(len, bps - pos % bps);
1234 if (bytes == bps)
1235 flags |= BLOCK_FLAGS_NOREAD;
1236
1237 boundary = ROUND_UP(nodep->size, bpc);
1238 if (pos < boundary) {
1239 /*
1240 * This is the easier case - we are either overwriting already
1241 * existing contents or writing behind the EOF, but still within
1242 * the limits of the last cluster. The node size may grow to the
1243 * next block size boundary.
1244 */
1245 rc = fat_fill_gap(bs, nodep, FAT_CLST_RES0, pos);
1246 if (rc != EOK) {
1247 (void) fat_node_put(fn);
1248 ipc_answer_0(callid, rc);
1249 ipc_answer_0(rid, rc);
1250 return;
1251 }
1252 rc = fat_block_get(&b, bs, nodep, pos / bps, flags);
1253 if (rc != EOK) {
1254 (void) fat_node_put(fn);
1255 ipc_answer_0(callid, rc);
1256 ipc_answer_0(rid, rc);
1257 return;
1258 }
1259 (void) async_data_write_finalize(callid, b->data + pos % bps,
1260 bytes);
1261 b->dirty = true; /* need to sync block */
1262 rc = block_put(b);
1263 if (rc != EOK) {
1264 (void) fat_node_put(fn);
1265 ipc_answer_0(rid, rc);
1266 return;
1267 }
1268 if (pos + bytes > nodep->size) {
1269 nodep->size = pos + bytes;
1270 nodep->dirty = true; /* need to sync node */
1271 }
1272 size = nodep->size;
1273 rc = fat_node_put(fn);
1274 ipc_answer_2(rid, rc, bytes, nodep->size);
1275 return;
1276 } else {
1277 /*
1278 * This is the more difficult case. We must allocate new
1279 * clusters for the node and zero them out.
1280 */
1281 unsigned nclsts;
1282 fat_cluster_t mcl, lcl;
1283
1284 nclsts = (ROUND_UP(pos + bytes, bpc) - boundary) / bpc;
1285 /* create an independent chain of nclsts clusters in all FATs */
1286 rc = fat_alloc_clusters(bs, dev_handle, nclsts, &mcl, &lcl);
1287 if (rc != EOK) {
1288 /* could not allocate a chain of nclsts clusters */
1289 (void) fat_node_put(fn);
1290 ipc_answer_0(callid, rc);
1291 ipc_answer_0(rid, rc);
1292 return;
1293 }
1294 /* zero fill any gaps */
1295 rc = fat_fill_gap(bs, nodep, mcl, pos);
1296 if (rc != EOK) {
1297 (void) fat_free_clusters(bs, dev_handle, mcl);
1298 (void) fat_node_put(fn);
1299 ipc_answer_0(callid, rc);
1300 ipc_answer_0(rid, rc);
1301 return;
1302 }
1303 rc = _fat_block_get(&b, bs, dev_handle, lcl, (pos / bps) % spc,
1304 flags);
1305 if (rc != EOK) {
1306 (void) fat_free_clusters(bs, dev_handle, mcl);
1307 (void) fat_node_put(fn);
1308 ipc_answer_0(callid, rc);
1309 ipc_answer_0(rid, rc);
1310 return;
1311 }
1312 (void) async_data_write_finalize(callid, b->data + pos % bps,
1313 bytes);
1314 b->dirty = true; /* need to sync block */
1315 rc = block_put(b);
1316 if (rc != EOK) {
1317 (void) fat_free_clusters(bs, dev_handle, mcl);
1318 (void) fat_node_put(fn);
1319 ipc_answer_0(rid, rc);
1320 return;
1321 }
1322 /*
1323 * Append the cluster chain starting in mcl to the end of the
1324 * node's cluster chain.
1325 */
1326 rc = fat_append_clusters(bs, nodep, mcl);
1327 if (rc != EOK) {
1328 (void) fat_free_clusters(bs, dev_handle, mcl);
1329 (void) fat_node_put(fn);
1330 ipc_answer_0(rid, rc);
1331 return;
1332 }
1333 nodep->size = size = pos + bytes;
1334 nodep->dirty = true; /* need to sync node */
1335 rc = fat_node_put(fn);
1336 ipc_answer_2(rid, rc, bytes, size);
1337 return;
1338 }
1339}
1340
1341void fat_truncate(ipc_callid_t rid, ipc_call_t *request)
1342{
1343 dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
1344 fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
1345 size_t size = (off_t)IPC_GET_ARG3(*request);
1346 fs_node_t *fn;
1347 fat_node_t *nodep;
1348 fat_bs_t *bs;
1349 uint16_t bps;
1350 uint8_t spc;
1351 unsigned bpc; /* bytes per cluster */
1352 int rc;
1353
1354 rc = fat_node_get(&fn, dev_handle, index);
1355 if (rc != EOK) {
1356 ipc_answer_0(rid, rc);
1357 return;
1358 }
1359 if (!fn) {
1360 ipc_answer_0(rid, ENOENT);
1361 return;
1362 }
1363 nodep = FAT_NODE(fn);
1364
1365 bs = block_bb_get(dev_handle);
1366 bps = uint16_t_le2host(bs->bps);
1367 spc = bs->spc;
1368 bpc = bps * spc;
1369
1370 if (nodep->size == size) {
1371 rc = EOK;
1372 } else if (nodep->size < size) {
1373 /*
1374 * The standard says we have the freedom to grow the node.
1375 * For now, we simply return an error.
1376 */
1377 rc = EINVAL;
1378 } else if (ROUND_UP(nodep->size, bpc) == ROUND_UP(size, bpc)) {
1379 /*
1380 * The node will be shrunk, but no clusters will be deallocated.
1381 */
1382 nodep->size = size;
1383 nodep->dirty = true; /* need to sync node */
1384 rc = EOK;
1385 } else {
1386 /*
1387 * The node will be shrunk, clusters will be deallocated.
1388 */
1389 if (size == 0) {
1390 rc = fat_chop_clusters(bs, nodep, FAT_CLST_RES0);
1391 if (rc != EOK)
1392 goto out;
1393 } else {
1394 fat_cluster_t lastc;
1395 rc = fat_cluster_walk(bs, dev_handle, nodep->firstc,
1396 &lastc, NULL, (size - 1) / bpc);
1397 if (rc != EOK)
1398 goto out;
1399 rc = fat_chop_clusters(bs, nodep, lastc);
1400 if (rc != EOK)
1401 goto out;
1402 }
1403 nodep->size = size;
1404 nodep->dirty = true; /* need to sync node */
1405 rc = EOK;
1406 }
1407out:
1408 fat_node_put(fn);
1409 ipc_answer_0(rid, rc);
1410 return;
1411}
1412
1413void fat_close(ipc_callid_t rid, ipc_call_t *request)
1414{
1415 ipc_answer_0(rid, EOK);
1416}
1417
1418void fat_destroy(ipc_callid_t rid, ipc_call_t *request)
1419{
1420 dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
1421 fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
1422 fs_node_t *fn;
1423 int rc;
1424
1425 rc = fat_node_get(&fn, dev_handle, index);
1426 if (rc != EOK) {
1427 ipc_answer_0(rid, rc);
1428 return;
1429 }
1430 if (!fn) {
1431 ipc_answer_0(rid, ENOENT);
1432 return;
1433 }
1434
1435 rc = fat_destroy_node(fn);
1436 ipc_answer_0(rid, rc);
1437}
1438
1439void fat_open_node(ipc_callid_t rid, ipc_call_t *request)
1440{
1441 libfs_open_node(&fat_libfs_ops, fat_reg.fs_handle, rid, request);
1442}
1443
1444void fat_stat(ipc_callid_t rid, ipc_call_t *request)
1445{
1446 libfs_stat(&fat_libfs_ops, fat_reg.fs_handle, rid, request);
1447}
1448
1449void fat_sync(ipc_callid_t rid, ipc_call_t *request)
1450{
1451 /* Dummy implementation */
1452 ipc_answer_0(rid, EOK);
1453}
1454
1455/**
1456 * @}
1457 */
Note: See TracBrowser for help on using the repository browser.