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

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

Add fat_node_fini_by_dev_handle() and finish fat_unmounted().

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