source: mainline/uspace/srv/fs/fat/fat_ops.c@ 6da81e0

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 6da81e0 was 6da81e0, checked in by Jakub Jermar <jakub@…>, 15 years ago

Modify _fat_block_get() to return the "current" cluster number to the caller.

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