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

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

Using new directory api to support creating long names

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