source: mainline/uspace/srv/fs/exfat/exfat_ops.c@ a78b0a0

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

Remove unused comm_size parameter of block_init()

  • Property mode set to 100644
File size: 38.1 KB
RevLine 
[db9aa04e]1/*
[7ae01d5]2 * Copyright (c) 2024 Jiri Svoboda
[db9aa04e]3 * Copyright (c) 2008 Jakub Jermar
4 * Copyright (c) 2011 Oleg Romanenko
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 exfat
[db9aa04e]32 * @{
33 */
34
35/**
36 * @file exfat_ops.c
[0dbe5ac]37 * @brief Implementation of VFS operations for the exFAT file system
38 * server.
[db9aa04e]39 */
40
41#include "exfat.h"
[4c3c4a5]42#include "exfat_fat.h"
[81d773f]43#include "exfat_dentry.h"
[a86e4f8]44#include "exfat_directory.h"
[5d5863c]45#include "exfat_bitmap.h"
[db9aa04e]46#include "../../vfs/vfs.h"
47#include <libfs.h>
[f73b291]48#include <block.h>
[db9aa04e]49#include <ipc/services.h>
[375ab5e]50#include <ipc/loc.h>
[db9aa04e]51#include <macros.h>
52#include <async.h>
53#include <errno.h>
54#include <str.h>
55#include <byteorder.h>
56#include <adt/hash_table.h>
[062d900]57#include <adt/hash.h>
[db9aa04e]58#include <adt/list.h>
59#include <assert.h>
60#include <fibril_synch.h>
61#include <align.h>
[4c3c4a5]62#include <stdio.h>
[38d150e]63#include <stdlib.h>
[db9aa04e]64
[4c3c4a5]65/** Mutex protecting the list of cached free FAT nodes. */
66static FIBRIL_MUTEX_INITIALIZE(ffn_mutex);
67
68/** List of cached free FAT nodes. */
[e97b8c6]69static LIST_INITIALIZE(ffn_list);
[6d57e1c]70
71/*
[4c3c4a5]72 * Forward declarations of FAT libfs operations.
73 */
[5d5863c]74
[8a8771c]75static void exfat_fsinfo(exfat_bs_t *, service_id_t);
[b7fd2a0]76static errno_t exfat_root_get(fs_node_t **, service_id_t);
77static errno_t exfat_match(fs_node_t **, fs_node_t *, const char *);
78static errno_t exfat_node_get(fs_node_t **, service_id_t, fs_index_t);
79static errno_t exfat_node_open(fs_node_t *);
80/* static errno_t exfat_node_put(fs_node_t *); */
81static errno_t exfat_create_node(fs_node_t **, service_id_t, int);
82static errno_t exfat_destroy_node(fs_node_t *);
83static errno_t exfat_link(fs_node_t *, fs_node_t *, const char *);
84static errno_t exfat_unlink(fs_node_t *, fs_node_t *, const char *);
85static errno_t exfat_has_children(bool *, fs_node_t *);
[4c3c4a5]86static fs_index_t exfat_index_get(fs_node_t *);
87static aoff64_t exfat_size_get(fs_node_t *);
88static unsigned exfat_lnkcnt_get(fs_node_t *);
89static bool exfat_is_directory(fs_node_t *);
90static bool exfat_is_file(fs_node_t *node);
[b33870b]91static service_id_t exfat_service_get(fs_node_t *node);
[b7fd2a0]92static errno_t exfat_size_block(service_id_t, uint32_t *);
93static errno_t exfat_total_block_count(service_id_t, uint64_t *);
94static errno_t exfat_free_block_count(service_id_t, uint64_t *);
[4c3c4a5]95
96/*
97 * Helper functions.
98 */
99static void exfat_node_initialize(exfat_node_t *node)
100{
101 fibril_mutex_initialize(&node->lock);
102 node->bp = NULL;
103 node->idx = NULL;
[81d773f]104 node->type = EXFAT_UNKNOW;
[4c3c4a5]105 link_initialize(&node->ffn_link);
106 node->size = 0;
107 node->lnkcnt = 0;
108 node->refcnt = 0;
109 node->dirty = false;
[998a78f]110 node->fragmented = false;
[4c3c4a5]111 node->lastc_cached_valid = false;
112 node->lastc_cached_value = 0;
113 node->currc_cached_valid = false;
114 node->currc_cached_bn = 0;
115 node->currc_cached_value = 0;
116}
117
[b7fd2a0]118static errno_t exfat_node_sync(exfat_node_t *node)
[4c3c4a5]119{
[b7fd2a0]120 errno_t rc;
[151a4e2]121 exfat_directory_t di;
122 exfat_file_dentry_t df;
123 exfat_stream_dentry_t ds;
124
125 if (!(node->type == EXFAT_DIRECTORY || node->type == EXFAT_FILE))
126 return EOK;
127
128 if (node->type == EXFAT_DIRECTORY)
129 df.attr = EXFAT_ATTR_SUBDIR;
130 else
131 df.attr = 0;
132
[c56c4576]133 ds.firstc = node->firstc;
[151a4e2]134 if (node->size == 0 && node->firstc == 0) {
135 ds.flags = 0;
136 } else {
137 ds.flags = 1;
138 ds.flags |= (!node->fragmented << 1);
139 }
[c56c4576]140 ds.valid_data_size = node->size;
141 ds.data_size = node->size;
[151a4e2]142
[1b20da0]143 exfat_directory_open_parent(&di, node->idx->service_id, node->idx->pfc,
[151a4e2]144 node->idx->parent_fragmented);
[93e12f3]145 rc = exfat_directory_seek(&di, node->idx->pdi);
146 if (rc != EOK) {
147 (void) exfat_directory_close(&di);
148 return rc;
149 }
150
[151a4e2]151 rc = exfat_directory_sync_file(&di, &df, &ds);
152 if (rc != EOK) {
153 (void) exfat_directory_close(&di);
[f061de75]154 return rc;
155 }
[151a4e2]156 return exfat_directory_close(&di);
[4c3c4a5]157}
158
[b7fd2a0]159static errno_t exfat_node_fini_by_service_id(service_id_t service_id)
[4c3c4a5]160{
[b7fd2a0]161 errno_t rc;
[4c3c4a5]162
163 /*
164 * We are called from fat_unmounted() and assume that there are already
165 * no nodes belonging to this instance with non-zero refcount. Therefore
166 * it is sufficient to clean up only the FAT free node list.
167 */
168
169restart:
170 fibril_mutex_lock(&ffn_mutex);
[feeac0d]171 list_foreach(ffn_list, ffn_link, exfat_node_t, nodep) {
[4c3c4a5]172 if (!fibril_mutex_trylock(&nodep->lock)) {
173 fibril_mutex_unlock(&ffn_mutex);
174 goto restart;
175 }
176 if (!fibril_mutex_trylock(&nodep->idx->lock)) {
177 fibril_mutex_unlock(&nodep->lock);
178 fibril_mutex_unlock(&ffn_mutex);
179 goto restart;
180 }
[375ab5e]181 if (nodep->idx->service_id != service_id) {
[4c3c4a5]182 fibril_mutex_unlock(&nodep->idx->lock);
183 fibril_mutex_unlock(&nodep->lock);
184 continue;
185 }
186
187 list_remove(&nodep->ffn_link);
188 fibril_mutex_unlock(&ffn_mutex);
189
190 /*
191 * We can unlock the node and its index structure because we are
192 * the last player on this playground and VFS is preventing new
193 * players from entering.
194 */
195 fibril_mutex_unlock(&nodep->idx->lock);
196 fibril_mutex_unlock(&nodep->lock);
197
198 if (nodep->dirty) {
199 rc = exfat_node_sync(nodep);
200 if (rc != EOK)
201 return rc;
202 }
203 nodep->idx->nodep = NULL;
204 free(nodep->bp);
205 free(nodep);
206
[e97b8c6]207 /* Need to restart because we changed the ffn_list. */
[4c3c4a5]208 goto restart;
209 }
210 fibril_mutex_unlock(&ffn_mutex);
211
212 return EOK;
213}
214
[b7fd2a0]215static errno_t exfat_node_get_new(exfat_node_t **nodepp)
[81d773f]216{
217 fs_node_t *fn;
218 exfat_node_t *nodep;
[b7fd2a0]219 errno_t rc;
[81d773f]220
221 fibril_mutex_lock(&ffn_mutex);
[e97b8c6]222 if (!list_empty(&ffn_list)) {
[81d773f]223 /* Try to use a cached free node structure. */
224 exfat_idx_t *idxp_tmp;
[e97b8c6]225 nodep = list_get_instance(list_first(&ffn_list), exfat_node_t,
226 ffn_link);
[81d773f]227 if (!fibril_mutex_trylock(&nodep->lock))
228 goto skip_cache;
229 idxp_tmp = nodep->idx;
230 if (!fibril_mutex_trylock(&idxp_tmp->lock)) {
231 fibril_mutex_unlock(&nodep->lock);
232 goto skip_cache;
233 }
234 list_remove(&nodep->ffn_link);
235 fibril_mutex_unlock(&ffn_mutex);
236 if (nodep->dirty) {
237 rc = exfat_node_sync(nodep);
238 if (rc != EOK) {
239 idxp_tmp->nodep = NULL;
240 fibril_mutex_unlock(&nodep->lock);
241 fibril_mutex_unlock(&idxp_tmp->lock);
242 free(nodep->bp);
243 free(nodep);
244 return rc;
245 }
246 }
247 idxp_tmp->nodep = NULL;
248 fibril_mutex_unlock(&nodep->lock);
249 fibril_mutex_unlock(&idxp_tmp->lock);
250 fn = FS_NODE(nodep);
251 } else {
[18b6a88]252 skip_cache:
[81d773f]253 /* Try to allocate a new node structure. */
254 fibril_mutex_unlock(&ffn_mutex);
255 fn = (fs_node_t *)malloc(sizeof(fs_node_t));
256 if (!fn)
257 return ENOMEM;
258 nodep = (exfat_node_t *)malloc(sizeof(exfat_node_t));
259 if (!nodep) {
260 free(fn);
261 return ENOMEM;
262 }
263 }
264 exfat_node_initialize(nodep);
265 fs_node_initialize(fn);
266 fn->data = nodep;
267 nodep->bp = fn;
268
269 *nodepp = nodep;
270 return EOK;
271}
272
[1b20da0]273static errno_t exfat_node_get_new_by_pos(exfat_node_t **nodepp,
[375ab5e]274 service_id_t service_id, exfat_cluster_t pfc, unsigned pdi)
[a86e4f8]275{
[375ab5e]276 exfat_idx_t *idxp = exfat_idx_get_by_pos(service_id, pfc, pdi);
[a86e4f8]277 if (!idxp)
278 return ENOMEM;
279 if (exfat_node_get_new(nodepp) != EOK)
280 return ENOMEM;
281 (*nodepp)->idx = idxp;
282 idxp->nodep = *nodepp;
283 return EOK;
284}
285
[81d773f]286/** Internal version of exfat_node_get().
287 *
288 * @param idxp Locked index structure.
289 */
[b7fd2a0]290static errno_t exfat_node_get_core(exfat_node_t **nodepp, exfat_idx_t *idxp)
[81d773f]291{
[f01fea3]292 exfat_dentry_t *d;
[81d773f]293 exfat_node_t *nodep = NULL;
[cd860fc]294 exfat_directory_t di;
[b7fd2a0]295 errno_t rc;
[81d773f]296
297 if (idxp->nodep) {
298 /*
299 * We are lucky.
300 * The node is already instantiated in memory.
301 */
302 fibril_mutex_lock(&idxp->nodep->lock);
303 if (!idxp->nodep->refcnt++) {
304 fibril_mutex_lock(&ffn_mutex);
305 list_remove(&idxp->nodep->ffn_link);
306 fibril_mutex_unlock(&ffn_mutex);
307 }
308 fibril_mutex_unlock(&idxp->nodep->lock);
309 *nodepp = idxp->nodep;
310 return EOK;
311 }
312
313 /*
314 * We must instantiate the node from the file system.
315 */
316
317 assert(idxp->pfc);
318
319 rc = exfat_node_get_new(&nodep);
320 if (rc != EOK)
321 return rc;
322
[1b20da0]323 exfat_directory_open_parent(&di, idxp->service_id, idxp->pfc,
[cd860fc]324 idxp->parent_fragmented);
325 rc = exfat_directory_seek(&di, idxp->pdi);
326 if (rc != EOK) {
327 (void) exfat_directory_close(&di);
328 (void) exfat_node_put(FS_NODE(nodep));
329 return rc;
330 }
331 rc = exfat_directory_get(&di, &d);
[f01fea3]332 if (rc != EOK) {
[cd860fc]333 (void) exfat_directory_close(&di);
[f01fea3]334 (void) exfat_node_put(FS_NODE(nodep));
335 return rc;
336 }
[81d773f]337
[f01fea3]338 switch (exfat_classify_dentry(d)) {
[81d773f]339 case EXFAT_DENTRY_FILE:
[0dbe5ac]340 nodep->type =
[1b20da0]341 (uint16_t_le2host(d->file.attr) & EXFAT_ATTR_SUBDIR) ?
[81d773f]342 EXFAT_DIRECTORY : EXFAT_FILE;
[cd860fc]343 rc = exfat_directory_next(&di);
[f01fea3]344 if (rc != EOK) {
[cd860fc]345 (void) exfat_directory_close(&di);
[f01fea3]346 (void) exfat_node_put(FS_NODE(nodep));
347 return rc;
348 }
[cd860fc]349 rc = exfat_directory_get(&di, &d);
[f01fea3]350 if (rc != EOK) {
[cd860fc]351 (void) exfat_directory_close(&di);
[f01fea3]352 (void) exfat_node_put(FS_NODE(nodep));
353 return rc;
354 }
[c56c4576]355 nodep->firstc = uint32_t_le2host(d->stream.firstc);
356 nodep->size = uint64_t_le2host(d->stream.data_size);
[f01fea3]357 nodep->fragmented = (d->stream.flags & 0x02) == 0;
[81d773f]358 break;
359 case EXFAT_DENTRY_BITMAP:
360 nodep->type = EXFAT_BITMAP;
[c56c4576]361 nodep->firstc = uint32_t_le2host(d->bitmap.firstc);
362 nodep->size = uint64_t_le2host(d->bitmap.size);
[f01fea3]363 nodep->fragmented = true;
[81d773f]364 break;
365 case EXFAT_DENTRY_UCTABLE:
366 nodep->type = EXFAT_UCTABLE;
[c56c4576]367 nodep->firstc = uint32_t_le2host(d->uctable.firstc);
368 nodep->size = uint64_t_le2host(d->uctable.size);
[f01fea3]369 nodep->fragmented = true;
[81d773f]370 break;
371 default:
372 case EXFAT_DENTRY_SKIP:
373 case EXFAT_DENTRY_LAST:
374 case EXFAT_DENTRY_FREE:
375 case EXFAT_DENTRY_VOLLABEL:
376 case EXFAT_DENTRY_GUID:
377 case EXFAT_DENTRY_STREAM:
378 case EXFAT_DENTRY_NAME:
[cd860fc]379 (void) exfat_directory_close(&di);
[f01fea3]380 (void) exfat_node_put(FS_NODE(nodep));
[81d773f]381 return ENOENT;
382 }
383
384 nodep->lnkcnt = 1;
385 nodep->refcnt = 1;
386
[cd860fc]387 rc = exfat_directory_close(&di);
[81d773f]388 if (rc != EOK) {
389 (void) exfat_node_put(FS_NODE(nodep));
390 return rc;
391 }
392
393 /* Link the idx structure with the node structure. */
394 nodep->idx = idxp;
395 idxp->nodep = nodep;
396
397 *nodepp = nodep;
398 return EOK;
399}
400
[b7fd2a0]401errno_t exfat_node_expand(service_id_t service_id, exfat_node_t *nodep,
[0dbe5ac]402 exfat_cluster_t clusters)
[998a78f]403{
404 exfat_bs_t *bs;
[b7fd2a0]405 errno_t rc;
[375ab5e]406 bs = block_bb_get(service_id);
[998a78f]407
[75513701]408 if (!nodep->fragmented) {
[e738d56]409 rc = exfat_bitmap_append_clusters(bs, nodep, clusters);
[998a78f]410 if (rc != ENOSPC)
411 return rc;
412 if (rc == ENOSPC) {
413 nodep->fragmented = true;
414 nodep->dirty = true; /* need to sync node */
[e738d56]415 rc = exfat_bitmap_replicate_clusters(bs, nodep);
[998a78f]416 if (rc != EOK)
417 return rc;
418 }
419 }
420
421 /* If we cant linear expand the node, we should use FAT instead */
422 exfat_cluster_t mcl, lcl;
423
424 /* create an independent chain of nclsts clusters in all FATs */
[375ab5e]425 rc = exfat_alloc_clusters(bs, service_id, clusters, &mcl, &lcl);
[998a78f]426 if (rc != EOK)
427 return rc;
[375ab5e]428 rc = exfat_zero_cluster(bs, service_id, mcl);
[d9aeab3]429 if (rc != EOK) {
[375ab5e]430 (void) exfat_free_clusters(bs, service_id, mcl);
[d9aeab3]431 return rc;
432 }
[998a78f]433 /*
434 * Append the cluster chain starting in mcl to the end of the
435 * node's cluster chain.
436 */
437 rc = exfat_append_clusters(bs, nodep, mcl, lcl);
438 if (rc != EOK) {
[375ab5e]439 (void) exfat_free_clusters(bs, service_id, mcl);
[998a78f]440 return rc;
441 }
442
443 return EOK;
444}
445
[b7fd2a0]446static errno_t exfat_node_shrink(service_id_t service_id, exfat_node_t *nodep,
[0dbe5ac]447 aoff64_t size)
[998a78f]448{
449 exfat_bs_t *bs;
[b7fd2a0]450 errno_t rc;
[375ab5e]451 bs = block_bb_get(service_id);
[998a78f]452
[75513701]453 if (!nodep->fragmented) {
[998a78f]454 exfat_cluster_t clsts, prev_clsts, new_clsts;
455 prev_clsts = ROUND_UP(nodep->size, BPC(bs)) / BPC(bs);
456 new_clsts = ROUND_UP(size, BPC(bs)) / BPC(bs);
457
458 assert(new_clsts < prev_clsts);
459
460 clsts = prev_clsts - new_clsts;
[e738d56]461 rc = exfat_bitmap_free_clusters(bs, nodep, clsts);
[998a78f]462 if (rc != EOK)
463 return rc;
464 } else {
465 /*
466 * The node will be shrunk, clusters will be deallocated.
467 */
468 if (size == 0) {
469 rc = exfat_chop_clusters(bs, nodep, 0);
470 if (rc != EOK)
471 return rc;
472 } else {
473 exfat_cluster_t lastc;
[375ab5e]474 rc = exfat_cluster_walk(bs, service_id, nodep->firstc,
[998a78f]475 &lastc, NULL, (size - 1) / BPC(bs));
476 if (rc != EOK)
477 return rc;
478 rc = exfat_chop_clusters(bs, nodep, lastc);
479 if (rc != EOK)
480 return rc;
481 }
482 }
483
484 nodep->size = size;
485 nodep->dirty = true; /* need to sync node */
486 return EOK;
487}
[81d773f]488
[4c3c4a5]489/*
[81d773f]490 * EXFAT libfs operations.
[4c3c4a5]491 */
492
[b7fd2a0]493errno_t exfat_root_get(fs_node_t **rfn, service_id_t service_id)
[4c3c4a5]494{
[375ab5e]495 return exfat_node_get(rfn, service_id, EXFAT_ROOT_IDX);
[81d773f]496}
497
[b7fd2a0]498errno_t exfat_bitmap_get(fs_node_t **rfn, service_id_t service_id)
[81d773f]499{
[375ab5e]500 return exfat_node_get(rfn, service_id, EXFAT_BITMAP_IDX);
[81d773f]501}
[5d5863c]502
[b7fd2a0]503errno_t exfat_uctable_get(fs_node_t **rfn, service_id_t service_id)
[81d773f]504{
[375ab5e]505 return exfat_node_get(rfn, service_id, EXFAT_UCTABLE_IDX);
[4c3c4a5]506}
[b289005]507
[b7fd2a0]508errno_t exfat_match(fs_node_t **rfn, fs_node_t *pfn, const char *component)
[4c3c4a5]509{
[0bd5ff1]510 exfat_node_t *parentp = EXFAT_NODE(pfn);
[0dbe5ac]511 char name[EXFAT_FILENAME_LEN + 1];
[0bd5ff1]512 exfat_file_dentry_t df;
513 exfat_stream_dentry_t ds;
[375ab5e]514 service_id_t service_id;
[b7fd2a0]515 errno_t rc;
[0bd5ff1]516
517 fibril_mutex_lock(&parentp->idx->lock);
[375ab5e]518 service_id = parentp->idx->service_id;
[0bd5ff1]519 fibril_mutex_unlock(&parentp->idx->lock);
[a35b458]520
[0bd5ff1]521 exfat_directory_t di;
522 rc = exfat_directory_open(parentp, &di);
523 if (rc != EOK)
524 return rc;
525
[0dbe5ac]526 while (exfat_directory_read_file(&di, name, EXFAT_FILENAME_LEN, &df,
527 &ds) == EOK) {
[0e7c3d9]528 if (str_casecmp(name, component) == 0) {
[0bd5ff1]529 /* hit */
530 exfat_node_t *nodep;
[0dbe5ac]531 aoff64_t o = di.pos %
532 (BPS(di.bs) / sizeof(exfat_dentry_t));
[375ab5e]533 exfat_idx_t *idx = exfat_idx_get_by_pos(service_id,
[18b6a88]534 parentp->firstc, di.bnum * DPS(di.bs) + o);
[0bd5ff1]535 if (!idx) {
536 /*
537 * Can happen if memory is low or if we
538 * run out of 32-bit indices.
539 */
540 rc = exfat_directory_close(&di);
541 return (rc == EOK) ? ENOMEM : rc;
542 }
543 rc = exfat_node_get_core(&nodep, idx);
544 fibril_mutex_unlock(&idx->lock);
545 if (rc != EOK) {
546 (void) exfat_directory_close(&di);
547 return rc;
548 }
549 *rfn = FS_NODE(nodep);
550 rc = exfat_directory_close(&di);
551 if (rc != EOK)
552 (void) exfat_node_put(*rfn);
553 return rc;
554 } else {
555 rc = exfat_directory_next(&di);
556 if (rc != EOK)
557 break;
558 }
559 }
560 (void) exfat_directory_close(&di);
[4c3c4a5]561 *rfn = NULL;
562 return EOK;
563}
564
565/** Instantiate a exFAT in-core node. */
[b7fd2a0]566errno_t exfat_node_get(fs_node_t **rfn, service_id_t service_id, fs_index_t index)
[4c3c4a5]567{
[81d773f]568 exfat_node_t *nodep;
569 exfat_idx_t *idxp;
[b7fd2a0]570 errno_t rc;
[81d773f]571
[375ab5e]572 idxp = exfat_idx_get_by_index(service_id, index);
[81d773f]573 if (!idxp) {
574 *rfn = NULL;
575 return EOK;
576 }
577 /* idxp->lock held */
578 rc = exfat_node_get_core(&nodep, idxp);
579 fibril_mutex_unlock(&idxp->lock);
580 if (rc == EOK)
581 *rfn = FS_NODE(nodep);
582 return rc;
[4c3c4a5]583}
584
[b7fd2a0]585errno_t exfat_node_open(fs_node_t *fn)
[4c3c4a5]586{
587 /*
588 * Opening a file is stateless, nothing
589 * to be done here.
590 */
591 return EOK;
592}
593
[b7fd2a0]594errno_t exfat_node_put(fs_node_t *fn)
[4c3c4a5]595{
[18902ca6]596 if (fn == NULL)
597 return EOK;
598
[8f54827]599 exfat_node_t *nodep = EXFAT_NODE(fn);
600 bool destroy = false;
601
602 fibril_mutex_lock(&nodep->lock);
603 if (!--nodep->refcnt) {
604 if (nodep->idx) {
605 fibril_mutex_lock(&ffn_mutex);
[e97b8c6]606 list_append(&nodep->ffn_link, &ffn_list);
[8f54827]607 fibril_mutex_unlock(&ffn_mutex);
608 } else {
609 /*
610 * The node does not have any index structure associated
611 * with itself. This can only mean that we are releasing
612 * the node after a failed attempt to allocate the index
613 * structure for it.
614 */
615 destroy = true;
616 }
617 }
618 fibril_mutex_unlock(&nodep->lock);
619 if (destroy) {
620 free(nodep->bp);
621 free(nodep);
622 }
[4c3c4a5]623 return EOK;
624}
625
[b7fd2a0]626errno_t exfat_create_node(fs_node_t **rfn, service_id_t service_id, int flags)
[4c3c4a5]627{
[998a78f]628 exfat_idx_t *idxp;
629 exfat_node_t *nodep;
[93e12f3]630 exfat_bs_t *bs;
[b7fd2a0]631 errno_t rc;
[998a78f]632
[375ab5e]633 bs = block_bb_get(service_id);
[998a78f]634 rc = exfat_node_get_new(&nodep);
635 if (rc != EOK)
636 return rc;
637
[375ab5e]638 rc = exfat_idx_get_new(&idxp, service_id);
[998a78f]639 if (rc != EOK) {
640 (void) exfat_node_put(FS_NODE(nodep));
641 return rc;
642 }
643
644 nodep->firstc = 0;
645 nodep->size = 0;
646 nodep->fragmented = false;
647 nodep->lnkcnt = 0; /* not linked anywhere */
648 nodep->refcnt = 1;
649 nodep->dirty = true;
650
651 nodep->idx = idxp;
652 idxp->nodep = nodep;
653 fibril_mutex_unlock(&idxp->lock);
[93e12f3]654
655 if (flags & L_DIRECTORY) {
656 nodep->type = EXFAT_DIRECTORY;
[375ab5e]657 rc = exfat_node_expand(service_id, nodep, 1);
[93e12f3]658 if (rc != EOK) {
659 (void) exfat_node_put(FS_NODE(nodep));
660 return rc;
661 }
[80ec9b8]662
663 rc = exfat_zero_cluster(bs, service_id, nodep->firstc);
664 if (rc != EOK) {
665 (void) exfat_node_put(FS_NODE(nodep));
666 return rc;
667 }
668
[93e12f3]669 nodep->size = BPC(bs);
670 } else {
671 nodep->type = EXFAT_FILE;
672 }
673
[998a78f]674 *rfn = FS_NODE(nodep);
[4c3c4a5]675 return EOK;
676}
677
[b7fd2a0]678errno_t exfat_destroy_node(fs_node_t *fn)
[4c3c4a5]679{
[998a78f]680 exfat_node_t *nodep = EXFAT_NODE(fn);
681 exfat_bs_t *bs;
682 bool has_children;
[b7fd2a0]683 errno_t rc;
[998a78f]684
685 /*
686 * The node is not reachable from the file system. This means that the
687 * link count should be zero and that the index structure cannot be
688 * found in the position hash. Obviously, we don't need to lock the node
689 * nor its index structure.
690 */
691 assert(nodep->lnkcnt == 0);
692
693 /*
694 * The node may not have any children.
695 */
696 rc = exfat_has_children(&has_children, fn);
697 if (rc != EOK)
698 return rc;
699 assert(!has_children);
700
[375ab5e]701 bs = block_bb_get(nodep->idx->service_id);
[998a78f]702 if (nodep->firstc != 0) {
703 assert(nodep->size);
704 /* Free all clusters allocated to the node. */
705 if (nodep->fragmented)
[375ab5e]706 rc = exfat_free_clusters(bs, nodep->idx->service_id,
[18b6a88]707 nodep->firstc);
[998a78f]708 else
[1b20da0]709 rc = exfat_bitmap_free_clusters(bs, nodep,
[998a78f]710 ROUND_UP(nodep->size, BPC(bs)) / BPC(bs));
[1b20da0]711 }
[998a78f]712
713 exfat_idx_destroy(nodep->idx);
714 free(nodep->bp);
715 free(nodep);
716 return rc;
[4c3c4a5]717}
718
[b7fd2a0]719errno_t exfat_link(fs_node_t *pfn, fs_node_t *cfn, const char *name)
[4c3c4a5]720{
[998a78f]721 exfat_node_t *parentp = EXFAT_NODE(pfn);
722 exfat_node_t *childp = EXFAT_NODE(cfn);
723 exfat_directory_t di;
[b7fd2a0]724 errno_t rc;
[998a78f]725
726 fibril_mutex_lock(&childp->lock);
727 if (childp->lnkcnt == 1) {
728 /*
[f7d90eb]729 * We don't support multiple hard links.
[998a78f]730 */
731 fibril_mutex_unlock(&childp->lock);
732 return EMLINK;
733 }
734 assert(childp->lnkcnt == 0);
735 fibril_mutex_unlock(&childp->lock);
736
737 if (!exfat_valid_name(name))
738 return ENOTSUP;
739
740 fibril_mutex_lock(&parentp->idx->lock);
741 rc = exfat_directory_open(parentp, &di);
742 if (rc != EOK)
743 return rc;
744 /*
745 * At this point we only establish the link between the parent and the
746 * child. The dentry, except of the name and the extension, will remain
747 * uninitialized until the corresponding node is synced. Thus the valid
748 * dentry data is kept in the child node structure.
749 */
750 rc = exfat_directory_write_file(&di, name);
[8d2cd8b]751 if (rc != EOK) {
752 (void) exfat_directory_close(&di);
753 fibril_mutex_unlock(&parentp->idx->lock);
[998a78f]754 return rc;
[8d2cd8b]755 }
[998a78f]756 rc = exfat_directory_close(&di);
[8d2cd8b]757 if (rc != EOK) {
758 fibril_mutex_unlock(&parentp->idx->lock);
[998a78f]759 return rc;
[8d2cd8b]760 }
[998a78f]761
762 fibril_mutex_unlock(&parentp->idx->lock);
763 fibril_mutex_lock(&childp->idx->lock);
764
765 childp->idx->pfc = parentp->firstc;
766 childp->idx->parent_fragmented = parentp->fragmented;
[93e12f3]767 childp->idx->pdi = di.pos;
[998a78f]768 fibril_mutex_unlock(&childp->idx->lock);
769
770 fibril_mutex_lock(&childp->lock);
771 childp->lnkcnt = 1;
772 childp->dirty = true; /* need to sync node */
773 fibril_mutex_unlock(&childp->lock);
774
775 /*
776 * Hash in the index structure into the position hash.
777 */
778 exfat_idx_hashin(childp->idx);
779
[4c3c4a5]780 return EOK;
[998a78f]781
[4c3c4a5]782}
783
[b7fd2a0]784errno_t exfat_unlink(fs_node_t *pfn, fs_node_t *cfn, const char *nm)
[4c3c4a5]785{
[998a78f]786 exfat_node_t *parentp = EXFAT_NODE(pfn);
787 exfat_node_t *childp = EXFAT_NODE(cfn);
788 bool has_children;
[b7fd2a0]789 errno_t rc;
[998a78f]790
791 if (!parentp)
792 return EBUSY;
793
794 rc = exfat_has_children(&has_children, cfn);
795 if (rc != EOK)
796 return rc;
797 if (has_children)
798 return ENOTEMPTY;
799
800 fibril_mutex_lock(&parentp->lock);
801 fibril_mutex_lock(&childp->lock);
802 assert(childp->lnkcnt == 1);
803 fibril_mutex_lock(&childp->idx->lock);
[a35b458]804
[998a78f]805 exfat_directory_t di;
[18b6a88]806 rc = exfat_directory_open(parentp, &di);
[998a78f]807 if (rc != EOK)
808 goto error;
809 rc = exfat_directory_erase_file(&di, childp->idx->pdi);
810 if (rc != EOK)
811 goto error;
812 rc = exfat_directory_close(&di);
813 if (rc != EOK)
814 goto error;
815
816 /* remove the index structure from the position hash */
817 exfat_idx_hashout(childp->idx);
818 /* clear position information */
819 childp->idx->pfc = 0;
820 childp->idx->pdi = 0;
821 fibril_mutex_unlock(&childp->idx->lock);
822 childp->lnkcnt = 0;
823 childp->refcnt++; /* keep the node in memory until destroyed */
824 childp->dirty = true;
825 fibril_mutex_unlock(&childp->lock);
826 fibril_mutex_unlock(&parentp->lock);
827
[4c3c4a5]828 return EOK;
[998a78f]829
830error:
831 (void) exfat_directory_close(&di);
832 fibril_mutex_unlock(&childp->idx->lock);
833 fibril_mutex_unlock(&childp->lock);
834 fibril_mutex_unlock(&parentp->lock);
835 return rc;
836
[4c3c4a5]837}
838
[b7fd2a0]839errno_t exfat_has_children(bool *has_children, fs_node_t *fn)
[4c3c4a5]840{
[59c07773]841 exfat_directory_t di;
842 exfat_dentry_t *d;
843 exfat_node_t *nodep = EXFAT_NODE(fn);
[b7fd2a0]844 errno_t rc;
[59c07773]845
[4c3c4a5]846 *has_children = false;
[59c07773]847
848 if (nodep->type != EXFAT_DIRECTORY)
849 return EOK;
850
851 fibril_mutex_lock(&nodep->idx->lock);
852
853 rc = exfat_directory_open(nodep, &di);
854 if (rc != EOK) {
855 fibril_mutex_unlock(&nodep->idx->lock);
856 return rc;
857 }
858
859 do {
860 rc = exfat_directory_get(&di, &d);
861 if (rc != EOK) {
862 (void) exfat_directory_close(&di);
863 fibril_mutex_unlock(&nodep->idx->lock);
864 return rc;
865 }
866 switch (exfat_classify_dentry(d)) {
867 case EXFAT_DENTRY_SKIP:
868 case EXFAT_DENTRY_FREE:
869 continue;
870 case EXFAT_DENTRY_LAST:
871 *has_children = false;
872 goto exit;
873 default:
874 *has_children = true;
875 goto exit;
876 }
877 } while (exfat_directory_next(&di) == EOK);
878
879exit:
880 rc = exfat_directory_close(&di);
881 fibril_mutex_unlock(&nodep->idx->lock);
882 return rc;
[4c3c4a5]883}
884
885fs_index_t exfat_index_get(fs_node_t *fn)
886{
887 return EXFAT_NODE(fn)->idx->index;
888}
889
890aoff64_t exfat_size_get(fs_node_t *fn)
891{
892 return EXFAT_NODE(fn)->size;
893}
894
895unsigned exfat_lnkcnt_get(fs_node_t *fn)
896{
897 return EXFAT_NODE(fn)->lnkcnt;
898}
899
900bool exfat_is_directory(fs_node_t *fn)
901{
902 return EXFAT_NODE(fn)->type == EXFAT_DIRECTORY;
903}
904
905bool exfat_is_file(fs_node_t *fn)
906{
907 return EXFAT_NODE(fn)->type == EXFAT_FILE;
908}
909
[b33870b]910service_id_t exfat_service_get(fs_node_t *node)
[4c3c4a5]911{
912 return 0;
913}
914
[b7fd2a0]915errno_t exfat_size_block(service_id_t service_id, uint32_t *size)
[951f32ce]916{
917 exfat_bs_t *bs;
918 bs = block_bb_get(service_id);
[3dd148d]919 *size = BPC(bs);
920
921 return EOK;
[951f32ce]922}
[4c3c4a5]923
[b7fd2a0]924errno_t exfat_total_block_count(service_id_t service_id, uint64_t *count)
[87159eb8]925{
926 exfat_bs_t *bs;
927 bs = block_bb_get(service_id);
[3dd148d]928 *count = DATA_CNT(bs);
[a35b458]929
[3dd148d]930 return EOK;
[87159eb8]931}
932
[b7fd2a0]933errno_t exfat_free_block_count(service_id_t service_id, uint64_t *count)
[87159eb8]934{
[18902ca6]935 fs_node_t *node = NULL;
[0e976d9b]936 exfat_node_t *bmap_node;
[87159eb8]937 exfat_bs_t *bs;
[0e976d9b]938 uint64_t free_block_count = 0;
939 uint64_t block_count;
940 unsigned sector;
[b7fd2a0]941 errno_t rc;
[0e976d9b]942
[3dd148d]943 rc = exfat_total_block_count(service_id, &block_count);
944 if (rc != EOK)
945 goto exit;
[0e976d9b]946
[87159eb8]947 bs = block_bb_get(service_id);
[0e976d9b]948 rc = exfat_bitmap_get(&node, service_id);
949 if (rc != EOK)
950 goto exit;
951
952 bmap_node = (exfat_node_t *) node->data;
953
[6b6f394d]954 unsigned const bmap_sectors = ROUND_UP(bmap_node->size, BPS(bs)) /
955 BPS(bs);
956
957 for (sector = 0; sector < bmap_sectors; ++sector) {
[0e976d9b]958
959 block_t *block;
960 uint8_t *bitmap;
961 unsigned bit;
962
[6b6f394d]963 rc = exfat_block_get(&block, bs, bmap_node, sector,
[0e976d9b]964 BLOCK_FLAGS_NONE);
965 if (rc != EOK) {
966 free_block_count = 0;
967 goto exit;
968 }
969
970 bitmap = (uint8_t *) block->data;
971
972 for (bit = 0; bit < BPS(bs) * 8 && block_count > 0;
973 ++bit, --block_count) {
974 if (!(bitmap[bit / 8] & (1 << (bit % 8))))
975 ++free_block_count;
976 }
977
978 block_put(block);
979
980 if (block_count == 0) {
981 /* Reached the end of the bitmap */
982 goto exit;
983 }
984 }
985
986exit:
987 exfat_node_put(node);
[3dd148d]988 *count = free_block_count;
989 return rc;
[87159eb8]990}
[4c3c4a5]991
992/** libfs operations */
[6d57e1c]993libfs_ops_t exfat_libfs_ops = {
994 .root_get = exfat_root_get,
995 .match = exfat_match,
996 .node_get = exfat_node_get,
997 .node_open = exfat_node_open,
998 .node_put = exfat_node_put,
999 .create = exfat_create_node,
1000 .destroy = exfat_destroy_node,
1001 .link = exfat_link,
1002 .unlink = exfat_unlink,
1003 .has_children = exfat_has_children,
1004 .index_get = exfat_index_get,
1005 .size_get = exfat_size_get,
1006 .lnkcnt_get = exfat_lnkcnt_get,
1007 .is_directory = exfat_is_directory,
1008 .is_file = exfat_is_file,
[951f32ce]1009 .service_get = exfat_service_get,
[87159eb8]1010 .size_block = exfat_size_block,
[c84146d3]1011 .total_block_count = exfat_total_block_count,
1012 .free_block_count = exfat_free_block_count
[6d57e1c]1013};
[4c3c4a5]1014
[b7fd2a0]1015static errno_t exfat_fs_open(service_id_t service_id, enum cache_mode cmode,
[f3504c1]1016 fs_node_t **rrfn, exfat_idx_t **rridxp, vfs_fs_probe_info_t *info)
[6d57e1c]1017{
[b7fd2a0]1018 errno_t rc;
[0dbe5ac]1019 exfat_node_t *rootp = NULL, *bitmapp = NULL, *uctablep = NULL;
[6d57e1c]1020 exfat_bs_t *bs;
1021
1022 /* initialize libblock */
[7ae01d5]1023 rc = block_init(service_id);
[e97b8c6]1024 if (rc != EOK)
1025 return rc;
[6d57e1c]1026
1027 /* prepare the boot block */
[375ab5e]1028 rc = block_bb_read(service_id, BS_BLOCK);
[6d57e1c]1029 if (rc != EOK) {
[375ab5e]1030 block_fini(service_id);
[e97b8c6]1031 return rc;
[6d57e1c]1032 }
1033
1034 /* get the buffer with the boot sector */
[375ab5e]1035 bs = block_bb_get(service_id);
[6d57e1c]1036
[395df52]1037 /* Do some simple sanity checks on the file system. */
1038 rc = exfat_sanity_check(bs);
[6d57e1c]1039 if (rc != EOK) {
[395df52]1040 (void) block_cache_fini(service_id);
[375ab5e]1041 block_fini(service_id);
[e97b8c6]1042 return rc;
[6d57e1c]1043 }
1044
[395df52]1045 /* Initialize the block cache */
1046 rc = block_cache_init(service_id, BPS(bs), 0 /* XXX */, cmode);
[4c3c4a5]1047 if (rc != EOK) {
[375ab5e]1048 block_fini(service_id);
[e97b8c6]1049 return rc;
[4c3c4a5]1050 }
1051
[375ab5e]1052 rc = exfat_idx_init_by_service_id(service_id);
[4c3c4a5]1053 if (rc != EOK) {
[375ab5e]1054 (void) block_cache_fini(service_id);
1055 block_fini(service_id);
[e97b8c6]1056 return rc;
[4c3c4a5]1057 }
1058
1059 /* Initialize the root node. */
[1b20da0]1060 rc = exfat_node_get_new_by_pos(&rootp, service_id, EXFAT_ROOT_PAR,
[a86e4f8]1061 EXFAT_ROOT_POS);
[18b6a88]1062 if (rc != EOK) {
[375ab5e]1063 (void) block_cache_fini(service_id);
1064 block_fini(service_id);
1065 exfat_idx_fini_by_service_id(service_id);
[e97b8c6]1066 return ENOMEM;
[4c3c4a5]1067 }
[a86e4f8]1068 assert(rootp->idx->index == EXFAT_ROOT_IDX);
1069
1070 rootp->type = EXFAT_DIRECTORY;
1071 rootp->firstc = ROOT_FC(bs);
1072 rootp->fragmented = true;
1073 rootp->refcnt = 1;
1074 rootp->lnkcnt = 0; /* FS root is not linked */
[4c3c4a5]1075
[a86e4f8]1076 uint32_t clusters;
[375ab5e]1077 rc = exfat_clusters_get(&clusters, bs, service_id, rootp->firstc);
[a86e4f8]1078 if (rc != EOK) {
1079 free(rootp);
[375ab5e]1080 (void) block_cache_fini(service_id);
1081 block_fini(service_id);
1082 exfat_idx_fini_by_service_id(service_id);
[e97b8c6]1083 return ENOTSUP;
[4c3c4a5]1084 }
[a86e4f8]1085 rootp->size = BPS(bs) * SPC(bs) * clusters;
1086 fibril_mutex_unlock(&rootp->idx->lock);
[4c3c4a5]1087
[a86e4f8]1088 /* Open root directory and looking for Bitmap and UC-Table */
1089 exfat_directory_t di;
1090 exfat_dentry_t *de;
1091 rc = exfat_directory_open(rootp, &di);
1092 if (rc != EOK) {
1093 free(rootp);
[375ab5e]1094 (void) block_cache_fini(service_id);
1095 block_fini(service_id);
1096 exfat_idx_fini_by_service_id(service_id);
[e97b8c6]1097 return ENOTSUP;
[a86e4f8]1098 }
[4c3c4a5]1099
[a86e4f8]1100 /* Initialize the bitmap node. */
1101 rc = exfat_directory_find(&di, EXFAT_DENTRY_BITMAP, &de);
1102 if (rc != EOK) {
1103 free(rootp);
[375ab5e]1104 (void) block_cache_fini(service_id);
1105 block_fini(service_id);
1106 exfat_idx_fini_by_service_id(service_id);
[e97b8c6]1107 return ENOTSUP;
[a86e4f8]1108 }
1109
[1b20da0]1110 rc = exfat_node_get_new_by_pos(&bitmapp, service_id, rootp->firstc,
[a86e4f8]1111 di.pos);
[0dbe5ac]1112 if (rc != EOK) {
[4c3c4a5]1113 free(rootp);
[375ab5e]1114 (void) block_cache_fini(service_id);
1115 block_fini(service_id);
1116 exfat_idx_fini_by_service_id(service_id);
[e97b8c6]1117 return ENOMEM;
[4c3c4a5]1118 }
[a86e4f8]1119 assert(bitmapp->idx->index == EXFAT_BITMAP_IDX);
1120 fibril_mutex_unlock(&bitmapp->idx->lock);
1121
1122 bitmapp->type = EXFAT_BITMAP;
[c56c4576]1123 bitmapp->firstc = uint32_t_le2host(de->bitmap.firstc);
[a86e4f8]1124 bitmapp->fragmented = true;
[1f1d96a]1125 bitmapp->idx->parent_fragmented = true;
[a86e4f8]1126 bitmapp->refcnt = 1;
1127 bitmapp->lnkcnt = 0;
[c56c4576]1128 bitmapp->size = uint64_t_le2host(de->bitmap.size);
[a86e4f8]1129
1130 /* Initialize the uctable node. */
1131 rc = exfat_directory_seek(&di, 0);
1132 if (rc != EOK) {
1133 free(rootp);
1134 free(bitmapp);
[375ab5e]1135 (void) block_cache_fini(service_id);
1136 block_fini(service_id);
1137 exfat_idx_fini_by_service_id(service_id);
[e97b8c6]1138 return ENOTSUP;
[a86e4f8]1139 }
[4c3c4a5]1140
[a86e4f8]1141 rc = exfat_directory_find(&di, EXFAT_DENTRY_UCTABLE, &de);
1142 if (rc != EOK) {
1143 free(rootp);
1144 free(bitmapp);
[375ab5e]1145 (void) block_cache_fini(service_id);
1146 block_fini(service_id);
1147 exfat_idx_fini_by_service_id(service_id);
[e97b8c6]1148 return ENOTSUP;
[a86e4f8]1149 }
[4c3c4a5]1150
[1b20da0]1151 rc = exfat_node_get_new_by_pos(&uctablep, service_id, rootp->firstc,
[a86e4f8]1152 di.pos);
[0dbe5ac]1153 if (rc != EOK) {
[a86e4f8]1154 free(rootp);
1155 free(bitmapp);
[375ab5e]1156 (void) block_cache_fini(service_id);
1157 block_fini(service_id);
1158 exfat_idx_fini_by_service_id(service_id);
[e97b8c6]1159 return ENOMEM;
[a86e4f8]1160 }
1161 assert(uctablep->idx->index == EXFAT_UCTABLE_IDX);
1162 fibril_mutex_unlock(&uctablep->idx->lock);
1163
1164 uctablep->type = EXFAT_UCTABLE;
[c56c4576]1165 uctablep->firstc = uint32_t_le2host(de->uctable.firstc);
[a86e4f8]1166 uctablep->fragmented = true;
[1f1d96a]1167 uctablep->idx->parent_fragmented = true;
[a86e4f8]1168 uctablep->refcnt = 1;
1169 uctablep->lnkcnt = 0;
[c56c4576]1170 uctablep->size = uint64_t_le2host(de->uctable.size);
[a86e4f8]1171
[f3504c1]1172 if (info != NULL) {
1173 /* Read volume label. */
1174 rc = exfat_directory_read_vollabel(&di, info->label,
1175 FS_LABEL_MAXLEN + 1);
1176 if (rc != EOK) {
1177 free(rootp);
1178 free(bitmapp);
1179 free(uctablep);
1180 (void) block_cache_fini(service_id);
1181 block_fini(service_id);
1182 exfat_idx_fini_by_service_id(service_id);
[18b6a88]1183 return ENOTSUP;
[f3504c1]1184 }
1185 }
1186
[a86e4f8]1187 rc = exfat_directory_close(&di);
[0dbe5ac]1188 if (rc != EOK) {
[a86e4f8]1189 free(rootp);
1190 free(bitmapp);
1191 free(uctablep);
[375ab5e]1192 (void) block_cache_fini(service_id);
1193 block_fini(service_id);
1194 exfat_idx_fini_by_service_id(service_id);
[e97b8c6]1195 return ENOMEM;
[a86e4f8]1196 }
1197
[e8975278]1198 if (0)
1199 exfat_fsinfo(bs, service_id);
[4c3c4a5]1200
[f3504c1]1201 *rrfn = FS_NODE(rootp);
1202 *rridxp = rootp->idx;
1203
1204 if (info != NULL) {
[3fafe5e0]1205#if 0
1206 str_cpy(info->label, FS_LABEL_MAXLEN + 1, label);
1207#endif
[f3504c1]1208 }
1209
[e97b8c6]1210 return EOK;
[6d57e1c]1211}
1212
[f3504c1]1213static void exfat_fs_close(service_id_t service_id, fs_node_t *rfn)
[6d57e1c]1214{
[4c3c4a5]1215 /*
1216 * Put the root node and force it to the FAT free node list.
1217 */
[f3504c1]1218 (void) exfat_node_put(rfn);
1219 (void) exfat_node_put(rfn);
[6d57e1c]1220
1221 /*
1222 * Perform cleanup of the node structures, index structures and
1223 * associated data. Write back this file system's dirty blocks and
1224 * stop using libblock for this instance.
1225 */
[375ab5e]1226 (void) exfat_node_fini_by_service_id(service_id);
1227 exfat_idx_fini_by_service_id(service_id);
1228 (void) block_cache_fini(service_id);
1229 block_fini(service_id);
[f3504c1]1230}
1231
1232/*
1233 * VFS_OUT operations.
1234 */
1235
1236/* Print debug info */
[8a8771c]1237static void exfat_fsinfo(exfat_bs_t *bs, service_id_t service_id)
[f3504c1]1238{
1239 printf("exFAT file system mounted\n");
1240 printf("Version: %d.%d\n", bs->version.major, bs->version.minor);
1241 printf("Volume serial: %d\n", uint32_t_le2host(bs->volume_serial));
[1938b381]1242 printf("Volume first sector: %" PRId64 "\n", VOL_FS(bs));
1243 printf("Volume sectors: %" PRId64 "\n", VOL_CNT(bs));
[f3504c1]1244 printf("FAT first sector: %d\n", FAT_FS(bs));
1245 printf("FAT sectors: %d\n", FAT_CNT(bs));
1246 printf("Data first sector: %d\n", DATA_FS(bs));
1247 printf("Data sectors: %d\n", DATA_CNT(bs));
1248 printf("Root dir first cluster: %d\n", ROOT_FC(bs));
1249 printf("Bytes per sector: %d\n", BPS(bs));
1250 printf("Sectors per cluster: %d\n", SPC(bs));
[e8975278]1251 printf("KBytes per cluster: %d\n", SPC(bs) * BPS(bs) / 1024);
[6d57e1c]1252
[f3504c1]1253 int i, rc;
1254 exfat_cluster_t clst;
[e8975278]1255 for (i = 0; i <= 7; i++) {
[f3504c1]1256 rc = exfat_get_cluster(bs, service_id, i, &clst);
1257 if (rc != EOK)
1258 return;
1259 printf("Clst %d: %x", i, clst);
[8a8771c]1260 if (i >= 2) {
1261 printf(", Bitmap: %d\n", exfat_bitmap_is_free(bs,
1262 service_id, i) != EOK);
1263 } else {
[f3504c1]1264 printf("\n");
[8a8771c]1265 }
[f3504c1]1266 }
1267}
1268
[b7fd2a0]1269static errno_t exfat_fsprobe(service_id_t service_id, vfs_fs_probe_info_t *info)
[f3504c1]1270{
[b7fd2a0]1271 errno_t rc;
[f3504c1]1272 exfat_idx_t *ridxp;
1273 fs_node_t *rfn;
1274
1275 rc = exfat_fs_open(service_id, CACHE_MODE_WT, &rfn, &ridxp, info);
1276 if (rc != EOK)
1277 return rc;
1278
1279 exfat_fs_close(service_id, rfn);
1280 return EOK;
1281}
1282
[b7fd2a0]1283static errno_t
[f3504c1]1284exfat_mounted(service_id_t service_id, const char *opts, fs_index_t *index,
1285 aoff64_t *size)
1286{
[b7fd2a0]1287 errno_t rc;
[f3504c1]1288 enum cache_mode cmode;
1289 exfat_idx_t *ridxp;
1290 fs_node_t *rfn;
1291
1292 /* Check for option enabling write through. */
1293 if (str_cmp(opts, "wtcache") == 0)
1294 cmode = CACHE_MODE_WT;
1295 else
1296 cmode = CACHE_MODE_WB;
1297
1298 rc = exfat_fs_open(service_id, cmode, &rfn, &ridxp, NULL);
1299 if (rc != EOK)
1300 return rc;
1301
1302 *index = ridxp->index;
1303 *size = EXFAT_NODE(rfn)->size;
1304
1305 return EOK;
1306}
1307
[b7fd2a0]1308static errno_t exfat_unmounted(service_id_t service_id)
[f3504c1]1309{
1310 fs_node_t *rfn;
[b7fd2a0]1311 errno_t rc;
[f3504c1]1312
1313 rc = exfat_root_get(&rfn, service_id);
1314 if (rc != EOK)
1315 return rc;
1316
1317 exfat_fs_close(service_id, rfn);
[e97b8c6]1318 return EOK;
[6d57e1c]1319}
[db9aa04e]1320
[b7fd2a0]1321static errno_t
[375ab5e]1322exfat_read(service_id_t service_id, fs_index_t index, aoff64_t pos,
[e97b8c6]1323 size_t *rbytes)
[36c2679]1324{
1325 fs_node_t *fn;
1326 exfat_node_t *nodep;
1327 exfat_bs_t *bs;
[0dbe5ac]1328 size_t bytes = 0;
[36c2679]1329 block_t *b;
[b7fd2a0]1330 errno_t rc;
[36c2679]1331
[375ab5e]1332 rc = exfat_node_get(&fn, service_id, index);
[e97b8c6]1333 if (rc != EOK)
1334 return rc;
1335 if (!fn)
1336 return ENOENT;
[36c2679]1337 nodep = EXFAT_NODE(fn);
1338
[984a9ba]1339 ipc_call_t call;
[36c2679]1340 size_t len;
[984a9ba]1341 if (!async_data_read_receive(&call, &len)) {
[36c2679]1342 exfat_node_put(fn);
[984a9ba]1343 async_answer_0(&call, EINVAL);
[e97b8c6]1344 return EINVAL;
[36c2679]1345 }
1346
[375ab5e]1347 bs = block_bb_get(service_id);
[36c2679]1348
1349 if (nodep->type == EXFAT_FILE) {
1350 /*
1351 * Our strategy for regular file reads is to read one block at
1352 * most and make use of the possibility to return less data than
1353 * requested. This keeps the code very simple.
1354 */
1355 if (pos >= nodep->size) {
1356 /* reading beyond the EOF */
1357 bytes = 0;
[984a9ba]1358 (void) async_data_read_finalize(&call, NULL, 0);
[36c2679]1359 } else {
1360 bytes = min(len, BPS(bs) - pos % BPS(bs));
1361 bytes = min(bytes, nodep->size - pos);
1362 rc = exfat_block_get(&b, bs, nodep, pos / BPS(bs),
1363 BLOCK_FLAGS_NONE);
1364 if (rc != EOK) {
1365 exfat_node_put(fn);
[984a9ba]1366 async_answer_0(&call, rc);
[e97b8c6]1367 return rc;
[36c2679]1368 }
[984a9ba]1369 (void) async_data_read_finalize(&call,
[36c2679]1370 b->data + pos % BPS(bs), bytes);
1371 rc = block_put(b);
1372 if (rc != EOK) {
1373 exfat_node_put(fn);
[e97b8c6]1374 return rc;
[36c2679]1375 }
1376 }
1377 } else {
1378 if (nodep->type != EXFAT_DIRECTORY) {
[984a9ba]1379 async_answer_0(&call, ENOTSUP);
[e97b8c6]1380 return ENOTSUP;
[36c2679]1381 }
[f3504c1]1382
[36c2679]1383 aoff64_t spos = pos;
[0dbe5ac]1384 char name[EXFAT_FILENAME_LEN + 1];
[36c2679]1385 exfat_file_dentry_t df;
1386 exfat_stream_dentry_t ds;
1387
1388 assert(nodep->size % BPS(bs) == 0);
1389 assert(BPS(bs) % sizeof(exfat_dentry_t) == 0);
1390
1391 exfat_directory_t di;
1392 rc = exfat_directory_open(nodep, &di);
[76da580a]1393 if (rc != EOK)
1394 goto err;
1395
[36c2679]1396 rc = exfat_directory_seek(&di, pos);
1397 if (rc != EOK) {
1398 (void) exfat_directory_close(&di);
1399 goto err;
1400 }
1401
[0dbe5ac]1402 rc = exfat_directory_read_file(&di, name, EXFAT_FILENAME_LEN,
1403 &df, &ds);
1404 if (rc == EOK)
[76da580a]1405 goto hit;
[6722766]1406 else if (rc == ENOENT)
[76da580a]1407 goto miss;
[36c2679]1408
[6722766]1409 (void) exfat_directory_close(&di);
1410
[18b6a88]1411 err:
[36c2679]1412 (void) exfat_node_put(fn);
[984a9ba]1413 async_answer_0(&call, rc);
[e97b8c6]1414 return rc;
[36c2679]1415
[18b6a88]1416 miss:
[36c2679]1417 rc = exfat_directory_close(&di);
[76da580a]1418 if (rc != EOK)
[36c2679]1419 goto err;
1420 rc = exfat_node_put(fn);
[984a9ba]1421 async_answer_0(&call, rc != EOK ? rc : ENOENT);
[e97b8c6]1422 *rbytes = 0;
1423 return rc != EOK ? rc : ENOENT;
[36c2679]1424
[18b6a88]1425 hit:
[36c2679]1426 pos = di.pos;
1427 rc = exfat_directory_close(&di);
[0dbe5ac]1428 if (rc != EOK)
[36c2679]1429 goto err;
[984a9ba]1430 (void) async_data_read_finalize(&call, name,
[0dbe5ac]1431 str_size(name) + 1);
1432 bytes = (pos - spos) + 1;
[36c2679]1433 }
1434
1435 rc = exfat_node_put(fn);
[e97b8c6]1436 *rbytes = bytes;
1437 return rc;
[f6641d2]1438}
1439
[b7fd2a0]1440static errno_t exfat_close(service_id_t service_id, fs_index_t index)
[d8634a79]1441{
[e97b8c6]1442 return EOK;
[d8634a79]1443}
1444
[b7fd2a0]1445static errno_t exfat_sync(service_id_t service_id, fs_index_t index)
[6f60727]1446{
1447 fs_node_t *fn;
[b7fd2a0]1448 errno_t rc = exfat_node_get(&fn, service_id, index);
[e97b8c6]1449 if (rc != EOK)
1450 return rc;
1451 if (!fn)
1452 return ENOENT;
[6f60727]1453
1454 exfat_node_t *nodep = EXFAT_NODE(fn);
1455
1456 nodep->dirty = true;
1457 rc = exfat_node_sync(nodep);
1458
1459 exfat_node_put(fn);
[e97b8c6]1460 return rc;
1461}
1462
[b7fd2a0]1463static errno_t
[375ab5e]1464exfat_write(service_id_t service_id, fs_index_t index, aoff64_t pos,
[e97b8c6]1465 size_t *wbytes, aoff64_t *nsize)
1466{
[998a78f]1467 fs_node_t *fn;
1468 exfat_node_t *nodep;
1469 exfat_bs_t *bs;
1470 size_t bytes;
1471 block_t *b;
1472 aoff64_t boundary;
1473 int flags = BLOCK_FLAGS_NONE;
[b7fd2a0]1474 errno_t rc;
[998a78f]1475
[375ab5e]1476 rc = exfat_node_get(&fn, service_id, index);
[998a78f]1477 if (rc != EOK)
1478 return rc;
1479 if (!fn)
1480 return ENOENT;
1481 nodep = EXFAT_NODE(fn);
1482
[984a9ba]1483 ipc_call_t call;
[998a78f]1484 size_t len;
[984a9ba]1485 if (!async_data_write_receive(&call, &len)) {
[998a78f]1486 (void) exfat_node_put(fn);
[984a9ba]1487 async_answer_0(&call, EINVAL);
[998a78f]1488 return EINVAL;
1489 }
1490
[375ab5e]1491 bs = block_bb_get(service_id);
[998a78f]1492
1493 /*
1494 * In all scenarios, we will attempt to write out only one block worth
1495 * of data at maximum. There might be some more efficient approaches,
1496 * but this one greatly simplifies fat_write(). Note that we can afford
1497 * to do this because the client must be ready to handle the return
1498 * value signalizing a smaller number of bytes written.
1499 */
1500 bytes = min(len, BPS(bs) - pos % BPS(bs));
1501 if (bytes == BPS(bs))
1502 flags |= BLOCK_FLAGS_NOREAD;
1503
1504 boundary = ROUND_UP(nodep->size, BPC(bs));
1505 if (pos >= boundary) {
1506 unsigned nclsts;
1507 nclsts = (ROUND_UP(pos + bytes, BPC(bs)) - boundary) / BPC(bs);
[375ab5e]1508 rc = exfat_node_expand(service_id, nodep, nclsts);
[998a78f]1509 if (rc != EOK) {
1510 /* could not expand node */
1511 (void) exfat_node_put(fn);
[984a9ba]1512 async_answer_0(&call, rc);
[998a78f]1513 return rc;
1514 }
1515 }
1516
1517 if (pos + bytes > nodep->size) {
1518 nodep->size = pos + bytes;
1519 nodep->dirty = true; /* need to sync node */
1520 }
1521
1522 /*
1523 * This is the easier case - we are either overwriting already
1524 * existing contents or writing behind the EOF, but still within
1525 * the limits of the last cluster. The node size may grow to the
1526 * next block size boundary.
1527 */
1528 rc = exfat_block_get(&b, bs, nodep, pos / BPS(bs), flags);
1529 if (rc != EOK) {
1530 (void) exfat_node_put(fn);
[984a9ba]1531 async_answer_0(&call, rc);
[998a78f]1532 return rc;
1533 }
1534
[984a9ba]1535 (void) async_data_write_finalize(&call,
[76da580a]1536 b->data + pos % BPS(bs), bytes);
[998a78f]1537 b->dirty = true; /* need to sync block */
1538 rc = block_put(b);
1539 if (rc != EOK) {
1540 (void) exfat_node_put(fn);
1541 return rc;
1542 }
1543
1544 *wbytes = bytes;
1545 *nsize = nodep->size;
1546 rc = exfat_node_put(fn);
1547 return rc;
[e97b8c6]1548}
1549
[b7fd2a0]1550static errno_t
[375ab5e]1551exfat_truncate(service_id_t service_id, fs_index_t index, aoff64_t size)
[e97b8c6]1552{
[998a78f]1553 fs_node_t *fn;
1554 exfat_node_t *nodep;
1555 exfat_bs_t *bs;
[b7fd2a0]1556 errno_t rc;
[998a78f]1557
[375ab5e]1558 rc = exfat_node_get(&fn, service_id, index);
[998a78f]1559 if (rc != EOK)
1560 return rc;
1561 if (!fn)
1562 return ENOENT;
1563 nodep = EXFAT_NODE(fn);
1564
[375ab5e]1565 bs = block_bb_get(service_id);
[998a78f]1566
1567 if (nodep->size == size) {
1568 rc = EOK;
1569 } else if (nodep->size < size) {
1570 /*
1571 * The standard says we have the freedom to grow the node.
1572 * For now, we simply return an error.
1573 */
1574 rc = EINVAL;
1575 } else if (ROUND_UP(nodep->size, BPC(bs)) == ROUND_UP(size, BPC(bs))) {
1576 /*
1577 * The node will be shrunk, but no clusters will be deallocated.
1578 */
1579 nodep->size = size;
1580 nodep->dirty = true; /* need to sync node */
1581 rc = EOK;
1582 } else {
[375ab5e]1583 rc = exfat_node_shrink(service_id, nodep, size);
[998a78f]1584 }
1585
[b7fd2a0]1586 errno_t rc2 = exfat_node_put(fn);
[7cede12c]1587 if (rc == EOK && rc2 != EOK)
1588 rc = rc2;
1589
[998a78f]1590 return rc;
[e97b8c6]1591}
[998a78f]1592
[b7fd2a0]1593static errno_t exfat_destroy(service_id_t service_id, fs_index_t index)
[e97b8c6]1594{
[998a78f]1595 fs_node_t *fn;
1596 exfat_node_t *nodep;
[b7fd2a0]1597 errno_t rc;
[998a78f]1598
[375ab5e]1599 rc = exfat_node_get(&fn, service_id, index);
[998a78f]1600 if (rc != EOK)
1601 return rc;
1602 if (!fn)
1603 return ENOENT;
1604
1605 nodep = EXFAT_NODE(fn);
1606 /*
1607 * We should have exactly two references. One for the above
1608 * call to fat_node_get() and one from fat_unlink().
1609 */
1610 assert(nodep->refcnt == 2);
1611
1612 rc = exfat_destroy_node(fn);
1613 return rc;
[6f60727]1614}
1615
[e97b8c6]1616vfs_out_ops_t exfat_ops = {
[d2c8533]1617 .fsprobe = exfat_fsprobe,
[e97b8c6]1618 .mounted = exfat_mounted,
1619 .unmounted = exfat_unmounted,
1620 .read = exfat_read,
1621 .write = exfat_write,
1622 .truncate = exfat_truncate,
1623 .close = exfat_close,
1624 .destroy = exfat_destroy,
1625 .sync = exfat_sync,
1626};
1627
[db9aa04e]1628/**
1629 * @}
1630 */
Note: See TracBrowser for help on using the repository browser.