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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since d044447 was d044447, checked in by Jakub Jermar <jakub@…>, 16 years ago

Merge FAT server error handling improvements.

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