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

lfn serial ticket/834-toolchain-update topic/fix-logger-deadlock topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 616e73c was 616e73c, checked in by Oleg Romanenko <romanenko.oleg@…>, 15 years ago

Minor fixes

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