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