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