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