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

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

Minor fixes for update

  • Property mode set to 100644
File size: 32.8 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. */
[b72efe8]72static LIST_INITIALIZE(ffn_list);
[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 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;
[616e73c]109 node->lastc_cached_value = 0;
[dba4a23]110 node->currc_cached_valid = false;
111 node->currc_cached_bn = 0;
[616e73c]112 node->currc_cached_value = 0;
[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 fat_node_t *nodep;
152 int rc;
153
154 /*
155 * We are called from fat_unmounted() and assume that there are already
156 * no nodes belonging to this instance with non-zero refcount. Therefore
157 * it is sufficient to clean up only the FAT free node list.
158 */
159
160restart:
161 fibril_mutex_lock(&ffn_mutex);
[b72efe8]162 list_foreach(ffn_list, lnk) {
[430de97]163 nodep = list_get_instance(lnk, fat_node_t, ffn_link);
164 if (!fibril_mutex_trylock(&nodep->lock)) {
165 fibril_mutex_unlock(&ffn_mutex);
166 goto restart;
167 }
168 if (!fibril_mutex_trylock(&nodep->idx->lock)) {
169 fibril_mutex_unlock(&nodep->lock);
170 fibril_mutex_unlock(&ffn_mutex);
171 goto restart;
172 }
[991f645]173 if (nodep->idx->devmap_handle != devmap_handle) {
[430de97]174 fibril_mutex_unlock(&nodep->idx->lock);
175 fibril_mutex_unlock(&nodep->lock);
176 continue;
177 }
178
179 list_remove(&nodep->ffn_link);
180 fibril_mutex_unlock(&ffn_mutex);
181
182 /*
183 * We can unlock the node and its index structure because we are
184 * the last player on this playground and VFS is preventing new
185 * players from entering.
186 */
187 fibril_mutex_unlock(&nodep->idx->lock);
188 fibril_mutex_unlock(&nodep->lock);
189
190 if (nodep->dirty) {
191 rc = fat_node_sync(nodep);
192 if (rc != EOK)
193 return rc;
194 }
195 nodep->idx->nodep = NULL;
196 free(nodep->bp);
197 free(nodep);
198
[b72efe8]199 /* Need to restart because we changed ffn_list. */
[430de97]200 goto restart;
201 }
202 fibril_mutex_unlock(&ffn_mutex);
203
204 return EOK;
205}
206
[17bf658]207static int fat_node_get_new(fat_node_t **nodepp)
[9a3d5f0]208{
[b6035ba]209 fs_node_t *fn;
[9a3d5f0]210 fat_node_t *nodep;
[4098e38]211 int rc;
[9a3d5f0]212
[6ebe721]213 fibril_mutex_lock(&ffn_mutex);
[b72efe8]214 if (!list_empty(&ffn_list)) {
[9a3d5f0]215 /* Try to use a cached free node structure. */
216 fat_idx_t *idxp_tmp;
[b72efe8]217 nodep = list_get_instance(list_first(&ffn_list), fat_node_t,
218 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 */
[5a9a1aaf]334 uint32_t clusters;
[0182e5cc]335 rc = fat_clusters_get(&clusters, bs, idxp->devmap_handle, nodep->firstc);
[0fc1e5d]336 if (rc != EOK) {
[9fec913]337 (void) block_put(b);
[0fc1e5d]338 (void) fat_node_put(FS_NODE(nodep));
339 return rc;
340 }
[7a23d60]341 nodep->size = BPS(bs) * SPC(bs) * clusters;
[2c4bbcde]342 } else {
343 nodep->type = FAT_FILE;
[2ab1023]344 nodep->size = uint32_t_le2host(d->size);
[2c4bbcde]345 }
[97bc3ee]346
[2c4bbcde]347 nodep->lnkcnt = 1;
348 nodep->refcnt = 1;
349
[c91f2d1b]350 rc = block_put(b);
[0fc1e5d]351 if (rc != EOK) {
352 (void) fat_node_put(FS_NODE(nodep));
353 return rc;
354 }
[2c4bbcde]355
356 /* Link the idx structure with the node structure. */
[add5835]357 nodep->idx = idxp;
358 idxp->nodep = nodep;
[2c4bbcde]359
[0fc1e5d]360 *nodepp = nodep;
361 return EOK;
[a2aa1dec]362}
363
[50e5b25]364/*
365 * FAT libfs operations.
366 */
367
[991f645]368int fat_root_get(fs_node_t **rfn, devmap_handle_t devmap_handle)
[073f550]369{
[991f645]370 return fat_node_get(rfn, devmap_handle, 0);
[073f550]371}
372
373int fat_match(fs_node_t **rfn, fs_node_t *pfn, const char *component)
374{
375 fat_node_t *parentp = FAT_NODE(pfn);
[cefd3ec]376 char name[FAT_LFN_NAME_SIZE];
[073f550]377 fat_dentry_t *d;
[991f645]378 devmap_handle_t devmap_handle;
[073f550]379 int rc;
380
381 fibril_mutex_lock(&parentp->idx->lock);
[991f645]382 devmap_handle = parentp->idx->devmap_handle;
[a93d79a]383 fibril_mutex_unlock(&parentp->idx->lock);
[cefd3ec]384
385 fat_directory_t di;
[620a367]386 rc = fat_directory_open(parentp, &di);
387 if (rc != EOK)
388 return rc;
[cefd3ec]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);
[b72efe8]465 list_append(&nodep->ffn_link, &ffn_list);
[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
[af4dec0]599 if (!fat_valid_name(name))
[bba3d90]600 return ENOTSUP;
[97bc3ee]601
[6ebe721]602 fibril_mutex_lock(&parentp->idx->lock);
[991f645]603 bs = block_bb_get(parentp->idx->devmap_handle);
[620a367]604 rc = fat_directory_open(parentp, &di);
605 if (rc != EOK)
606 return rc;
[0fdd6bb]607
608 /*
609 * At this point we only establish the link between the parent and the
610 * child. The dentry, except of the name and the extension, will remain
[e32b65a]611 * uninitialized until the corresponding node is synced. Thus the valid
612 * dentry data is kept in the child node structure.
[0fdd6bb]613 */
[bba3d90]614 memset(&de, 0, sizeof(fat_dentry_t));
615
616 rc = fat_directory_write(&di, name, &de);
617 if (rc!=EOK)
618 return rc;
619 rc = fat_directory_close(&di);
620 if (rc!=EOK)
621 return rc;
622
[6ebe721]623 fibril_mutex_unlock(&parentp->idx->lock);
[97bc3ee]624 if (rc != EOK)
[4b4668e]625 return rc;
[0fdd6bb]626
[6ebe721]627 fibril_mutex_lock(&childp->idx->lock);
[97bc3ee]628
[24a2517]629 if (childp->type == FAT_DIRECTORY) {
[4b4668e]630 /*
[24a2517]631 * If possible, create the Sub-directory Identifier Entry and
632 * the Sub-directory Parent Pointer Entry (i.e. "." and "..").
633 * These entries are not mandatory according to Standard
634 * ECMA-107 and HelenOS VFS does not use them anyway, so this is
635 * rather a sign of our good will.
[4b4668e]636 */
[24a2517]637 rc = fat_block_get(&b, bs, childp, 0, BLOCK_FLAGS_NONE);
638 if (rc != EOK) {
639 /*
640 * Rather than returning an error, simply skip the
641 * creation of these two entries.
642 */
643 goto skip_dots;
644 }
[ed903174]645 d = (fat_dentry_t *) b->data;
646 if ((fat_classify_dentry(d) == FAT_DENTRY_LAST) ||
[ce8f4f4]647 (bcmp(d->name, FAT_NAME_DOT, FAT_NAME_LEN)) == 0) {
[24a2517]648 memset(d, 0, sizeof(fat_dentry_t));
[b62dc100]649 memcpy(d->name, FAT_NAME_DOT, FAT_NAME_LEN);
650 memcpy(d->ext, FAT_EXT_PAD, FAT_EXT_LEN);
[24a2517]651 d->attr = FAT_ATTR_SUBDIR;
652 d->firstc = host2uint16_t_le(childp->firstc);
653 /* TODO: initialize also the date/time members. */
654 }
655 d++;
[ed903174]656 if ((fat_classify_dentry(d) == FAT_DENTRY_LAST) ||
[ce8f4f4]657 (bcmp(d->name, FAT_NAME_DOT_DOT, FAT_NAME_LEN) == 0)) {
[24a2517]658 memset(d, 0, sizeof(fat_dentry_t));
[b62dc100]659 memcpy(d->name, FAT_NAME_DOT_DOT, FAT_NAME_LEN);
660 memcpy(d->ext, FAT_EXT_PAD, FAT_EXT_LEN);
[24a2517]661 d->attr = FAT_ATTR_SUBDIR;
[b5db2ae]662 d->firstc = (parentp->firstc == FAT_ROOT_CLST(bs)) ?
663 host2uint16_t_le(FAT_CLST_ROOTPAR) :
[24a2517]664 host2uint16_t_le(parentp->firstc);
665 /* TODO: initialize also the date/time members. */
666 }
667 b->dirty = true; /* need to sync block */
668 /*
669 * Ignore the return value as we would have fallen through on error
670 * anyway.
671 */
672 (void) block_put(b);
[1baec4b]673 }
[4b4668e]674skip_dots:
[1baec4b]675
[0fdd6bb]676 childp->idx->pfc = parentp->firstc;
[bba3d90]677 childp->idx->pdi = di.pos; /* di.pos holds absolute position of SFN entry */
[6ebe721]678 fibril_mutex_unlock(&childp->idx->lock);
[0fdd6bb]679
[6ebe721]680 fibril_mutex_lock(&childp->lock);
[0fdd6bb]681 childp->lnkcnt = 1;
682 childp->dirty = true; /* need to sync node */
[6ebe721]683 fibril_mutex_unlock(&childp->lock);
[0fdd6bb]684
685 /*
686 * Hash in the index structure into the position hash.
687 */
688 fat_idx_hashin(childp->idx);
689
690 return EOK;
[80e8482]691}
692
[cf95bc0]693int fat_unlink(fs_node_t *pfn, fs_node_t *cfn, const char *nm)
[80e8482]694{
[b6035ba]695 fat_node_t *parentp = FAT_NODE(pfn);
696 fat_node_t *childp = FAT_NODE(cfn);
[073f550]697 bool has_children;
[c91f2d1b]698 int rc;
[a31c1ccf]699
[770d281]700 if (!parentp)
701 return EBUSY;
[97bc3ee]702
[073f550]703 rc = fat_has_children(&has_children, cfn);
704 if (rc != EOK)
705 return rc;
706 if (has_children)
[0be3e8b]707 return ENOTEMPTY;
[770d281]708
[6ebe721]709 fibril_mutex_lock(&parentp->lock);
710 fibril_mutex_lock(&childp->lock);
[a31c1ccf]711 assert(childp->lnkcnt == 1);
[6ebe721]712 fibril_mutex_lock(&childp->idx->lock);
[b85c19a]713
714 fat_directory_t di;
715 rc = fat_directory_open(parentp,&di);
[97bc3ee]716 if (rc != EOK)
[46c0498]717 goto error;
[b85c19a]718 rc = fat_directory_seek(&di, childp->idx->pdi);
719 if (rc != EOK)
720 goto error;
721 rc = fat_directory_erase(&di);
722 if (rc != EOK)
723 goto error;
724 rc = fat_directory_close(&di);
[46c0498]725 if (rc != EOK)
726 goto error;
[a31c1ccf]727
728 /* remove the index structure from the position hash */
729 fat_idx_hashout(childp->idx);
730 /* clear position information */
731 childp->idx->pfc = FAT_CLST_RES0;
732 childp->idx->pdi = 0;
[6ebe721]733 fibril_mutex_unlock(&childp->idx->lock);
[a31c1ccf]734 childp->lnkcnt = 0;
[5ca5eaa7]735 childp->refcnt++; /* keep the node in memory until destroyed */
[a31c1ccf]736 childp->dirty = true;
[6ebe721]737 fibril_mutex_unlock(&childp->lock);
738 fibril_mutex_unlock(&parentp->lock);
[a31c1ccf]739
740 return EOK;
[46c0498]741
742error:
[b85c19a]743 (void) fat_directory_close(&di);
[46c0498]744 fibril_mutex_unlock(&childp->idx->lock);
[b85c19a]745 fibril_mutex_unlock(&childp->lock);
746 fibril_mutex_unlock(&parentp->lock);
[46c0498]747 return rc;
[80e8482]748}
749
[073f550]750int fat_has_children(bool *has_children, fs_node_t *fn)
[32fb10ed]751{
[7858bc5f]752 fat_bs_t *bs;
[b6035ba]753 fat_node_t *nodep = FAT_NODE(fn);
[32fb10ed]754 unsigned blocks;
[7858bc5f]755 block_t *b;
[32fb10ed]756 unsigned i, j;
[c91f2d1b]757 int rc;
[32fb10ed]758
[073f550]759 if (nodep->type != FAT_DIRECTORY) {
760 *has_children = false;
761 return EOK;
762 }
[97bc3ee]763
[6ebe721]764 fibril_mutex_lock(&nodep->idx->lock);
[991f645]765 bs = block_bb_get(nodep->idx->devmap_handle);
[32fb10ed]766
[7a23d60]767 blocks = nodep->size / BPS(bs);
[32fb10ed]768
769 for (i = 0; i < blocks; i++) {
770 fat_dentry_t *d;
[97bc3ee]771
[684b655]772 rc = fat_block_get(&b, bs, nodep, i, BLOCK_FLAGS_NONE);
[073f550]773 if (rc != EOK) {
774 fibril_mutex_unlock(&nodep->idx->lock);
775 return rc;
776 }
[7a23d60]777 for (j = 0; j < DPS(bs); j++) {
[32fb10ed]778 d = ((fat_dentry_t *)b->data) + j;
779 switch (fat_classify_dentry(d)) {
780 case FAT_DENTRY_SKIP:
[0fdd6bb]781 case FAT_DENTRY_FREE:
[32fb10ed]782 continue;
783 case FAT_DENTRY_LAST:
[c91f2d1b]784 rc = block_put(b);
[6ebe721]785 fibril_mutex_unlock(&nodep->idx->lock);
[073f550]786 *has_children = false;
[8810c63]787 return rc;
[32fb10ed]788 default:
789 case FAT_DENTRY_VALID:
[c91f2d1b]790 rc = block_put(b);
[6ebe721]791 fibril_mutex_unlock(&nodep->idx->lock);
[073f550]792 *has_children = true;
[8810c63]793 return rc;
[32fb10ed]794 }
795 }
[c91f2d1b]796 rc = block_put(b);
[8810c63]797 if (rc != EOK) {
798 fibril_mutex_unlock(&nodep->idx->lock);
[97bc3ee]799 return rc;
[8810c63]800 }
[32fb10ed]801 }
802
[6ebe721]803 fibril_mutex_unlock(&nodep->idx->lock);
[073f550]804 *has_children = false;
805 return EOK;
806}
807
808
809fs_index_t fat_index_get(fs_node_t *fn)
810{
811 return FAT_NODE(fn)->idx->index;
812}
813
[ed903174]814aoff64_t fat_size_get(fs_node_t *fn)
[073f550]815{
816 return FAT_NODE(fn)->size;
[32fb10ed]817}
818
[073f550]819unsigned fat_lnkcnt_get(fs_node_t *fn)
[74ea3c6]820{
[073f550]821 return FAT_NODE(fn)->lnkcnt;
[74ea3c6]822}
823
[b6035ba]824bool fat_is_directory(fs_node_t *fn)
[e1e3b26]825{
[b6035ba]826 return FAT_NODE(fn)->type == FAT_DIRECTORY;
[e1e3b26]827}
828
[b6035ba]829bool fat_is_file(fs_node_t *fn)
[e1e3b26]830{
[b6035ba]831 return FAT_NODE(fn)->type == FAT_FILE;
[e1e3b26]832}
833
[991f645]834devmap_handle_t fat_device_get(fs_node_t *node)
[1313ee9]835{
836 return 0;
837}
838
[a2aa1dec]839/** libfs operations */
840libfs_ops_t fat_libfs_ops = {
[073f550]841 .root_get = fat_root_get,
[a2aa1dec]842 .match = fat_match,
843 .node_get = fat_node_get,
[1313ee9]844 .node_open = fat_node_open,
[06901c6b]845 .node_put = fat_node_put,
[6571b78]846 .create = fat_create_node,
847 .destroy = fat_destroy_node,
[80e8482]848 .link = fat_link,
849 .unlink = fat_unlink,
[073f550]850 .has_children = fat_has_children,
[e1e3b26]851 .index_get = fat_index_get,
852 .size_get = fat_size_get,
853 .lnkcnt_get = fat_lnkcnt_get,
854 .is_directory = fat_is_directory,
[1313ee9]855 .is_file = fat_is_file,
856 .device_get = fat_device_get
[a2aa1dec]857};
858
[0013b9ce]859/*
[efcebe1]860 * FAT VFS_OUT operations.
[0013b9ce]861 */
862
[efcebe1]863static int
864fat_mounted(devmap_handle_t devmap_handle, const char *opts, fs_index_t *index,
865 aoff64_t *size, unsigned *linkcnt)
[cde485d]866{
[1fbe064b]867 enum cache_mode cmode;
[7858bc5f]868 fat_bs_t *bs;
[efcebe1]869 int rc;
[594303b]870
[1fbe064b]871 /* Check for option enabling write through. */
872 if (str_cmp(opts, "wtcache") == 0)
873 cmode = CACHE_MODE_WT;
874 else
875 cmode = CACHE_MODE_WB;
876
[7858bc5f]877 /* initialize libblock */
[79ae36dd]878 rc = block_init(EXCHANGE_SERIALIZE, devmap_handle, BS_SIZE);
[efcebe1]879 if (rc != EOK)
880 return rc;
[6284978]881
882 /* prepare the boot block */
[991f645]883 rc = block_bb_read(devmap_handle, BS_BLOCK);
[6284978]884 if (rc != EOK) {
[991f645]885 block_fini(devmap_handle);
[efcebe1]886 return rc;
[7a35204a]887 }
888
[7858bc5f]889 /* get the buffer with the boot sector */
[991f645]890 bs = block_bb_get(devmap_handle);
[97bc3ee]891
[7a23d60]892 if (BPS(bs) != BS_SIZE) {
[991f645]893 block_fini(devmap_handle);
[efcebe1]894 return ENOTSUP;
[7a35204a]895 }
896
[f1ba5d6]897 /* Initialize the block cache */
[991f645]898 rc = block_cache_init(devmap_handle, BPS(bs), 0 /* XXX */, cmode);
[f1ba5d6]899 if (rc != EOK) {
[991f645]900 block_fini(devmap_handle);
[efcebe1]901 return rc;
[f1ba5d6]902 }
903
[2ffaab5]904 /* Do some simple sanity checks on the file system. */
[991f645]905 rc = fat_sanity_check(bs, devmap_handle);
[711e1f32]906 if (rc != EOK) {
[991f645]907 (void) block_cache_fini(devmap_handle);
908 block_fini(devmap_handle);
[efcebe1]909 return rc;
[711e1f32]910 }
911
[991f645]912 rc = fat_idx_init_by_devmap_handle(devmap_handle);
[cde485d]913 if (rc != EOK) {
[991f645]914 (void) block_cache_fini(devmap_handle);
915 block_fini(devmap_handle);
[efcebe1]916 return rc;
[cde485d]917 }
918
[689f036]919 /* Initialize the root node. */
[b6035ba]920 fs_node_t *rfn = (fs_node_t *)malloc(sizeof(fs_node_t));
921 if (!rfn) {
[991f645]922 (void) block_cache_fini(devmap_handle);
923 block_fini(devmap_handle);
924 fat_idx_fini_by_devmap_handle(devmap_handle);
[efcebe1]925 return ENOMEM;
[b6035ba]926 }
[0182e5cc]927
[83937ccd]928 fs_node_initialize(rfn);
[689f036]929 fat_node_t *rootp = (fat_node_t *)malloc(sizeof(fat_node_t));
930 if (!rootp) {
[b6035ba]931 free(rfn);
[991f645]932 (void) block_cache_fini(devmap_handle);
933 block_fini(devmap_handle);
934 fat_idx_fini_by_devmap_handle(devmap_handle);
[efcebe1]935 return ENOMEM;
[689f036]936 }
937 fat_node_initialize(rootp);
938
[991f645]939 fat_idx_t *ridxp = fat_idx_get_by_pos(devmap_handle, FAT_CLST_ROOTPAR, 0);
[689f036]940 if (!ridxp) {
[b6035ba]941 free(rfn);
[689f036]942 free(rootp);
[991f645]943 (void) block_cache_fini(devmap_handle);
944 block_fini(devmap_handle);
945 fat_idx_fini_by_devmap_handle(devmap_handle);
[efcebe1]946 return ENOMEM;
[689f036]947 }
948 assert(ridxp->index == 0);
949 /* ridxp->lock held */
950
951 rootp->type = FAT_DIRECTORY;
[b5db2ae]952 rootp->firstc = FAT_ROOT_CLST(bs);
[689f036]953 rootp->refcnt = 1;
[5ab597d]954 rootp->lnkcnt = 0; /* FS root is not linked */
[b5db2ae]955
956 if (FAT_IS_FAT32(bs)) {
[5a9a1aaf]957 uint32_t clusters;
[b5db2ae]958 rc = fat_clusters_get(&clusters, bs, devmap_handle, rootp->firstc);
959 if (rc != EOK) {
960 free(rfn);
961 free(rootp);
962 (void) block_cache_fini(devmap_handle);
963 block_fini(devmap_handle);
964 fat_idx_fini_by_devmap_handle(devmap_handle);
[1940326]965 return ENOTSUP;
[b5db2ae]966 }
967 rootp->size = BPS(bs) * SPC(bs) * clusters;
968 } else
969 rootp->size = RDE(bs) * sizeof(fat_dentry_t);
970
[689f036]971 rootp->idx = ridxp;
972 ridxp->nodep = rootp;
[b6035ba]973 rootp->bp = rfn;
974 rfn->data = rootp;
[97bc3ee]975
[6ebe721]976 fibril_mutex_unlock(&ridxp->lock);
[689f036]977
[efcebe1]978 *index = ridxp->index;
979 *size = rootp->size;
980 *linkcnt = rootp->lnkcnt;
[cde485d]981
[efcebe1]982 return EOK;
[cde485d]983}
984
[efcebe1]985static int fat_unmounted(devmap_handle_t devmap_handle)
[3c11713]986{
[430de97]987 fs_node_t *fn;
988 fat_node_t *nodep;
989 int rc;
990
[991f645]991 rc = fat_root_get(&fn, devmap_handle);
[efcebe1]992 if (rc != EOK)
993 return rc;
[430de97]994 nodep = FAT_NODE(fn);
995
996 /*
997 * We expect exactly two references on the root node. One for the
998 * fat_root_get() above and one created in fat_mounted().
999 */
1000 if (nodep->refcnt != 2) {
1001 (void) fat_node_put(fn);
[efcebe1]1002 return EBUSY;
[430de97]1003 }
[97bc3ee]1004
[430de97]1005 /*
1006 * Put the root node and force it to the FAT free node list.
1007 */
1008 (void) fat_node_put(fn);
1009 (void) fat_node_put(fn);
1010
1011 /*
1012 * Perform cleanup of the node structures, index structures and
1013 * associated data. Write back this file system's dirty blocks and
1014 * stop using libblock for this instance.
1015 */
[991f645]1016 (void) fat_node_fini_by_devmap_handle(devmap_handle);
1017 fat_idx_fini_by_devmap_handle(devmap_handle);
1018 (void) block_cache_fini(devmap_handle);
1019 block_fini(devmap_handle);
[430de97]1020
[efcebe1]1021 return EOK;
[be815bc]1022}
1023
[efcebe1]1024static int
1025fat_read(devmap_handle_t devmap_handle, fs_index_t index, aoff64_t pos,
1026 size_t *rbytes)
[4bf40f6]1027{
[073f550]1028 fs_node_t *fn;
[b6035ba]1029 fat_node_t *nodep;
[7858bc5f]1030 fat_bs_t *bs;
[79d031b]1031 size_t bytes;
[7858bc5f]1032 block_t *b;
[c91f2d1b]1033 int rc;
[79d031b]1034
[991f645]1035 rc = fat_node_get(&fn, devmap_handle, index);
[efcebe1]1036 if (rc != EOK)
1037 return rc;
1038 if (!fn)
1039 return ENOENT;
[b6035ba]1040 nodep = FAT_NODE(fn);
[4bf40f6]1041
1042 ipc_callid_t callid;
1043 size_t len;
[0da4e41]1044 if (!async_data_read_receive(&callid, &len)) {
[b6035ba]1045 fat_node_put(fn);
[ffa2c8ef]1046 async_answer_0(callid, EINVAL);
[efcebe1]1047 return EINVAL;
[4bf40f6]1048 }
1049
[991f645]1050 bs = block_bb_get(devmap_handle);
[cb682eb]1051
[4bf40f6]1052 if (nodep->type == FAT_FILE) {
[ddd1219]1053 /*
1054 * Our strategy for regular file reads is to read one block at
1055 * most and make use of the possibility to return less data than
1056 * requested. This keeps the code very simple.
1057 */
[0d974d8]1058 if (pos >= nodep->size) {
[7d861950]1059 /* reading beyond the EOF */
1060 bytes = 0;
[0da4e41]1061 (void) async_data_read_finalize(callid, NULL, 0);
[0d974d8]1062 } else {
[7a23d60]1063 bytes = min(len, BPS(bs) - pos % BPS(bs));
[0d974d8]1064 bytes = min(bytes, nodep->size - pos);
[7a23d60]1065 rc = fat_block_get(&b, bs, nodep, pos / BPS(bs),
[1d8cdb1]1066 BLOCK_FLAGS_NONE);
[453f2e75]1067 if (rc != EOK) {
1068 fat_node_put(fn);
[ffa2c8ef]1069 async_answer_0(callid, rc);
[efcebe1]1070 return rc;
[453f2e75]1071 }
[7a23d60]1072 (void) async_data_read_finalize(callid,
1073 b->data + pos % BPS(bs), bytes);
[c91f2d1b]1074 rc = block_put(b);
[453f2e75]1075 if (rc != EOK) {
1076 fat_node_put(fn);
[efcebe1]1077 return rc;
[453f2e75]1078 }
[0d974d8]1079 }
[4bf40f6]1080 } else {
[ed903174]1081 aoff64_t spos = pos;
[65ccd23]1082 char name[FAT_LFN_NAME_SIZE];
[ddd1219]1083 fat_dentry_t *d;
1084
[4bf40f6]1085 assert(nodep->type == FAT_DIRECTORY);
[7a23d60]1086 assert(nodep->size % BPS(bs) == 0);
1087 assert(BPS(bs) % sizeof(fat_dentry_t) == 0);
[ddd1219]1088
[8a40c49]1089 fat_directory_t di;
[563686b]1090 rc = fat_directory_open(nodep, &di);
1091 if (rc != EOK) goto err;
1092 rc = fat_directory_seek(&di, pos);
1093 if (rc != EOK) {
1094 (void) fat_directory_close(&di);
1095 goto err;
1096 }
[ddd1219]1097
[8a40c49]1098 rc = fat_directory_read(&di, name, &d);
1099 if (rc == EOK) goto hit;
1100 if (rc == ENOENT) goto miss;
[453f2e75]1101
1102err:
1103 (void) fat_node_put(fn);
[ffa2c8ef]1104 async_answer_0(callid, rc);
[efcebe1]1105 return rc;
[453f2e75]1106
[8a40c49]1107miss:
1108 rc = fat_directory_close(&di);
1109 if (rc!=EOK)
1110 goto err;
1111 rc = fat_node_put(fn);
1112 async_answer_0(callid, rc != EOK ? rc : ENOENT);
[a33f0a6]1113 *rbytes = 0;
1114 return rc != EOK ? rc : ENOENT;
[8a40c49]1115
[ddd1219]1116hit:
[8a40c49]1117 pos = di.pos;
1118 rc = fat_directory_close(&di);
1119 if (rc!=EOK)
1120 goto err;
[0da4e41]1121 (void) async_data_read_finalize(callid, name, str_size(name) + 1);
[b85c19a]1122 bytes = (pos - spos)+1;
[4bf40f6]1123 }
1124
[453f2e75]1125 rc = fat_node_put(fn);
[efcebe1]1126 *rbytes = bytes;
1127 return rc;
[4bf40f6]1128}
1129
[efcebe1]1130static int
1131fat_write(devmap_handle_t devmap_handle, fs_index_t index, aoff64_t pos,
1132 size_t *wbytes, aoff64_t *nsize)
[c947dda]1133{
[073f550]1134 fs_node_t *fn;
[b6035ba]1135 fat_node_t *nodep;
[7858bc5f]1136 fat_bs_t *bs;
[efcebe1]1137 size_t bytes;
[7858bc5f]1138 block_t *b;
[ed903174]1139 aoff64_t boundary;
[1d8cdb1]1140 int flags = BLOCK_FLAGS_NONE;
[c91f2d1b]1141 int rc;
[97bc3ee]1142
[991f645]1143 rc = fat_node_get(&fn, devmap_handle, index);
[efcebe1]1144 if (rc != EOK)
1145 return rc;
1146 if (!fn)
1147 return ENOENT;
[b6035ba]1148 nodep = FAT_NODE(fn);
[97bc3ee]1149
[8d32152]1150 ipc_callid_t callid;
1151 size_t len;
[0da4e41]1152 if (!async_data_write_receive(&callid, &len)) {
[dfddfcd]1153 (void) fat_node_put(fn);
[ffa2c8ef]1154 async_answer_0(callid, EINVAL);
[efcebe1]1155 return EINVAL;
[8d32152]1156 }
1157
[991f645]1158 bs = block_bb_get(devmap_handle);
[913a821c]1159
[8d32152]1160 /*
1161 * In all scenarios, we will attempt to write out only one block worth
1162 * of data at maximum. There might be some more efficient approaches,
1163 * but this one greatly simplifies fat_write(). Note that we can afford
1164 * to do this because the client must be ready to handle the return
[97bc3ee]1165 * value signalizing a smaller number of bytes written.
1166 */
[7a23d60]1167 bytes = min(len, BPS(bs) - pos % BPS(bs));
1168 if (bytes == BPS(bs))
[1d8cdb1]1169 flags |= BLOCK_FLAGS_NOREAD;
[97bc3ee]1170
[7a23d60]1171 boundary = ROUND_UP(nodep->size, BPC(bs));
[b4b7187]1172 if (pos < boundary) {
[8d32152]1173 /*
1174 * This is the easier case - we are either overwriting already
1175 * existing contents or writing behind the EOF, but still within
1176 * the limits of the last cluster. The node size may grow to the
1177 * next block size boundary.
1178 */
[cca29e3c]1179 rc = fat_fill_gap(bs, nodep, FAT_CLST_RES0, pos);
[dfddfcd]1180 if (rc != EOK) {
1181 (void) fat_node_put(fn);
[ffa2c8ef]1182 async_answer_0(callid, rc);
[efcebe1]1183 return rc;
[dfddfcd]1184 }
[7a23d60]1185 rc = fat_block_get(&b, bs, nodep, pos / BPS(bs), flags);
[dfddfcd]1186 if (rc != EOK) {
1187 (void) fat_node_put(fn);
[ffa2c8ef]1188 async_answer_0(callid, rc);
[efcebe1]1189 return rc;
[dfddfcd]1190 }
[7a23d60]1191 (void) async_data_write_finalize(callid,
1192 b->data + pos % BPS(bs), bytes);
[8d32152]1193 b->dirty = true; /* need to sync block */
[c91f2d1b]1194 rc = block_put(b);
[dfddfcd]1195 if (rc != EOK) {
1196 (void) fat_node_put(fn);
[efcebe1]1197 return rc;
[dfddfcd]1198 }
[8d32152]1199 if (pos + bytes > nodep->size) {
1200 nodep->size = pos + bytes;
1201 nodep->dirty = true; /* need to sync node */
1202 }
[efcebe1]1203 *wbytes = bytes;
1204 *nsize = nodep->size;
[dfddfcd]1205 rc = fat_node_put(fn);
[efcebe1]1206 return rc;
[8d32152]1207 } else {
1208 /*
1209 * This is the more difficult case. We must allocate new
1210 * clusters for the node and zero them out.
1211 */
1212 unsigned nclsts;
[97bc3ee]1213 fat_cluster_t mcl, lcl;
1214
[7a23d60]1215 nclsts = (ROUND_UP(pos + bytes, BPC(bs)) - boundary) / BPC(bs);
[6f2dfd1]1216 /* create an independent chain of nclsts clusters in all FATs */
[991f645]1217 rc = fat_alloc_clusters(bs, devmap_handle, nclsts, &mcl, &lcl);
[dfddfcd]1218 if (rc != EOK) {
[6f2dfd1]1219 /* could not allocate a chain of nclsts clusters */
[dfddfcd]1220 (void) fat_node_put(fn);
[ffa2c8ef]1221 async_answer_0(callid, rc);
[efcebe1]1222 return rc;
[6f2dfd1]1223 }
1224 /* zero fill any gaps */
[cca29e3c]1225 rc = fat_fill_gap(bs, nodep, mcl, pos);
[dfddfcd]1226 if (rc != EOK) {
[991f645]1227 (void) fat_free_clusters(bs, devmap_handle, mcl);
[dfddfcd]1228 (void) fat_node_put(fn);
[ffa2c8ef]1229 async_answer_0(callid, rc);
[efcebe1]1230 return rc;
[dfddfcd]1231 }
[991f645]1232 rc = _fat_block_get(&b, bs, devmap_handle, lcl, NULL,
[7a23d60]1233 (pos / BPS(bs)) % SPC(bs), flags);
[dfddfcd]1234 if (rc != EOK) {
[991f645]1235 (void) fat_free_clusters(bs, devmap_handle, mcl);
[dfddfcd]1236 (void) fat_node_put(fn);
[ffa2c8ef]1237 async_answer_0(callid, rc);
[efcebe1]1238 return rc;
[dfddfcd]1239 }
[7a23d60]1240 (void) async_data_write_finalize(callid,
1241 b->data + pos % BPS(bs), bytes);
[b4b7187]1242 b->dirty = true; /* need to sync block */
[c91f2d1b]1243 rc = block_put(b);
[dfddfcd]1244 if (rc != EOK) {
[991f645]1245 (void) fat_free_clusters(bs, devmap_handle, mcl);
[dfddfcd]1246 (void) fat_node_put(fn);
[efcebe1]1247 return rc;
[dfddfcd]1248 }
[6f2dfd1]1249 /*
1250 * Append the cluster chain starting in mcl to the end of the
1251 * node's cluster chain.
1252 */
[377cce8]1253 rc = fat_append_clusters(bs, nodep, mcl, lcl);
[dfddfcd]1254 if (rc != EOK) {
[991f645]1255 (void) fat_free_clusters(bs, devmap_handle, mcl);
[dfddfcd]1256 (void) fat_node_put(fn);
[efcebe1]1257 return rc;
[dfddfcd]1258 }
[efcebe1]1259 *nsize = nodep->size = pos + bytes;
[dfddfcd]1260 rc = fat_node_put(fn);
[efcebe1]1261 nodep->dirty = true; /* need to sync node */
1262 *wbytes = bytes;
1263 return rc;
[8d32152]1264 }
[c947dda]1265}
1266
[efcebe1]1267static int
1268fat_truncate(devmap_handle_t devmap_handle, fs_index_t index, aoff64_t size)
[6c71a1f]1269{
[073f550]1270 fs_node_t *fn;
[b6035ba]1271 fat_node_t *nodep;
[913a821c]1272 fat_bs_t *bs;
[8334a427]1273 int rc;
1274
[991f645]1275 rc = fat_node_get(&fn, devmap_handle, index);
[efcebe1]1276 if (rc != EOK)
1277 return rc;
1278 if (!fn)
1279 return ENOENT;
[b6035ba]1280 nodep = FAT_NODE(fn);
[8334a427]1281
[991f645]1282 bs = block_bb_get(devmap_handle);
[913a821c]1283
[8334a427]1284 if (nodep->size == size) {
1285 rc = EOK;
1286 } else if (nodep->size < size) {
1287 /*
[913a821c]1288 * The standard says we have the freedom to grow the node.
[8334a427]1289 * For now, we simply return an error.
1290 */
1291 rc = EINVAL;
[7a23d60]1292 } else if (ROUND_UP(nodep->size, BPC(bs)) == ROUND_UP(size, BPC(bs))) {
[913a821c]1293 /*
1294 * The node will be shrunk, but no clusters will be deallocated.
1295 */
1296 nodep->size = size;
1297 nodep->dirty = true; /* need to sync node */
[97bc3ee]1298 rc = EOK;
[8334a427]1299 } else {
1300 /*
[913a821c]1301 * The node will be shrunk, clusters will be deallocated.
[8334a427]1302 */
[913a821c]1303 if (size == 0) {
[cca29e3c]1304 rc = fat_chop_clusters(bs, nodep, FAT_CLST_RES0);
1305 if (rc != EOK)
1306 goto out;
[913a821c]1307 } else {
1308 fat_cluster_t lastc;
[991f645]1309 rc = fat_cluster_walk(bs, devmap_handle, nodep->firstc,
[7a23d60]1310 &lastc, NULL, (size - 1) / BPC(bs));
[e402382]1311 if (rc != EOK)
1312 goto out;
[cca29e3c]1313 rc = fat_chop_clusters(bs, nodep, lastc);
1314 if (rc != EOK)
1315 goto out;
[913a821c]1316 }
1317 nodep->size = size;
1318 nodep->dirty = true; /* need to sync node */
[97bc3ee]1319 rc = EOK;
[8334a427]1320 }
[e402382]1321out:
[b6035ba]1322 fat_node_put(fn);
[efcebe1]1323 return rc;
[6c71a1f]1324}
1325
[efcebe1]1326static int fat_close(devmap_handle_t devmap_handle, fs_index_t index)
[c20aa06]1327{
[efcebe1]1328 return EOK;
[c20aa06]1329}
1330
[efcebe1]1331static int fat_destroy(devmap_handle_t devmap_handle, fs_index_t index)
[50e5b25]1332{
[073f550]1333 fs_node_t *fn;
[5ca5eaa7]1334 fat_node_t *nodep;
[50e5b25]1335 int rc;
1336
[991f645]1337 rc = fat_node_get(&fn, devmap_handle, index);
[efcebe1]1338 if (rc != EOK)
1339 return rc;
1340 if (!fn)
1341 return ENOENT;
[50e5b25]1342
[5ca5eaa7]1343 nodep = FAT_NODE(fn);
1344 /*
1345 * We should have exactly two references. One for the above
1346 * call to fat_node_get() and one from fat_unlink().
1347 */
1348 assert(nodep->refcnt == 2);
1349
[b6035ba]1350 rc = fat_destroy_node(fn);
[efcebe1]1351 return rc;
[c20aa06]1352}
1353
[efcebe1]1354static int fat_sync(devmap_handle_t devmap_handle, fs_index_t index)
[c20aa06]1355{
[69a60c4]1356 fs_node_t *fn;
[991f645]1357 int rc = fat_node_get(&fn, devmap_handle, index);
[efcebe1]1358 if (rc != EOK)
1359 return rc;
1360 if (!fn)
1361 return ENOENT;
[97bc3ee]1362
[69a60c4]1363 fat_node_t *nodep = FAT_NODE(fn);
[97bc3ee]1364
[69a60c4]1365 nodep->dirty = true;
1366 rc = fat_node_sync(nodep);
[97bc3ee]1367
[69a60c4]1368 fat_node_put(fn);
[efcebe1]1369 return rc;
[c20aa06]1370}
1371
[efcebe1]1372vfs_out_ops_t fat_ops = {
1373 .mounted = fat_mounted,
1374 .unmounted = fat_unmounted,
1375 .read = fat_read,
1376 .write = fat_write,
1377 .truncate = fat_truncate,
1378 .close = fat_close,
1379 .destroy = fat_destroy,
1380 .sync = fat_sync,
1381};
1382
[be815bc]1383/**
1384 * @}
[c20aa06]1385 */
Note: See TracBrowser for help on using the repository browser.