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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 9256ad29 was 19f857a, checked in by Jiri Svoboda <jiri@…>, 15 years ago

Rename string.h to str.h to avoid header conflict with standard C string.h.

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