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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 675a2bb was 675a2bb, checked in by Manuele Conti <conti.ma@…>, 12 years ago

Fix typo into free count fat operation.

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