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

Last change on this file was 7ae01d5, checked in by Jiri Svoboda <jiri@…>, 15 months ago

Remove unused comm_size parameter of block_init()

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