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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 979c313a was b5db2ae, checked in by Oleg Romanenko <romanenko.oleg@…>, 14 years ago
  1. Add macros for determining first cluster of root directory (FAT_ROOT_CLST)
  2. Root directory (FAT32) can consist of many clusters rather than of only one.
  3. Using FAT_CLST_ROOT value to determining root directory first cluster

is applicable only to FAT12/16 filesystem.

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