Rev 4419 | Rev 4537 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 4419 | Rev 4420 | ||
---|---|---|---|
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 | 139 | ||
- | 140 | #define NODES_KEY_INDEX 0 |
|
- | 141 | #define NODES_KEY_DEV 1 |
|
- | 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_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; |
153 | return (nodep->index == key[NODES_KEY_INDEX] && |
- | 154 | nodep->dev_handle == key[NODES_KEY_DEV]); |
|
157 | } |
155 | } |
158 | 156 | ||
159 | static void dentries_remove_callback(link_t *item) |
157 | static void nodes_remove_callback(link_t *item) |
160 | { |
158 | { |
161 | } |
159 | } |
162 | 160 | ||
163 | /** TMPFS dentries hash table operations. */ |
161 | /** TMPFS nodes hash table operations. */ |
164 | hash_table_operations_t dentries_ops = { |
162 | hash_table_operations_t nodes_ops = { |
165 | .hash = dentries_hash, |
163 | .hash = nodes_hash, |
166 | .compare = dentries_compare, |
164 | .compare = nodes_compare, |
167 | .remove_callback = dentries_remove_callback |
165 | .remove_callback = nodes_remove_callback |
168 | }; |
166 | }; |
169 | 167 | ||
170 | fs_index_t tmpfs_next_index = 1; |
- | |
171 | - | ||
172 | typedef struct { |
- | |
173 | char *name; |
- | |
174 | tmpfs_dentry_t *parent; |
- | |
175 | link_t link; |
- | |
176 | } tmpfs_name_t; |
- | |
177 | - | ||
178 | /* Implementation of hash table interface for the names hash table. */ |
- | |
179 | static hash_index_t names_hash(unsigned long *key) |
168 | static void tmpfs_node_initialize(tmpfs_node_t *nodep) |
180 | { |
169 | { |
- | 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; |
|
- | 176 | nodep->data = NULL; |
|
181 | tmpfs_dentry_t *dentry = (tmpfs_dentry_t *) *key; |
177 | link_initialize(&nodep->nh_link); |
182 | return dentry->index % NAMES_BUCKETS; |
178 | list_initialize(&nodep->cs_head); |
183 | } |
179 | } |
184 | 180 | ||
185 | static int names_compare(unsigned long *key, hash_count_t keys, link_t *item) |
181 | static void tmpfs_dentry_initialize(tmpfs_dentry_t *dentryp) |
186 | { |
182 | { |
187 | tmpfs_dentry_t *dentry = (tmpfs_dentry_t *) *key; |
183 | link_initialize(&dentryp->link); |
188 | tmpfs_name_t *namep = hash_table_get_instance(item, tmpfs_name_t, |
- | |
189 | link); |
184 | dentryp->name = NULL; |
190 | return dentry == namep->parent; |
185 | dentryp->node = NULL; |
191 | } |
186 | } |
192 | 187 | ||
193 | static void names_remove_callback(link_t *item) |
188 | bool tmpfs_init(void) |
194 | { |
189 | { |
195 | tmpfs_name_t *namep = hash_table_get_instance(item, tmpfs_name_t, |
190 | if (!hash_table_create(&nodes, NODES_BUCKETS, 2, &nodes_ops)) |
196 | link); |
- | |
197 | free(namep->name); |
- | |
198 | free(namep); |
191 | return false; |
199 | } |
- | |
200 | - | ||
201 | /** TMPFS node names hash table operations. */ |
- | |
202 | static hash_table_operations_t names_ops = { |
- | |
203 | .hash = names_hash, |
- | |
204 | .compare = names_compare, |
- | |
205 | .remove_callback = names_remove_callback |
- | |
206 | }; |
- | |
207 | 192 | ||
208 | static void tmpfs_name_initialize(tmpfs_name_t *namep) |
- | |
209 | { |
- | |
210 | namep->name = NULL; |
193 | return true; |
211 | namep->parent = NULL; |
- | |
212 | link_initialize(&namep->link); |
- | |
213 | } |
194 | } |
214 | 195 | ||
215 | static bool tmpfs_dentry_initialize(tmpfs_dentry_t *dentry) |
196 | static bool tmpfs_instance_init(dev_handle_t dev_handle) |
216 | { |
197 | { |
217 | dentry->index = 0; |
- | |
218 | dentry->sibling = NULL; |
- | |
219 | dentry->child = NULL; |
- | |
220 | dentry->type = TMPFS_NONE; |
- | |
221 | dentry->lnkcnt = 0; |
- | |
222 | dentry->size = 0; |
- | |
223 | dentry->data = NULL; |
- | |
224 | link_initialize(&dentry->dh_link); |
- | |
225 | return (bool)hash_table_create(&dentry->names, NAMES_BUCKETS, 1, |
- | |
226 | &names_ops); |
198 | fs_node_t *rfn; |
227 | } |
- | |
228 | 199 | ||
229 | static bool tmpfs_init(void) |
- | |
230 | { |
- | |
231 | if (!hash_table_create(&dentries, DENTRIES_BUCKETS, 1, &dentries_ops)) |
- | |
232 | return false; |
- | |
233 | root = (tmpfs_dentry_t *) tmpfs_create_node(TMPFS_DEV, L_DIRECTORY); |
200 | rfn = tmpfs_create_node(dev_handle, L_DIRECTORY); |
234 | if (!root) { |
201 | if (!rfn) |
235 | hash_table_destroy(&dentries); |
- | |
236 | return false; |
202 | return false; |
237 | } |
- | |
238 | root->lnkcnt = 0; /* FS root is not linked */ |
203 | TMPFS_NODE(rfn)->lnkcnt = 0; /* FS root is not linked */ |
239 | return true; |
204 | return true; |
240 | } |
205 | } |
241 | 206 | ||
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. |
207 | fs_node_t *tmpfs_match(fs_node_t *pfn, const char *component) |
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, |
- | |
252 | const char *component) |
- | |
253 | { |
208 | { |
254 | unsigned long key = (unsigned long) parentp; |
209 | tmpfs_node_t *parentp = TMPFS_NODE(pfn); |
255 | link_t *hlp = hash_table_find(&childp->names, &key); |
- | |
256 | assert(hlp); |
210 | link_t *lnk; |
257 | tmpfs_name_t *namep = hash_table_get_instance(hlp, tmpfs_name_t, link); |
- | |
258 | return !str_cmp(namep->name, component); |
- | |
259 | } |
- | |
260 | - | ||
261 | void *tmpfs_match(void *prnt, const char *component) |
- | |
262 | { |
- | |
263 | tmpfs_dentry_t *parentp = (tmpfs_dentry_t *) prnt; |
- | |
264 | tmpfs_dentry_t *childp = parentp->child; |
- | |
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 | fs_node_initialize(nodep->bp); |
|
- | 254 | nodep->bp->data = nodep; /* link the FS and TMPFS nodes */ |
|
- | 255 | if (!tmpfs_root_get(dev_handle)) |
|
- | 256 | nodep->index = TMPFS_SOME_ROOT; |
|
- | 257 | else |
|
299 | node->index = tmpfs_next_index++; |
258 | nodep->index = tmpfs_next_index++; |
- | 259 | nodep->dev_handle = dev_handle; |
|
300 | if (lflag & L_DIRECTORY) |
260 | if (lflag & L_DIRECTORY) |
301 | node->type = TMPFS_DIRECTORY; |
261 | nodep->type = TMPFS_DIRECTORY; |
302 | else |
262 | else |
303 | node->type = TMPFS_FILE; |
263 | nodep->type = TMPFS_FILE; |
304 | 264 | ||
305 | /* Insert the new node into the dentry hash table. */ |
265 | /* Insert the new node into the nodes hash table. */ |
306 | unsigned long key = node->index; |
266 | unsigned long key[] = { |
- | 267 | [NODES_KEY_INDEX] = nodep->index, |
|
- | 268 | [NODES_KEY_DEV] = nodep->dev_handle |
|
- | 269 | }; |
|
307 | hash_table_insert(&dentries, &key, &node->dh_link); |
270 | hash_table_insert(&nodes, key, &nodep->nh_link); |
308 | return (void *) node; |
271 | return FS_NODE(nodep); |
309 | } |
272 | } |
310 | 273 | ||
311 | int tmpfs_link_node(void *prnt, void *chld, const char *nm) |
274 | int tmpfs_link_node(fs_node_t *pfn, fs_node_t *cfn, const char *nm) |
312 | { |
275 | { |
313 | tmpfs_dentry_t *parentp = (tmpfs_dentry_t *) prnt; |
276 | tmpfs_node_t *parentp = TMPFS_NODE(pfn); |
314 | tmpfs_dentry_t *childp = (tmpfs_dentry_t *) chld; |
277 | tmpfs_node_t *childp = TMPFS_NODE(cfn); |
- | 278 | tmpfs_dentry_t *dentryp; |
|
- | 279 | link_t *lnk; |
|
315 | 280 | ||
316 | assert(parentp->type == TMPFS_DIRECTORY); |
281 | assert(parentp->type == TMPFS_DIRECTORY); |
317 | 282 | ||
- | 283 | /* Check for duplicit entries. */ |
|
- | 284 | for (lnk = parentp->cs_head.next; lnk != &parentp->cs_head; |
|
- | 285 | lnk = lnk->next) { |
|
- | 286 | dentryp = list_get_instance(lnk, tmpfs_dentry_t, link); |
|
- | 287 | if (!str_cmp(dentryp->name, nm)) |
|
- | 288 | return EEXIST; |
|
- | 289 | } |
|
- | 290 | ||
- | 291 | /* Allocate and initialize the dentry. */ |
|
318 | tmpfs_name_t *namep = malloc(sizeof(tmpfs_name_t)); |
292 | dentryp = malloc(sizeof(tmpfs_dentry_t)); |
319 | if (!namep) |
293 | if (!dentryp) |
320 | return ENOMEM; |
294 | return ENOMEM; |
321 | tmpfs_name_initialize(namep); |
295 | tmpfs_dentry_initialize(dentryp); |
- | 296 | ||
- | 297 | /* Populate and link the new dentry. */ |
|
322 | size_t size = str_size(nm); |
298 | size_t size = str_size(nm); |
323 | namep->name = malloc(size + 1); |
299 | dentryp->name = malloc(size + 1); |
324 | if (!namep->name) { |
300 | if (!dentryp->name) { |
325 | free(namep); |
301 | free(dentryp); |
326 | return ENOMEM; |
302 | return ENOMEM; |
327 | } |
303 | } |
328 | str_cpy(namep->name, size + 1, nm); |
304 | str_cpy(dentryp->name, size + 1, nm); |
329 | namep->parent = parentp; |
305 | dentryp->node = childp; |
330 | - | ||
331 | childp->lnkcnt++; |
306 | childp->lnkcnt++; |
332 | - | ||
333 | unsigned long key = (unsigned long) parentp; |
- | |
334 | hash_table_insert(&childp->names, &key, &namep->link); |
- | |
335 | - | ||
336 | /* Insert the new node into the namespace. */ |
- | |
337 | if (parentp->child) { |
- | |
338 | tmpfs_dentry_t *tmp = parentp->child; |
307 | list_append(&dentryp->link, &parentp->cs_head); |
339 | while (tmp->sibling) |
- | |
340 | tmp = tmp->sibling; |
- | |
341 | tmp->sibling = childp; |
- | |
342 | } else { |
- | |
343 | parentp->child = childp; |
- | |
344 | } |
- | |
345 | 308 | ||
346 | return EOK; |
309 | return EOK; |
347 | } |
310 | } |
348 | 311 | ||
349 | int tmpfs_unlink_node(void *prnt, void *chld) |
312 | int tmpfs_unlink_node(fs_node_t *pfn, fs_node_t *cfn, const char *nm) |
350 | { |
313 | { |
351 | tmpfs_dentry_t *parentp = (tmpfs_dentry_t *)prnt; |
314 | tmpfs_node_t *parentp = TMPFS_NODE(pfn); |
- | 315 | tmpfs_node_t *childp = NULL; |
|
352 | tmpfs_dentry_t *childp = (tmpfs_dentry_t *)chld; |
316 | tmpfs_dentry_t *dentryp; |
- | 317 | link_t *lnk; |
|
353 | 318 | ||
354 | if (!parentp) |
319 | if (!parentp) |
355 | return EBUSY; |
320 | return EBUSY; |
356 | 321 | ||
357 | if (childp->child) |
- | |
358 | return ENOTEMPTY; |
- | |
359 | - | ||
360 | if (parentp->child == childp) { |
322 | for (lnk = parentp->cs_head.next; lnk != &parentp->cs_head; |
361 | parentp->child = childp->sibling; |
323 | lnk = lnk->next) { |
362 | } else { |
- | |
363 | /* TODO: consider doubly linked list for organizing siblings. */ |
- | |
364 | tmpfs_dentry_t *tmp = parentp->child; |
324 | dentryp = list_get_instance(lnk, tmpfs_dentry_t, link); |
365 | while (tmp->sibling != childp) |
325 | if (!str_cmp(dentryp->name, nm)) { |
366 | tmp = tmp->sibling; |
326 | childp = dentryp->node; |
367 | tmp->sibling = childp->sibling; |
327 | assert(FS_NODE(childp) == cfn); |
- | 328 | break; |
|
368 | } |
329 | } |
- | 330 | } |
|
- | 331 | ||
- | 332 | if (!childp) |
|
369 | childp->sibling = NULL; |
333 | return ENOENT; |
370 | 334 | ||
371 | unsigned long key = (unsigned long) parentp; |
335 | if ((childp->lnkcnt == 1) && !list_empty(&childp->cs_head)) |
372 | hash_table_remove(&childp->names, &key, 1); |
336 | return ENOTEMPTY; |
373 | 337 | ||
- | 338 | list_remove(&dentryp->link); |
|
- | 339 | free(dentryp); |
|
374 | childp->lnkcnt--; |
340 | childp->lnkcnt--; |
375 | 341 | ||
376 | return EOK; |
342 | return EOK; |
377 | } |
343 | } |
378 | 344 | ||
379 | int tmpfs_destroy_node(void *nodep) |
345 | int tmpfs_destroy_node(fs_node_t *fn) |
380 | { |
346 | { |
381 | tmpfs_dentry_t *dentry = (tmpfs_dentry_t *) nodep; |
347 | tmpfs_node_t *nodep = TMPFS_NODE(fn); |
382 | - | ||
383 | assert(!dentry->lnkcnt); |
- | |
384 | assert(!dentry->child); |
- | |
385 | assert(!dentry->sibling); |
- | |
386 | 348 | ||
387 | unsigned long key = dentry->index; |
349 | assert(!nodep->lnkcnt); |
388 | hash_table_remove(&dentries, &key, 1); |
350 | assert(list_empty(&nodep->cs_head)); |
389 | 351 | ||
- | 352 | unsigned long key[] = { |
|
- | 353 | [NODES_KEY_INDEX] = nodep->index, |
|
- | 354 | [NODES_KEY_DEV] = nodep->dev_handle |
|
- | 355 | }; |
|
390 | hash_table_destroy(&dentry->names); |
356 | hash_table_remove(&nodes, key, 2); |
391 | 357 | ||
392 | if (dentry->type == TMPFS_FILE) |
358 | if (nodep->type == TMPFS_FILE) |
393 | free(dentry->data); |
359 | free(nodep->data); |
- | 360 | free(nodep->bp); |
|
394 | free(dentry); |
361 | free(nodep); |
395 | return EOK; |
362 | return EOK; |
396 | } |
363 | } |
397 | 364 | ||
398 | void tmpfs_mounted(ipc_callid_t rid, ipc_call_t *request) |
365 | void tmpfs_mounted(ipc_callid_t rid, ipc_call_t *request) |
399 | { |
366 | { |
400 | dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request); |
367 | dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request); |
401 | 368 | ||
- | 369 | /* accept the mount options */ |
|
- | 370 | ipc_callid_t callid; |
|
- | 371 | size_t size; |
|
- | 372 | if (!ipc_data_write_receive(&callid, &size)) { |
|
- | 373 | ipc_answer_0(callid, EINVAL); |
|
- | 374 | ipc_answer_0(rid, EINVAL); |
|
- | 375 | return; |
|
- | 376 | } |
|
- | 377 | char *opts = malloc(size + 1); |
|
- | 378 | if (!opts) { |
|
- | 379 | ipc_answer_0(callid, ENOMEM); |
|
- | 380 | ipc_answer_0(rid, ENOMEM); |
|
- | 381 | return; |
|
- | 382 | } |
|
- | 383 | ipcarg_t retval = ipc_data_write_finalize(callid, opts, size); |
|
- | 384 | if (retval != EOK) { |
|
- | 385 | ipc_answer_0(rid, retval); |
|
- | 386 | free(opts); |
|
- | 387 | return; |
|
- | 388 | } |
|
- | 389 | opts[size] = '\0'; |
|
- | 390 | ||
402 | /* Initialize TMPFS. */ |
391 | /* Initialize TMPFS instance. */ |
403 | if (!root && !tmpfs_init()) { |
392 | if (!tmpfs_instance_init(dev_handle)) { |
404 | ipc_answer_0(rid, ENOMEM); |
393 | ipc_answer_0(rid, ENOMEM); |
405 | return; |
394 | return; |
406 | } |
395 | } |
407 | 396 | ||
- | 397 | tmpfs_node_t *rootp = TMPFS_NODE(tmpfs_root_get(dev_handle)); |
|
408 | if (dev_handle >= 0) { |
398 | if (str_cmp(opts, "restore") == 0) { |
409 | if (tmpfs_restore(dev_handle)) |
399 | if (tmpfs_restore(dev_handle)) |
410 | ipc_answer_3(rid, EOK, root->index, root->size, |
400 | ipc_answer_3(rid, EOK, rootp->index, rootp->size, |
411 | root->lnkcnt); |
401 | rootp->lnkcnt); |
412 | else |
402 | else |
413 | ipc_answer_0(rid, ELIMIT); |
403 | ipc_answer_0(rid, ELIMIT); |
414 | } else { |
404 | } else { |
415 | ipc_answer_3(rid, EOK, root->index, root->size, root->lnkcnt); |
405 | ipc_answer_3(rid, EOK, rootp->index, rootp->size, |
- | 406 | rootp->lnkcnt); |
|
416 | } |
407 | } |
417 | } |
408 | } |
418 | 409 | ||
419 | void tmpfs_mount(ipc_callid_t rid, ipc_call_t *request) |
410 | void tmpfs_mount(ipc_callid_t rid, ipc_call_t *request) |
420 | { |
411 | { |
Line 436... | Line 427... | ||
436 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
427 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
437 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); |
428 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); |
438 | off_t pos = (off_t)IPC_GET_ARG3(*request); |
429 | off_t pos = (off_t)IPC_GET_ARG3(*request); |
439 | 430 | ||
440 | /* |
431 | /* |
441 | * Lookup the respective dentry. |
432 | * Lookup the respective TMPFS node. |
442 | */ |
433 | */ |
443 | link_t *hlp; |
434 | link_t *hlp; |
444 | unsigned long key = index; |
435 | unsigned long key[] = { |
- | 436 | [NODES_KEY_INDEX] = index, |
|
- | 437 | [NODES_KEY_DEV] = dev_handle, |
|
- | 438 | }; |
|
445 | hlp = hash_table_find(&dentries, &key); |
439 | hlp = hash_table_find(&nodes, key); |
446 | if (!hlp) { |
440 | if (!hlp) { |
447 | ipc_answer_0(rid, ENOENT); |
441 | ipc_answer_0(rid, ENOENT); |
448 | return; |
442 | return; |
449 | } |
443 | } |
450 | tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t, |
444 | tmpfs_node_t *nodep = hash_table_get_instance(hlp, tmpfs_node_t, |
451 | dh_link); |
445 | nh_link); |
452 | 446 | ||
453 | /* |
447 | /* |
454 | * Receive the read request. |
448 | * Receive the read request. |
455 | */ |
449 | */ |
456 | ipc_callid_t callid; |
450 | ipc_callid_t callid; |
Line 460... | Line 454... | ||
460 | ipc_answer_0(rid, EINVAL); |
454 | ipc_answer_0(rid, EINVAL); |
461 | return; |
455 | return; |
462 | } |
456 | } |
463 | 457 | ||
464 | size_t bytes; |
458 | size_t bytes; |
465 | if (dentry->type == TMPFS_FILE) { |
459 | if (nodep->type == TMPFS_FILE) { |
466 | bytes = max(0, min(dentry->size - pos, size)); |
460 | bytes = max(0, min(nodep->size - pos, size)); |
467 | (void) ipc_data_read_finalize(callid, dentry->data + pos, |
461 | (void) ipc_data_read_finalize(callid, nodep->data + pos, |
468 | bytes); |
462 | bytes); |
469 | } else { |
463 | } else { |
- | 464 | tmpfs_dentry_t *dentryp; |
|
- | 465 | link_t *lnk; |
|
470 | int i; |
466 | int i; |
471 | tmpfs_dentry_t *cur; |
- | |
472 | 467 | ||
473 | assert(dentry->type == TMPFS_DIRECTORY); |
468 | assert(nodep->type == TMPFS_DIRECTORY); |
474 | 469 | ||
475 | /* |
470 | /* |
476 | * Yes, we really use O(n) algorithm here. |
471 | * Yes, we really use O(n) algorithm here. |
477 | * If it bothers someone, it could be fixed by introducing a |
472 | * If it bothers someone, it could be fixed by introducing a |
478 | * hash table. |
473 | * hash table. |
479 | */ |
474 | */ |
480 | for (i = 0, cur = dentry->child; i < pos && cur; i++, |
475 | for (i = 0, lnk = nodep->cs_head.next; |
- | 476 | i < pos && lnk != &nodep->cs_head; |
|
481 | cur = cur->sibling) |
477 | i++, lnk = lnk->next) |
482 | ; |
478 | ; |
483 | 479 | ||
484 | if (!cur) { |
480 | if (lnk == &nodep->cs_head) { |
485 | ipc_answer_0(callid, ENOENT); |
481 | ipc_answer_0(callid, ENOENT); |
486 | ipc_answer_1(rid, ENOENT, 0); |
482 | ipc_answer_1(rid, ENOENT, 0); |
487 | return; |
483 | return; |
488 | } |
484 | } |
489 | 485 | ||
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, |
486 | dentryp = list_get_instance(lnk, tmpfs_dentry_t, link); |
494 | link); |
- | |
495 | 487 | ||
496 | (void) ipc_data_read_finalize(callid, namep->name, |
488 | (void) ipc_data_read_finalize(callid, dentryp->name, |
497 | str_size(namep->name) + 1); |
489 | str_size(dentryp->name) + 1); |
498 | bytes = 1; |
490 | bytes = 1; |
499 | } |
491 | } |
500 | 492 | ||
501 | /* |
493 | /* |
502 | * Answer the VFS_READ call. |
494 | * Answer the VFS_READ call. |
Line 509... | Line 501... | ||
509 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
501 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
510 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); |
502 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); |
511 | off_t pos = (off_t)IPC_GET_ARG3(*request); |
503 | off_t pos = (off_t)IPC_GET_ARG3(*request); |
512 | 504 | ||
513 | /* |
505 | /* |
514 | * Lookup the respective dentry. |
506 | * Lookup the respective TMPFS node. |
515 | */ |
507 | */ |
516 | link_t *hlp; |
508 | link_t *hlp; |
517 | unsigned long key = index; |
509 | unsigned long key[] = { |
- | 510 | [NODES_KEY_INDEX] = index, |
|
- | 511 | [NODES_KEY_DEV] = dev_handle |
|
- | 512 | }; |
|
518 | hlp = hash_table_find(&dentries, &key); |
513 | hlp = hash_table_find(&nodes, key); |
519 | if (!hlp) { |
514 | if (!hlp) { |
520 | ipc_answer_0(rid, ENOENT); |
515 | ipc_answer_0(rid, ENOENT); |
521 | return; |
516 | return; |
522 | } |
517 | } |
523 | tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t, |
518 | tmpfs_node_t *nodep = hash_table_get_instance(hlp, tmpfs_node_t, |
524 | dh_link); |
519 | nh_link); |
525 | 520 | ||
526 | /* |
521 | /* |
527 | * Receive the write request. |
522 | * Receive the write request. |
528 | */ |
523 | */ |
529 | ipc_callid_t callid; |
524 | ipc_callid_t callid; |
Line 535... | Line 530... | ||
535 | } |
530 | } |
536 | 531 | ||
537 | /* |
532 | /* |
538 | * Check whether the file needs to grow. |
533 | * Check whether the file needs to grow. |
539 | */ |
534 | */ |
540 | if (pos + size <= dentry->size) { |
535 | if (pos + size <= nodep->size) { |
541 | /* The file size is not changing. */ |
536 | /* The file size is not changing. */ |
542 | (void) ipc_data_write_finalize(callid, dentry->data + pos, size); |
537 | (void) ipc_data_write_finalize(callid, nodep->data + pos, size); |
543 | ipc_answer_2(rid, EOK, size, dentry->size); |
538 | ipc_answer_2(rid, EOK, size, nodep->size); |
544 | return; |
539 | return; |
545 | } |
540 | } |
546 | size_t delta = (pos + size) - dentry->size; |
541 | size_t delta = (pos + size) - nodep->size; |
547 | /* |
542 | /* |
548 | * At this point, we are deliberately extremely straightforward and |
543 | * At this point, we are deliberately extremely straightforward and |
549 | * simply realloc the contents of the file on every write that grows the |
544 | * 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: |
545 | * 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 |
546 | * our heap allocator can save us and just grow the block whenever |
552 | * possible. |
547 | * possible. |
553 | */ |
548 | */ |
554 | void *newdata = realloc(dentry->data, dentry->size + delta); |
549 | void *newdata = realloc(nodep->data, nodep->size + delta); |
555 | if (!newdata) { |
550 | if (!newdata) { |
556 | ipc_answer_0(callid, ENOMEM); |
551 | ipc_answer_0(callid, ENOMEM); |
557 | ipc_answer_2(rid, EOK, 0, dentry->size); |
552 | ipc_answer_2(rid, EOK, 0, nodep->size); |
558 | return; |
553 | return; |
559 | } |
554 | } |
560 | /* Clear any newly allocated memory in order to emulate gaps. */ |
555 | /* Clear any newly allocated memory in order to emulate gaps. */ |
561 | memset(newdata + dentry->size, 0, delta); |
556 | memset(newdata + nodep->size, 0, delta); |
562 | dentry->size += delta; |
557 | nodep->size += delta; |
563 | dentry->data = newdata; |
558 | nodep->data = newdata; |
564 | (void) ipc_data_write_finalize(callid, dentry->data + pos, size); |
559 | (void) ipc_data_write_finalize(callid, nodep->data + pos, size); |
565 | ipc_answer_2(rid, EOK, size, dentry->size); |
560 | ipc_answer_2(rid, EOK, size, nodep->size); |
566 | } |
561 | } |
567 | 562 | ||
568 | void tmpfs_truncate(ipc_callid_t rid, ipc_call_t *request) |
563 | void tmpfs_truncate(ipc_callid_t rid, ipc_call_t *request) |
569 | { |
564 | { |
570 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
565 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
571 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); |
566 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); |
572 | size_t size = (off_t)IPC_GET_ARG3(*request); |
567 | size_t size = (off_t)IPC_GET_ARG3(*request); |
573 | 568 | ||
574 | /* |
569 | /* |
575 | * Lookup the respective dentry. |
570 | * Lookup the respective TMPFS node. |
576 | */ |
571 | */ |
577 | link_t *hlp; |
572 | link_t *hlp; |
578 | unsigned long key = index; |
573 | unsigned long key[] = { |
- | 574 | [NODES_KEY_INDEX] = index, |
|
- | 575 | [NODES_KEY_DEV] = dev_handle |
|
- | 576 | }; |
|
579 | hlp = hash_table_find(&dentries, &key); |
577 | hlp = hash_table_find(&nodes, key); |
580 | if (!hlp) { |
578 | if (!hlp) { |
581 | ipc_answer_0(rid, ENOENT); |
579 | ipc_answer_0(rid, ENOENT); |
582 | return; |
580 | return; |
583 | } |
581 | } |
584 | tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t, |
582 | tmpfs_node_t *nodep = hash_table_get_instance(hlp, tmpfs_node_t, |
585 | dh_link); |
583 | nh_link); |
586 | 584 | ||
587 | if (size == dentry->size) { |
585 | if (size == nodep->size) { |
588 | ipc_answer_0(rid, EOK); |
586 | ipc_answer_0(rid, EOK); |
589 | return; |
587 | return; |
590 | } |
588 | } |
591 | 589 | ||
592 | void *newdata = realloc(dentry->data, size); |
590 | void *newdata = realloc(nodep->data, size); |
593 | if (!newdata) { |
591 | if (!newdata) { |
594 | ipc_answer_0(rid, ENOMEM); |
592 | ipc_answer_0(rid, ENOMEM); |
595 | return; |
593 | return; |
596 | } |
594 | } |
597 | if (size > dentry->size) { |
595 | if (size > nodep->size) { |
598 | size_t delta = size - dentry->size; |
596 | size_t delta = size - nodep->size; |
599 | memset(newdata + dentry->size, 0, delta); |
597 | memset(newdata + nodep->size, 0, delta); |
600 | } |
598 | } |
601 | dentry->size = size; |
599 | nodep->size = size; |
602 | dentry->data = newdata; |
600 | nodep->data = newdata; |
603 | ipc_answer_0(rid, EOK); |
601 | ipc_answer_0(rid, EOK); |
604 | } |
602 | } |
605 | 603 | ||
606 | void tmpfs_destroy(ipc_callid_t rid, ipc_call_t *request) |
604 | void tmpfs_destroy(ipc_callid_t rid, ipc_call_t *request) |
607 | { |
605 | { |
608 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
606 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
609 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); |
607 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); |
610 | int rc; |
608 | int rc; |
611 | 609 | ||
612 | link_t *hlp; |
610 | link_t *hlp; |
613 | unsigned long key = index; |
611 | unsigned long key[] = { |
- | 612 | [NODES_KEY_INDEX] = index, |
|
- | 613 | [NODES_KEY_DEV] = dev_handle |
|
- | 614 | }; |
|
614 | hlp = hash_table_find(&dentries, &key); |
615 | hlp = hash_table_find(&nodes, key); |
615 | if (!hlp) { |
616 | if (!hlp) { |
616 | ipc_answer_0(rid, ENOENT); |
617 | ipc_answer_0(rid, ENOENT); |
617 | return; |
618 | return; |
618 | } |
619 | } |
619 | tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t, |
620 | tmpfs_node_t *nodep = hash_table_get_instance(hlp, tmpfs_node_t, |
620 | dh_link); |
621 | nh_link); |
621 | rc = tmpfs_destroy_node(dentry); |
622 | rc = tmpfs_destroy_node(FS_NODE(nodep)); |
622 | ipc_answer_0(rid, rc); |
623 | ipc_answer_0(rid, rc); |
623 | } |
624 | } |
624 | 625 | ||
625 | /** |
626 | /** |
626 | * @} |
627 | * @} |