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

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

Do not hold the parent→idx→lock in when calling fat_idx_get_by_pos() as it
both is not necessary and violates the natural locking order used by
fat_idx_get_by_pos() and fat_idx_get_by_index().

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