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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 3dd148d was 3dd148d, checked in by Manuele Conti <conti.ma@…>, 12 years ago

Change stafs function operation to allow correct error handling.

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