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

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

Do not hold the parent→idx→lock in when calling fat_idx_get_by_pos() as it
both is not necessary and violates the natural locking order used by
fat_idx_get_by_pos() and fat_idx_get_by_index().

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