source: mainline/uspace/srv/fs/fat/fat_ops.c@ 711e1f32

lfn serial ticket/834-toolchain-update topic/fix-logger-deadlock topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 711e1f32 was 711e1f32, checked in by Jiri Svoboda <jiri@…>, 17 years ago

Do some sanity checks when mounting a fat file system.

  • Property mode set to 100644
File size: 36.3 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/** Perform basic sanity checks on the file system.
296 *
297 * Verify if values of boot sector fields are sane. Also verify media
298 * descriptor. This is used to rule out cases when a device obviously
299 * does not contain a fat file system.
300 */
301static int fat_sanity_check(fat_bs_t *bs, dev_handle_t dev_handle)
302{
303 fat_cluster_t e0, e1;
304 unsigned fat_no;
305 int rc;
306
307 printf("fatcnt\n");
308 /* Check number of FATs. */
309 if (bs->fatcnt == 0)
310 return ENOTSUP;
311
312 /* Check total number of sectors. */
313
314 printf("totsec\n");
315 if (bs->totsec16 == 0 && bs->totsec32 == 0)
316 return ENOTSUP;
317
318 if (bs->totsec16 != 0 && bs->totsec32 != 0 &&
319 bs->totsec16 != bs->totsec32)
320 return ENOTSUP;
321
322 printf("mdesc\n");
323 /* Check media descriptor. Must be between 0xf0 and 0xff. */
324 if ((bs->mdesc & 0xf0) != 0xf0)
325 return ENOTSUP;
326
327 printf("sec_per_fat\n");
328 /* Check number of sectors per FAT. */
329 if (bs->sec_per_fat == 0)
330 return ENOTSUP;
331
332 /* Check signature of each FAT. */
333
334 for (fat_no = 0; fat_no < bs->fatcnt; fat_no++) {
335 printf("clst-read\n");
336 rc = fat_get_cluster(bs, dev_handle, fat_no, 0, &e0);
337 if (rc != EOK)
338 return EIO;
339
340 rc = fat_get_cluster(bs, dev_handle, fat_no, 1, &e1);
341 if (rc != EOK)
342 return EIO;
343
344 printf("mdesc-fat\n");
345 /* Check that first byte of FAT contains the media descriptor. */
346 if ((e0 & 0xff) != bs->mdesc)
347 return ENOTSUP;
348
349 printf("fat-signat\n");
350 /*
351 * Check that remaining bits of the first two entries are
352 * set to one.
353 */
354 if ((e0 >> 8) != 0xff || e1 != 0xffff)
355 return ENOTSUP;
356 }
357
358 return EOK;
359}
360
361/*
362 * FAT libfs operations.
363 */
364
365int fat_root_get(fs_node_t **rfn, dev_handle_t dev_handle)
366{
367 return fat_node_get(rfn, dev_handle, 0);
368}
369
370int fat_match(fs_node_t **rfn, fs_node_t *pfn, const char *component)
371{
372 fat_bs_t *bs;
373 fat_node_t *parentp = FAT_NODE(pfn);
374 char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1];
375 unsigned i, j;
376 unsigned bps; /* bytes per sector */
377 unsigned dps; /* dentries per sector */
378 unsigned blocks;
379 fat_dentry_t *d;
380 block_t *b;
381 int rc;
382
383 fibril_mutex_lock(&parentp->idx->lock);
384 bs = block_bb_get(parentp->idx->dev_handle);
385 bps = uint16_t_le2host(bs->bps);
386 dps = bps / sizeof(fat_dentry_t);
387 blocks = parentp->size / bps;
388 for (i = 0; i < blocks; i++) {
389 rc = fat_block_get(&b, bs, parentp, i, BLOCK_FLAGS_NONE);
390 if (rc != EOK) {
391 fibril_mutex_unlock(&parentp->idx->lock);
392 return rc;
393 }
394 for (j = 0; j < dps; j++) {
395 d = ((fat_dentry_t *)b->data) + j;
396 switch (fat_classify_dentry(d)) {
397 case FAT_DENTRY_SKIP:
398 case FAT_DENTRY_FREE:
399 continue;
400 case FAT_DENTRY_LAST:
401 /* miss */
402 rc = block_put(b);
403 fibril_mutex_unlock(&parentp->idx->lock);
404 *rfn = NULL;
405 return rc;
406 default:
407 case FAT_DENTRY_VALID:
408 fat_dentry_name_get(d, name);
409 break;
410 }
411 if (fat_dentry_namecmp(name, component) == 0) {
412 /* hit */
413 fat_node_t *nodep;
414 /*
415 * Assume tree hierarchy for locking. We
416 * already have the parent and now we are going
417 * to lock the child. Never lock in the oposite
418 * order.
419 */
420 fat_idx_t *idx = fat_idx_get_by_pos(
421 parentp->idx->dev_handle, parentp->firstc,
422 i * dps + j);
423 fibril_mutex_unlock(&parentp->idx->lock);
424 if (!idx) {
425 /*
426 * Can happen if memory is low or if we
427 * run out of 32-bit indices.
428 */
429 rc = block_put(b);
430 return (rc == EOK) ? ENOMEM : rc;
431 }
432 rc = fat_node_get_core(&nodep, idx);
433 fibril_mutex_unlock(&idx->lock);
434 if (rc != EOK) {
435 (void) block_put(b);
436 return rc;
437 }
438 *rfn = FS_NODE(nodep);
439 rc = block_put(b);
440 if (rc != EOK)
441 (void) fat_node_put(*rfn);
442 return rc;
443 }
444 }
445 rc = block_put(b);
446 if (rc != EOK) {
447 fibril_mutex_unlock(&parentp->idx->lock);
448 return rc;
449 }
450 }
451
452 fibril_mutex_unlock(&parentp->idx->lock);
453 *rfn = NULL;
454 return EOK;
455}
456
457/** Instantiate a FAT in-core node. */
458int fat_node_get(fs_node_t **rfn, dev_handle_t dev_handle, fs_index_t index)
459{
460 fat_node_t *nodep;
461 fat_idx_t *idxp;
462 int rc;
463
464 idxp = fat_idx_get_by_index(dev_handle, index);
465 if (!idxp) {
466 *rfn = NULL;
467 return EOK;
468 }
469 /* idxp->lock held */
470 rc = fat_node_get_core(&nodep, idxp);
471 fibril_mutex_unlock(&idxp->lock);
472 if (rc == EOK)
473 *rfn = FS_NODE(nodep);
474 return rc;
475}
476
477int fat_node_open(fs_node_t *fn)
478{
479 /*
480 * Opening a file is stateless, nothing
481 * to be done here.
482 */
483 return EOK;
484}
485
486int fat_node_put(fs_node_t *fn)
487{
488 fat_node_t *nodep = FAT_NODE(fn);
489 bool destroy = false;
490
491 fibril_mutex_lock(&nodep->lock);
492 if (!--nodep->refcnt) {
493 if (nodep->idx) {
494 fibril_mutex_lock(&ffn_mutex);
495 list_append(&nodep->ffn_link, &ffn_head);
496 fibril_mutex_unlock(&ffn_mutex);
497 } else {
498 /*
499 * The node does not have any index structure associated
500 * with itself. This can only mean that we are releasing
501 * the node after a failed attempt to allocate the index
502 * structure for it.
503 */
504 destroy = true;
505 }
506 }
507 fibril_mutex_unlock(&nodep->lock);
508 if (destroy) {
509 free(nodep->bp);
510 free(nodep);
511 }
512 return EOK;
513}
514
515int fat_create_node(fs_node_t **rfn, dev_handle_t dev_handle, int flags)
516{
517 fat_idx_t *idxp;
518 fat_node_t *nodep;
519 fat_bs_t *bs;
520 fat_cluster_t mcl, lcl;
521 uint16_t bps;
522 int rc;
523
524 bs = block_bb_get(dev_handle);
525 bps = uint16_t_le2host(bs->bps);
526 if (flags & L_DIRECTORY) {
527 /* allocate a cluster */
528 rc = fat_alloc_clusters(bs, dev_handle, 1, &mcl, &lcl);
529 if (rc != EOK)
530 return rc;
531 /* populate the new cluster with unused dentries */
532 rc = fat_zero_cluster(bs, dev_handle, mcl);
533 if (rc != EOK) {
534 (void) fat_free_clusters(bs, dev_handle, mcl);
535 return rc;
536 }
537 }
538
539 rc = fat_node_get_new(&nodep);
540 if (rc != EOK) {
541 (void) fat_free_clusters(bs, dev_handle, mcl);
542 return rc;
543 }
544 rc = fat_idx_get_new(&idxp, dev_handle);
545 if (rc != EOK) {
546 (void) fat_free_clusters(bs, dev_handle, mcl);
547 (void) fat_node_put(FS_NODE(nodep));
548 return rc;
549 }
550 /* idxp->lock held */
551 if (flags & L_DIRECTORY) {
552 nodep->type = FAT_DIRECTORY;
553 nodep->firstc = mcl;
554 nodep->size = bps * bs->spc;
555 } else {
556 nodep->type = FAT_FILE;
557 nodep->firstc = FAT_CLST_RES0;
558 nodep->size = 0;
559 }
560 nodep->lnkcnt = 0; /* not linked anywhere */
561 nodep->refcnt = 1;
562 nodep->dirty = true;
563
564 nodep->idx = idxp;
565 idxp->nodep = nodep;
566
567 fibril_mutex_unlock(&idxp->lock);
568 *rfn = FS_NODE(nodep);
569 return EOK;
570}
571
572int fat_destroy_node(fs_node_t *fn)
573{
574 fat_node_t *nodep = FAT_NODE(fn);
575 fat_bs_t *bs;
576 bool has_children;
577 int rc;
578
579 /*
580 * The node is not reachable from the file system. This means that the
581 * link count should be zero and that the index structure cannot be
582 * found in the position hash. Obviously, we don't need to lock the node
583 * nor its index structure.
584 */
585 assert(nodep->lnkcnt == 0);
586
587 /*
588 * The node may not have any children.
589 */
590 rc = fat_has_children(&has_children, fn);
591 if (rc != EOK)
592 return rc;
593 assert(!has_children);
594
595 bs = block_bb_get(nodep->idx->dev_handle);
596 if (nodep->firstc != FAT_CLST_RES0) {
597 assert(nodep->size);
598 /* Free all clusters allocated to the node. */
599 rc = fat_free_clusters(bs, nodep->idx->dev_handle,
600 nodep->firstc);
601 }
602
603 fat_idx_destroy(nodep->idx);
604 free(nodep->bp);
605 free(nodep);
606 return rc;
607}
608
609int fat_link(fs_node_t *pfn, fs_node_t *cfn, const char *name)
610{
611 fat_node_t *parentp = FAT_NODE(pfn);
612 fat_node_t *childp = FAT_NODE(cfn);
613 fat_dentry_t *d;
614 fat_bs_t *bs;
615 block_t *b;
616 unsigned i, j;
617 uint16_t bps;
618 unsigned dps;
619 unsigned blocks;
620 fat_cluster_t mcl, lcl;
621 int rc;
622
623 fibril_mutex_lock(&childp->lock);
624 if (childp->lnkcnt == 1) {
625 /*
626 * On FAT, we don't support multiple hard links.
627 */
628 fibril_mutex_unlock(&childp->lock);
629 return EMLINK;
630 }
631 assert(childp->lnkcnt == 0);
632 fibril_mutex_unlock(&childp->lock);
633
634 if (!fat_dentry_name_verify(name)) {
635 /*
636 * Attempt to create unsupported name.
637 */
638 return ENOTSUP;
639 }
640
641 /*
642 * Get us an unused parent node's dentry or grow the parent and allocate
643 * a new one.
644 */
645
646 fibril_mutex_lock(&parentp->idx->lock);
647 bs = block_bb_get(parentp->idx->dev_handle);
648 bps = uint16_t_le2host(bs->bps);
649 dps = bps / sizeof(fat_dentry_t);
650
651 blocks = parentp->size / bps;
652
653 for (i = 0; i < blocks; i++) {
654 rc = fat_block_get(&b, bs, parentp, i, BLOCK_FLAGS_NONE);
655 if (rc != EOK) {
656 fibril_mutex_unlock(&parentp->idx->lock);
657 return rc;
658 }
659 for (j = 0; j < dps; j++) {
660 d = ((fat_dentry_t *)b->data) + j;
661 switch (fat_classify_dentry(d)) {
662 case FAT_DENTRY_SKIP:
663 case FAT_DENTRY_VALID:
664 /* skipping used and meta entries */
665 continue;
666 case FAT_DENTRY_FREE:
667 case FAT_DENTRY_LAST:
668 /* found an empty slot */
669 goto hit;
670 }
671 }
672 rc = block_put(b);
673 if (rc != EOK) {
674 fibril_mutex_unlock(&parentp->idx->lock);
675 return rc;
676 }
677 }
678 j = 0;
679
680 /*
681 * We need to grow the parent in order to create a new unused dentry.
682 */
683 if (parentp->firstc == FAT_CLST_ROOT) {
684 /* Can't grow the root directory. */
685 fibril_mutex_unlock(&parentp->idx->lock);
686 return ENOSPC;
687 }
688 rc = fat_alloc_clusters(bs, parentp->idx->dev_handle, 1, &mcl, &lcl);
689 if (rc != EOK) {
690 fibril_mutex_unlock(&parentp->idx->lock);
691 return rc;
692 }
693 rc = fat_zero_cluster(bs, parentp->idx->dev_handle, mcl);
694 if (rc != EOK) {
695 (void) fat_free_clusters(bs, parentp->idx->dev_handle, mcl);
696 fibril_mutex_unlock(&parentp->idx->lock);
697 return rc;
698 }
699 rc = fat_append_clusters(bs, parentp, mcl);
700 if (rc != EOK) {
701 (void) fat_free_clusters(bs, parentp->idx->dev_handle, mcl);
702 fibril_mutex_unlock(&parentp->idx->lock);
703 return rc;
704 }
705 parentp->size += bps * bs->spc;
706 parentp->dirty = true; /* need to sync node */
707 rc = fat_block_get(&b, bs, parentp, i, BLOCK_FLAGS_NONE);
708 if (rc != EOK) {
709 fibril_mutex_unlock(&parentp->idx->lock);
710 return rc;
711 }
712 d = (fat_dentry_t *)b->data;
713
714hit:
715 /*
716 * At this point we only establish the link between the parent and the
717 * child. The dentry, except of the name and the extension, will remain
718 * uninitialized until the corresponding node is synced. Thus the valid
719 * dentry data is kept in the child node structure.
720 */
721 memset(d, 0, sizeof(fat_dentry_t));
722 fat_dentry_name_set(d, name);
723 b->dirty = true; /* need to sync block */
724 rc = block_put(b);
725 fibril_mutex_unlock(&parentp->idx->lock);
726 if (rc != EOK)
727 return rc;
728
729 fibril_mutex_lock(&childp->idx->lock);
730
731 /*
732 * If possible, create the Sub-directory Identifier Entry and the
733 * Sub-directory Parent Pointer Entry (i.e. "." and ".."). These entries
734 * are not mandatory according to Standard ECMA-107 and HelenOS VFS does
735 * not use them anyway, so this is rather a sign of our good will.
736 */
737 rc = fat_block_get(&b, bs, childp, 0, BLOCK_FLAGS_NONE);
738 if (rc != EOK) {
739 /*
740 * Rather than returning an error, simply skip the creation of
741 * these two entries.
742 */
743 goto skip_dots;
744 }
745 d = (fat_dentry_t *)b->data;
746 if (fat_classify_dentry(d) == FAT_DENTRY_LAST ||
747 str_cmp(d->name, FAT_NAME_DOT) == 0) {
748 memset(d, 0, sizeof(fat_dentry_t));
749 str_cpy(d->name, 8, FAT_NAME_DOT);
750 str_cpy(d->ext, 3, FAT_EXT_PAD);
751 d->attr = FAT_ATTR_SUBDIR;
752 d->firstc = host2uint16_t_le(childp->firstc);
753 /* TODO: initialize also the date/time members. */
754 }
755 d++;
756 if (fat_classify_dentry(d) == FAT_DENTRY_LAST ||
757 str_cmp(d->name, FAT_NAME_DOT_DOT) == 0) {
758 memset(d, 0, sizeof(fat_dentry_t));
759 str_cpy(d->name, 8, FAT_NAME_DOT_DOT);
760 str_cpy(d->ext, 3, FAT_EXT_PAD);
761 d->attr = FAT_ATTR_SUBDIR;
762 d->firstc = (parentp->firstc == FAT_CLST_ROOT) ?
763 host2uint16_t_le(FAT_CLST_RES0) :
764 host2uint16_t_le(parentp->firstc);
765 /* TODO: initialize also the date/time members. */
766 }
767 b->dirty = true; /* need to sync block */
768 /*
769 * Ignore the return value as we would have fallen through on error
770 * anyway.
771 */
772 (void) block_put(b);
773skip_dots:
774
775 childp->idx->pfc = parentp->firstc;
776 childp->idx->pdi = i * dps + j;
777 fibril_mutex_unlock(&childp->idx->lock);
778
779 fibril_mutex_lock(&childp->lock);
780 childp->lnkcnt = 1;
781 childp->dirty = true; /* need to sync node */
782 fibril_mutex_unlock(&childp->lock);
783
784 /*
785 * Hash in the index structure into the position hash.
786 */
787 fat_idx_hashin(childp->idx);
788
789 return EOK;
790}
791
792int fat_unlink(fs_node_t *pfn, fs_node_t *cfn, const char *nm)
793{
794 fat_node_t *parentp = FAT_NODE(pfn);
795 fat_node_t *childp = FAT_NODE(cfn);
796 fat_bs_t *bs;
797 fat_dentry_t *d;
798 uint16_t bps;
799 block_t *b;
800 bool has_children;
801 int rc;
802
803 if (!parentp)
804 return EBUSY;
805
806 rc = fat_has_children(&has_children, cfn);
807 if (rc != EOK)
808 return rc;
809 if (has_children)
810 return ENOTEMPTY;
811
812 fibril_mutex_lock(&parentp->lock);
813 fibril_mutex_lock(&childp->lock);
814 assert(childp->lnkcnt == 1);
815 fibril_mutex_lock(&childp->idx->lock);
816 bs = block_bb_get(childp->idx->dev_handle);
817 bps = uint16_t_le2host(bs->bps);
818
819 rc = _fat_block_get(&b, bs, childp->idx->dev_handle, childp->idx->pfc,
820 (childp->idx->pdi * sizeof(fat_dentry_t)) / bps,
821 BLOCK_FLAGS_NONE);
822 if (rc != EOK)
823 goto error;
824 d = (fat_dentry_t *)b->data +
825 (childp->idx->pdi % (bps / sizeof(fat_dentry_t)));
826 /* mark the dentry as not-currently-used */
827 d->name[0] = FAT_DENTRY_ERASED;
828 b->dirty = true; /* need to sync block */
829 rc = block_put(b);
830 if (rc != EOK)
831 goto error;
832
833 /* remove the index structure from the position hash */
834 fat_idx_hashout(childp->idx);
835 /* clear position information */
836 childp->idx->pfc = FAT_CLST_RES0;
837 childp->idx->pdi = 0;
838 fibril_mutex_unlock(&childp->idx->lock);
839 childp->lnkcnt = 0;
840 childp->dirty = true;
841 fibril_mutex_unlock(&childp->lock);
842 fibril_mutex_unlock(&parentp->lock);
843
844 return EOK;
845
846error:
847 fibril_mutex_unlock(&parentp->idx->lock);
848 fibril_mutex_unlock(&childp->lock);
849 fibril_mutex_unlock(&childp->idx->lock);
850 return rc;
851}
852
853int fat_has_children(bool *has_children, fs_node_t *fn)
854{
855 fat_bs_t *bs;
856 fat_node_t *nodep = FAT_NODE(fn);
857 unsigned bps;
858 unsigned dps;
859 unsigned blocks;
860 block_t *b;
861 unsigned i, j;
862 int rc;
863
864 if (nodep->type != FAT_DIRECTORY) {
865 *has_children = false;
866 return EOK;
867 }
868
869 fibril_mutex_lock(&nodep->idx->lock);
870 bs = block_bb_get(nodep->idx->dev_handle);
871 bps = uint16_t_le2host(bs->bps);
872 dps = bps / sizeof(fat_dentry_t);
873
874 blocks = nodep->size / bps;
875
876 for (i = 0; i < blocks; i++) {
877 fat_dentry_t *d;
878
879 rc = fat_block_get(&b, bs, nodep, i, BLOCK_FLAGS_NONE);
880 if (rc != EOK) {
881 fibril_mutex_unlock(&nodep->idx->lock);
882 return rc;
883 }
884 for (j = 0; j < dps; j++) {
885 d = ((fat_dentry_t *)b->data) + j;
886 switch (fat_classify_dentry(d)) {
887 case FAT_DENTRY_SKIP:
888 case FAT_DENTRY_FREE:
889 continue;
890 case FAT_DENTRY_LAST:
891 rc = block_put(b);
892 fibril_mutex_unlock(&nodep->idx->lock);
893 *has_children = false;
894 return rc;
895 default:
896 case FAT_DENTRY_VALID:
897 rc = block_put(b);
898 fibril_mutex_unlock(&nodep->idx->lock);
899 *has_children = true;
900 return rc;
901 }
902 }
903 rc = block_put(b);
904 if (rc != EOK) {
905 fibril_mutex_unlock(&nodep->idx->lock);
906 return rc;
907 }
908 }
909
910 fibril_mutex_unlock(&nodep->idx->lock);
911 *has_children = false;
912 return EOK;
913}
914
915
916fs_index_t fat_index_get(fs_node_t *fn)
917{
918 return FAT_NODE(fn)->idx->index;
919}
920
921size_t fat_size_get(fs_node_t *fn)
922{
923 return FAT_NODE(fn)->size;
924}
925
926unsigned fat_lnkcnt_get(fs_node_t *fn)
927{
928 return FAT_NODE(fn)->lnkcnt;
929}
930
931char fat_plb_get_char(unsigned pos)
932{
933 return fat_reg.plb_ro[pos % PLB_SIZE];
934}
935
936bool fat_is_directory(fs_node_t *fn)
937{
938 return FAT_NODE(fn)->type == FAT_DIRECTORY;
939}
940
941bool fat_is_file(fs_node_t *fn)
942{
943 return FAT_NODE(fn)->type == FAT_FILE;
944}
945
946dev_handle_t fat_device_get(fs_node_t *node)
947{
948 return 0;
949}
950
951/** libfs operations */
952libfs_ops_t fat_libfs_ops = {
953 .root_get = fat_root_get,
954 .match = fat_match,
955 .node_get = fat_node_get,
956 .node_open = fat_node_open,
957 .node_put = fat_node_put,
958 .create = fat_create_node,
959 .destroy = fat_destroy_node,
960 .link = fat_link,
961 .unlink = fat_unlink,
962 .has_children = fat_has_children,
963 .index_get = fat_index_get,
964 .size_get = fat_size_get,
965 .lnkcnt_get = fat_lnkcnt_get,
966 .plb_get_char = fat_plb_get_char,
967 .is_directory = fat_is_directory,
968 .is_file = fat_is_file,
969 .device_get = fat_device_get
970};
971
972/*
973 * VFS operations.
974 */
975
976void fat_mounted(ipc_callid_t rid, ipc_call_t *request)
977{
978 dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request);
979 enum cache_mode cmode;
980 fat_bs_t *bs;
981 uint16_t bps;
982 uint16_t rde;
983 int rc;
984
985 /* accept the mount options */
986 ipc_callid_t callid;
987 size_t size;
988 if (!async_data_write_receive(&callid, &size)) {
989 ipc_answer_0(callid, EINVAL);
990 ipc_answer_0(rid, EINVAL);
991 return;
992 }
993 char *opts = malloc(size + 1);
994 if (!opts) {
995 ipc_answer_0(callid, ENOMEM);
996 ipc_answer_0(rid, ENOMEM);
997 return;
998 }
999 ipcarg_t retval = async_data_write_finalize(callid, opts, size);
1000 if (retval != EOK) {
1001 ipc_answer_0(rid, retval);
1002 free(opts);
1003 return;
1004 }
1005 opts[size] = '\0';
1006
1007 /* Check for option enabling write through. */
1008 if (str_cmp(opts, "wtcache") == 0)
1009 cmode = CACHE_MODE_WT;
1010 else
1011 cmode = CACHE_MODE_WB;
1012
1013 /* initialize libblock */
1014 rc = block_init(dev_handle, BS_SIZE);
1015 if (rc != EOK) {
1016 ipc_answer_0(rid, rc);
1017 return;
1018 }
1019
1020 /* prepare the boot block */
1021 rc = block_bb_read(dev_handle, BS_BLOCK);
1022 if (rc != EOK) {
1023 block_fini(dev_handle);
1024 ipc_answer_0(rid, rc);
1025 return;
1026 }
1027
1028 /* get the buffer with the boot sector */
1029 bs = block_bb_get(dev_handle);
1030
1031 /* Read the number of root directory entries. */
1032 bps = uint16_t_le2host(bs->bps);
1033 rde = uint16_t_le2host(bs->root_ent_max);
1034
1035 if (bps != BS_SIZE) {
1036 block_fini(dev_handle);
1037 ipc_answer_0(rid, ENOTSUP);
1038 return;
1039 }
1040
1041 /* Initialize the block cache */
1042 rc = block_cache_init(dev_handle, bps, 0 /* XXX */, cmode);
1043 if (rc != EOK) {
1044 block_fini(dev_handle);
1045 ipc_answer_0(rid, rc);
1046 return;
1047 }
1048
1049 /* Do some simple sanity checks on the boot blocks. */
1050 rc = fat_sanity_check(bs, dev_handle);
1051 if (rc != EOK) {
1052 block_fini(dev_handle);
1053 ipc_answer_0(rid, rc);
1054 return;
1055 }
1056
1057 rc = fat_idx_init_by_dev_handle(dev_handle);
1058 if (rc != EOK) {
1059 block_fini(dev_handle);
1060 ipc_answer_0(rid, rc);
1061 return;
1062 }
1063
1064 /* Initialize the root node. */
1065 fs_node_t *rfn = (fs_node_t *)malloc(sizeof(fs_node_t));
1066 if (!rfn) {
1067 block_fini(dev_handle);
1068 fat_idx_fini_by_dev_handle(dev_handle);
1069 ipc_answer_0(rid, ENOMEM);
1070 return;
1071 }
1072 fs_node_initialize(rfn);
1073 fat_node_t *rootp = (fat_node_t *)malloc(sizeof(fat_node_t));
1074 if (!rootp) {
1075 free(rfn);
1076 block_fini(dev_handle);
1077 fat_idx_fini_by_dev_handle(dev_handle);
1078 ipc_answer_0(rid, ENOMEM);
1079 return;
1080 }
1081 fat_node_initialize(rootp);
1082
1083 fat_idx_t *ridxp = fat_idx_get_by_pos(dev_handle, FAT_CLST_ROOTPAR, 0);
1084 if (!ridxp) {
1085 free(rfn);
1086 free(rootp);
1087 block_fini(dev_handle);
1088 fat_idx_fini_by_dev_handle(dev_handle);
1089 ipc_answer_0(rid, ENOMEM);
1090 return;
1091 }
1092 assert(ridxp->index == 0);
1093 /* ridxp->lock held */
1094
1095 rootp->type = FAT_DIRECTORY;
1096 rootp->firstc = FAT_CLST_ROOT;
1097 rootp->refcnt = 1;
1098 rootp->lnkcnt = 0; /* FS root is not linked */
1099 rootp->size = rde * sizeof(fat_dentry_t);
1100 rootp->idx = ridxp;
1101 ridxp->nodep = rootp;
1102 rootp->bp = rfn;
1103 rfn->data = rootp;
1104
1105 fibril_mutex_unlock(&ridxp->lock);
1106
1107 ipc_answer_3(rid, EOK, ridxp->index, rootp->size, rootp->lnkcnt);
1108}
1109
1110void fat_mount(ipc_callid_t rid, ipc_call_t *request)
1111{
1112 libfs_mount(&fat_libfs_ops, fat_reg.fs_handle, rid, request);
1113}
1114
1115void fat_lookup(ipc_callid_t rid, ipc_call_t *request)
1116{
1117 libfs_lookup(&fat_libfs_ops, fat_reg.fs_handle, rid, request);
1118}
1119
1120void fat_read(ipc_callid_t rid, ipc_call_t *request)
1121{
1122 dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
1123 fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
1124 off_t pos = (off_t)IPC_GET_ARG3(*request);
1125 fs_node_t *fn;
1126 fat_node_t *nodep;
1127 fat_bs_t *bs;
1128 uint16_t bps;
1129 size_t bytes;
1130 block_t *b;
1131 int rc;
1132
1133 rc = fat_node_get(&fn, dev_handle, index);
1134 if (rc != EOK) {
1135 ipc_answer_0(rid, rc);
1136 return;
1137 }
1138 if (!fn) {
1139 ipc_answer_0(rid, ENOENT);
1140 return;
1141 }
1142 nodep = FAT_NODE(fn);
1143
1144 ipc_callid_t callid;
1145 size_t len;
1146 if (!async_data_read_receive(&callid, &len)) {
1147 fat_node_put(fn);
1148 ipc_answer_0(callid, EINVAL);
1149 ipc_answer_0(rid, EINVAL);
1150 return;
1151 }
1152
1153 bs = block_bb_get(dev_handle);
1154 bps = uint16_t_le2host(bs->bps);
1155
1156 if (nodep->type == FAT_FILE) {
1157 /*
1158 * Our strategy for regular file reads is to read one block at
1159 * most and make use of the possibility to return less data than
1160 * requested. This keeps the code very simple.
1161 */
1162 if (pos >= nodep->size) {
1163 /* reading beyond the EOF */
1164 bytes = 0;
1165 (void) async_data_read_finalize(callid, NULL, 0);
1166 } else {
1167 bytes = min(len, bps - pos % bps);
1168 bytes = min(bytes, nodep->size - pos);
1169 rc = fat_block_get(&b, bs, nodep, pos / bps,
1170 BLOCK_FLAGS_NONE);
1171 if (rc != EOK) {
1172 fat_node_put(fn);
1173 ipc_answer_0(callid, rc);
1174 ipc_answer_0(rid, rc);
1175 return;
1176 }
1177 (void) async_data_read_finalize(callid, b->data + pos % bps,
1178 bytes);
1179 rc = block_put(b);
1180 if (rc != EOK) {
1181 fat_node_put(fn);
1182 ipc_answer_0(rid, rc);
1183 return;
1184 }
1185 }
1186 } else {
1187 unsigned bnum;
1188 off_t spos = pos;
1189 char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1];
1190 fat_dentry_t *d;
1191
1192 assert(nodep->type == FAT_DIRECTORY);
1193 assert(nodep->size % bps == 0);
1194 assert(bps % sizeof(fat_dentry_t) == 0);
1195
1196 /*
1197 * Our strategy for readdir() is to use the position pointer as
1198 * an index into the array of all dentries. On entry, it points
1199 * to the first unread dentry. If we skip any dentries, we bump
1200 * the position pointer accordingly.
1201 */
1202 bnum = (pos * sizeof(fat_dentry_t)) / bps;
1203 while (bnum < nodep->size / bps) {
1204 off_t o;
1205
1206 rc = fat_block_get(&b, bs, nodep, bnum,
1207 BLOCK_FLAGS_NONE);
1208 if (rc != EOK)
1209 goto err;
1210 for (o = pos % (bps / sizeof(fat_dentry_t));
1211 o < bps / sizeof(fat_dentry_t);
1212 o++, pos++) {
1213 d = ((fat_dentry_t *)b->data) + o;
1214 switch (fat_classify_dentry(d)) {
1215 case FAT_DENTRY_SKIP:
1216 case FAT_DENTRY_FREE:
1217 continue;
1218 case FAT_DENTRY_LAST:
1219 rc = block_put(b);
1220 if (rc != EOK)
1221 goto err;
1222 goto miss;
1223 default:
1224 case FAT_DENTRY_VALID:
1225 fat_dentry_name_get(d, name);
1226 rc = block_put(b);
1227 if (rc != EOK)
1228 goto err;
1229 goto hit;
1230 }
1231 }
1232 rc = block_put(b);
1233 if (rc != EOK)
1234 goto err;
1235 bnum++;
1236 }
1237miss:
1238 rc = fat_node_put(fn);
1239 ipc_answer_0(callid, rc != EOK ? rc : ENOENT);
1240 ipc_answer_1(rid, rc != EOK ? rc : ENOENT, 0);
1241 return;
1242
1243err:
1244 (void) fat_node_put(fn);
1245 ipc_answer_0(callid, rc);
1246 ipc_answer_0(rid, rc);
1247 return;
1248
1249hit:
1250 (void) async_data_read_finalize(callid, name, str_size(name) + 1);
1251 bytes = (pos - spos) + 1;
1252 }
1253
1254 rc = fat_node_put(fn);
1255 ipc_answer_1(rid, rc, (ipcarg_t)bytes);
1256}
1257
1258void fat_write(ipc_callid_t rid, ipc_call_t *request)
1259{
1260 dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
1261 fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
1262 off_t pos = (off_t)IPC_GET_ARG3(*request);
1263 fs_node_t *fn;
1264 fat_node_t *nodep;
1265 fat_bs_t *bs;
1266 size_t bytes, size;
1267 block_t *b;
1268 uint16_t bps;
1269 unsigned spc;
1270 unsigned bpc; /* bytes per cluster */
1271 off_t boundary;
1272 int flags = BLOCK_FLAGS_NONE;
1273 int rc;
1274
1275 rc = fat_node_get(&fn, dev_handle, index);
1276 if (rc != EOK) {
1277 ipc_answer_0(rid, rc);
1278 return;
1279 }
1280 if (!fn) {
1281 ipc_answer_0(rid, ENOENT);
1282 return;
1283 }
1284 nodep = FAT_NODE(fn);
1285
1286 ipc_callid_t callid;
1287 size_t len;
1288 if (!async_data_write_receive(&callid, &len)) {
1289 (void) fat_node_put(fn);
1290 ipc_answer_0(callid, EINVAL);
1291 ipc_answer_0(rid, EINVAL);
1292 return;
1293 }
1294
1295 bs = block_bb_get(dev_handle);
1296 bps = uint16_t_le2host(bs->bps);
1297 spc = bs->spc;
1298 bpc = bps * spc;
1299
1300 /*
1301 * In all scenarios, we will attempt to write out only one block worth
1302 * of data at maximum. There might be some more efficient approaches,
1303 * but this one greatly simplifies fat_write(). Note that we can afford
1304 * to do this because the client must be ready to handle the return
1305 * value signalizing a smaller number of bytes written.
1306 */
1307 bytes = min(len, bps - pos % bps);
1308 if (bytes == bps)
1309 flags |= BLOCK_FLAGS_NOREAD;
1310
1311 boundary = ROUND_UP(nodep->size, bpc);
1312 if (pos < boundary) {
1313 /*
1314 * This is the easier case - we are either overwriting already
1315 * existing contents or writing behind the EOF, but still within
1316 * the limits of the last cluster. The node size may grow to the
1317 * next block size boundary.
1318 */
1319 rc = fat_fill_gap(bs, nodep, FAT_CLST_RES0, pos);
1320 if (rc != EOK) {
1321 (void) fat_node_put(fn);
1322 ipc_answer_0(callid, rc);
1323 ipc_answer_0(rid, rc);
1324 return;
1325 }
1326 rc = fat_block_get(&b, bs, nodep, pos / bps, flags);
1327 if (rc != EOK) {
1328 (void) fat_node_put(fn);
1329 ipc_answer_0(callid, rc);
1330 ipc_answer_0(rid, rc);
1331 return;
1332 }
1333 (void) async_data_write_finalize(callid, b->data + pos % bps,
1334 bytes);
1335 b->dirty = true; /* need to sync block */
1336 rc = block_put(b);
1337 if (rc != EOK) {
1338 (void) fat_node_put(fn);
1339 ipc_answer_0(rid, rc);
1340 return;
1341 }
1342 if (pos + bytes > nodep->size) {
1343 nodep->size = pos + bytes;
1344 nodep->dirty = true; /* need to sync node */
1345 }
1346 size = nodep->size;
1347 rc = fat_node_put(fn);
1348 ipc_answer_2(rid, rc, bytes, nodep->size);
1349 return;
1350 } else {
1351 /*
1352 * This is the more difficult case. We must allocate new
1353 * clusters for the node and zero them out.
1354 */
1355 unsigned nclsts;
1356 fat_cluster_t mcl, lcl;
1357
1358 nclsts = (ROUND_UP(pos + bytes, bpc) - boundary) / bpc;
1359 /* create an independent chain of nclsts clusters in all FATs */
1360 rc = fat_alloc_clusters(bs, dev_handle, nclsts, &mcl, &lcl);
1361 if (rc != EOK) {
1362 /* could not allocate a chain of nclsts clusters */
1363 (void) fat_node_put(fn);
1364 ipc_answer_0(callid, rc);
1365 ipc_answer_0(rid, rc);
1366 return;
1367 }
1368 /* zero fill any gaps */
1369 rc = fat_fill_gap(bs, nodep, mcl, pos);
1370 if (rc != EOK) {
1371 (void) fat_free_clusters(bs, dev_handle, mcl);
1372 (void) fat_node_put(fn);
1373 ipc_answer_0(callid, rc);
1374 ipc_answer_0(rid, rc);
1375 return;
1376 }
1377 rc = _fat_block_get(&b, bs, dev_handle, lcl, (pos / bps) % spc,
1378 flags);
1379 if (rc != EOK) {
1380 (void) fat_free_clusters(bs, dev_handle, mcl);
1381 (void) fat_node_put(fn);
1382 ipc_answer_0(callid, rc);
1383 ipc_answer_0(rid, rc);
1384 return;
1385 }
1386 (void) async_data_write_finalize(callid, b->data + pos % bps,
1387 bytes);
1388 b->dirty = true; /* need to sync block */
1389 rc = block_put(b);
1390 if (rc != EOK) {
1391 (void) fat_free_clusters(bs, dev_handle, mcl);
1392 (void) fat_node_put(fn);
1393 ipc_answer_0(rid, rc);
1394 return;
1395 }
1396 /*
1397 * Append the cluster chain starting in mcl to the end of the
1398 * node's cluster chain.
1399 */
1400 rc = fat_append_clusters(bs, nodep, mcl);
1401 if (rc != EOK) {
1402 (void) fat_free_clusters(bs, dev_handle, mcl);
1403 (void) fat_node_put(fn);
1404 ipc_answer_0(rid, rc);
1405 return;
1406 }
1407 nodep->size = size = pos + bytes;
1408 nodep->dirty = true; /* need to sync node */
1409 rc = fat_node_put(fn);
1410 ipc_answer_2(rid, rc, bytes, size);
1411 return;
1412 }
1413}
1414
1415void fat_truncate(ipc_callid_t rid, ipc_call_t *request)
1416{
1417 dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
1418 fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
1419 size_t size = (off_t)IPC_GET_ARG3(*request);
1420 fs_node_t *fn;
1421 fat_node_t *nodep;
1422 fat_bs_t *bs;
1423 uint16_t bps;
1424 uint8_t spc;
1425 unsigned bpc; /* bytes per cluster */
1426 int rc;
1427
1428 rc = fat_node_get(&fn, dev_handle, index);
1429 if (rc != EOK) {
1430 ipc_answer_0(rid, rc);
1431 return;
1432 }
1433 if (!fn) {
1434 ipc_answer_0(rid, ENOENT);
1435 return;
1436 }
1437 nodep = FAT_NODE(fn);
1438
1439 bs = block_bb_get(dev_handle);
1440 bps = uint16_t_le2host(bs->bps);
1441 spc = bs->spc;
1442 bpc = bps * spc;
1443
1444 if (nodep->size == size) {
1445 rc = EOK;
1446 } else if (nodep->size < size) {
1447 /*
1448 * The standard says we have the freedom to grow the node.
1449 * For now, we simply return an error.
1450 */
1451 rc = EINVAL;
1452 } else if (ROUND_UP(nodep->size, bpc) == ROUND_UP(size, bpc)) {
1453 /*
1454 * The node will be shrunk, but no clusters will be deallocated.
1455 */
1456 nodep->size = size;
1457 nodep->dirty = true; /* need to sync node */
1458 rc = EOK;
1459 } else {
1460 /*
1461 * The node will be shrunk, clusters will be deallocated.
1462 */
1463 if (size == 0) {
1464 rc = fat_chop_clusters(bs, nodep, FAT_CLST_RES0);
1465 if (rc != EOK)
1466 goto out;
1467 } else {
1468 fat_cluster_t lastc;
1469 rc = fat_cluster_walk(bs, dev_handle, nodep->firstc,
1470 &lastc, NULL, (size - 1) / bpc);
1471 if (rc != EOK)
1472 goto out;
1473 rc = fat_chop_clusters(bs, nodep, lastc);
1474 if (rc != EOK)
1475 goto out;
1476 }
1477 nodep->size = size;
1478 nodep->dirty = true; /* need to sync node */
1479 rc = EOK;
1480 }
1481out:
1482 fat_node_put(fn);
1483 ipc_answer_0(rid, rc);
1484 return;
1485}
1486
1487void fat_close(ipc_callid_t rid, ipc_call_t *request)
1488{
1489 ipc_answer_0(rid, EOK);
1490}
1491
1492void fat_destroy(ipc_callid_t rid, ipc_call_t *request)
1493{
1494 dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
1495 fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
1496 fs_node_t *fn;
1497 int rc;
1498
1499 rc = fat_node_get(&fn, dev_handle, index);
1500 if (rc != EOK) {
1501 ipc_answer_0(rid, rc);
1502 return;
1503 }
1504 if (!fn) {
1505 ipc_answer_0(rid, ENOENT);
1506 return;
1507 }
1508
1509 rc = fat_destroy_node(fn);
1510 ipc_answer_0(rid, rc);
1511}
1512
1513void fat_open_node(ipc_callid_t rid, ipc_call_t *request)
1514{
1515 libfs_open_node(&fat_libfs_ops, fat_reg.fs_handle, rid, request);
1516}
1517
1518void fat_stat(ipc_callid_t rid, ipc_call_t *request)
1519{
1520 libfs_stat(&fat_libfs_ops, fat_reg.fs_handle, rid, request);
1521}
1522
1523void fat_sync(ipc_callid_t rid, ipc_call_t *request)
1524{
1525 /* Dummy implementation */
1526 ipc_answer_0(rid, EOK);
1527}
1528
1529/**
1530 * @}
1531 */
Note: See TracBrowser for help on using the repository browser.