[1] | 1 | /* |
---|
| 2 | * metafs.h - Meta File-System manages parent-child accesses |
---|
| 3 | * |
---|
| 4 | * Authors Ghassan Almaless (2008,2009,2010,2011,2012) |
---|
| 5 | * Alain Greiner (2016) |
---|
| 6 | * |
---|
| 7 | * Copyright (c) UPMC Sorbonne Universites |
---|
| 8 | * |
---|
| 9 | * This file is part of ALMOS-MKH. |
---|
| 10 | * |
---|
| 11 | * ALMOS-MKH is free software; you can redistribute it and/or modify it |
---|
| 12 | * under the terms of the GNU General Public License as published by |
---|
| 13 | * the Free Software Foundation; version 2.0 of the License. |
---|
| 14 | * |
---|
| 15 | * ALMOS-MKH is distributed in the hope that it will be useful, but |
---|
| 16 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
| 18 | * General Public License for more details. |
---|
| 19 | * |
---|
| 20 | * You should have received a copy of the GNU General Public License |
---|
| 21 | * along with ALMOS-MKH; if not, write to the Free Software Foundation, |
---|
| 22 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
| 23 | */ |
---|
| 24 | |
---|
| 25 | #ifndef _METAFS_H_ |
---|
| 26 | #define _METAFS_H_ |
---|
| 27 | |
---|
| 28 | #include <almos_config.h> |
---|
| 29 | #include <hal_types.h> |
---|
| 30 | #include <list.h> |
---|
| 31 | #include <string.h> |
---|
| 32 | |
---|
| 33 | #define METAFS_HASH_SIZE 10 |
---|
| 34 | |
---|
| 35 | /****************************************************************************************** |
---|
| 36 | * This structure define a node in the metafs tree. |
---|
| 37 | *****************************************************************************************/ |
---|
| 38 | |
---|
| 39 | typedef struct metafs_s |
---|
| 40 | { |
---|
| 41 | char * name; /*! name represented by this node */ |
---|
| 42 | uint32_t children_nr; /*! number of chidren for this node */ |
---|
| 43 | list_entry_t list; /*! child : member of a children list */ |
---|
| 44 | list_entry_t hash_tbl[METAFS_HASH_SIZE]; /*! parent : array of roots of children */ |
---|
| 45 | /*! lists, sorted by key value */ |
---|
| 46 | } |
---|
| 47 | metafs_t; |
---|
| 48 | |
---|
| 49 | /****************************************************************************************** |
---|
| 50 | * This function compute the hash key for a given name. |
---|
| 51 | ****************************************************************************************** |
---|
| 52 | * @ name : string to be analysed. |
---|
| 53 | * return the key value, from 0 to (METAFS_HASH_SIZE - 1) |
---|
| 54 | *****************************************************************************************/ |
---|
| 55 | static inline uint32_t metafs_hash_index( char * dev_name ) |
---|
| 56 | { |
---|
| 57 | register char * str = dev_name; |
---|
| 58 | register uint32_t index = 0; |
---|
| 59 | |
---|
| 60 | while(*str) |
---|
| 61 | index = index + (*(str++) ^ index); |
---|
| 62 | |
---|
| 63 | return index % METAFS_HASH_SIZE; |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | /****************************************************************************************** |
---|
| 67 | * This function initializes a node with zero children. |
---|
| 68 | ****************************************************************************************** |
---|
| 69 | * @ node : pointer on node to be initialized. |
---|
| 70 | * @ name : name for this node. |
---|
| 71 | *****************************************************************************************/ |
---|
| 72 | static inline void metafs_init( metafs_t * node, |
---|
| 73 | char * name ) |
---|
| 74 | { |
---|
| 75 | register uint32_t i; |
---|
| 76 | |
---|
| 77 | node->name = name; |
---|
| 78 | node->children_nr = 0; |
---|
| 79 | |
---|
| 80 | for(i=0; i < METAFS_HASH_SIZE; i++) |
---|
| 81 | list_root_init( &node->hash_tbl[i] ); |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | /****************************************************************************************** |
---|
| 85 | * This function register a children metafs node in a parent metafs node |
---|
| 86 | ****************************************************************************************** |
---|
| 87 | * @ parent : pointer on parent node. |
---|
| 88 | * @ child : pointer on child node. |
---|
| 89 | *****************************************************************************************/ |
---|
| 90 | static inline void metafs_register( metafs_t * parent, |
---|
| 91 | metafs_t * child ) |
---|
| 92 | { |
---|
| 93 | register uint32_t index; |
---|
| 94 | |
---|
| 95 | parent->children_nr ++; |
---|
| 96 | index = metafs_hash_index( child->name ); |
---|
| 97 | list_add_last( &parent->hash_tbl[index], &child->list ); |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | /****************************************************************************************** |
---|
| 101 | * This function search a child node by its name in a parent node. |
---|
| 102 | ****************************************************************************************** |
---|
| 103 | * @ parent : pointer on parent node. |
---|
| 104 | * @ child : pointer on child node. |
---|
| 105 | * @ return pointer on child node if found / return NULL if not found. |
---|
| 106 | *****************************************************************************************/ |
---|
| 107 | static inline metafs_t * metafs_lookup( metafs_t * parent, |
---|
| 108 | char * name ) |
---|
| 109 | { |
---|
| 110 | register list_entry_t * iter; |
---|
| 111 | register metafs_t * current; |
---|
| 112 | register uint32_t index; |
---|
| 113 | |
---|
| 114 | index = metafs_hash_index(name); |
---|
| 115 | |
---|
| 116 | LIST_FOREACH( &parent->hash_tbl[index] , iter ) |
---|
| 117 | { |
---|
| 118 | current = LIST_ELEMENT ( iter , metafs_t , list ); |
---|
| 119 | if( strcmp( current->name , name ) != 0 ) return current; |
---|
| 120 | } |
---|
| 121 | return NULL; |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | /****************************************************************************************** |
---|
| 125 | * This function remove a child from the children list of a parent node. |
---|
| 126 | ****************************************************************************************** |
---|
| 127 | * @ parent : pointer on parent node. |
---|
| 128 | * @ child : pointer on child node. |
---|
| 129 | *****************************************************************************************/ |
---|
| 130 | static inline void metafs_unregister( metafs_t * parent, |
---|
| 131 | metafs_t * child ) |
---|
| 132 | { |
---|
| 133 | list_unlink( &child->list ); |
---|
| 134 | parent->children_nr --; |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | |
---|
| 138 | |
---|
| 139 | |
---|
| 140 | |
---|
| 141 | #define metafs_get_name(_node) ((_node)->name) |
---|
| 142 | |
---|
| 143 | #define metafs_set_name(_node, _ptr) do{(_node)->name = (_ptr);}while(0) |
---|
| 144 | |
---|
| 145 | #define metafs_get_children_nr(_parent) ((_parent)->children_nr) |
---|
| 146 | |
---|
| 147 | #define metafs_hasChild(_parent) ((_parent)->children_nr != 0) |
---|
| 148 | |
---|
| 149 | #define metafs_container(_metafs, _type, _member) \ |
---|
| 150 | list_container(_metafs, _type, _member) |
---|
| 151 | |
---|
| 152 | |
---|
| 153 | #if CONFIG_METAFS_DEBUG |
---|
| 154 | #include <kdmsg.h> |
---|
| 155 | |
---|
| 156 | static inline void metafs_print( metafs_t * node ) |
---|
| 157 | { |
---|
| 158 | uint32_t i; |
---|
| 159 | list_entry_t * iter; |
---|
| 160 | |
---|
| 161 | printk("metafs_print [%s]\n", node->name); |
---|
| 162 | |
---|
| 163 | for(i=0; i<METAFS_HASH_SIZE; i++) |
---|
| 164 | { |
---|
| 165 | printk(DEBUG, "Entry %d: ", i); |
---|
| 166 | list_foreach(&node->hash_tbl[i], iter) |
---|
| 167 | { |
---|
| 168 | printk(DEBUG, "%s, ", list_element(iter, metafs_t, list)->name); |
---|
| 169 | } |
---|
| 170 | printk(DEBUG, "\n"); |
---|
| 171 | } |
---|
| 172 | } |
---|
| 173 | #else |
---|
| 174 | static inline void metafs_print( metafs_t * node ) |
---|
| 175 | { |
---|
| 176 | } |
---|
| 177 | #endif /* CONFIG_METAFS_DEBUG */ |
---|
| 178 | |
---|
| 179 | /* deprecated |
---|
| 180 | |
---|
| 181 | //////////////////////////// |
---|
| 182 | typedef struct metafs_iter_s |
---|
| 183 | { |
---|
| 184 | uint32_t hash_index; |
---|
| 185 | metafs_t * current; |
---|
| 186 | } |
---|
| 187 | metafs_iter_t; |
---|
| 188 | |
---|
| 189 | |
---|
| 190 | ////////////////////////////////////////////////////////// |
---|
| 191 | static inline void metafs_iter_init( metafs_t * node, |
---|
| 192 | metafs_iter_t * iter ) |
---|
| 193 | { |
---|
| 194 | register uint32_t i; |
---|
| 195 | |
---|
| 196 | for(i=0; i < METAFS_HASH_SIZE; i++) |
---|
| 197 | { |
---|
| 198 | if( !(list_is_empty( &node->hash_tbl[i] ) ) ) |
---|
| 199 | { |
---|
| 200 | iter->hash_index = i; |
---|
| 201 | iter->current = LIST_FIRST( &node->hash_tbl[i] , metafs_t , list ); |
---|
| 202 | return; |
---|
| 203 | } |
---|
| 204 | } |
---|
| 205 | |
---|
| 206 | iter->current = NULL; |
---|
| 207 | } |
---|
| 208 | |
---|
| 209 | ///////////////////////////////////////////////////////////////////// |
---|
| 210 | static inline metafs_t * metafs_lookup_next( metafs_t * parent, |
---|
| 211 | metafs_iter_t * iter ) |
---|
| 212 | { |
---|
| 213 | register list_entry_t * next; |
---|
| 214 | register metafs_t * child; |
---|
| 215 | register uint32_t i; |
---|
| 216 | |
---|
| 217 | if(iter->current == NULL) |
---|
| 218 | return NULL; |
---|
| 219 | |
---|
| 220 | child = iter->current; |
---|
| 221 | i = iter->hash_index; |
---|
| 222 | |
---|
| 223 | if((next = list_next(&parent->hash_tbl[i], &iter->current->list)) == NULL) |
---|
| 224 | { |
---|
| 225 | for(i = i + 1; i < METAFS_HASH_SIZE; i++) |
---|
| 226 | { |
---|
| 227 | if(!(list_empty(&parent->hash_tbl[i]))) |
---|
| 228 | { |
---|
| 229 | iter->hash_index = i; |
---|
| 230 | iter->current = list_first(&parent->hash_tbl[i], metafs_t, list); |
---|
| 231 | return child; |
---|
| 232 | } |
---|
| 233 | } |
---|
| 234 | iter->current = NULL; |
---|
| 235 | return child; |
---|
| 236 | } |
---|
| 237 | |
---|
| 238 | iter->current = list_element(next, metafs_t, list); |
---|
| 239 | return child; |
---|
| 240 | } |
---|
| 241 | |
---|
| 242 | */ |
---|
| 243 | |
---|
| 244 | |
---|
| 245 | #endif /* _METAFS_H_ */ |
---|