Rev 3675 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
| Rev 3675 | Rev 4377 | ||
|---|---|---|---|
| Line 52... | Line 52... | ||
| 52 | #include <libfs.h> |
52 | #include <libfs.h> |
| 53 | 53 | ||
| 54 | #define min(a, b) ((a) < (b) ? (a) : (b)) |
54 | #define min(a, b) ((a) < (b) ? (a) : (b)) |
| 55 | #define max(a, b) ((a) > (b) ? (a) : (b)) |
55 | #define max(a, b) ((a) > (b) ? (a) : (b)) |
| 56 | 56 | ||
| 57 | #define DENTRIES_BUCKETS 256 |
57 | #define NODES_BUCKETS 256 |
| 58 | 58 | ||
| - | 59 | /** All root nodes have index 0. */ |
|
| 59 | #define NAMES_BUCKETS 4 |
60 | #define TMPFS_SOME_ROOT 0 |
| 60 | - | ||
| 61 | /* |
- | |
| 62 | * For now, we don't distinguish between different dev_handles/instances. All |
61 | /** Global counter for assigning node indices. Shared by all instances. */ |
| 63 | * requests resolve to the only instance, rooted in the following variable. |
- | |
| 64 | */ |
- | |
| 65 | static tmpfs_dentry_t *root; |
62 | fs_index_t tmpfs_next_index = 1; |
| 66 | - | ||
| 67 | #define TMPFS_DEV 0 /**< Dummy device handle for TMPFS */ |
- | |
| 68 | 63 | ||
| 69 | /* |
64 | /* |
| 70 | * Implementation of the libfs interface. |
65 | * Implementation of the libfs interface. |
| 71 | */ |
66 | */ |
| 72 | 67 | ||
| 73 | /* Forward declarations of static functions. */ |
68 | /* Forward declarations of static functions. */ |
| 74 | static void *tmpfs_match(void *, const char *); |
69 | static fs_node_t *tmpfs_match(fs_node_t *, const char *); |
| 75 | static void *tmpfs_node_get(dev_handle_t, fs_index_t); |
70 | static fs_node_t *tmpfs_node_get(dev_handle_t, fs_index_t); |
| 76 | static void tmpfs_node_put(void *); |
71 | static void tmpfs_node_put(fs_node_t *); |
| 77 | static void *tmpfs_create_node(dev_handle_t, int); |
72 | static fs_node_t *tmpfs_create_node(dev_handle_t, int); |
| 78 | static int tmpfs_link_node(void *, void *, const char *); |
73 | static int tmpfs_link_node(fs_node_t *, fs_node_t *, const char *); |
| 79 | static int tmpfs_unlink_node(void *, void *); |
74 | static int tmpfs_unlink_node(fs_node_t *, fs_node_t *, const char *); |
| 80 | static int tmpfs_destroy_node(void *); |
75 | static int tmpfs_destroy_node(fs_node_t *); |
| 81 | 76 | ||
| 82 | /* Implementation of helper functions. */ |
77 | /* Implementation of helper functions. */ |
| 83 | static fs_index_t tmpfs_index_get(void *nodep) |
78 | static fs_index_t tmpfs_index_get(fs_node_t *fn) |
| 84 | { |
79 | { |
| 85 | return ((tmpfs_dentry_t *) nodep)->index; |
80 | return TMPFS_NODE(fn)->index; |
| 86 | } |
81 | } |
| 87 | 82 | ||
| 88 | static size_t tmpfs_size_get(void *nodep) |
83 | static size_t tmpfs_size_get(fs_node_t *fn) |
| 89 | { |
84 | { |
| 90 | return ((tmpfs_dentry_t *) nodep)->size; |
85 | return TMPFS_NODE(fn)->size; |
| 91 | } |
86 | } |
| 92 | 87 | ||
| 93 | static unsigned tmpfs_lnkcnt_get(void *nodep) |
88 | static unsigned tmpfs_lnkcnt_get(fs_node_t *fn) |
| 94 | { |
89 | { |
| 95 | return ((tmpfs_dentry_t *) nodep)->lnkcnt; |
90 | return TMPFS_NODE(fn)->lnkcnt; |
| 96 | } |
91 | } |
| 97 | 92 | ||
| 98 | static bool tmpfs_has_children(void *nodep) |
93 | static bool tmpfs_has_children(fs_node_t *fn) |
| 99 | { |
94 | { |
| 100 | return ((tmpfs_dentry_t *) nodep)->child != NULL; |
95 | return !list_empty(&TMPFS_NODE(fn)->cs_head); |
| 101 | } |
96 | } |
| 102 | 97 | ||
| 103 | static void *tmpfs_root_get(dev_handle_t dev_handle) |
98 | static fs_node_t *tmpfs_root_get(dev_handle_t dev_handle) |
| 104 | { |
99 | { |
| 105 | return root; |
100 | return tmpfs_node_get(dev_handle, TMPFS_SOME_ROOT); |
| 106 | } |
101 | } |
| 107 | 102 | ||
| 108 | static char tmpfs_plb_get_char(unsigned pos) |
103 | static char tmpfs_plb_get_char(unsigned pos) |
| 109 | { |
104 | { |
| 110 | return tmpfs_reg.plb_ro[pos % PLB_SIZE]; |
105 | return tmpfs_reg.plb_ro[pos % PLB_SIZE]; |
| 111 | } |
106 | } |
| 112 | 107 | ||
| 113 | static bool tmpfs_is_directory(void *nodep) |
108 | static bool tmpfs_is_directory(fs_node_t *fn) |
| 114 | { |
109 | { |
| 115 | return ((tmpfs_dentry_t *) nodep)->type == TMPFS_DIRECTORY; |
110 | return TMPFS_NODE(fn)->type == TMPFS_DIRECTORY; |
| 116 | } |
111 | } |
| 117 | 112 | ||
| 118 | static bool tmpfs_is_file(void *nodep) |
113 | static bool tmpfs_is_file(fs_node_t *fn) |
| 119 | { |
114 | { |
| 120 | return ((tmpfs_dentry_t *) nodep)->type == TMPFS_FILE; |
115 | return TMPFS_NODE(fn)->type == TMPFS_FILE; |
| 121 | } |
116 | } |
| 122 | 117 | ||
| 123 | /** libfs operations */ |
118 | /** libfs operations */ |
| 124 | libfs_ops_t tmpfs_libfs_ops = { |
119 | libfs_ops_t tmpfs_libfs_ops = { |
| 125 | .match = tmpfs_match, |
120 | .match = tmpfs_match, |
| Line 137... | Line 132... | ||
| 137 | .plb_get_char = tmpfs_plb_get_char, |
132 | .plb_get_char = tmpfs_plb_get_char, |
| 138 | .is_directory = tmpfs_is_directory, |
133 | .is_directory = tmpfs_is_directory, |
| 139 | .is_file = tmpfs_is_file |
134 | .is_file = tmpfs_is_file |
| 140 | }; |
135 | }; |
| 141 | 136 | ||
| 142 | /** Hash table of all directory entries. */ |
137 | /** Hash table of all TMPFS nodes. */ |
| 143 | hash_table_t dentries; |
138 | hash_table_t nodes; |
| 144 | - | ||
| 145 | /* Implementation of hash table interface for the dentries hash table. */ |
- | |
| 146 | static hash_index_t dentries_hash(unsigned long *key) |
- | |
| 147 | { |
- | |
| 148 | return *key % DENTRIES_BUCKETS; |
- | |
| 149 | } |
- | |
| 150 | - | ||
| 151 | static int dentries_compare(unsigned long *key, hash_count_t keys, |
- | |
| 152 | link_t *item) |
- | |
| 153 | { |
- | |
| 154 | tmpfs_dentry_t *dentry = hash_table_get_instance(item, tmpfs_dentry_t, |
- | |
| 155 | dh_link); |
- | |
| 156 | return dentry->index == *key; |
- | |
| 157 | } |
- | |
| 158 | - | ||
| 159 | static void dentries_remove_callback(link_t *item) |
- | |
| 160 | { |
- | |
| 161 | } |
- | |
| 162 | - | ||
| 163 | /** TMPFS dentries hash table operations. */ |
- | |
| 164 | hash_table_operations_t dentries_ops = { |
- | |
| 165 | .hash = dentries_hash, |
- | |
| 166 | .compare = dentries_compare, |
- | |
| 167 | .remove_callback = dentries_remove_callback |
- | |
| 168 | }; |
- | |
| 169 | - | ||
| 170 | fs_index_t tmpfs_next_index = 1; |
- | |
| 171 | 139 | ||
| 172 | typedef struct { |
- | |
| 173 | char *name; |
- | |
| 174 | tmpfs_dentry_t *parent; |
140 | #define NODES_KEY_INDEX 0 |
| 175 | link_t link; |
141 | #define NODES_KEY_DEV 1 |
| 176 | } tmpfs_name_t; |
- | |
| 177 | 142 | ||
| 178 | /* Implementation of hash table interface for the names hash table. */ |
143 | /* Implementation of hash table interface for the nodes hash table. */ |
| 179 | static hash_index_t names_hash(unsigned long *key) |
144 | static hash_index_t nodes_hash(unsigned long key[]) |
| 180 | { |
145 | { |
| 181 | tmpfs_dentry_t *dentry = (tmpfs_dentry_t *) *key; |
- | |
| 182 | return dentry->index % NAMES_BUCKETS; |
146 | return key[NODES_KEY_INDEX] % NODES_BUCKETS; |
| 183 | } |
147 | } |
| 184 | 148 | ||
| 185 | static int names_compare(unsigned long *key, hash_count_t keys, link_t *item) |
149 | static int nodes_compare(unsigned long key[], hash_count_t keys, link_t *item) |
| 186 | { |
150 | { |
| 187 | tmpfs_dentry_t *dentry = (tmpfs_dentry_t *) *key; |
- | |
| 188 | tmpfs_name_t *namep = hash_table_get_instance(item, tmpfs_name_t, |
151 | tmpfs_node_t *nodep = hash_table_get_instance(item, tmpfs_node_t, |
| 189 | link); |
152 | nh_link); |
| 190 | return dentry == namep->parent; |
153 | return (nodep->index == key[NODES_KEY_INDEX] && |
| - | 154 | nodep->dev_handle == key[NODES_KEY_DEV]); |
|
| 191 | } |
155 | } |
| 192 | 156 | ||
| 193 | static void names_remove_callback(link_t *item) |
157 | static void nodes_remove_callback(link_t *item) |
| 194 | { |
158 | { |
| 195 | tmpfs_name_t *namep = hash_table_get_instance(item, tmpfs_name_t, |
- | |
| 196 | link); |
- | |
| 197 | free(namep->name); |
- | |
| 198 | free(namep); |
- | |
| 199 | } |
159 | } |
| 200 | 160 | ||
| 201 | /** TMPFS node names hash table operations. */ |
161 | /** TMPFS nodes hash table operations. */ |
| 202 | static hash_table_operations_t names_ops = { |
162 | hash_table_operations_t nodes_ops = { |
| 203 | .hash = names_hash, |
163 | .hash = nodes_hash, |
| 204 | .compare = names_compare, |
164 | .compare = nodes_compare, |
| 205 | .remove_callback = names_remove_callback |
165 | .remove_callback = nodes_remove_callback |
| 206 | }; |
166 | }; |
| 207 | 167 | ||
| 208 | static void tmpfs_name_initialize(tmpfs_name_t *namep) |
168 | static void tmpfs_node_initialize(tmpfs_node_t *nodep) |
| 209 | { |
169 | { |
| 210 | namep->name = NULL; |
170 | nodep->bp = NULL; |
| - | 171 | nodep->index = 0; |
|
| - | 172 | nodep->dev_handle = 0; |
|
| - | 173 | nodep->type = TMPFS_NONE; |
|
| - | 174 | nodep->lnkcnt = 0; |
|
| - | 175 | nodep->size = 0; |
|
| 211 | namep->parent = NULL; |
176 | nodep->data = NULL; |
| 212 | link_initialize(&namep->link); |
177 | link_initialize(&nodep->nh_link); |
| - | 178 | list_initialize(&nodep->cs_head); |
|
| 213 | } |
179 | } |
| 214 | 180 | ||
| 215 | static bool tmpfs_dentry_initialize(tmpfs_dentry_t *dentry) |
181 | static void tmpfs_dentry_initialize(tmpfs_dentry_t *dentryp) |
| 216 | { |
182 | { |
| 217 | dentry->index = 0; |
- | |
| 218 | dentry->sibling = NULL; |
183 | link_initialize(&dentryp->link); |
| 219 | dentry->child = NULL; |
184 | dentryp->name = NULL; |
| 220 | dentry->type = TMPFS_NONE; |
- | |
| 221 | dentry->lnkcnt = 0; |
- | |
| 222 | dentry->size = 0; |
- | |
| 223 | dentry->data = NULL; |
185 | dentryp->node = NULL; |
| 224 | link_initialize(&dentry->dh_link); |
- | |
| 225 | return (bool)hash_table_create(&dentry->names, NAMES_BUCKETS, 1, |
- | |
| 226 | &names_ops); |
- | |
| 227 | } |
186 | } |
| 228 | 187 | ||
| 229 | static bool tmpfs_init(void) |
188 | bool tmpfs_init(void) |
| 230 | { |
189 | { |
| 231 | if (!hash_table_create(&dentries, DENTRIES_BUCKETS, 1, &dentries_ops)) |
190 | if (!hash_table_create(&nodes, NODES_BUCKETS, 2, &nodes_ops)) |
| 232 | return false; |
- | |
| 233 | root = (tmpfs_dentry_t *) tmpfs_create_node(TMPFS_DEV, L_DIRECTORY); |
- | |
| 234 | if (!root) { |
- | |
| 235 | hash_table_destroy(&dentries); |
- | |
| 236 | return false; |
191 | return false; |
| 237 | } |
192 | |
| 238 | root->lnkcnt = 0; /* FS root is not linked */ |
- | |
| 239 | return true; |
193 | return true; |
| 240 | } |
194 | } |
| 241 | 195 | ||
| 242 | /** Compare one component of path to a directory entry. |
- | |
| 243 | * |
- | |
| 244 | * @param parentp Pointer to node from which we descended. |
- | |
| 245 | * @param childp Pointer to node to compare the path component with. |
- | |
| 246 | * @param component Array of characters holding component name. |
- | |
| 247 | * |
- | |
| 248 | * @return True on match, false otherwise. |
- | |
| 249 | */ |
- | |
| 250 | static bool |
- | |
| 251 | tmpfs_match_one(tmpfs_dentry_t *parentp, tmpfs_dentry_t *childp, |
196 | static bool tmpfs_instance_init(dev_handle_t dev_handle) |
| 252 | const char *component) |
- | |
| 253 | { |
197 | { |
| 254 | unsigned long key = (unsigned long) parentp; |
198 | fs_node_t *rfn; |
| - | 199 | ||
| 255 | link_t *hlp = hash_table_find(&childp->names, &key); |
200 | rfn = tmpfs_create_node(dev_handle, L_DIRECTORY); |
| - | 201 | if (!rfn) |
|
| 256 | assert(hlp); |
202 | return false; |
| 257 | tmpfs_name_t *namep = hash_table_get_instance(hlp, tmpfs_name_t, link); |
203 | TMPFS_NODE(rfn)->lnkcnt = 0; /* FS root is not linked */ |
| 258 | return !strcmp(namep->name, component); |
204 | return true; |
| 259 | } |
205 | } |
| 260 | 206 | ||
| 261 | void *tmpfs_match(void *prnt, const char *component) |
207 | fs_node_t *tmpfs_match(fs_node_t *pfn, const char *component) |
| 262 | { |
208 | { |
| 263 | tmpfs_dentry_t *parentp = (tmpfs_dentry_t *) prnt; |
209 | tmpfs_node_t *parentp = TMPFS_NODE(pfn); |
| 264 | tmpfs_dentry_t *childp = parentp->child; |
210 | link_t *lnk; |
| 265 | 211 | ||
| - | 212 | for (lnk = parentp->cs_head.next; lnk != &parentp->cs_head; |
|
| - | 213 | lnk = lnk->next) { |
|
| - | 214 | tmpfs_dentry_t *dentryp = list_get_instance(lnk, tmpfs_dentry_t, |
|
| - | 215 | link); |
|
| 266 | while (childp && !tmpfs_match_one(parentp, childp, component)) |
216 | if (!str_cmp(dentryp->name, component)) |
| 267 | childp = childp->sibling; |
217 | return FS_NODE(dentryp->node); |
| - | 218 | } |
|
| 268 | 219 | ||
| 269 | return (void *) childp; |
220 | return NULL; |
| 270 | } |
221 | } |
| 271 | 222 | ||
| 272 | void * |
- | |
| 273 | tmpfs_node_get(dev_handle_t dev_handle, fs_index_t index) |
223 | fs_node_t *tmpfs_node_get(dev_handle_t dev_handle, fs_index_t index) |
| 274 | { |
224 | { |
| 275 | unsigned long key = index; |
225 | unsigned long key[] = { |
| - | 226 | [NODES_KEY_INDEX] = index, |
|
| - | 227 | [NODES_KEY_DEV] = dev_handle |
|
| - | 228 | }; |
|
| 276 | link_t *lnk = hash_table_find(&dentries, &key); |
229 | link_t *lnk = hash_table_find(&nodes, key); |
| 277 | if (!lnk) |
230 | if (!lnk) |
| 278 | return NULL; |
231 | return NULL; |
| 279 | return hash_table_get_instance(lnk, tmpfs_dentry_t, dh_link); |
232 | return FS_NODE(hash_table_get_instance(lnk, tmpfs_node_t, nh_link)); |
| 280 | } |
233 | } |
| 281 | 234 | ||
| 282 | void tmpfs_node_put(void *node) |
235 | void tmpfs_node_put(fs_node_t *fn) |
| 283 | { |
236 | { |
| 284 | /* nothing to do */ |
237 | /* nothing to do */ |
| 285 | } |
238 | } |
| 286 | 239 | ||
| 287 | void *tmpfs_create_node(dev_handle_t dev_handle, int lflag) |
240 | fs_node_t *tmpfs_create_node(dev_handle_t dev_handle, int lflag) |
| 288 | { |
241 | { |
| 289 | assert((lflag & L_FILE) ^ (lflag & L_DIRECTORY)); |
242 | assert((lflag & L_FILE) ^ (lflag & L_DIRECTORY)); |
| 290 | 243 | ||
| 291 | tmpfs_dentry_t *node = malloc(sizeof(tmpfs_dentry_t)); |
244 | tmpfs_node_t *nodep = malloc(sizeof(tmpfs_node_t)); |
| 292 | if (!node) |
245 | if (!nodep) |
| 293 | return NULL; |
246 | return NULL; |
| 294 | - | ||
| 295 | if (!tmpfs_dentry_initialize(node)) { |
247 | tmpfs_node_initialize(nodep); |
| - | 248 | nodep->bp = malloc(sizeof(fs_node_t)); |
|
| - | 249 | if (!nodep->bp) { |
|
| 296 | free(node); |
250 | free(nodep); |
| 297 | return NULL; |
251 | return NULL; |
| 298 | } |
252 | } |
| - | 253 | nodep->bp->data = nodep; /* link the FS and TMPFS nodes */ |
|
| - | 254 | if (!tmpfs_root_get(dev_handle)) |
|
| - | 255 | nodep->index = TMPFS_SOME_ROOT; |
|
| - | 256 | else |
|
| 299 | node->index = tmpfs_next_index++; |
257 | nodep->index = tmpfs_next_index++; |
| - | 258 | nodep->dev_handle = dev_handle; |
|
| 300 | if (lflag & L_DIRECTORY) |
259 | if (lflag & L_DIRECTORY) |
| 301 | node->type = TMPFS_DIRECTORY; |
260 | nodep->type = TMPFS_DIRECTORY; |
| 302 | else |
261 | else |
| 303 | node->type = TMPFS_FILE; |
262 | nodep->type = TMPFS_FILE; |
| 304 | 263 | ||
| 305 | /* Insert the new node into the dentry hash table. */ |
264 | /* Insert the new node into the nodes hash table. */ |
| 306 | unsigned long key = node->index; |
265 | unsigned long key[] = { |
| - | 266 | [NODES_KEY_INDEX] = nodep->index, |
|
| - | 267 | [NODES_KEY_DEV] = nodep->dev_handle |
|
| - | 268 | }; |
|
| 307 | hash_table_insert(&dentries, &key, &node->dh_link); |
269 | hash_table_insert(&nodes, key, &nodep->nh_link); |
| 308 | return (void *) node; |
270 | return FS_NODE(nodep); |
| 309 | } |
271 | } |
| 310 | 272 | ||
| 311 | int tmpfs_link_node(void *prnt, void *chld, const char *nm) |
273 | int tmpfs_link_node(fs_node_t *pfn, fs_node_t *cfn, const char *nm) |
| 312 | { |
274 | { |
| 313 | tmpfs_dentry_t *parentp = (tmpfs_dentry_t *) prnt; |
275 | tmpfs_node_t *parentp = TMPFS_NODE(pfn); |
| 314 | tmpfs_dentry_t *childp = (tmpfs_dentry_t *) chld; |
276 | tmpfs_node_t *childp = TMPFS_NODE(cfn); |
| - | 277 | tmpfs_dentry_t *dentryp; |
|
| - | 278 | link_t *lnk; |
|
| 315 | 279 | ||
| 316 | assert(parentp->type == TMPFS_DIRECTORY); |
280 | assert(parentp->type == TMPFS_DIRECTORY); |
| 317 | 281 | ||
| 318 | tmpfs_name_t *namep = malloc(sizeof(tmpfs_name_t)); |
282 | /* Check for duplicit entries. */ |
| 319 | if (!namep) |
- | |
| 320 | return ENOMEM; |
- | |
| 321 | tmpfs_name_initialize(namep); |
283 | for (lnk = parentp->cs_head.next; lnk != &parentp->cs_head; |
| 322 | size_t len = strlen(nm); |
284 | lnk = lnk->next) { |
| 323 | namep->name = malloc(len + 1); |
285 | dentryp = list_get_instance(lnk, tmpfs_dentry_t, link); |
| 324 | if (!namep->name) { |
286 | if (!str_cmp(dentryp->name, nm)) |
| 325 | free(namep); |
- | |
| 326 | return ENOMEM; |
287 | return EEXIST; |
| 327 | } |
288 | } |
| 328 | strcpy(namep->name, nm); |
- | |
| 329 | namep->parent = parentp; |
- | |
| 330 | - | ||
| 331 | childp->lnkcnt++; |
- | |
| 332 | 289 | ||
| - | 290 | /* Allocate and initialize the dentry. */ |
|
| 333 | unsigned long key = (unsigned long) parentp; |
291 | dentryp = malloc(sizeof(tmpfs_dentry_t)); |
| - | 292 | if (!dentryp) |
|
| - | 293 | return ENOMEM; |
|
| 334 | hash_table_insert(&childp->names, &key, &namep->link); |
294 | tmpfs_dentry_initialize(dentryp); |
| 335 | 295 | ||
| 336 | /* Insert the new node into the namespace. */ |
296 | /* Populate and link the new dentry. */ |
| 337 | if (parentp->child) { |
297 | size_t size = str_size(nm); |
| 338 | tmpfs_dentry_t *tmp = parentp->child; |
298 | dentryp->name = malloc(size + 1); |
| 339 | while (tmp->sibling) |
- | |
| 340 | tmp = tmp->sibling; |
299 | if (!dentryp->name) { |
| 341 | tmp->sibling = childp; |
- | |
| 342 | } else { |
300 | free(dentryp); |
| 343 | parentp->child = childp; |
301 | return ENOMEM; |
| 344 | } |
302 | } |
| - | 303 | str_cpy(dentryp->name, size + 1, nm); |
|
| - | 304 | dentryp->node = childp; |
|
| - | 305 | childp->lnkcnt++; |
|
| - | 306 | list_append(&dentryp->link, &parentp->cs_head); |
|
| 345 | 307 | ||
| 346 | return EOK; |
308 | return EOK; |
| 347 | } |
309 | } |
| 348 | 310 | ||
| 349 | int tmpfs_unlink_node(void *prnt, void *chld) |
311 | int tmpfs_unlink_node(fs_node_t *pfn, fs_node_t *cfn, const char *nm) |
| 350 | { |
312 | { |
| 351 | tmpfs_dentry_t *parentp = (tmpfs_dentry_t *)prnt; |
313 | tmpfs_node_t *parentp = TMPFS_NODE(pfn); |
| - | 314 | tmpfs_node_t *childp = NULL; |
|
| 352 | tmpfs_dentry_t *childp = (tmpfs_dentry_t *)chld; |
315 | tmpfs_dentry_t *dentryp; |
| - | 316 | link_t *lnk; |
|
| 353 | 317 | ||
| 354 | if (!parentp) |
318 | if (!parentp) |
| 355 | return EBUSY; |
319 | return EBUSY; |
| 356 | - | ||
| 357 | if (childp->child) |
- | |
| 358 | return ENOTEMPTY; |
- | |
| 359 | 320 | ||
| 360 | if (parentp->child == childp) { |
321 | for (lnk = parentp->cs_head.next; lnk != &parentp->cs_head; |
| 361 | parentp->child = childp->sibling; |
322 | lnk = lnk->next) { |
| 362 | } else { |
- | |
| 363 | /* TODO: consider doubly linked list for organizing siblings. */ |
- | |
| 364 | tmpfs_dentry_t *tmp = parentp->child; |
323 | dentryp = list_get_instance(lnk, tmpfs_dentry_t, link); |
| 365 | while (tmp->sibling != childp) |
324 | if (!str_cmp(dentryp->name, nm)) { |
| 366 | tmp = tmp->sibling; |
325 | childp = dentryp->node; |
| 367 | tmp->sibling = childp->sibling; |
326 | assert(FS_NODE(childp) == cfn); |
| - | 327 | break; |
|
| - | 328 | } |
|
| 368 | } |
329 | } |
| 369 | childp->sibling = NULL; |
- | |
| 370 | 330 | ||
| - | 331 | if (!childp) |
|
| - | 332 | return ENOENT; |
|
| - | 333 | ||
| 371 | unsigned long key = (unsigned long) parentp; |
334 | if ((childp->lnkcnt == 1) && !list_empty(&childp->cs_head)) |
| 372 | hash_table_remove(&childp->names, &key, 1); |
335 | return ENOTEMPTY; |
| 373 | 336 | ||
| - | 337 | list_remove(&dentryp->link); |
|
| - | 338 | free(dentryp); |
|
| 374 | childp->lnkcnt--; |
339 | childp->lnkcnt--; |
| 375 | 340 | ||
| 376 | return EOK; |
341 | return EOK; |
| 377 | } |
342 | } |
| 378 | 343 | ||
| 379 | int tmpfs_destroy_node(void *nodep) |
344 | int tmpfs_destroy_node(fs_node_t *fn) |
| 380 | { |
345 | { |
| 381 | tmpfs_dentry_t *dentry = (tmpfs_dentry_t *) nodep; |
346 | tmpfs_node_t *nodep = TMPFS_NODE(fn); |
| 382 | 347 | ||
| 383 | assert(!dentry->lnkcnt); |
348 | assert(!nodep->lnkcnt); |
| 384 | assert(!dentry->child); |
349 | assert(list_empty(&nodep->cs_head)); |
| 385 | assert(!dentry->sibling); |
- | |
| 386 | 350 | ||
| 387 | unsigned long key = dentry->index; |
351 | unsigned long key[] = { |
| 388 | hash_table_remove(&dentries, &key, 1); |
352 | [NODES_KEY_INDEX] = nodep->index, |
| - | 353 | [NODES_KEY_DEV] = nodep->dev_handle |
|
| 389 | 354 | }; |
|
| 390 | hash_table_destroy(&dentry->names); |
355 | hash_table_remove(&nodes, key, 2); |
| 391 | 356 | ||
| 392 | if (dentry->type == TMPFS_FILE) |
357 | if (nodep->type == TMPFS_FILE) |
| 393 | free(dentry->data); |
358 | free(nodep->data); |
| - | 359 | free(nodep->bp); |
|
| 394 | free(dentry); |
360 | free(nodep); |
| 395 | return EOK; |
361 | return EOK; |
| 396 | } |
362 | } |
| 397 | 363 | ||
| 398 | void tmpfs_mounted(ipc_callid_t rid, ipc_call_t *request) |
364 | void tmpfs_mounted(ipc_callid_t rid, ipc_call_t *request) |
| 399 | { |
365 | { |
| 400 | dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request); |
366 | dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request); |
| 401 | 367 | ||
| - | 368 | /* accept the mount options */ |
|
| - | 369 | ipc_callid_t callid; |
|
| - | 370 | size_t size; |
|
| - | 371 | if (!ipc_data_write_receive(&callid, &size)) { |
|
| - | 372 | ipc_answer_0(callid, EINVAL); |
|
| - | 373 | ipc_answer_0(rid, EINVAL); |
|
| - | 374 | return; |
|
| - | 375 | } |
|
| - | 376 | char *opts = malloc(size + 1); |
|
| - | 377 | if (!opts) { |
|
| - | 378 | ipc_answer_0(callid, ENOMEM); |
|
| - | 379 | ipc_answer_0(rid, ENOMEM); |
|
| - | 380 | return; |
|
| - | 381 | } |
|
| - | 382 | ipcarg_t retval = ipc_data_write_finalize(callid, opts, size); |
|
| - | 383 | if (retval != EOK) { |
|
| - | 384 | ipc_answer_0(rid, retval); |
|
| - | 385 | free(opts); |
|
| - | 386 | return; |
|
| - | 387 | } |
|
| - | 388 | opts[size] = '\0'; |
|
| - | 389 | ||
| 402 | /* Initialize TMPFS. */ |
390 | /* Initialize TMPFS instance. */ |
| 403 | if (!root && !tmpfs_init()) { |
391 | if (!tmpfs_instance_init(dev_handle)) { |
| 404 | ipc_answer_0(rid, ENOMEM); |
392 | ipc_answer_0(rid, ENOMEM); |
| 405 | return; |
393 | return; |
| 406 | } |
394 | } |
| 407 | 395 | ||
| - | 396 | tmpfs_node_t *rootp = TMPFS_NODE(tmpfs_root_get(dev_handle)); |
|
| 408 | if (dev_handle >= 0) { |
397 | if (str_cmp(opts, "restore") == 0) { |
| 409 | if (tmpfs_restore(dev_handle)) |
398 | if (tmpfs_restore(dev_handle)) |
| 410 | ipc_answer_3(rid, EOK, root->index, root->size, |
399 | ipc_answer_3(rid, EOK, rootp->index, rootp->size, |
| 411 | root->lnkcnt); |
400 | rootp->lnkcnt); |
| 412 | else |
401 | else |
| 413 | ipc_answer_0(rid, ELIMIT); |
402 | ipc_answer_0(rid, ELIMIT); |
| 414 | } else { |
403 | } else { |
| 415 | ipc_answer_3(rid, EOK, root->index, root->size, root->lnkcnt); |
404 | ipc_answer_3(rid, EOK, rootp->index, rootp->size, |
| - | 405 | rootp->lnkcnt); |
|
| 416 | } |
406 | } |
| 417 | } |
407 | } |
| 418 | 408 | ||
| 419 | void tmpfs_mount(ipc_callid_t rid, ipc_call_t *request) |
409 | void tmpfs_mount(ipc_callid_t rid, ipc_call_t *request) |
| 420 | { |
410 | { |
| Line 436... | Line 426... | ||
| 436 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
426 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
| 437 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); |
427 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); |
| 438 | off_t pos = (off_t)IPC_GET_ARG3(*request); |
428 | off_t pos = (off_t)IPC_GET_ARG3(*request); |
| 439 | 429 | ||
| 440 | /* |
430 | /* |
| 441 | * Lookup the respective dentry. |
431 | * Lookup the respective TMPFS node. |
| 442 | */ |
432 | */ |
| 443 | link_t *hlp; |
433 | link_t *hlp; |
| 444 | unsigned long key = index; |
434 | unsigned long key[] = { |
| - | 435 | [NODES_KEY_INDEX] = index, |
|
| - | 436 | [NODES_KEY_DEV] = dev_handle, |
|
| - | 437 | }; |
|
| 445 | hlp = hash_table_find(&dentries, &key); |
438 | hlp = hash_table_find(&nodes, key); |
| 446 | if (!hlp) { |
439 | if (!hlp) { |
| 447 | ipc_answer_0(rid, ENOENT); |
440 | ipc_answer_0(rid, ENOENT); |
| 448 | return; |
441 | return; |
| 449 | } |
442 | } |
| 450 | tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t, |
443 | tmpfs_node_t *nodep = hash_table_get_instance(hlp, tmpfs_node_t, |
| 451 | dh_link); |
444 | nh_link); |
| 452 | 445 | ||
| 453 | /* |
446 | /* |
| 454 | * Receive the read request. |
447 | * Receive the read request. |
| 455 | */ |
448 | */ |
| 456 | ipc_callid_t callid; |
449 | ipc_callid_t callid; |
| 457 | size_t len; |
450 | size_t size; |
| 458 | if (!ipc_data_read_receive(&callid, &len)) { |
451 | if (!ipc_data_read_receive(&callid, &size)) { |
| 459 | ipc_answer_0(callid, EINVAL); |
452 | ipc_answer_0(callid, EINVAL); |
| 460 | ipc_answer_0(rid, EINVAL); |
453 | ipc_answer_0(rid, EINVAL); |
| 461 | return; |
454 | return; |
| 462 | } |
455 | } |
| 463 | 456 | ||
| 464 | size_t bytes; |
457 | size_t bytes; |
| 465 | if (dentry->type == TMPFS_FILE) { |
458 | if (nodep->type == TMPFS_FILE) { |
| 466 | bytes = max(0, min(dentry->size - pos, len)); |
459 | bytes = max(0, min(nodep->size - pos, size)); |
| 467 | (void) ipc_data_read_finalize(callid, dentry->data + pos, |
460 | (void) ipc_data_read_finalize(callid, nodep->data + pos, |
| 468 | bytes); |
461 | bytes); |
| 469 | } else { |
462 | } else { |
| - | 463 | tmpfs_dentry_t *dentryp; |
|
| - | 464 | link_t *lnk; |
|
| 470 | int i; |
465 | int i; |
| 471 | tmpfs_dentry_t *cur; |
- | |
| 472 | 466 | ||
| 473 | assert(dentry->type == TMPFS_DIRECTORY); |
467 | assert(nodep->type == TMPFS_DIRECTORY); |
| 474 | 468 | ||
| 475 | /* |
469 | /* |
| 476 | * Yes, we really use O(n) algorithm here. |
470 | * Yes, we really use O(n) algorithm here. |
| 477 | * If it bothers someone, it could be fixed by introducing a |
471 | * If it bothers someone, it could be fixed by introducing a |
| 478 | * hash table. |
472 | * hash table. |
| 479 | */ |
473 | */ |
| 480 | for (i = 0, cur = dentry->child; i < pos && cur; i++, |
474 | for (i = 0, lnk = nodep->cs_head.next; |
| - | 475 | i < pos && lnk != &nodep->cs_head; |
|
| 481 | cur = cur->sibling) |
476 | i++, lnk = lnk->next) |
| 482 | ; |
477 | ; |
| 483 | 478 | ||
| 484 | if (!cur) { |
479 | if (lnk == &nodep->cs_head) { |
| 485 | ipc_answer_0(callid, ENOENT); |
480 | ipc_answer_0(callid, ENOENT); |
| 486 | ipc_answer_1(rid, ENOENT, 0); |
481 | ipc_answer_1(rid, ENOENT, 0); |
| 487 | return; |
482 | return; |
| 488 | } |
483 | } |
| 489 | 484 | ||
| 490 | unsigned long key = (unsigned long) dentry; |
- | |
| 491 | link_t *hlp = hash_table_find(&cur->names, &key); |
- | |
| 492 | assert(hlp); |
- | |
| 493 | tmpfs_name_t *namep = hash_table_get_instance(hlp, tmpfs_name_t, |
485 | dentryp = list_get_instance(lnk, tmpfs_dentry_t, link); |
| 494 | link); |
- | |
| 495 | 486 | ||
| 496 | (void) ipc_data_read_finalize(callid, namep->name, |
487 | (void) ipc_data_read_finalize(callid, dentryp->name, |
| 497 | strlen(namep->name) + 1); |
488 | str_size(dentryp->name) + 1); |
| 498 | bytes = 1; |
489 | bytes = 1; |
| 499 | } |
490 | } |
| 500 | 491 | ||
| 501 | /* |
492 | /* |
| 502 | * Answer the VFS_READ call. |
493 | * Answer the VFS_READ call. |
| Line 509... | Line 500... | ||
| 509 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
500 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
| 510 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); |
501 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); |
| 511 | off_t pos = (off_t)IPC_GET_ARG3(*request); |
502 | off_t pos = (off_t)IPC_GET_ARG3(*request); |
| 512 | 503 | ||
| 513 | /* |
504 | /* |
| 514 | * Lookup the respective dentry. |
505 | * Lookup the respective TMPFS node. |
| 515 | */ |
506 | */ |
| 516 | link_t *hlp; |
507 | link_t *hlp; |
| 517 | unsigned long key = index; |
508 | unsigned long key[] = { |
| - | 509 | [NODES_KEY_INDEX] = index, |
|
| - | 510 | [NODES_KEY_DEV] = dev_handle |
|
| - | 511 | }; |
|
| 518 | hlp = hash_table_find(&dentries, &key); |
512 | hlp = hash_table_find(&nodes, key); |
| 519 | if (!hlp) { |
513 | if (!hlp) { |
| 520 | ipc_answer_0(rid, ENOENT); |
514 | ipc_answer_0(rid, ENOENT); |
| 521 | return; |
515 | return; |
| 522 | } |
516 | } |
| 523 | tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t, |
517 | tmpfs_node_t *nodep = hash_table_get_instance(hlp, tmpfs_node_t, |
| 524 | dh_link); |
518 | nh_link); |
| 525 | 519 | ||
| 526 | /* |
520 | /* |
| 527 | * Receive the write request. |
521 | * Receive the write request. |
| 528 | */ |
522 | */ |
| 529 | ipc_callid_t callid; |
523 | ipc_callid_t callid; |
| 530 | size_t len; |
524 | size_t size; |
| 531 | if (!ipc_data_write_receive(&callid, &len)) { |
525 | if (!ipc_data_write_receive(&callid, &size)) { |
| 532 | ipc_answer_0(callid, EINVAL); |
526 | ipc_answer_0(callid, EINVAL); |
| 533 | ipc_answer_0(rid, EINVAL); |
527 | ipc_answer_0(rid, EINVAL); |
| 534 | return; |
528 | return; |
| 535 | } |
529 | } |
| 536 | 530 | ||
| 537 | /* |
531 | /* |
| 538 | * Check whether the file needs to grow. |
532 | * Check whether the file needs to grow. |
| 539 | */ |
533 | */ |
| 540 | if (pos + len <= dentry->size) { |
534 | if (pos + size <= nodep->size) { |
| 541 | /* The file size is not changing. */ |
535 | /* The file size is not changing. */ |
| 542 | (void) ipc_data_write_finalize(callid, dentry->data + pos, len); |
536 | (void) ipc_data_write_finalize(callid, nodep->data + pos, size); |
| 543 | ipc_answer_2(rid, EOK, len, dentry->size); |
537 | ipc_answer_2(rid, EOK, size, nodep->size); |
| 544 | return; |
538 | return; |
| 545 | } |
539 | } |
| 546 | size_t delta = (pos + len) - dentry->size; |
540 | size_t delta = (pos + size) - nodep->size; |
| 547 | /* |
541 | /* |
| 548 | * At this point, we are deliberately extremely straightforward and |
542 | * At this point, we are deliberately extremely straightforward and |
| 549 | * simply realloc the contents of the file on every write that grows the |
543 | * simply realloc the contents of the file on every write that grows the |
| 550 | * file. In the end, the situation might not be as bad as it may look: |
544 | * file. In the end, the situation might not be as bad as it may look: |
| 551 | * our heap allocator can save us and just grow the block whenever |
545 | * our heap allocator can save us and just grow the block whenever |
| 552 | * possible. |
546 | * possible. |
| 553 | */ |
547 | */ |
| 554 | void *newdata = realloc(dentry->data, dentry->size + delta); |
548 | void *newdata = realloc(nodep->data, nodep->size + delta); |
| 555 | if (!newdata) { |
549 | if (!newdata) { |
| 556 | ipc_answer_0(callid, ENOMEM); |
550 | ipc_answer_0(callid, ENOMEM); |
| 557 | ipc_answer_2(rid, EOK, 0, dentry->size); |
551 | ipc_answer_2(rid, EOK, 0, nodep->size); |
| 558 | return; |
552 | return; |
| 559 | } |
553 | } |
| 560 | /* Clear any newly allocated memory in order to emulate gaps. */ |
554 | /* Clear any newly allocated memory in order to emulate gaps. */ |
| 561 | memset(newdata + dentry->size, 0, delta); |
555 | memset(newdata + nodep->size, 0, delta); |
| 562 | dentry->size += delta; |
556 | nodep->size += delta; |
| 563 | dentry->data = newdata; |
557 | nodep->data = newdata; |
| 564 | (void) ipc_data_write_finalize(callid, dentry->data + pos, len); |
558 | (void) ipc_data_write_finalize(callid, nodep->data + pos, size); |
| 565 | ipc_answer_2(rid, EOK, len, dentry->size); |
559 | ipc_answer_2(rid, EOK, size, nodep->size); |
| 566 | } |
560 | } |
| 567 | 561 | ||
| 568 | void tmpfs_truncate(ipc_callid_t rid, ipc_call_t *request) |
562 | void tmpfs_truncate(ipc_callid_t rid, ipc_call_t *request) |
| 569 | { |
563 | { |
| 570 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
564 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
| 571 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); |
565 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); |
| 572 | size_t size = (off_t)IPC_GET_ARG3(*request); |
566 | size_t size = (off_t)IPC_GET_ARG3(*request); |
| 573 | 567 | ||
| 574 | /* |
568 | /* |
| 575 | * Lookup the respective dentry. |
569 | * Lookup the respective TMPFS node. |
| 576 | */ |
570 | */ |
| 577 | link_t *hlp; |
571 | link_t *hlp; |
| 578 | unsigned long key = index; |
572 | unsigned long key[] = { |
| - | 573 | [NODES_KEY_INDEX] = index, |
|
| - | 574 | [NODES_KEY_DEV] = dev_handle |
|
| - | 575 | }; |
|
| 579 | hlp = hash_table_find(&dentries, &key); |
576 | hlp = hash_table_find(&nodes, key); |
| 580 | if (!hlp) { |
577 | if (!hlp) { |
| 581 | ipc_answer_0(rid, ENOENT); |
578 | ipc_answer_0(rid, ENOENT); |
| 582 | return; |
579 | return; |
| 583 | } |
580 | } |
| 584 | tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t, |
581 | tmpfs_node_t *nodep = hash_table_get_instance(hlp, tmpfs_node_t, |
| 585 | dh_link); |
582 | nh_link); |
| 586 | 583 | ||
| 587 | if (size == dentry->size) { |
584 | if (size == nodep->size) { |
| 588 | ipc_answer_0(rid, EOK); |
585 | ipc_answer_0(rid, EOK); |
| 589 | return; |
586 | return; |
| 590 | } |
587 | } |
| 591 | 588 | ||
| 592 | void *newdata = realloc(dentry->data, size); |
589 | void *newdata = realloc(nodep->data, size); |
| 593 | if (!newdata) { |
590 | if (!newdata) { |
| 594 | ipc_answer_0(rid, ENOMEM); |
591 | ipc_answer_0(rid, ENOMEM); |
| 595 | return; |
592 | return; |
| 596 | } |
593 | } |
| 597 | if (size > dentry->size) { |
594 | if (size > nodep->size) { |
| 598 | size_t delta = size - dentry->size; |
595 | size_t delta = size - nodep->size; |
| 599 | memset(newdata + dentry->size, 0, delta); |
596 | memset(newdata + nodep->size, 0, delta); |
| 600 | } |
597 | } |
| 601 | dentry->size = size; |
598 | nodep->size = size; |
| 602 | dentry->data = newdata; |
599 | nodep->data = newdata; |
| 603 | ipc_answer_0(rid, EOK); |
600 | ipc_answer_0(rid, EOK); |
| 604 | } |
601 | } |
| 605 | 602 | ||
| 606 | void tmpfs_destroy(ipc_callid_t rid, ipc_call_t *request) |
603 | void tmpfs_destroy(ipc_callid_t rid, ipc_call_t *request) |
| 607 | { |
604 | { |
| 608 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
605 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
| 609 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); |
606 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); |
| 610 | int rc; |
607 | int rc; |
| 611 | 608 | ||
| 612 | link_t *hlp; |
609 | link_t *hlp; |
| 613 | unsigned long key = index; |
610 | unsigned long key[] = { |
| - | 611 | [NODES_KEY_INDEX] = index, |
|
| - | 612 | [NODES_KEY_DEV] = dev_handle |
|
| - | 613 | }; |
|
| 614 | hlp = hash_table_find(&dentries, &key); |
614 | hlp = hash_table_find(&nodes, key); |
| 615 | if (!hlp) { |
615 | if (!hlp) { |
| 616 | ipc_answer_0(rid, ENOENT); |
616 | ipc_answer_0(rid, ENOENT); |
| 617 | return; |
617 | return; |
| 618 | } |
618 | } |
| 619 | tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t, |
619 | tmpfs_node_t *nodep = hash_table_get_instance(hlp, tmpfs_node_t, |
| 620 | dh_link); |
620 | nh_link); |
| 621 | rc = tmpfs_destroy_node(dentry); |
621 | rc = tmpfs_destroy_node(FS_NODE(nodep)); |
| 622 | ipc_answer_0(rid, rc); |
622 | ipc_answer_0(rid, rc); |
| 623 | } |
623 | } |
| 624 | 624 | ||
| 625 | /** |
625 | /** |
| 626 | * @} |
626 | * @} |