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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 7cede12c was 7cede12c, checked in by Maurizio Lombardi <m.lombardi85@…>, 14 years ago

exfat: Do not ignore the return value of exfat_node_put().

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