[5d5863c] | 1 | /*
|
---|
| 2 | * Copyright (c) 2011 Oleg Romanenko
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup fs
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 |
|
---|
| 33 | /**
|
---|
| 34 | * @file exfat_bitmap.c
|
---|
| 35 | * @brief Functions that manipulate the Bitmap Table.
|
---|
| 36 | */
|
---|
| 37 |
|
---|
| 38 | #include "exfat_bitmap.h"
|
---|
| 39 | #include "../../vfs/vfs.h"
|
---|
| 40 | #include <libfs.h>
|
---|
| 41 | #include <libblock.h>
|
---|
| 42 | #include <errno.h>
|
---|
| 43 | #include <byteorder.h>
|
---|
| 44 | #include <align.h>
|
---|
| 45 | #include <assert.h>
|
---|
| 46 | #include <fibril_synch.h>
|
---|
| 47 | #include <malloc.h>
|
---|
| 48 | #include <mem.h>
|
---|
| 49 |
|
---|
| 50 |
|
---|
[375ab5e] | 51 | int bitmap_is_free(exfat_bs_t *bs, service_id_t service_id,
|
---|
[5d5863c] | 52 | exfat_cluster_t clst)
|
---|
| 53 | {
|
---|
[25c60f4] | 54 | fs_node_t *fn;
|
---|
[ff0c270] | 55 | block_t *b = NULL;
|
---|
[25c60f4] | 56 | exfat_node_t *bitmapp;
|
---|
| 57 | uint8_t *bitmap;
|
---|
| 58 | int rc;
|
---|
| 59 | bool alloc;
|
---|
| 60 |
|
---|
| 61 | clst -= EXFAT_CLST_FIRST;
|
---|
| 62 |
|
---|
[375ab5e] | 63 | rc = exfat_bitmap_get(&fn, service_id);
|
---|
[25c60f4] | 64 | if (rc != EOK)
|
---|
| 65 | return rc;
|
---|
| 66 | bitmapp = EXFAT_NODE(fn);
|
---|
| 67 |
|
---|
| 68 | aoff64_t offset = clst / 8;
|
---|
| 69 | rc = exfat_block_get(&b, bs, bitmapp, offset / BPS(bs), BLOCK_FLAGS_NONE);
|
---|
| 70 | if (rc != EOK) {
|
---|
| 71 | (void) exfat_node_put(fn);
|
---|
| 72 | return rc;
|
---|
| 73 | }
|
---|
| 74 | bitmap = (uint8_t *)b->data;
|
---|
| 75 | alloc = bitmap[offset % BPS(bs)] & (1 << (clst % 8));
|
---|
| 76 |
|
---|
| 77 | rc = block_put(b);
|
---|
| 78 | if (rc != EOK) {
|
---|
| 79 | (void) exfat_node_put(fn);
|
---|
| 80 | return rc;
|
---|
| 81 | }
|
---|
| 82 | rc = exfat_node_put(fn);
|
---|
| 83 | if (rc != EOK)
|
---|
| 84 | return rc;
|
---|
| 85 |
|
---|
| 86 | if (alloc)
|
---|
| 87 | return ENOENT;
|
---|
| 88 |
|
---|
[5d5863c] | 89 | return EOK;
|
---|
| 90 | }
|
---|
| 91 |
|
---|
[375ab5e] | 92 | int bitmap_set_cluster(exfat_bs_t *bs, service_id_t service_id,
|
---|
[25c60f4] | 93 | exfat_cluster_t clst)
|
---|
[5d5863c] | 94 | {
|
---|
| 95 | fs_node_t *fn;
|
---|
[ff0c270] | 96 | block_t *b = NULL;
|
---|
[5d5863c] | 97 | exfat_node_t *bitmapp;
|
---|
| 98 | uint8_t *bitmap;
|
---|
| 99 | int rc;
|
---|
| 100 |
|
---|
[25c60f4] | 101 | clst -= EXFAT_CLST_FIRST;
|
---|
[5d5863c] | 102 |
|
---|
[375ab5e] | 103 | rc = exfat_bitmap_get(&fn, service_id);
|
---|
[5d5863c] | 104 | if (rc != EOK)
|
---|
| 105 | return rc;
|
---|
| 106 | bitmapp = EXFAT_NODE(fn);
|
---|
| 107 |
|
---|
[25c60f4] | 108 | aoff64_t offset = clst / 8;
|
---|
[5d5863c] | 109 | rc = exfat_block_get(&b, bs, bitmapp, offset / BPS(bs), BLOCK_FLAGS_NONE);
|
---|
| 110 | if (rc != EOK) {
|
---|
| 111 | (void) exfat_node_put(fn);
|
---|
| 112 | return rc;
|
---|
| 113 | }
|
---|
| 114 | bitmap = (uint8_t *)b->data;
|
---|
[25c60f4] | 115 | bitmap[offset % BPS(bs)] |= (1 << (clst % 8));
|
---|
[5d5863c] | 116 |
|
---|
| 117 | b->dirty = true;
|
---|
| 118 | rc = block_put(b);
|
---|
| 119 | if (rc != EOK) {
|
---|
| 120 | (void) exfat_node_put(fn);
|
---|
| 121 | return rc;
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | return exfat_node_put(fn);
|
---|
| 125 | }
|
---|
| 126 |
|
---|
[375ab5e] | 127 | int bitmap_clear_cluster(exfat_bs_t *bs, service_id_t service_id,
|
---|
[25c60f4] | 128 | exfat_cluster_t clst)
|
---|
[5d5863c] | 129 | {
|
---|
[25c60f4] | 130 | fs_node_t *fn;
|
---|
[ff0c270] | 131 | block_t *b = NULL;
|
---|
[25c60f4] | 132 | exfat_node_t *bitmapp;
|
---|
| 133 | uint8_t *bitmap;
|
---|
| 134 | int rc;
|
---|
| 135 |
|
---|
| 136 | clst -= EXFAT_CLST_FIRST;
|
---|
| 137 |
|
---|
[375ab5e] | 138 | rc = exfat_bitmap_get(&fn, service_id);
|
---|
[25c60f4] | 139 | if (rc != EOK)
|
---|
| 140 | return rc;
|
---|
| 141 | bitmapp = EXFAT_NODE(fn);
|
---|
| 142 |
|
---|
| 143 | aoff64_t offset = clst / 8;
|
---|
[ff0c270] | 144 | rc = exfat_block_get(&b, bs, bitmapp, offset / BPS(bs),
|
---|
| 145 | BLOCK_FLAGS_NONE);
|
---|
[25c60f4] | 146 | if (rc != EOK) {
|
---|
| 147 | (void) exfat_node_put(fn);
|
---|
| 148 | return rc;
|
---|
| 149 | }
|
---|
| 150 | bitmap = (uint8_t *)b->data;
|
---|
| 151 | bitmap[offset % BPS(bs)] &= ~(1 << (clst % 8));
|
---|
| 152 |
|
---|
| 153 | b->dirty = true;
|
---|
| 154 | rc = block_put(b);
|
---|
| 155 | if (rc != EOK) {
|
---|
| 156 | (void) exfat_node_put(fn);
|
---|
| 157 | return rc;
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 | return exfat_node_put(fn);
|
---|
[5d5863c] | 161 | }
|
---|
| 162 |
|
---|
[375ab5e] | 163 | int bitmap_set_clusters(exfat_bs_t *bs, service_id_t service_id,
|
---|
[5d5863c] | 164 | exfat_cluster_t firstc, exfat_cluster_t count)
|
---|
| 165 | {
|
---|
| 166 | int rc;
|
---|
| 167 | exfat_cluster_t clst;
|
---|
| 168 | clst = firstc;
|
---|
| 169 |
|
---|
[ff0c270] | 170 | while (clst < firstc + count ) {
|
---|
[375ab5e] | 171 | rc = bitmap_set_cluster(bs, service_id, clst);
|
---|
[5d5863c] | 172 | if (rc != EOK) {
|
---|
[ff0c270] | 173 | if (clst - firstc > 0)
|
---|
| 174 | (void) bitmap_clear_clusters(bs, service_id,
|
---|
| 175 | firstc, clst - firstc);
|
---|
[5d5863c] | 176 | return rc;
|
---|
| 177 | }
|
---|
| 178 | clst++;
|
---|
| 179 | }
|
---|
| 180 | return EOK;
|
---|
| 181 | }
|
---|
| 182 |
|
---|
[375ab5e] | 183 | int bitmap_clear_clusters(exfat_bs_t *bs, service_id_t service_id,
|
---|
[5d5863c] | 184 | exfat_cluster_t firstc, exfat_cluster_t count)
|
---|
| 185 | {
|
---|
| 186 | int rc;
|
---|
| 187 | exfat_cluster_t clst;
|
---|
| 188 | clst = firstc;
|
---|
| 189 |
|
---|
[ff0c270] | 190 | while (clst < firstc + count) {
|
---|
[375ab5e] | 191 | rc = bitmap_clear_cluster(bs, service_id, clst);
|
---|
[5d5863c] | 192 | if (rc != EOK)
|
---|
| 193 | return rc;
|
---|
| 194 | clst++;
|
---|
| 195 | }
|
---|
| 196 | return EOK;
|
---|
| 197 | }
|
---|
| 198 |
|
---|
[375ab5e] | 199 | int bitmap_alloc_clusters(exfat_bs_t *bs, service_id_t service_id,
|
---|
[5d5863c] | 200 | exfat_cluster_t *firstc, exfat_cluster_t count)
|
---|
| 201 | {
|
---|
| 202 | exfat_cluster_t startc, endc;
|
---|
| 203 | startc = EXFAT_CLST_FIRST;
|
---|
| 204 |
|
---|
[ff0c270] | 205 | while (startc < DATA_CNT(bs) + 2) {
|
---|
[e0d98b2] | 206 | endc = startc;
|
---|
[375ab5e] | 207 | while (bitmap_is_free(bs, service_id, endc) == EOK) {
|
---|
[ff0c270] | 208 | if ((endc - startc) + 1 == count) {
|
---|
[e0d98b2] | 209 | *firstc = startc;
|
---|
[375ab5e] | 210 | return bitmap_set_clusters(bs, service_id, startc, count);
|
---|
[ff0c270] | 211 | } else
|
---|
[e0d98b2] | 212 | endc++;
|
---|
| 213 | }
|
---|
| 214 | startc = endc+1;
|
---|
[5d5863c] | 215 | }
|
---|
| 216 | return ENOSPC;
|
---|
| 217 | }
|
---|
| 218 |
|
---|
| 219 |
|
---|
| 220 | int bitmap_append_clusters(exfat_bs_t *bs, exfat_node_t *nodep,
|
---|
| 221 | exfat_cluster_t count)
|
---|
| 222 | {
|
---|
| 223 | if (nodep->firstc == 0) {
|
---|
[375ab5e] | 224 | return bitmap_alloc_clusters(bs, nodep->idx->service_id,
|
---|
[5d5863c] | 225 | &nodep->firstc, count);
|
---|
| 226 | } else {
|
---|
| 227 | exfat_cluster_t lastc, clst;
|
---|
| 228 | lastc = nodep->firstc + ROUND_UP(nodep->size, BPC(bs)) / BPC(bs) - 1;
|
---|
| 229 |
|
---|
[ff0c270] | 230 | clst = lastc + 1;
|
---|
[375ab5e] | 231 | while (bitmap_is_free(bs, nodep->idx->service_id, clst) == EOK) {
|
---|
[ff0c270] | 232 | if (clst - lastc == count){
|
---|
[375ab5e] | 233 | return bitmap_set_clusters(bs, nodep->idx->service_id,
|
---|
[ff0c270] | 234 | lastc + 1, count);
|
---|
| 235 | } else
|
---|
[5d5863c] | 236 | clst++;
|
---|
| 237 | }
|
---|
| 238 | return ENOSPC;
|
---|
| 239 | }
|
---|
| 240 | }
|
---|
| 241 |
|
---|
| 242 |
|
---|
| 243 | int bitmap_free_clusters(exfat_bs_t *bs, exfat_node_t *nodep,
|
---|
| 244 | exfat_cluster_t count)
|
---|
| 245 | {
|
---|
| 246 | exfat_cluster_t lastc;
|
---|
| 247 | lastc = nodep->firstc + ROUND_UP(nodep->size, BPC(bs)) / BPC(bs) - 1;
|
---|
| 248 | lastc -= count;
|
---|
| 249 |
|
---|
[ff0c270] | 250 | return bitmap_clear_clusters(bs, nodep->idx->service_id, lastc + 1, count);
|
---|
[5d5863c] | 251 | }
|
---|
| 252 |
|
---|
| 253 |
|
---|
| 254 | int bitmap_replicate_clusters(exfat_bs_t *bs, exfat_node_t *nodep)
|
---|
| 255 | {
|
---|
| 256 | int rc;
|
---|
| 257 | exfat_cluster_t lastc, clst;
|
---|
[375ab5e] | 258 | service_id_t service_id = nodep->idx->service_id;
|
---|
[5d5863c] | 259 | lastc = nodep->firstc + ROUND_UP(nodep->size, BPC(bs)) / BPC(bs) - 1;
|
---|
| 260 |
|
---|
| 261 | for (clst = nodep->firstc; clst < lastc; clst++) {
|
---|
[ff0c270] | 262 | rc = exfat_set_cluster(bs, service_id, clst, clst + 1);
|
---|
[5d5863c] | 263 | if (rc != EOK)
|
---|
| 264 | return rc;
|
---|
| 265 | }
|
---|
| 266 |
|
---|
[375ab5e] | 267 | return exfat_set_cluster(bs, service_id, lastc, EXFAT_CLST_EOF);
|
---|
[5d5863c] | 268 | }
|
---|
| 269 |
|
---|
| 270 |
|
---|
| 271 |
|
---|
| 272 | /**
|
---|
| 273 | * @}
|
---|
| 274 | */
|
---|