Rev 2890 | Rev 2945 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 2876 | jermar | 1 | /* |
| 2 | * Copyright (c) 2008 Jakub Jermar |
||
| 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 fat_idx.c |
||
| 35 | * @brief Layer for translating FAT entities to VFS node indices. |
||
| 36 | */ |
||
| 37 | |||
| 38 | #include "fat.h" |
||
| 2881 | jermar | 39 | #include "../../vfs/vfs.h" |
| 2876 | jermar | 40 | #include <errno.h> |
| 41 | #include <string.h> |
||
| 42 | #include <libadt/hash_table.h> |
||
| 43 | #include <libadt/list.h> |
||
| 44 | #include <assert.h> |
||
| 45 | #include <futex.h> |
||
| 46 | |||
| 2881 | jermar | 47 | /** Each instance of this type describes one interval of freed VFS indices. */ |
| 48 | typedef struct { |
||
| 49 | link_t link; |
||
| 50 | fs_index_t first; |
||
| 51 | fs_index_t last; |
||
| 52 | } freed_t; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Each instance of this type describes state of all VFS indices that |
||
| 56 | * are currently unused. |
||
| 57 | */ |
||
| 58 | typedef struct { |
||
| 59 | link_t link; |
||
| 60 | dev_handle_t dev_handle; |
||
| 61 | |||
| 62 | /** Next unassigned index. */ |
||
| 63 | fs_index_t next; |
||
| 64 | /** Number of remaining unassigned indices. */ |
||
| 65 | uint64_t remaining; |
||
| 66 | |||
| 67 | /** Sorted list of intervals of freed indices. */ |
||
| 68 | link_t freed_head; |
||
| 69 | } unused_t; |
||
| 70 | |||
| 2944 | jermar | 71 | /** Futex protecting the list of unused structures. */ |
| 72 | static futex_t unused_futex = FUTEX_INITIALIZER; |
||
| 73 | |||
| 74 | /** List of unused structures. */ |
||
| 2881 | jermar | 75 | static LIST_INITIALIZE(unused_head); |
| 76 | |||
| 2890 | jermar | 77 | /** |
| 78 | * Global hash table of all used fat_idx_t structures. |
||
| 79 | * The index structures are hashed by the dev_handle, parent node's first |
||
| 80 | * cluster and index within the parent directory. |
||
| 81 | */ |
||
| 82 | static hash_table_t up_hash; |
||
| 2889 | jermar | 83 | |
| 2890 | jermar | 84 | #define UPH_BUCKETS_LOG 12 |
| 85 | #define UPH_BUCKETS (1 << UPH_BUCKETS_LOG) |
||
| 2889 | jermar | 86 | |
| 2890 | jermar | 87 | #define UPH_DH_KEY 0 |
| 88 | #define UPH_PFC_KEY 1 |
||
| 89 | #define UPH_PDI_KEY 2 |
||
| 2889 | jermar | 90 | |
| 2890 | jermar | 91 | static hash_index_t pos_hash(unsigned long key[]) |
| 2889 | jermar | 92 | { |
| 2890 | jermar | 93 | dev_handle_t dev_handle = (dev_handle_t)key[UPH_DH_KEY]; |
| 94 | fat_cluster_t pfc = (fat_cluster_t)key[UPH_PFC_KEY]; |
||
| 95 | unsigned pdi = (unsigned)key[UPH_PDI_KEY]; |
||
| 2889 | jermar | 96 | |
| 97 | hash_index_t h; |
||
| 98 | |||
| 99 | /* |
||
| 100 | * The least significant half of all bits are the least significant bits |
||
| 101 | * of the parent node's first cluster. |
||
| 102 | * |
||
| 103 | * The least significant half of the most significant half of all bits |
||
| 104 | * are the least significant bits of the node's dentry index within the |
||
| 105 | * parent directory node. |
||
| 106 | * |
||
| 107 | * The most significant half of the most significant half of all bits |
||
| 108 | * are the least significant bits of the device handle. |
||
| 109 | */ |
||
| 2890 | jermar | 110 | h = pfc & ((1 << (UPH_BUCKETS_LOG / 2)) - 1); |
| 111 | h |= (pdi & ((1 << (UPH_BUCKETS_LOG / 4)) - 1)) << |
||
| 112 | (UPH_BUCKETS_LOG / 2); |
||
| 113 | h |= (dev_handle & ((1 << (UPH_BUCKETS_LOG / 4)) - 1)) << |
||
| 114 | (3 * (UPH_BUCKETS_LOG / 4)); |
||
| 2889 | jermar | 115 | |
| 116 | return h; |
||
| 117 | } |
||
| 118 | |||
| 2890 | jermar | 119 | static int pos_compare(unsigned long key[], hash_count_t keys, link_t *item) |
| 2889 | jermar | 120 | { |
| 2890 | jermar | 121 | dev_handle_t dev_handle = (dev_handle_t)key[UPH_DH_KEY]; |
| 122 | fat_cluster_t pfc = (fat_cluster_t)key[UPH_PFC_KEY]; |
||
| 123 | unsigned pdi = (unsigned)key[UPH_PDI_KEY]; |
||
| 124 | fat_idx_t *fidx = list_get_instance(item, fat_idx_t, uph_link); |
||
| 2889 | jermar | 125 | |
| 126 | return (dev_handle == fidx->dev_handle) && (pfc == fidx->pfc) && |
||
| 127 | (pdi == fidx->pdi); |
||
| 128 | } |
||
| 129 | |||
| 2890 | jermar | 130 | static void pos_remove_callback(link_t *item) |
| 131 | { |
||
| 132 | /* nothing to do */ |
||
| 133 | } |
||
| 134 | |||
| 135 | static hash_table_operations_t uph_ops = { |
||
| 136 | .hash = pos_hash, |
||
| 137 | .compare = pos_compare, |
||
| 138 | .remove_callback = pos_remove_callback, |
||
| 139 | }; |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Global hash table of all used fat_idx_t structures. |
||
| 143 | * The index structures are hashed by the dev_handle and index. |
||
| 144 | */ |
||
| 145 | static hash_table_t ui_hash; |
||
| 146 | |||
| 147 | #define UIH_BUCKETS_LOG 12 |
||
| 148 | #define UIH_BUCKETS (1 << UIH_BUCKETS_LOG) |
||
| 149 | |||
| 150 | #define UIH_DH_KEY 0 |
||
| 151 | #define UIH_INDEX_KEY 1 |
||
| 152 | |||
| 153 | static hash_index_t idx_hash(unsigned long key[]) |
||
| 154 | { |
||
| 155 | dev_handle_t dev_handle = (dev_handle_t)key[UIH_DH_KEY]; |
||
| 156 | fs_index_t index = (fs_index_t)key[UIH_INDEX_KEY]; |
||
| 157 | |||
| 158 | hash_index_t h; |
||
| 159 | |||
| 160 | h = dev_handle & ((1 << (UIH_BUCKETS_LOG / 2)) - 1); |
||
| 161 | h |= (index & ((1 << (UIH_BUCKETS_LOG / 2)) - 1)) << |
||
| 162 | (UIH_BUCKETS_LOG / 2); |
||
| 163 | |||
| 164 | return h; |
||
| 165 | } |
||
| 166 | |||
| 167 | static int idx_compare(unsigned long key[], hash_count_t keys, link_t *item) |
||
| 168 | { |
||
| 169 | dev_handle_t dev_handle = (dev_handle_t)key[UIH_DH_KEY]; |
||
| 170 | fs_index_t index = (fs_index_t)key[UIH_INDEX_KEY]; |
||
| 171 | fat_idx_t *fidx = list_get_instance(item, fat_idx_t, uih_link); |
||
| 172 | |||
| 173 | return (dev_handle == fidx->dev_handle) && (index == fidx->index); |
||
| 174 | } |
||
| 175 | |||
| 2889 | jermar | 176 | static void idx_remove_callback(link_t *item) |
| 177 | { |
||
| 178 | /* nothing to do */ |
||
| 179 | } |
||
| 180 | |||
| 2890 | jermar | 181 | static hash_table_operations_t uih_ops = { |
| 2889 | jermar | 182 | .hash = idx_hash, |
| 183 | .compare = idx_compare, |
||
| 184 | .remove_callback = idx_remove_callback, |
||
| 185 | }; |
||
| 186 | |||
| 2881 | jermar | 187 | /** Allocate a VFS index which is not currently in use. */ |
| 188 | static bool fat_idx_alloc(dev_handle_t dev_handle, fs_index_t *index) |
||
| 2876 | jermar | 189 | { |
| 2881 | jermar | 190 | link_t *l; |
| 191 | unused_t *u; |
||
| 192 | |||
| 193 | assert(index); |
||
| 2944 | jermar | 194 | futex_down(&unused_futex); |
| 2881 | jermar | 195 | for (l = unused_head.next; l != &unused_head; l = l->next) { |
| 196 | u = list_get_instance(l, unused_t, link); |
||
| 197 | if (u->dev_handle == dev_handle) |
||
| 198 | goto hit; |
||
| 199 | } |
||
| 2944 | jermar | 200 | futex_up(&unused_futex); |
| 201 | |||
| 2881 | jermar | 202 | /* dev_handle not found */ |
| 203 | return false; |
||
| 204 | |||
| 205 | hit: |
||
| 206 | if (list_empty(&u->freed_head)) { |
||
| 207 | if (u->remaining) { |
||
| 208 | /* |
||
| 209 | * There are no freed indices, allocate one directly |
||
| 210 | * from the counter. |
||
| 211 | */ |
||
| 212 | *index = u->next++; |
||
| 213 | --u->remaining; |
||
| 2944 | jermar | 214 | futex_up(&unused_futex); |
| 2881 | jermar | 215 | return true; |
| 216 | } |
||
| 217 | } else { |
||
| 218 | /* There are some freed indices which we can reuse. */ |
||
| 219 | freed_t *f = list_get_instance(u->freed_head.next, freed_t, |
||
| 220 | link); |
||
| 221 | *index = f->first; |
||
| 222 | if (f->first++ == f->last) { |
||
| 223 | /* Destroy the interval. */ |
||
| 224 | list_remove(&f->link); |
||
| 225 | free(f); |
||
| 226 | } |
||
| 2944 | jermar | 227 | futex_up(&unused_futex); |
| 2881 | jermar | 228 | return true; |
| 229 | } |
||
| 230 | /* |
||
| 231 | * We ran out of indices, which is extremely unlikely with FAT16, but |
||
| 232 | * theoretically still possible (e.g. too many open unlinked nodes or |
||
| 233 | * too many zero-sized nodes). |
||
| 234 | */ |
||
| 2944 | jermar | 235 | futex_up(&unused_futex); |
| 2881 | jermar | 236 | return false; |
| 237 | } |
||
| 238 | |||
| 2884 | jermar | 239 | /** If possible, coalesce two intervals of freed indices. */ |
| 240 | static void try_coalesce_intervals(link_t *l, link_t *r, link_t *cur) |
||
| 2881 | jermar | 241 | { |
| 242 | freed_t *fl = list_get_instance(l, freed_t, link); |
||
| 243 | freed_t *fr = list_get_instance(r, freed_t, link); |
||
| 244 | |||
| 245 | if (fl->last + 1 == fr->first) { |
||
| 246 | if (cur == l) { |
||
| 247 | fl->last = fr->last; |
||
| 248 | list_remove(r); |
||
| 249 | free(r); |
||
| 250 | } else { |
||
| 251 | fr->first = fl->first; |
||
| 252 | list_remove(l); |
||
| 253 | free(l); |
||
| 254 | } |
||
| 255 | } |
||
| 256 | } |
||
| 257 | |||
| 258 | /** Free a VFS index, which is no longer in use. */ |
||
| 259 | static void fat_idx_free(dev_handle_t dev_handle, fs_index_t index) |
||
| 260 | { |
||
| 261 | link_t *l; |
||
| 262 | unused_t *u; |
||
| 263 | |||
| 2944 | jermar | 264 | futex_down(&unused_futex); |
| 2881 | jermar | 265 | for (l = unused_head.next; l != &unused_head; l = l->next) { |
| 266 | u = list_get_instance(l, unused_t, link); |
||
| 267 | if (u->dev_handle == dev_handle) |
||
| 268 | goto hit; |
||
| 269 | } |
||
| 2944 | jermar | 270 | futex_up(&unused_futex); |
| 2881 | jermar | 271 | |
| 272 | /* should not happen */ |
||
| 273 | assert(0); |
||
| 274 | |||
| 275 | hit: |
||
| 276 | if (u->next == index + 1) { |
||
| 277 | /* The index can be returned directly to the counter. */ |
||
| 278 | u->next--; |
||
| 279 | u->remaining++; |
||
| 280 | } else { |
||
| 281 | /* |
||
| 282 | * The index must be returned either to an existing freed |
||
| 283 | * interval or a new interval must be created. |
||
| 284 | */ |
||
| 285 | link_t *lnk; |
||
| 286 | freed_t *n; |
||
| 287 | for (lnk = u->freed_head.next; lnk != &u->freed_head; |
||
| 288 | lnk = lnk->next) { |
||
| 289 | freed_t *f = list_get_instance(lnk, freed_t, link); |
||
| 290 | if (f->first == index + 1) { |
||
| 291 | f->first--; |
||
| 292 | if (lnk->prev != &u->freed_head) |
||
| 2884 | jermar | 293 | try_coalesce_intervals(lnk->prev, lnk, |
| 2881 | jermar | 294 | lnk); |
| 2944 | jermar | 295 | futex_up(&unused_futex); |
| 2881 | jermar | 296 | return; |
| 297 | } |
||
| 298 | if (f->last == index - 1) { |
||
| 299 | f->last++; |
||
| 300 | if (lnk->next != &u->freed_head) |
||
| 2884 | jermar | 301 | try_coalesce_intervals(lnk, lnk->next, |
| 2881 | jermar | 302 | lnk); |
| 2944 | jermar | 303 | futex_up(&unused_futex); |
| 2881 | jermar | 304 | return; |
| 305 | } |
||
| 306 | if (index > f->first) { |
||
| 307 | n = malloc(sizeof(freed_t)); |
||
| 308 | /* TODO: sleep until allocation succeeds */ |
||
| 309 | assert(n); |
||
| 310 | link_initialize(&n->link); |
||
| 311 | n->first = index; |
||
| 312 | n->last = index; |
||
| 313 | list_insert_before(&n->link, lnk); |
||
| 2944 | jermar | 314 | futex_up(&unused_futex); |
| 2881 | jermar | 315 | return; |
| 316 | } |
||
| 317 | |||
| 318 | } |
||
| 319 | /* The index will form the last interval. */ |
||
| 320 | n = malloc(sizeof(freed_t)); |
||
| 321 | /* TODO: sleep until allocation succeeds */ |
||
| 322 | assert(n); |
||
| 323 | link_initialize(&n->link); |
||
| 324 | n->first = index; |
||
| 325 | n->last = index; |
||
| 326 | list_append(&n->link, &u->freed_head); |
||
| 327 | } |
||
| 2944 | jermar | 328 | futex_up(&unused_futex); |
| 2881 | jermar | 329 | } |
| 330 | |||
| 2890 | jermar | 331 | fat_idx_t * |
| 332 | fat_idx_get_by_pos(dev_handle_t dev_handle, fat_cluster_t pfc, unsigned pdi) |
||
| 2881 | jermar | 333 | { |
| 2889 | jermar | 334 | fat_idx_t *fidx; |
| 335 | link_t *l; |
||
| 2890 | jermar | 336 | unsigned long pkey[] = { |
| 337 | [UPH_DH_KEY] = dev_handle, |
||
| 338 | [UPH_PFC_KEY] = pfc, |
||
| 339 | [UPH_PDI_KEY] = pdi, |
||
| 2889 | jermar | 340 | }; |
| 341 | |||
| 2890 | jermar | 342 | l = hash_table_find(&up_hash, pkey); |
| 2889 | jermar | 343 | if (l) { |
| 2890 | jermar | 344 | fidx = hash_table_get_instance(l, fat_idx_t, uph_link); |
| 2889 | jermar | 345 | } else { |
| 346 | fidx = (fat_idx_t *) malloc(sizeof(fat_idx_t)); |
||
| 347 | if (!fidx) { |
||
| 348 | return NULL; |
||
| 349 | } |
||
| 350 | if (!fat_idx_alloc(dev_handle, &fidx->index)) { |
||
| 351 | free(fidx); |
||
| 352 | return NULL; |
||
| 353 | } |
||
| 2890 | jermar | 354 | |
| 355 | unsigned long ikey[] = { |
||
| 356 | [UIH_DH_KEY] = dev_handle, |
||
| 357 | [UIH_INDEX_KEY] = fidx->index, |
||
| 358 | }; |
||
| 359 | |||
| 360 | link_initialize(&fidx->uph_link); |
||
| 361 | link_initialize(&fidx->uih_link); |
||
| 2889 | jermar | 362 | fidx->dev_handle = dev_handle; |
| 363 | fidx->pfc = pfc; |
||
| 364 | fidx->pdi = pdi; |
||
| 365 | fidx->nodep = NULL; |
||
| 2890 | jermar | 366 | |
| 367 | hash_table_insert(&up_hash, pkey, &fidx->uph_link); |
||
| 368 | hash_table_insert(&ui_hash, ikey, &fidx->uih_link); |
||
| 2889 | jermar | 369 | } |
| 370 | |||
| 371 | return fidx; |
||
| 2876 | jermar | 372 | } |
| 2889 | jermar | 373 | |
| 2890 | jermar | 374 | fat_idx_t * |
| 375 | fat_idx_get_by_index(dev_handle_t dev_handle, fs_index_t index) |
||
| 376 | { |
||
| 377 | fat_idx_t *fidx = NULL; |
||
| 378 | link_t *l; |
||
| 379 | unsigned long ikey[] = { |
||
| 380 | [UIH_DH_KEY] = dev_handle, |
||
| 381 | [UIH_INDEX_KEY] = index, |
||
| 382 | }; |
||
| 383 | |||
| 384 | l = hash_table_find(&ui_hash, ikey); |
||
| 385 | if (l) { |
||
| 386 | fidx = hash_table_get_instance(l, fat_idx_t, uih_link); |
||
| 387 | } |
||
| 388 | |||
| 389 | return fidx; |
||
| 390 | } |
||
| 391 |