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