Subversion Repositories HelenOS

Rev

Rev 2944 | Rev 2951 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

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