Rev 2787 | Rev 3011 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 2787 | Rev 2925 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (c) 2008 Jakub Jermar |
2 | * Copyright (c) 2008 Jakub Jermar |
3 | * All rights reserved. |
3 | * All rights reserved. |
4 | * |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
7 | * are met: |
8 | * |
8 | * |
9 | * - Redistributions of source code must retain the above copyright |
9 | * - Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * - Redistributions in binary form must reproduce the above copyright |
11 | * - Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
13 | * documentation and/or other materials provided with the distribution. |
14 | * - The name of the author may not be used to endorse or promote products |
14 | * - The name of the author may not be used to endorse or promote products |
15 | * derived from this software without specific prior written permission. |
15 | * derived from this software without specific prior written permission. |
16 | * |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
27 | */ |
28 | 28 | ||
29 | /** @addtogroup fs |
29 | /** @addtogroup fs |
30 | * @{ |
30 | * @{ |
31 | */ |
31 | */ |
32 | 32 | ||
33 | /** |
33 | /** |
34 | * @file tmpfs_ops.c |
34 | * @file tmpfs_ops.c |
35 | * @brief Implementation of VFS operations for the TMPFS file system |
35 | * @brief Implementation of VFS operations for the TMPFS file system |
36 | * server. |
36 | * server. |
37 | */ |
37 | */ |
38 | 38 | ||
39 | #include "tmpfs.h" |
39 | #include "tmpfs.h" |
40 | #include "../../vfs/vfs.h" |
40 | #include "../../vfs/vfs.h" |
41 | #include <ipc/ipc.h> |
41 | #include <ipc/ipc.h> |
42 | #include <async.h> |
42 | #include <async.h> |
43 | #include <errno.h> |
43 | #include <errno.h> |
44 | #include <atomic.h> |
44 | #include <atomic.h> |
45 | #include <stdlib.h> |
45 | #include <stdlib.h> |
46 | #include <string.h> |
46 | #include <string.h> |
47 | #include <stdio.h> |
47 | #include <stdio.h> |
48 | #include <assert.h> |
48 | #include <assert.h> |
49 | #include <sys/types.h> |
49 | #include <sys/types.h> |
50 | #include <libadt/hash_table.h> |
50 | #include <libadt/hash_table.h> |
51 | #include <as.h> |
51 | #include <as.h> |
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 DENTRIES_BUCKETS 256 |
58 | 58 | ||
59 | #define NAMES_BUCKETS 4 |
59 | #define NAMES_BUCKETS 4 |
60 | 60 | ||
61 | /* |
61 | /* |
62 | * For now, we don't distinguish between different dev_handles/instances. All |
62 | * For now, we don't distinguish between different dev_handles/instances. All |
63 | * requests resolve to the only instance, rooted in the following variable. |
63 | * requests resolve to the only instance, rooted in the following variable. |
64 | */ |
64 | */ |
65 | static tmpfs_dentry_t *root; |
65 | static tmpfs_dentry_t *root; |
66 | 66 | ||
67 | /* |
67 | /* |
68 | * Implementation of the libfs interface. |
68 | * Implementation of the libfs interface. |
69 | */ |
69 | */ |
70 | 70 | ||
71 | /* Forward declarations of static functions. */ |
71 | /* Forward declarations of static functions. */ |
72 | static bool tmpfs_match(void *, void *, const char *); |
72 | static void *tmpfs_match(void *, const char *); |
73 | static void *tmpfs_node_get(fs_handle_t, dev_handle_t, fs_index_t); |
73 | static void *tmpfs_node_get(dev_handle_t, fs_index_t); |
- | 74 | static void tmpfs_node_put(void *); |
|
74 | static void *tmpfs_create_node(int); |
75 | static void *tmpfs_create_node(int); |
75 | static bool tmpfs_link_node(void *, void *, const char *); |
76 | static bool tmpfs_link_node(void *, void *, const char *); |
76 | static int tmpfs_unlink_node(void *, void *); |
77 | static int tmpfs_unlink_node(void *, void *); |
77 | static void tmpfs_destroy_node(void *); |
78 | static int tmpfs_destroy_node(void *); |
78 | 79 | ||
79 | /* Implementation of helper functions. */ |
80 | /* Implementation of helper functions. */ |
80 | static fs_index_t tmpfs_index_get(void *nodep) |
81 | static fs_index_t tmpfs_index_get(void *nodep) |
81 | { |
82 | { |
82 | return ((tmpfs_dentry_t *) nodep)->index; |
83 | return ((tmpfs_dentry_t *) nodep)->index; |
83 | } |
84 | } |
84 | 85 | ||
85 | static size_t tmpfs_size_get(void *nodep) |
86 | static size_t tmpfs_size_get(void *nodep) |
86 | { |
87 | { |
87 | return ((tmpfs_dentry_t *) nodep)->size; |
88 | return ((tmpfs_dentry_t *) nodep)->size; |
88 | } |
89 | } |
89 | 90 | ||
90 | static unsigned tmpfs_lnkcnt_get(void *nodep) |
91 | static unsigned tmpfs_lnkcnt_get(void *nodep) |
91 | { |
92 | { |
92 | return ((tmpfs_dentry_t *) nodep)->lnkcnt; |
93 | return ((tmpfs_dentry_t *) nodep)->lnkcnt; |
93 | } |
94 | } |
94 | 95 | ||
95 | static void *tmpfs_child_get(void *nodep) |
96 | static bool tmpfs_has_children(void *nodep) |
96 | { |
97 | { |
97 | return ((tmpfs_dentry_t *) nodep)->child; |
98 | return ((tmpfs_dentry_t *) nodep)->child != NULL; |
98 | } |
99 | } |
99 | 100 | ||
100 | static void *tmpfs_sibling_get(void *nodep) |
- | |
101 | { |
- | |
102 | return ((tmpfs_dentry_t *) nodep)->sibling; |
- | |
103 | } |
- | |
104 | - | ||
105 | static void *tmpfs_root_get(void) |
101 | static void *tmpfs_root_get(dev_handle_t dev_handle) |
106 | { |
102 | { |
107 | return root; |
103 | return root; |
108 | } |
104 | } |
109 | 105 | ||
110 | static char tmpfs_plb_get_char(unsigned pos) |
106 | static char tmpfs_plb_get_char(unsigned pos) |
111 | { |
107 | { |
112 | return tmpfs_reg.plb_ro[pos % PLB_SIZE]; |
108 | return tmpfs_reg.plb_ro[pos % PLB_SIZE]; |
113 | } |
109 | } |
114 | 110 | ||
115 | static bool tmpfs_is_directory(void *nodep) |
111 | static bool tmpfs_is_directory(void *nodep) |
116 | { |
112 | { |
117 | return ((tmpfs_dentry_t *) nodep)->type == TMPFS_DIRECTORY; |
113 | return ((tmpfs_dentry_t *) nodep)->type == TMPFS_DIRECTORY; |
118 | } |
114 | } |
119 | 115 | ||
120 | static bool tmpfs_is_file(void *nodep) |
116 | static bool tmpfs_is_file(void *nodep) |
121 | { |
117 | { |
122 | return ((tmpfs_dentry_t *) nodep)->type == TMPFS_FILE; |
118 | return ((tmpfs_dentry_t *) nodep)->type == TMPFS_FILE; |
123 | } |
119 | } |
124 | 120 | ||
125 | /** libfs operations */ |
121 | /** libfs operations */ |
126 | libfs_ops_t tmpfs_libfs_ops = { |
122 | libfs_ops_t tmpfs_libfs_ops = { |
127 | .match = tmpfs_match, |
123 | .match = tmpfs_match, |
128 | .node_get = tmpfs_node_get, |
124 | .node_get = tmpfs_node_get, |
- | 125 | .node_put = tmpfs_node_put, |
|
129 | .create = tmpfs_create_node, |
126 | .create = tmpfs_create_node, |
130 | .destroy = tmpfs_destroy_node, |
127 | .destroy = tmpfs_destroy_node, |
131 | .link = tmpfs_link_node, |
128 | .link = tmpfs_link_node, |
132 | .unlink = tmpfs_unlink_node, |
129 | .unlink = tmpfs_unlink_node, |
133 | .index_get = tmpfs_index_get, |
130 | .index_get = tmpfs_index_get, |
134 | .size_get = tmpfs_size_get, |
131 | .size_get = tmpfs_size_get, |
135 | .lnkcnt_get = tmpfs_lnkcnt_get, |
132 | .lnkcnt_get = tmpfs_lnkcnt_get, |
136 | .child_get = tmpfs_child_get, |
133 | .has_children = tmpfs_has_children, |
137 | .sibling_get = tmpfs_sibling_get, |
- | |
138 | .root_get = tmpfs_root_get, |
134 | .root_get = tmpfs_root_get, |
139 | .plb_get_char = tmpfs_plb_get_char, |
135 | .plb_get_char = tmpfs_plb_get_char, |
140 | .is_directory = tmpfs_is_directory, |
136 | .is_directory = tmpfs_is_directory, |
141 | .is_file = tmpfs_is_file |
137 | .is_file = tmpfs_is_file |
142 | }; |
138 | }; |
143 | 139 | ||
144 | /** Hash table of all directory entries. */ |
140 | /** Hash table of all directory entries. */ |
145 | hash_table_t dentries; |
141 | hash_table_t dentries; |
146 | 142 | ||
147 | /* Implementation of hash table interface for the dentries hash table. */ |
143 | /* Implementation of hash table interface for the dentries hash table. */ |
148 | static hash_index_t dentries_hash(unsigned long *key) |
144 | static hash_index_t dentries_hash(unsigned long *key) |
149 | { |
145 | { |
150 | return *key % DENTRIES_BUCKETS; |
146 | return *key % DENTRIES_BUCKETS; |
151 | } |
147 | } |
152 | 148 | ||
153 | static int dentries_compare(unsigned long *key, hash_count_t keys, |
149 | static int dentries_compare(unsigned long *key, hash_count_t keys, |
154 | link_t *item) |
150 | link_t *item) |
155 | { |
151 | { |
156 | tmpfs_dentry_t *dentry = hash_table_get_instance(item, tmpfs_dentry_t, |
152 | tmpfs_dentry_t *dentry = hash_table_get_instance(item, tmpfs_dentry_t, |
157 | dh_link); |
153 | dh_link); |
158 | return dentry->index == *key; |
154 | return dentry->index == *key; |
159 | } |
155 | } |
160 | 156 | ||
161 | static void dentries_remove_callback(link_t *item) |
157 | static void dentries_remove_callback(link_t *item) |
162 | { |
158 | { |
163 | } |
159 | } |
164 | 160 | ||
165 | /** TMPFS dentries hash table operations. */ |
161 | /** TMPFS dentries hash table operations. */ |
166 | hash_table_operations_t dentries_ops = { |
162 | hash_table_operations_t dentries_ops = { |
167 | .hash = dentries_hash, |
163 | .hash = dentries_hash, |
168 | .compare = dentries_compare, |
164 | .compare = dentries_compare, |
169 | .remove_callback = dentries_remove_callback |
165 | .remove_callback = dentries_remove_callback |
170 | }; |
166 | }; |
171 | 167 | ||
172 | fs_index_t tmpfs_next_index = 1; |
168 | fs_index_t tmpfs_next_index = 1; |
173 | 169 | ||
174 | typedef struct { |
170 | typedef struct { |
175 | char *name; |
171 | char *name; |
176 | tmpfs_dentry_t *parent; |
172 | tmpfs_dentry_t *parent; |
177 | link_t link; |
173 | link_t link; |
178 | } tmpfs_name_t; |
174 | } tmpfs_name_t; |
179 | 175 | ||
180 | /* Implementation of hash table interface for the names hash table. */ |
176 | /* Implementation of hash table interface for the names hash table. */ |
181 | static hash_index_t names_hash(unsigned long *key) |
177 | static hash_index_t names_hash(unsigned long *key) |
182 | { |
178 | { |
183 | tmpfs_dentry_t *dentry = (tmpfs_dentry_t *) *key; |
179 | tmpfs_dentry_t *dentry = (tmpfs_dentry_t *) *key; |
184 | return dentry->index % NAMES_BUCKETS; |
180 | return dentry->index % NAMES_BUCKETS; |
185 | } |
181 | } |
186 | 182 | ||
187 | static int names_compare(unsigned long *key, hash_count_t keys, link_t *item) |
183 | static int names_compare(unsigned long *key, hash_count_t keys, link_t *item) |
188 | { |
184 | { |
189 | tmpfs_dentry_t *dentry = (tmpfs_dentry_t *) *key; |
185 | tmpfs_dentry_t *dentry = (tmpfs_dentry_t *) *key; |
190 | tmpfs_name_t *namep = hash_table_get_instance(item, tmpfs_name_t, |
186 | tmpfs_name_t *namep = hash_table_get_instance(item, tmpfs_name_t, |
191 | link); |
187 | link); |
192 | return dentry == namep->parent; |
188 | return dentry == namep->parent; |
193 | } |
189 | } |
194 | 190 | ||
195 | static void names_remove_callback(link_t *item) |
191 | static void names_remove_callback(link_t *item) |
196 | { |
192 | { |
197 | tmpfs_name_t *namep = hash_table_get_instance(item, tmpfs_name_t, |
193 | tmpfs_name_t *namep = hash_table_get_instance(item, tmpfs_name_t, |
198 | link); |
194 | link); |
199 | free(namep->name); |
195 | free(namep->name); |
200 | free(namep); |
196 | free(namep); |
201 | } |
197 | } |
202 | 198 | ||
203 | /** TMPFS node names hash table operations. */ |
199 | /** TMPFS node names hash table operations. */ |
204 | static hash_table_operations_t names_ops = { |
200 | static hash_table_operations_t names_ops = { |
205 | .hash = names_hash, |
201 | .hash = names_hash, |
206 | .compare = names_compare, |
202 | .compare = names_compare, |
207 | .remove_callback = names_remove_callback |
203 | .remove_callback = names_remove_callback |
208 | }; |
204 | }; |
209 | 205 | ||
210 | static void tmpfs_name_initialize(tmpfs_name_t *namep) |
206 | static void tmpfs_name_initialize(tmpfs_name_t *namep) |
211 | { |
207 | { |
212 | namep->name = NULL; |
208 | namep->name = NULL; |
213 | namep->parent = NULL; |
209 | namep->parent = NULL; |
214 | link_initialize(&namep->link); |
210 | link_initialize(&namep->link); |
215 | } |
211 | } |
216 | 212 | ||
217 | static bool tmpfs_dentry_initialize(tmpfs_dentry_t *dentry) |
213 | static bool tmpfs_dentry_initialize(tmpfs_dentry_t *dentry) |
218 | { |
214 | { |
219 | dentry->index = 0; |
215 | dentry->index = 0; |
220 | dentry->sibling = NULL; |
216 | dentry->sibling = NULL; |
221 | dentry->child = NULL; |
217 | dentry->child = NULL; |
222 | dentry->type = TMPFS_NONE; |
218 | dentry->type = TMPFS_NONE; |
223 | dentry->lnkcnt = 0; |
219 | dentry->lnkcnt = 0; |
224 | dentry->size = 0; |
220 | dentry->size = 0; |
225 | dentry->data = NULL; |
221 | dentry->data = NULL; |
226 | link_initialize(&dentry->dh_link); |
222 | link_initialize(&dentry->dh_link); |
227 | return (bool)hash_table_create(&dentry->names, NAMES_BUCKETS, 1, |
223 | return (bool)hash_table_create(&dentry->names, NAMES_BUCKETS, 1, |
228 | &names_ops); |
224 | &names_ops); |
229 | } |
225 | } |
230 | 226 | ||
231 | static bool tmpfs_init(void) |
227 | static bool tmpfs_init(void) |
232 | { |
228 | { |
233 | if (!hash_table_create(&dentries, DENTRIES_BUCKETS, 1, &dentries_ops)) |
229 | if (!hash_table_create(&dentries, DENTRIES_BUCKETS, 1, &dentries_ops)) |
234 | return false; |
230 | return false; |
235 | root = (tmpfs_dentry_t *) tmpfs_create_node(L_DIRECTORY); |
231 | root = (tmpfs_dentry_t *) tmpfs_create_node(L_DIRECTORY); |
236 | if (!root) { |
232 | if (!root) { |
237 | hash_table_destroy(&dentries); |
233 | hash_table_destroy(&dentries); |
238 | return false; |
234 | return false; |
239 | } |
235 | } |
240 | root->lnkcnt = 1; |
236 | root->lnkcnt = 1; |
241 | return true; |
237 | return true; |
242 | } |
238 | } |
243 | 239 | ||
244 | /** Compare one component of path to a directory entry. |
240 | /** Compare one component of path to a directory entry. |
245 | * |
241 | * |
246 | * @param prnt Node from which we descended. |
242 | * @param parentp Pointer to node from which we descended. |
247 | * @param chld Node to compare the path component with. |
243 | * @param childp Pointer to node to compare the path component with. |
248 | * @param component Array of characters holding component name. |
244 | * @param component Array of characters holding component name. |
249 | * |
245 | * |
250 | * @return True on match, false otherwise. |
246 | * @return True on match, false otherwise. |
251 | */ |
247 | */ |
- | 248 | static bool |
|
252 | bool tmpfs_match(void *prnt, void *chld, const char *component) |
249 | tmpfs_match_one(tmpfs_dentry_t *parentp, tmpfs_dentry_t *childp, |
- | 250 | const char *component) |
|
253 | { |
251 | { |
254 | tmpfs_dentry_t *parentp = (tmpfs_dentry_t *) prnt; |
- | |
255 | tmpfs_dentry_t *childp = (tmpfs_dentry_t *) chld; |
- | |
256 | - | ||
257 | unsigned long key = (unsigned long) parentp; |
252 | unsigned long key = (unsigned long) parentp; |
258 | link_t *hlp = hash_table_find(&childp->names, &key); |
253 | link_t *hlp = hash_table_find(&childp->names, &key); |
259 | assert(hlp); |
254 | assert(hlp); |
260 | tmpfs_name_t *namep = hash_table_get_instance(hlp, tmpfs_name_t, link); |
255 | tmpfs_name_t *namep = hash_table_get_instance(hlp, tmpfs_name_t, link); |
261 | - | ||
262 | return !strcmp(namep->name, component); |
256 | return !strcmp(namep->name, component); |
263 | } |
257 | } |
264 | 258 | ||
- | 259 | void *tmpfs_match(void *prnt, const char *component) |
|
- | 260 | { |
|
- | 261 | tmpfs_dentry_t *parentp = (tmpfs_dentry_t *) prnt; |
|
- | 262 | tmpfs_dentry_t *childp = parentp->child; |
|
- | 263 | ||
- | 264 | while (childp && !tmpfs_match_one(parentp, childp, component)) |
|
- | 265 | childp = childp->sibling; |
|
- | 266 | ||
- | 267 | return (void *) childp; |
|
- | 268 | } |
|
- | 269 | ||
265 | void * |
270 | void * |
266 | tmpfs_node_get(fs_handle_t fs_handle, dev_handle_t dev_handle, fs_index_t index) |
271 | tmpfs_node_get(dev_handle_t dev_handle, fs_index_t index) |
267 | { |
272 | { |
268 | unsigned long key = index; |
273 | unsigned long key = index; |
269 | link_t *lnk = hash_table_find(&dentries, &key); |
274 | link_t *lnk = hash_table_find(&dentries, &key); |
270 | if (!lnk) |
275 | if (!lnk) |
271 | return NULL; |
276 | return NULL; |
272 | return hash_table_get_instance(lnk, tmpfs_dentry_t, dh_link); |
277 | return hash_table_get_instance(lnk, tmpfs_dentry_t, dh_link); |
273 | } |
278 | } |
274 | 279 | ||
- | 280 | void tmpfs_node_put(void *node) |
|
- | 281 | { |
|
- | 282 | /* nothing to do */ |
|
- | 283 | } |
|
- | 284 | ||
275 | void *tmpfs_create_node(int lflag) |
285 | void *tmpfs_create_node(int lflag) |
276 | { |
286 | { |
277 | assert((lflag & L_FILE) ^ (lflag & L_DIRECTORY)); |
287 | assert((lflag & L_FILE) ^ (lflag & L_DIRECTORY)); |
278 | 288 | ||
279 | tmpfs_dentry_t *node = malloc(sizeof(tmpfs_dentry_t)); |
289 | tmpfs_dentry_t *node = malloc(sizeof(tmpfs_dentry_t)); |
280 | if (!node) |
290 | if (!node) |
281 | return NULL; |
291 | return NULL; |
282 | 292 | ||
283 | if (!tmpfs_dentry_initialize(node)) { |
293 | if (!tmpfs_dentry_initialize(node)) { |
284 | free(node); |
294 | free(node); |
285 | return NULL; |
295 | return NULL; |
286 | } |
296 | } |
287 | node->index = tmpfs_next_index++; |
297 | node->index = tmpfs_next_index++; |
288 | if (lflag & L_DIRECTORY) |
298 | if (lflag & L_DIRECTORY) |
289 | node->type = TMPFS_DIRECTORY; |
299 | node->type = TMPFS_DIRECTORY; |
290 | else |
300 | else |
291 | node->type = TMPFS_FILE; |
301 | node->type = TMPFS_FILE; |
292 | 302 | ||
293 | /* Insert the new node into the dentry hash table. */ |
303 | /* Insert the new node into the dentry hash table. */ |
294 | unsigned long key = node->index; |
304 | unsigned long key = node->index; |
295 | hash_table_insert(&dentries, &key, &node->dh_link); |
305 | hash_table_insert(&dentries, &key, &node->dh_link); |
296 | return (void *) node; |
306 | return (void *) node; |
297 | } |
307 | } |
298 | 308 | ||
299 | bool tmpfs_link_node(void *prnt, void *chld, const char *nm) |
309 | bool tmpfs_link_node(void *prnt, void *chld, const char *nm) |
300 | { |
310 | { |
301 | tmpfs_dentry_t *parentp = (tmpfs_dentry_t *) prnt; |
311 | tmpfs_dentry_t *parentp = (tmpfs_dentry_t *) prnt; |
302 | tmpfs_dentry_t *childp = (tmpfs_dentry_t *) chld; |
312 | tmpfs_dentry_t *childp = (tmpfs_dentry_t *) chld; |
303 | 313 | ||
304 | assert(parentp->type == TMPFS_DIRECTORY); |
314 | assert(parentp->type == TMPFS_DIRECTORY); |
305 | 315 | ||
306 | tmpfs_name_t *namep = malloc(sizeof(tmpfs_name_t)); |
316 | tmpfs_name_t *namep = malloc(sizeof(tmpfs_name_t)); |
307 | if (!namep) |
317 | if (!namep) |
308 | return false; |
318 | return false; |
309 | tmpfs_name_initialize(namep); |
319 | tmpfs_name_initialize(namep); |
310 | size_t len = strlen(nm); |
320 | size_t len = strlen(nm); |
311 | namep->name = malloc(len + 1); |
321 | namep->name = malloc(len + 1); |
312 | if (!namep->name) { |
322 | if (!namep->name) { |
313 | free(namep); |
323 | free(namep); |
314 | return false; |
324 | return false; |
315 | } |
325 | } |
316 | strcpy(namep->name, nm); |
326 | strcpy(namep->name, nm); |
317 | namep->parent = parentp; |
327 | namep->parent = parentp; |
318 | 328 | ||
319 | childp->lnkcnt++; |
329 | childp->lnkcnt++; |
320 | 330 | ||
321 | unsigned long key = (unsigned long) parentp; |
331 | unsigned long key = (unsigned long) parentp; |
322 | hash_table_insert(&childp->names, &key, &namep->link); |
332 | hash_table_insert(&childp->names, &key, &namep->link); |
323 | 333 | ||
324 | /* Insert the new node into the namespace. */ |
334 | /* Insert the new node into the namespace. */ |
325 | if (parentp->child) { |
335 | if (parentp->child) { |
326 | tmpfs_dentry_t *tmp = parentp->child; |
336 | tmpfs_dentry_t *tmp = parentp->child; |
327 | while (tmp->sibling) |
337 | while (tmp->sibling) |
328 | tmp = tmp->sibling; |
338 | tmp = tmp->sibling; |
329 | tmp->sibling = childp; |
339 | tmp->sibling = childp; |
330 | } else { |
340 | } else { |
331 | parentp->child = childp; |
341 | parentp->child = childp; |
332 | } |
342 | } |
333 | 343 | ||
334 | return true; |
344 | return true; |
335 | } |
345 | } |
336 | 346 | ||
337 | int tmpfs_unlink_node(void *prnt, void *chld) |
347 | int tmpfs_unlink_node(void *prnt, void *chld) |
338 | { |
348 | { |
339 | tmpfs_dentry_t *parentp = (tmpfs_dentry_t *)prnt; |
349 | tmpfs_dentry_t *parentp = (tmpfs_dentry_t *)prnt; |
340 | tmpfs_dentry_t *childp = (tmpfs_dentry_t *)chld; |
350 | tmpfs_dentry_t *childp = (tmpfs_dentry_t *)chld; |
341 | 351 | ||
342 | if (!parentp) |
352 | if (!parentp) |
343 | return EBUSY; |
353 | return EBUSY; |
344 | 354 | ||
345 | if (childp->child) |
355 | if (childp->child) |
346 | return ENOTEMPTY; |
356 | return ENOTEMPTY; |
347 | 357 | ||
348 | if (parentp->child == childp) { |
358 | if (parentp->child == childp) { |
349 | parentp->child = childp->sibling; |
359 | parentp->child = childp->sibling; |
350 | } else { |
360 | } else { |
351 | /* TODO: consider doubly linked list for organizing siblings. */ |
361 | /* TODO: consider doubly linked list for organizing siblings. */ |
352 | tmpfs_dentry_t *tmp = parentp->child; |
362 | tmpfs_dentry_t *tmp = parentp->child; |
353 | while (tmp->sibling != childp) |
363 | while (tmp->sibling != childp) |
354 | tmp = tmp->sibling; |
364 | tmp = tmp->sibling; |
355 | tmp->sibling = childp->sibling; |
365 | tmp->sibling = childp->sibling; |
356 | } |
366 | } |
357 | childp->sibling = NULL; |
367 | childp->sibling = NULL; |
358 | 368 | ||
359 | unsigned long key = (unsigned long) parentp; |
369 | unsigned long key = (unsigned long) parentp; |
360 | hash_table_remove(&childp->names, &key, 1); |
370 | hash_table_remove(&childp->names, &key, 1); |
361 | 371 | ||
362 | childp->lnkcnt--; |
372 | childp->lnkcnt--; |
363 | 373 | ||
364 | return EOK; |
374 | return EOK; |
365 | } |
375 | } |
366 | 376 | ||
367 | void tmpfs_destroy_node(void *nodep) |
377 | int tmpfs_destroy_node(void *nodep) |
368 | { |
378 | { |
369 | tmpfs_dentry_t *dentry = (tmpfs_dentry_t *) nodep; |
379 | tmpfs_dentry_t *dentry = (tmpfs_dentry_t *) nodep; |
370 | 380 | ||
371 | assert(!dentry->lnkcnt); |
381 | assert(!dentry->lnkcnt); |
372 | assert(!dentry->child); |
382 | assert(!dentry->child); |
373 | assert(!dentry->sibling); |
383 | assert(!dentry->sibling); |
374 | 384 | ||
375 | unsigned long key = dentry->index; |
385 | unsigned long key = dentry->index; |
376 | hash_table_remove(&dentries, &key, 1); |
386 | hash_table_remove(&dentries, &key, 1); |
377 | 387 | ||
378 | hash_table_destroy(&dentry->names); |
388 | hash_table_destroy(&dentry->names); |
379 | 389 | ||
380 | if (dentry->type == TMPFS_FILE) |
390 | if (dentry->type == TMPFS_FILE) |
381 | free(dentry->data); |
391 | free(dentry->data); |
382 | free(dentry); |
392 | free(dentry); |
- | 393 | return EOK; |
|
383 | } |
394 | } |
384 | 395 | ||
385 | void tmpfs_lookup(ipc_callid_t rid, ipc_call_t *request) |
396 | void tmpfs_lookup(ipc_callid_t rid, ipc_call_t *request) |
386 | { |
397 | { |
387 | /* Initialize TMPFS. */ |
398 | /* Initialize TMPFS. */ |
388 | if (!root && !tmpfs_init()) { |
399 | if (!root && !tmpfs_init()) { |
389 | ipc_answer_0(rid, ENOMEM); |
400 | ipc_answer_0(rid, ENOMEM); |
390 | return; |
401 | return; |
391 | } |
402 | } |
392 | libfs_lookup(&tmpfs_libfs_ops, tmpfs_reg.fs_handle, rid, request); |
403 | libfs_lookup(&tmpfs_libfs_ops, tmpfs_reg.fs_handle, rid, request); |
393 | } |
404 | } |
394 | 405 | ||
395 | void tmpfs_read(ipc_callid_t rid, ipc_call_t *request) |
406 | void tmpfs_read(ipc_callid_t rid, ipc_call_t *request) |
396 | { |
407 | { |
397 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
408 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
398 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); |
409 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); |
399 | off_t pos = (off_t)IPC_GET_ARG3(*request); |
410 | off_t pos = (off_t)IPC_GET_ARG3(*request); |
400 | 411 | ||
401 | /* |
412 | /* |
402 | * Lookup the respective dentry. |
413 | * Lookup the respective dentry. |
403 | */ |
414 | */ |
404 | link_t *hlp; |
415 | link_t *hlp; |
405 | unsigned long key = index; |
416 | unsigned long key = index; |
406 | hlp = hash_table_find(&dentries, &key); |
417 | hlp = hash_table_find(&dentries, &key); |
407 | if (!hlp) { |
418 | if (!hlp) { |
408 | ipc_answer_0(rid, ENOENT); |
419 | ipc_answer_0(rid, ENOENT); |
409 | return; |
420 | return; |
410 | } |
421 | } |
411 | tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t, |
422 | tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t, |
412 | dh_link); |
423 | dh_link); |
413 | 424 | ||
414 | /* |
425 | /* |
415 | * Receive the read request. |
426 | * Receive the read request. |
416 | */ |
427 | */ |
417 | ipc_callid_t callid; |
428 | ipc_callid_t callid; |
418 | size_t len; |
429 | size_t len; |
419 | if (!ipc_data_read_receive(&callid, &len)) { |
430 | if (!ipc_data_read_receive(&callid, &len)) { |
420 | ipc_answer_0(callid, EINVAL); |
431 | ipc_answer_0(callid, EINVAL); |
421 | ipc_answer_0(rid, EINVAL); |
432 | ipc_answer_0(rid, EINVAL); |
422 | return; |
433 | return; |
423 | } |
434 | } |
424 | 435 | ||
425 | size_t bytes; |
436 | size_t bytes; |
426 | if (dentry->type == TMPFS_FILE) { |
437 | if (dentry->type == TMPFS_FILE) { |
427 | bytes = max(0, min(dentry->size - pos, len)); |
438 | bytes = max(0, min(dentry->size - pos, len)); |
428 | (void) ipc_data_read_finalize(callid, dentry->data + pos, |
439 | (void) ipc_data_read_finalize(callid, dentry->data + pos, |
429 | bytes); |
440 | bytes); |
430 | } else { |
441 | } else { |
431 | int i; |
442 | int i; |
432 | tmpfs_dentry_t *cur; |
443 | tmpfs_dentry_t *cur; |
433 | 444 | ||
434 | assert(dentry->type == TMPFS_DIRECTORY); |
445 | assert(dentry->type == TMPFS_DIRECTORY); |
435 | 446 | ||
436 | /* |
447 | /* |
437 | * Yes, we really use O(n) algorithm here. |
448 | * Yes, we really use O(n) algorithm here. |
438 | * If it bothers someone, it could be fixed by introducing a |
449 | * If it bothers someone, it could be fixed by introducing a |
439 | * hash table. |
450 | * hash table. |
440 | */ |
451 | */ |
441 | for (i = 0, cur = dentry->child; i < pos && cur; i++, |
452 | for (i = 0, cur = dentry->child; i < pos && cur; i++, |
442 | cur = cur->sibling) |
453 | cur = cur->sibling) |
443 | ; |
454 | ; |
444 | 455 | ||
445 | if (!cur) { |
456 | if (!cur) { |
446 | ipc_answer_0(callid, ENOENT); |
457 | ipc_answer_0(callid, ENOENT); |
447 | ipc_answer_1(rid, ENOENT, 0); |
458 | ipc_answer_1(rid, ENOENT, 0); |
448 | return; |
459 | return; |
449 | } |
460 | } |
450 | 461 | ||
451 | unsigned long key = (unsigned long) dentry; |
462 | unsigned long key = (unsigned long) dentry; |
452 | link_t *hlp = hash_table_find(&cur->names, &key); |
463 | link_t *hlp = hash_table_find(&cur->names, &key); |
453 | assert(hlp); |
464 | assert(hlp); |
454 | tmpfs_name_t *namep = hash_table_get_instance(hlp, tmpfs_name_t, |
465 | tmpfs_name_t *namep = hash_table_get_instance(hlp, tmpfs_name_t, |
455 | link); |
466 | link); |
456 | 467 | ||
457 | (void) ipc_data_read_finalize(callid, namep->name, |
468 | (void) ipc_data_read_finalize(callid, namep->name, |
458 | strlen(namep->name) + 1); |
469 | strlen(namep->name) + 1); |
459 | bytes = 1; |
470 | bytes = 1; |
460 | } |
471 | } |
461 | 472 | ||
462 | /* |
473 | /* |
463 | * Answer the VFS_READ call. |
474 | * Answer the VFS_READ call. |
464 | */ |
475 | */ |
465 | ipc_answer_1(rid, EOK, bytes); |
476 | ipc_answer_1(rid, EOK, bytes); |
466 | } |
477 | } |
467 | 478 | ||
468 | void tmpfs_write(ipc_callid_t rid, ipc_call_t *request) |
479 | void tmpfs_write(ipc_callid_t rid, ipc_call_t *request) |
469 | { |
480 | { |
470 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
481 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
471 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); |
482 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); |
472 | off_t pos = (off_t)IPC_GET_ARG3(*request); |
483 | off_t pos = (off_t)IPC_GET_ARG3(*request); |
473 | 484 | ||
474 | /* |
485 | /* |
475 | * Lookup the respective dentry. |
486 | * Lookup the respective dentry. |
476 | */ |
487 | */ |
477 | link_t *hlp; |
488 | link_t *hlp; |
478 | unsigned long key = index; |
489 | unsigned long key = index; |
479 | hlp = hash_table_find(&dentries, &key); |
490 | hlp = hash_table_find(&dentries, &key); |
480 | if (!hlp) { |
491 | if (!hlp) { |
481 | ipc_answer_0(rid, ENOENT); |
492 | ipc_answer_0(rid, ENOENT); |
482 | return; |
493 | return; |
483 | } |
494 | } |
484 | tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t, |
495 | tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t, |
485 | dh_link); |
496 | dh_link); |
486 | 497 | ||
487 | /* |
498 | /* |
488 | * Receive the write request. |
499 | * Receive the write request. |
489 | */ |
500 | */ |
490 | ipc_callid_t callid; |
501 | ipc_callid_t callid; |
491 | size_t len; |
502 | size_t len; |
492 | if (!ipc_data_write_receive(&callid, &len)) { |
503 | if (!ipc_data_write_receive(&callid, &len)) { |
493 | ipc_answer_0(callid, EINVAL); |
504 | ipc_answer_0(callid, EINVAL); |
494 | ipc_answer_0(rid, EINVAL); |
505 | ipc_answer_0(rid, EINVAL); |
495 | return; |
506 | return; |
496 | } |
507 | } |
497 | 508 | ||
498 | /* |
509 | /* |
499 | * Check whether the file needs to grow. |
510 | * Check whether the file needs to grow. |
500 | */ |
511 | */ |
501 | if (pos + len <= dentry->size) { |
512 | if (pos + len <= dentry->size) { |
502 | /* The file size is not changing. */ |
513 | /* The file size is not changing. */ |
503 | (void) ipc_data_write_finalize(callid, dentry->data + pos, len); |
514 | (void) ipc_data_write_finalize(callid, dentry->data + pos, len); |
504 | ipc_answer_2(rid, EOK, len, dentry->size); |
515 | ipc_answer_2(rid, EOK, len, dentry->size); |
505 | return; |
516 | return; |
506 | } |
517 | } |
507 | size_t delta = (pos + len) - dentry->size; |
518 | size_t delta = (pos + len) - dentry->size; |
508 | /* |
519 | /* |
509 | * At this point, we are deliberately extremely straightforward and |
520 | * At this point, we are deliberately extremely straightforward and |
510 | * simply realloc the contents of the file on every write that grows the |
521 | * simply realloc the contents of the file on every write that grows the |
511 | * file. In the end, the situation might not be as bad as it may look: |
522 | * file. In the end, the situation might not be as bad as it may look: |
512 | * our heap allocator can save us and just grow the block whenever |
523 | * our heap allocator can save us and just grow the block whenever |
513 | * possible. |
524 | * possible. |
514 | */ |
525 | */ |
515 | void *newdata = realloc(dentry->data, dentry->size + delta); |
526 | void *newdata = realloc(dentry->data, dentry->size + delta); |
516 | if (!newdata) { |
527 | if (!newdata) { |
517 | ipc_answer_0(callid, ENOMEM); |
528 | ipc_answer_0(callid, ENOMEM); |
518 | ipc_answer_2(rid, EOK, 0, dentry->size); |
529 | ipc_answer_2(rid, EOK, 0, dentry->size); |
519 | return; |
530 | return; |
520 | } |
531 | } |
521 | /* Clear any newly allocated memory in order to emulate gaps. */ |
532 | /* Clear any newly allocated memory in order to emulate gaps. */ |
522 | memset(newdata + dentry->size, 0, delta); |
533 | memset(newdata + dentry->size, 0, delta); |
523 | dentry->size += delta; |
534 | dentry->size += delta; |
524 | dentry->data = newdata; |
535 | dentry->data = newdata; |
525 | (void) ipc_data_write_finalize(callid, dentry->data + pos, len); |
536 | (void) ipc_data_write_finalize(callid, dentry->data + pos, len); |
526 | ipc_answer_2(rid, EOK, len, dentry->size); |
537 | ipc_answer_2(rid, EOK, len, dentry->size); |
527 | } |
538 | } |
528 | 539 | ||
529 | void tmpfs_truncate(ipc_callid_t rid, ipc_call_t *request) |
540 | void tmpfs_truncate(ipc_callid_t rid, ipc_call_t *request) |
530 | { |
541 | { |
531 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
542 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
532 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); |
543 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); |
533 | size_t size = (off_t)IPC_GET_ARG3(*request); |
544 | size_t size = (off_t)IPC_GET_ARG3(*request); |
534 | 545 | ||
535 | /* |
546 | /* |
536 | * Lookup the respective dentry. |
547 | * Lookup the respective dentry. |
537 | */ |
548 | */ |
538 | link_t *hlp; |
549 | link_t *hlp; |
539 | unsigned long key = index; |
550 | unsigned long key = index; |
540 | hlp = hash_table_find(&dentries, &key); |
551 | hlp = hash_table_find(&dentries, &key); |
541 | if (!hlp) { |
552 | if (!hlp) { |
542 | ipc_answer_0(rid, ENOENT); |
553 | ipc_answer_0(rid, ENOENT); |
543 | return; |
554 | return; |
544 | } |
555 | } |
545 | tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t, |
556 | tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t, |
546 | dh_link); |
557 | dh_link); |
547 | 558 | ||
548 | if (size == dentry->size) { |
559 | if (size == dentry->size) { |
549 | ipc_answer_0(rid, EOK); |
560 | ipc_answer_0(rid, EOK); |
550 | return; |
561 | return; |
551 | } |
562 | } |
552 | 563 | ||
553 | void *newdata = realloc(dentry->data, size); |
564 | void *newdata = realloc(dentry->data, size); |
554 | if (!newdata) { |
565 | if (!newdata) { |
555 | ipc_answer_0(rid, ENOMEM); |
566 | ipc_answer_0(rid, ENOMEM); |
556 | return; |
567 | return; |
557 | } |
568 | } |
558 | if (size > dentry->size) { |
569 | if (size > dentry->size) { |
559 | size_t delta = size - dentry->size; |
570 | size_t delta = size - dentry->size; |
560 | memset(newdata + dentry->size, 0, delta); |
571 | memset(newdata + dentry->size, 0, delta); |
561 | } |
572 | } |
562 | dentry->size = size; |
573 | dentry->size = size; |
563 | dentry->data = newdata; |
574 | dentry->data = newdata; |
564 | ipc_answer_0(rid, EOK); |
575 | ipc_answer_0(rid, EOK); |
565 | } |
576 | } |
566 | 577 | ||
567 | void tmpfs_destroy(ipc_callid_t rid, ipc_call_t *request) |
578 | void tmpfs_destroy(ipc_callid_t rid, ipc_call_t *request) |
568 | { |
579 | { |
569 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
580 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
570 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); |
581 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); |
- | 582 | int rc; |
|
571 | 583 | ||
572 | link_t *hlp; |
584 | link_t *hlp; |
573 | unsigned long key = index; |
585 | unsigned long key = index; |
574 | hlp = hash_table_find(&dentries, &key); |
586 | hlp = hash_table_find(&dentries, &key); |
575 | if (!hlp) { |
587 | if (!hlp) { |
576 | ipc_answer_0(rid, ENOENT); |
588 | ipc_answer_0(rid, ENOENT); |
577 | return; |
589 | return; |
578 | } |
590 | } |
579 | tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t, |
591 | tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t, |
580 | dh_link); |
592 | dh_link); |
581 | tmpfs_destroy_node(dentry); |
593 | rc = tmpfs_destroy_node(dentry); |
582 | ipc_answer_0(rid, EOK); |
594 | ipc_answer_0(rid, rc); |
583 | } |
595 | } |
584 | 596 | ||
585 | /** |
597 | /** |
586 | * @} |
598 | * @} |
587 | */ |
599 | */ |
588 | 600 |