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