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

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

Make fat_write() never assert on an I/O error.

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