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

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

improve naming conventions:
merge async_data_receive() and async_string_receive() into async_data_write_accept()
rename async_data_void() to async_data_write_void()
rename async_data_forward_fast() to async_data_write_forward_fast()
rename async_data_forward_n_m to async_data_write_forward_n_m

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