Rev 3087 | Rev 3103 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 3087 | Rev 3091 | ||
---|---|---|---|
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 | #include <ipc/services.h> |
53 | #include <ipc/services.h> |
54 | #include <ipc/devmap.h> |
54 | #include <ipc/devmap.h> |
55 | #include <sys/mman.h> |
55 | #include <sys/mman.h> |
56 | #include <byteorder.h> |
56 | #include <byteorder.h> |
57 | 57 | ||
58 | #define min(a, b) ((a) < (b) ? (a) : (b)) |
58 | #define min(a, b) ((a) < (b) ? (a) : (b)) |
59 | #define max(a, b) ((a) > (b) ? (a) : (b)) |
59 | #define max(a, b) ((a) > (b) ? (a) : (b)) |
60 | 60 | ||
61 | #define DENTRIES_BUCKETS 256 |
61 | #define DENTRIES_BUCKETS 256 |
62 | 62 | ||
63 | #define NAMES_BUCKETS 4 |
63 | #define NAMES_BUCKETS 4 |
64 | 64 | ||
65 | #define BLOCK_SIZE 1024 // FIXME |
65 | #define BLOCK_SIZE 1024 // FIXME |
66 | #define RD_BASE 1024 // FIXME |
66 | #define RD_BASE 1024 // FIXME |
67 | #define RD_READ_BLOCK (RD_BASE + 1) |
67 | #define RD_READ_BLOCK (RD_BASE + 1) |
68 | 68 | ||
69 | /* |
69 | /* |
70 | * For now, we don't distinguish between different dev_handles/instances. All |
70 | * For now, we don't distinguish between different dev_handles/instances. All |
71 | * requests resolve to the only instance, rooted in the following variable. |
71 | * requests resolve to the only instance, rooted in the following variable. |
72 | */ |
72 | */ |
73 | static tmpfs_dentry_t *root; |
73 | static tmpfs_dentry_t *root; |
74 | 74 | ||
75 | /* |
75 | /* |
76 | * Implementation of the libfs interface. |
76 | * Implementation of the libfs interface. |
77 | */ |
77 | */ |
78 | 78 | ||
79 | /* Forward declarations of static functions. */ |
79 | /* Forward declarations of static functions. */ |
80 | static void *tmpfs_match(void *, const char *); |
80 | static void *tmpfs_match(void *, const char *); |
81 | static void *tmpfs_node_get(dev_handle_t, fs_index_t); |
81 | static void *tmpfs_node_get(dev_handle_t, fs_index_t); |
82 | static void tmpfs_node_put(void *); |
82 | static void tmpfs_node_put(void *); |
83 | static void *tmpfs_create_node(int); |
83 | static void *tmpfs_create_node(int); |
84 | static bool tmpfs_link_node(void *, void *, const char *); |
84 | static bool tmpfs_link_node(void *, void *, const char *); |
85 | static int tmpfs_unlink_node(void *, void *); |
85 | static int tmpfs_unlink_node(void *, void *); |
86 | static int tmpfs_destroy_node(void *); |
86 | static int tmpfs_destroy_node(void *); |
87 | 87 | ||
88 | /* Implementation of helper functions. */ |
88 | /* Implementation of helper functions. */ |
89 | static fs_index_t tmpfs_index_get(void *nodep) |
89 | static fs_index_t tmpfs_index_get(void *nodep) |
90 | { |
90 | { |
91 | return ((tmpfs_dentry_t *) nodep)->index; |
91 | return ((tmpfs_dentry_t *) nodep)->index; |
92 | } |
92 | } |
93 | 93 | ||
94 | static size_t tmpfs_size_get(void *nodep) |
94 | static size_t tmpfs_size_get(void *nodep) |
95 | { |
95 | { |
96 | return ((tmpfs_dentry_t *) nodep)->size; |
96 | return ((tmpfs_dentry_t *) nodep)->size; |
97 | } |
97 | } |
98 | 98 | ||
99 | static unsigned tmpfs_lnkcnt_get(void *nodep) |
99 | static unsigned tmpfs_lnkcnt_get(void *nodep) |
100 | { |
100 | { |
101 | return ((tmpfs_dentry_t *) nodep)->lnkcnt; |
101 | return ((tmpfs_dentry_t *) nodep)->lnkcnt; |
102 | } |
102 | } |
103 | 103 | ||
104 | static bool tmpfs_has_children(void *nodep) |
104 | static bool tmpfs_has_children(void *nodep) |
105 | { |
105 | { |
106 | return ((tmpfs_dentry_t *) nodep)->child != NULL; |
106 | return ((tmpfs_dentry_t *) nodep)->child != NULL; |
107 | } |
107 | } |
108 | 108 | ||
109 | static void *tmpfs_root_get(dev_handle_t dev_handle) |
109 | static void *tmpfs_root_get(dev_handle_t dev_handle) |
110 | { |
110 | { |
111 | return root; |
111 | return root; |
112 | } |
112 | } |
113 | 113 | ||
114 | static char tmpfs_plb_get_char(unsigned pos) |
114 | static char tmpfs_plb_get_char(unsigned pos) |
115 | { |
115 | { |
116 | return tmpfs_reg.plb_ro[pos % PLB_SIZE]; |
116 | return tmpfs_reg.plb_ro[pos % PLB_SIZE]; |
117 | } |
117 | } |
118 | 118 | ||
119 | static bool tmpfs_is_directory(void *nodep) |
119 | static bool tmpfs_is_directory(void *nodep) |
120 | { |
120 | { |
121 | return ((tmpfs_dentry_t *) nodep)->type == TMPFS_DIRECTORY; |
121 | return ((tmpfs_dentry_t *) nodep)->type == TMPFS_DIRECTORY; |
122 | } |
122 | } |
123 | 123 | ||
124 | static bool tmpfs_is_file(void *nodep) |
124 | static bool tmpfs_is_file(void *nodep) |
125 | { |
125 | { |
126 | return ((tmpfs_dentry_t *) nodep)->type == TMPFS_FILE; |
126 | return ((tmpfs_dentry_t *) nodep)->type == TMPFS_FILE; |
127 | } |
127 | } |
128 | 128 | ||
129 | /** libfs operations */ |
129 | /** libfs operations */ |
130 | libfs_ops_t tmpfs_libfs_ops = { |
130 | libfs_ops_t tmpfs_libfs_ops = { |
131 | .match = tmpfs_match, |
131 | .match = tmpfs_match, |
132 | .node_get = tmpfs_node_get, |
132 | .node_get = tmpfs_node_get, |
133 | .node_put = tmpfs_node_put, |
133 | .node_put = tmpfs_node_put, |
134 | .create = tmpfs_create_node, |
134 | .create = tmpfs_create_node, |
135 | .destroy = tmpfs_destroy_node, |
135 | .destroy = tmpfs_destroy_node, |
136 | .link = tmpfs_link_node, |
136 | .link = tmpfs_link_node, |
137 | .unlink = tmpfs_unlink_node, |
137 | .unlink = tmpfs_unlink_node, |
138 | .index_get = tmpfs_index_get, |
138 | .index_get = tmpfs_index_get, |
139 | .size_get = tmpfs_size_get, |
139 | .size_get = tmpfs_size_get, |
140 | .lnkcnt_get = tmpfs_lnkcnt_get, |
140 | .lnkcnt_get = tmpfs_lnkcnt_get, |
141 | .has_children = tmpfs_has_children, |
141 | .has_children = tmpfs_has_children, |
142 | .root_get = tmpfs_root_get, |
142 | .root_get = tmpfs_root_get, |
143 | .plb_get_char = tmpfs_plb_get_char, |
143 | .plb_get_char = tmpfs_plb_get_char, |
144 | .is_directory = tmpfs_is_directory, |
144 | .is_directory = tmpfs_is_directory, |
145 | .is_file = tmpfs_is_file |
145 | .is_file = tmpfs_is_file |
146 | }; |
146 | }; |
147 | 147 | ||
148 | /** Hash table of all directory entries. */ |
148 | /** Hash table of all directory entries. */ |
149 | hash_table_t dentries; |
149 | hash_table_t dentries; |
150 | 150 | ||
151 | struct rdentry { |
151 | struct rdentry { |
152 | uint8_t type; |
152 | uint8_t type; |
153 | uint32_t len; |
153 | uint32_t len; |
154 | } __attribute__((packed)); |
154 | } __attribute__((packed)); |
155 | 155 | ||
156 | /* Implementation of hash table interface for the dentries hash table. */ |
156 | /* Implementation of hash table interface for the dentries hash table. */ |
157 | static hash_index_t dentries_hash(unsigned long *key) |
157 | static hash_index_t dentries_hash(unsigned long *key) |
158 | { |
158 | { |
159 | return *key % DENTRIES_BUCKETS; |
159 | return *key % DENTRIES_BUCKETS; |
160 | } |
160 | } |
161 | 161 | ||
162 | static int dentries_compare(unsigned long *key, hash_count_t keys, |
162 | static int dentries_compare(unsigned long *key, hash_count_t keys, |
163 | link_t *item) |
163 | link_t *item) |
164 | { |
164 | { |
165 | tmpfs_dentry_t *dentry = hash_table_get_instance(item, tmpfs_dentry_t, |
165 | tmpfs_dentry_t *dentry = hash_table_get_instance(item, tmpfs_dentry_t, |
166 | dh_link); |
166 | dh_link); |
167 | return dentry->index == *key; |
167 | return dentry->index == *key; |
168 | } |
168 | } |
169 | 169 | ||
170 | static void dentries_remove_callback(link_t *item) |
170 | static void dentries_remove_callback(link_t *item) |
171 | { |
171 | { |
172 | } |
172 | } |
173 | 173 | ||
174 | /** TMPFS dentries hash table operations. */ |
174 | /** TMPFS dentries hash table operations. */ |
175 | hash_table_operations_t dentries_ops = { |
175 | hash_table_operations_t dentries_ops = { |
176 | .hash = dentries_hash, |
176 | .hash = dentries_hash, |
177 | .compare = dentries_compare, |
177 | .compare = dentries_compare, |
178 | .remove_callback = dentries_remove_callback |
178 | .remove_callback = dentries_remove_callback |
179 | }; |
179 | }; |
180 | 180 | ||
181 | fs_index_t tmpfs_next_index = 1; |
181 | fs_index_t tmpfs_next_index = 1; |
182 | 182 | ||
183 | typedef struct { |
183 | typedef struct { |
184 | char *name; |
184 | char *name; |
185 | tmpfs_dentry_t *parent; |
185 | tmpfs_dentry_t *parent; |
186 | link_t link; |
186 | link_t link; |
187 | } tmpfs_name_t; |
187 | } tmpfs_name_t; |
188 | 188 | ||
189 | /* Implementation of hash table interface for the names hash table. */ |
189 | /* Implementation of hash table interface for the names hash table. */ |
190 | static hash_index_t names_hash(unsigned long *key) |
190 | static hash_index_t names_hash(unsigned long *key) |
191 | { |
191 | { |
192 | tmpfs_dentry_t *dentry = (tmpfs_dentry_t *) *key; |
192 | tmpfs_dentry_t *dentry = (tmpfs_dentry_t *) *key; |
193 | return dentry->index % NAMES_BUCKETS; |
193 | return dentry->index % NAMES_BUCKETS; |
194 | } |
194 | } |
195 | 195 | ||
196 | static int names_compare(unsigned long *key, hash_count_t keys, link_t *item) |
196 | static int names_compare(unsigned long *key, hash_count_t keys, link_t *item) |
197 | { |
197 | { |
198 | tmpfs_dentry_t *dentry = (tmpfs_dentry_t *) *key; |
198 | tmpfs_dentry_t *dentry = (tmpfs_dentry_t *) *key; |
199 | tmpfs_name_t *namep = hash_table_get_instance(item, tmpfs_name_t, |
199 | tmpfs_name_t *namep = hash_table_get_instance(item, tmpfs_name_t, |
200 | link); |
200 | link); |
201 | return dentry == namep->parent; |
201 | return dentry == namep->parent; |
202 | } |
202 | } |
203 | 203 | ||
204 | static void names_remove_callback(link_t *item) |
204 | static void names_remove_callback(link_t *item) |
205 | { |
205 | { |
206 | tmpfs_name_t *namep = hash_table_get_instance(item, tmpfs_name_t, |
206 | tmpfs_name_t *namep = hash_table_get_instance(item, tmpfs_name_t, |
207 | link); |
207 | link); |
208 | free(namep->name); |
208 | free(namep->name); |
209 | free(namep); |
209 | free(namep); |
210 | } |
210 | } |
211 | 211 | ||
212 | /** TMPFS node names hash table operations. */ |
212 | /** TMPFS node names hash table operations. */ |
213 | static hash_table_operations_t names_ops = { |
213 | static hash_table_operations_t names_ops = { |
214 | .hash = names_hash, |
214 | .hash = names_hash, |
215 | .compare = names_compare, |
215 | .compare = names_compare, |
216 | .remove_callback = names_remove_callback |
216 | .remove_callback = names_remove_callback |
217 | }; |
217 | }; |
218 | 218 | ||
219 | static void tmpfs_name_initialize(tmpfs_name_t *namep) |
219 | static void tmpfs_name_initialize(tmpfs_name_t *namep) |
220 | { |
220 | { |
221 | namep->name = NULL; |
221 | namep->name = NULL; |
222 | namep->parent = NULL; |
222 | namep->parent = NULL; |
223 | link_initialize(&namep->link); |
223 | link_initialize(&namep->link); |
224 | } |
224 | } |
225 | 225 | ||
226 | static bool tmpfs_dentry_initialize(tmpfs_dentry_t *dentry) |
226 | static bool tmpfs_dentry_initialize(tmpfs_dentry_t *dentry) |
227 | { |
227 | { |
228 | dentry->index = 0; |
228 | dentry->index = 0; |
229 | dentry->sibling = NULL; |
229 | dentry->sibling = NULL; |
230 | dentry->child = NULL; |
230 | dentry->child = NULL; |
231 | dentry->type = TMPFS_NONE; |
231 | dentry->type = TMPFS_NONE; |
232 | dentry->lnkcnt = 0; |
232 | dentry->lnkcnt = 0; |
233 | dentry->size = 0; |
233 | dentry->size = 0; |
234 | dentry->data = NULL; |
234 | dentry->data = NULL; |
235 | link_initialize(&dentry->dh_link); |
235 | link_initialize(&dentry->dh_link); |
236 | return (bool)hash_table_create(&dentry->names, NAMES_BUCKETS, 1, |
236 | return (bool)hash_table_create(&dentry->names, NAMES_BUCKETS, 1, |
237 | &names_ops); |
237 | &names_ops); |
238 | } |
238 | } |
239 | 239 | ||
240 | static bool tmpfs_init(void) |
240 | static bool tmpfs_init(void) |
241 | { |
241 | { |
242 | if (!hash_table_create(&dentries, DENTRIES_BUCKETS, 1, &dentries_ops)) |
242 | if (!hash_table_create(&dentries, DENTRIES_BUCKETS, 1, &dentries_ops)) |
243 | return false; |
243 | return false; |
244 | root = (tmpfs_dentry_t *) tmpfs_create_node(L_DIRECTORY); |
244 | root = (tmpfs_dentry_t *) tmpfs_create_node(L_DIRECTORY); |
245 | if (!root) { |
245 | if (!root) { |
246 | hash_table_destroy(&dentries); |
246 | hash_table_destroy(&dentries); |
247 | return false; |
247 | return false; |
248 | } |
248 | } |
249 | root->lnkcnt = 1; |
249 | root->lnkcnt = 1; |
250 | return true; |
250 | return true; |
251 | } |
251 | } |
252 | 252 | ||
253 | static bool tmpfs_blockread(int phone, void *buffer, size_t *bufpos, size_t *buflen, size_t *pos, void *dst, size_t size) |
253 | static bool tmpfs_blockread(int phone, void *buffer, size_t *bufpos, size_t *buflen, size_t *pos, void *dst, size_t size) |
254 | { |
254 | { |
255 | size_t offset = 0; |
255 | size_t offset = 0; |
256 | size_t left = size; |
256 | size_t left = size; |
257 | 257 | ||
258 | while (left > 0) { |
258 | while (left > 0) { |
259 | size_t rd; |
259 | size_t rd; |
260 | 260 | ||
261 | if (*bufpos + left < *buflen) |
261 | if (*bufpos + left < *buflen) |
262 | rd = left; |
262 | rd = left; |
263 | else |
263 | else |
264 | rd = *buflen - *bufpos; |
264 | rd = *buflen - *bufpos; |
265 | 265 | ||
266 | if (rd > 0) { |
266 | if (rd > 0) { |
267 | memcpy(dst + offset, buffer + *bufpos, rd); |
267 | memcpy(dst + offset, buffer + *bufpos, rd); |
268 | offset += rd; |
268 | offset += rd; |
269 | *bufpos += rd; |
269 | *bufpos += rd; |
270 | *pos += rd; |
270 | *pos += rd; |
271 | left -= rd; |
271 | left -= rd; |
272 | } |
272 | } |
273 | 273 | ||
274 | if (*bufpos == *buflen) { |
274 | if (*bufpos == *buflen) { |
275 | int retval; |
275 | ipcarg_t retval; |
276 | int rc = ipc_call_sync_2_1(phone, RD_READ_BLOCK, *pos / BLOCK_SIZE, BLOCK_SIZE, (sysarg_t *) &retval); |
276 | int rc = ipc_call_sync_2_1(phone, RD_READ_BLOCK, |
- | 277 | *pos / BLOCK_SIZE, BLOCK_SIZE, |
|
- | 278 | &retval); |
|
277 | if ((rc != EOK) || (retval != EOK)) |
279 | if ((rc != EOK) || (retval != EOK)) |
278 | return false; |
280 | return false; |
279 | 281 | ||
280 | *bufpos = 0; |
282 | *bufpos = 0; |
281 | *buflen = BLOCK_SIZE; |
283 | *buflen = BLOCK_SIZE; |
282 | } |
284 | } |
283 | } |
285 | } |
284 | 286 | ||
285 | return true; |
287 | return true; |
286 | } |
288 | } |
287 | 289 | ||
288 | static bool tmpfs_restore_recursion(int phone, void *block, size_t *bufpos, size_t *buflen, size_t *pos, tmpfs_dentry_t *parent) |
290 | static bool tmpfs_restore_recursion(int phone, void *block, size_t *bufpos, size_t *buflen, size_t *pos, tmpfs_dentry_t *parent) |
289 | { |
291 | { |
290 | struct rdentry entry; |
292 | struct rdentry entry; |
291 | 293 | ||
292 | do { |
294 | do { |
293 | char *fname; |
295 | char *fname; |
294 | tmpfs_dentry_t *node; |
296 | tmpfs_dentry_t *node; |
295 | uint32_t size; |
297 | uint32_t size; |
296 | 298 | ||
297 | if (!tmpfs_blockread(phone, block, bufpos, buflen, pos, &entry, sizeof(entry))) |
299 | if (!tmpfs_blockread(phone, block, bufpos, buflen, pos, &entry, sizeof(entry))) |
298 | return false; |
300 | return false; |
299 | 301 | ||
300 | entry.len = uint32_t_le2host(entry.len); |
302 | entry.len = uint32_t_le2host(entry.len); |
301 | 303 | ||
302 | switch (entry.type) { |
304 | switch (entry.type) { |
303 | case 0: |
305 | case 0: |
304 | break; |
306 | break; |
305 | case 1: |
307 | case 1: |
306 | fname = malloc(entry.len + 1); |
308 | fname = malloc(entry.len + 1); |
307 | if (fname == NULL) |
309 | if (fname == NULL) |
308 | return false; |
310 | return false; |
309 | 311 | ||
310 | node = (tmpfs_dentry_t *) tmpfs_create_node(L_FILE); |
312 | node = (tmpfs_dentry_t *) tmpfs_create_node(L_FILE); |
311 | if (node == NULL) { |
313 | if (node == NULL) { |
312 | free(fname); |
314 | free(fname); |
313 | return false; |
315 | return false; |
314 | } |
316 | } |
315 | 317 | ||
316 | if (!tmpfs_blockread(phone, block, bufpos, buflen, pos, fname, entry.len)) { |
318 | if (!tmpfs_blockread(phone, block, bufpos, buflen, pos, fname, entry.len)) { |
317 | tmpfs_destroy_node((void *) node); |
319 | tmpfs_destroy_node((void *) node); |
318 | free(fname); |
320 | free(fname); |
319 | return false; |
321 | return false; |
320 | } |
322 | } |
321 | fname[entry.len] = 0; |
323 | fname[entry.len] = 0; |
322 | 324 | ||
323 | if (!tmpfs_link_node((void *) parent, (void *) node, fname)) { |
325 | if (!tmpfs_link_node((void *) parent, (void *) node, fname)) { |
324 | tmpfs_destroy_node((void *) node); |
326 | tmpfs_destroy_node((void *) node); |
325 | free(fname); |
327 | free(fname); |
326 | return false; |
328 | return false; |
327 | } |
329 | } |
328 | free(fname); |
330 | free(fname); |
329 | 331 | ||
330 | if (!tmpfs_blockread(phone, block, bufpos, buflen, pos, &size, sizeof(size))) |
332 | if (!tmpfs_blockread(phone, block, bufpos, buflen, pos, &size, sizeof(size))) |
331 | return false; |
333 | return false; |
332 | 334 | ||
333 | size = uint32_t_le2host(size); |
335 | size = uint32_t_le2host(size); |
334 | 336 | ||
335 | node->data = malloc(size); |
337 | node->data = malloc(size); |
336 | if (node->data == NULL) |
338 | if (node->data == NULL) |
337 | return false; |
339 | return false; |
338 | 340 | ||
339 | node->size = size; |
341 | node->size = size; |
340 | if (!tmpfs_blockread(phone, block, bufpos, buflen, pos, node->data, size)) |
342 | if (!tmpfs_blockread(phone, block, bufpos, buflen, pos, node->data, size)) |
341 | return false; |
343 | return false; |
342 | 344 | ||
343 | break; |
345 | break; |
344 | case 2: |
346 | case 2: |
345 | fname = malloc(entry.len + 1); |
347 | fname = malloc(entry.len + 1); |
346 | if (fname == NULL) |
348 | if (fname == NULL) |
347 | return false; |
349 | return false; |
348 | 350 | ||
349 | node = (tmpfs_dentry_t *) tmpfs_create_node(L_DIRECTORY); |
351 | node = (tmpfs_dentry_t *) tmpfs_create_node(L_DIRECTORY); |
350 | if (node == NULL) { |
352 | if (node == NULL) { |
351 | free(fname); |
353 | free(fname); |
352 | return false; |
354 | return false; |
353 | } |
355 | } |
354 | 356 | ||
355 | if (!tmpfs_blockread(phone, block, bufpos, buflen, pos, fname, entry.len)) { |
357 | if (!tmpfs_blockread(phone, block, bufpos, buflen, pos, fname, entry.len)) { |
356 | tmpfs_destroy_node((void *) node); |
358 | tmpfs_destroy_node((void *) node); |
357 | free(fname); |
359 | free(fname); |
358 | return false; |
360 | return false; |
359 | } |
361 | } |
360 | fname[entry.len] = 0; |
362 | fname[entry.len] = 0; |
361 | 363 | ||
362 | if (!tmpfs_link_node((void *) parent, (void *) node, fname)) { |
364 | if (!tmpfs_link_node((void *) parent, (void *) node, fname)) { |
363 | tmpfs_destroy_node((void *) node); |
365 | tmpfs_destroy_node((void *) node); |
364 | free(fname); |
366 | free(fname); |
365 | return false; |
367 | return false; |
366 | } |
368 | } |
367 | free(fname); |
369 | free(fname); |
368 | 370 | ||
369 | if (!tmpfs_restore_recursion(phone, block, bufpos, buflen, pos, node)) |
371 | if (!tmpfs_restore_recursion(phone, block, bufpos, buflen, pos, node)) |
370 | return false; |
372 | return false; |
371 | 373 | ||
372 | break; |
374 | break; |
373 | default: |
375 | default: |
374 | return false; |
376 | return false; |
375 | } |
377 | } |
376 | } while (entry.type != 0); |
378 | } while (entry.type != 0); |
377 | 379 | ||
378 | return true; |
380 | return true; |
379 | } |
381 | } |
380 | 382 | ||
381 | static bool tmpfs_restore(dev_handle_t dev) |
383 | static bool tmpfs_restore(dev_handle_t dev) |
382 | { |
384 | { |
383 | void *block = mmap(NULL, BLOCK_SIZE, |
385 | void *block = mmap(NULL, BLOCK_SIZE, |
384 | PROTO_READ | PROTO_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, 0, 0); |
386 | PROTO_READ | PROTO_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, 0, 0); |
385 | 387 | ||
386 | if (block == NULL) |
388 | if (block == NULL) |
387 | return false; |
389 | return false; |
388 | 390 | ||
389 | int phone = ipc_connect_me_to(PHONE_NS, SERVICE_DEVMAP, DEVMAP_CONNECT_TO_DEVICE, dev); |
391 | int phone = ipc_connect_me_to(PHONE_NS, SERVICE_DEVMAP, DEVMAP_CONNECT_TO_DEVICE, dev); |
390 | 392 | ||
391 | if (phone < 0) { |
393 | if (phone < 0) { |
392 | munmap(block, BLOCK_SIZE); |
394 | munmap(block, BLOCK_SIZE); |
393 | return false; |
395 | return false; |
394 | } |
396 | } |
395 | 397 | ||
396 | if (ipc_share_out_start(phone, block, AS_AREA_READ | AS_AREA_WRITE) != EOK) |
398 | if (ipc_share_out_start(phone, block, AS_AREA_READ | AS_AREA_WRITE) != EOK) |
397 | goto error; |
399 | goto error; |
398 | 400 | ||
399 | size_t bufpos = 0; |
401 | size_t bufpos = 0; |
400 | size_t buflen = 0; |
402 | size_t buflen = 0; |
401 | size_t pos = 0; |
403 | size_t pos = 0; |
402 | 404 | ||
403 | char tag[6]; |
405 | char tag[6]; |
404 | if (!tmpfs_blockread(phone, block, &bufpos, &buflen, &pos, tag, 5)) |
406 | if (!tmpfs_blockread(phone, block, &bufpos, &buflen, &pos, tag, 5)) |
405 | goto error; |
407 | goto error; |
406 | 408 | ||
407 | tag[5] = 0; |
409 | tag[5] = 0; |
408 | if (strcmp(tag, "TMPFS") != 0) |
410 | if (strcmp(tag, "TMPFS") != 0) |
409 | goto error; |
411 | goto error; |
410 | 412 | ||
411 | if (!tmpfs_restore_recursion(phone, block, &bufpos, &buflen, &pos, root)) |
413 | if (!tmpfs_restore_recursion(phone, block, &bufpos, &buflen, &pos, root)) |
412 | goto error; |
414 | goto error; |
413 | 415 | ||
414 | ipc_hangup(phone); |
416 | ipc_hangup(phone); |
415 | munmap(block, BLOCK_SIZE); |
417 | munmap(block, BLOCK_SIZE); |
416 | return true; |
418 | return true; |
417 | 419 | ||
418 | error: |
420 | error: |
419 | ipc_hangup(phone); |
421 | ipc_hangup(phone); |
420 | munmap(block, BLOCK_SIZE); |
422 | munmap(block, BLOCK_SIZE); |
421 | return false; |
423 | return false; |
422 | } |
424 | } |
423 | 425 | ||
424 | /** Compare one component of path to a directory entry. |
426 | /** Compare one component of path to a directory entry. |
425 | * |
427 | * |
426 | * @param parentp Pointer to node from which we descended. |
428 | * @param parentp Pointer to node from which we descended. |
427 | * @param childp Pointer to node to compare the path component with. |
429 | * @param childp Pointer to node to compare the path component with. |
428 | * @param component Array of characters holding component name. |
430 | * @param component Array of characters holding component name. |
429 | * |
431 | * |
430 | * @return True on match, false otherwise. |
432 | * @return True on match, false otherwise. |
431 | */ |
433 | */ |
432 | static bool |
434 | static bool |
433 | tmpfs_match_one(tmpfs_dentry_t *parentp, tmpfs_dentry_t *childp, |
435 | tmpfs_match_one(tmpfs_dentry_t *parentp, tmpfs_dentry_t *childp, |
434 | const char *component) |
436 | const char *component) |
435 | { |
437 | { |
436 | unsigned long key = (unsigned long) parentp; |
438 | unsigned long key = (unsigned long) parentp; |
437 | link_t *hlp = hash_table_find(&childp->names, &key); |
439 | link_t *hlp = hash_table_find(&childp->names, &key); |
438 | assert(hlp); |
440 | assert(hlp); |
439 | tmpfs_name_t *namep = hash_table_get_instance(hlp, tmpfs_name_t, link); |
441 | tmpfs_name_t *namep = hash_table_get_instance(hlp, tmpfs_name_t, link); |
440 | return !strcmp(namep->name, component); |
442 | return !strcmp(namep->name, component); |
441 | } |
443 | } |
442 | 444 | ||
443 | void *tmpfs_match(void *prnt, const char *component) |
445 | void *tmpfs_match(void *prnt, const char *component) |
444 | { |
446 | { |
445 | tmpfs_dentry_t *parentp = (tmpfs_dentry_t *) prnt; |
447 | tmpfs_dentry_t *parentp = (tmpfs_dentry_t *) prnt; |
446 | tmpfs_dentry_t *childp = parentp->child; |
448 | tmpfs_dentry_t *childp = parentp->child; |
447 | 449 | ||
448 | while (childp && !tmpfs_match_one(parentp, childp, component)) |
450 | while (childp && !tmpfs_match_one(parentp, childp, component)) |
449 | childp = childp->sibling; |
451 | childp = childp->sibling; |
450 | 452 | ||
451 | return (void *) childp; |
453 | return (void *) childp; |
452 | } |
454 | } |
453 | 455 | ||
454 | void * |
456 | void * |
455 | tmpfs_node_get(dev_handle_t dev_handle, fs_index_t index) |
457 | tmpfs_node_get(dev_handle_t dev_handle, fs_index_t index) |
456 | { |
458 | { |
457 | unsigned long key = index; |
459 | unsigned long key = index; |
458 | link_t *lnk = hash_table_find(&dentries, &key); |
460 | link_t *lnk = hash_table_find(&dentries, &key); |
459 | if (!lnk) |
461 | if (!lnk) |
460 | return NULL; |
462 | return NULL; |
461 | return hash_table_get_instance(lnk, tmpfs_dentry_t, dh_link); |
463 | return hash_table_get_instance(lnk, tmpfs_dentry_t, dh_link); |
462 | } |
464 | } |
463 | 465 | ||
464 | void tmpfs_node_put(void *node) |
466 | void tmpfs_node_put(void *node) |
465 | { |
467 | { |
466 | /* nothing to do */ |
468 | /* nothing to do */ |
467 | } |
469 | } |
468 | 470 | ||
469 | void *tmpfs_create_node(int lflag) |
471 | void *tmpfs_create_node(int lflag) |
470 | { |
472 | { |
471 | assert((lflag & L_FILE) ^ (lflag & L_DIRECTORY)); |
473 | assert((lflag & L_FILE) ^ (lflag & L_DIRECTORY)); |
472 | 474 | ||
473 | tmpfs_dentry_t *node = malloc(sizeof(tmpfs_dentry_t)); |
475 | tmpfs_dentry_t *node = malloc(sizeof(tmpfs_dentry_t)); |
474 | if (!node) |
476 | if (!node) |
475 | return NULL; |
477 | return NULL; |
476 | 478 | ||
477 | if (!tmpfs_dentry_initialize(node)) { |
479 | if (!tmpfs_dentry_initialize(node)) { |
478 | free(node); |
480 | free(node); |
479 | return NULL; |
481 | return NULL; |
480 | } |
482 | } |
481 | node->index = tmpfs_next_index++; |
483 | node->index = tmpfs_next_index++; |
482 | if (lflag & L_DIRECTORY) |
484 | if (lflag & L_DIRECTORY) |
483 | node->type = TMPFS_DIRECTORY; |
485 | node->type = TMPFS_DIRECTORY; |
484 | else |
486 | else |
485 | node->type = TMPFS_FILE; |
487 | node->type = TMPFS_FILE; |
486 | 488 | ||
487 | /* Insert the new node into the dentry hash table. */ |
489 | /* Insert the new node into the dentry hash table. */ |
488 | unsigned long key = node->index; |
490 | unsigned long key = node->index; |
489 | hash_table_insert(&dentries, &key, &node->dh_link); |
491 | hash_table_insert(&dentries, &key, &node->dh_link); |
490 | return (void *) node; |
492 | return (void *) node; |
491 | } |
493 | } |
492 | 494 | ||
493 | bool tmpfs_link_node(void *prnt, void *chld, const char *nm) |
495 | bool tmpfs_link_node(void *prnt, void *chld, const char *nm) |
494 | { |
496 | { |
495 | tmpfs_dentry_t *parentp = (tmpfs_dentry_t *) prnt; |
497 | tmpfs_dentry_t *parentp = (tmpfs_dentry_t *) prnt; |
496 | tmpfs_dentry_t *childp = (tmpfs_dentry_t *) chld; |
498 | tmpfs_dentry_t *childp = (tmpfs_dentry_t *) chld; |
497 | 499 | ||
498 | assert(parentp->type == TMPFS_DIRECTORY); |
500 | assert(parentp->type == TMPFS_DIRECTORY); |
499 | 501 | ||
500 | tmpfs_name_t *namep = malloc(sizeof(tmpfs_name_t)); |
502 | tmpfs_name_t *namep = malloc(sizeof(tmpfs_name_t)); |
501 | if (!namep) |
503 | if (!namep) |
502 | return false; |
504 | return false; |
503 | tmpfs_name_initialize(namep); |
505 | tmpfs_name_initialize(namep); |
504 | size_t len = strlen(nm); |
506 | size_t len = strlen(nm); |
505 | namep->name = malloc(len + 1); |
507 | namep->name = malloc(len + 1); |
506 | if (!namep->name) { |
508 | if (!namep->name) { |
507 | free(namep); |
509 | free(namep); |
508 | return false; |
510 | return false; |
509 | } |
511 | } |
510 | strcpy(namep->name, nm); |
512 | strcpy(namep->name, nm); |
511 | namep->parent = parentp; |
513 | namep->parent = parentp; |
512 | 514 | ||
513 | childp->lnkcnt++; |
515 | childp->lnkcnt++; |
514 | 516 | ||
515 | unsigned long key = (unsigned long) parentp; |
517 | unsigned long key = (unsigned long) parentp; |
516 | hash_table_insert(&childp->names, &key, &namep->link); |
518 | hash_table_insert(&childp->names, &key, &namep->link); |
517 | 519 | ||
518 | /* Insert the new node into the namespace. */ |
520 | /* Insert the new node into the namespace. */ |
519 | if (parentp->child) { |
521 | if (parentp->child) { |
520 | tmpfs_dentry_t *tmp = parentp->child; |
522 | tmpfs_dentry_t *tmp = parentp->child; |
521 | while (tmp->sibling) |
523 | while (tmp->sibling) |
522 | tmp = tmp->sibling; |
524 | tmp = tmp->sibling; |
523 | tmp->sibling = childp; |
525 | tmp->sibling = childp; |
524 | } else { |
526 | } else { |
525 | parentp->child = childp; |
527 | parentp->child = childp; |
526 | } |
528 | } |
527 | 529 | ||
528 | return true; |
530 | return true; |
529 | } |
531 | } |
530 | 532 | ||
531 | int tmpfs_unlink_node(void *prnt, void *chld) |
533 | int tmpfs_unlink_node(void *prnt, void *chld) |
532 | { |
534 | { |
533 | tmpfs_dentry_t *parentp = (tmpfs_dentry_t *)prnt; |
535 | tmpfs_dentry_t *parentp = (tmpfs_dentry_t *)prnt; |
534 | tmpfs_dentry_t *childp = (tmpfs_dentry_t *)chld; |
536 | tmpfs_dentry_t *childp = (tmpfs_dentry_t *)chld; |
535 | 537 | ||
536 | if (!parentp) |
538 | if (!parentp) |
537 | return EBUSY; |
539 | return EBUSY; |
538 | 540 | ||
539 | if (childp->child) |
541 | if (childp->child) |
540 | return ENOTEMPTY; |
542 | return ENOTEMPTY; |
541 | 543 | ||
542 | if (parentp->child == childp) { |
544 | if (parentp->child == childp) { |
543 | parentp->child = childp->sibling; |
545 | parentp->child = childp->sibling; |
544 | } else { |
546 | } else { |
545 | /* TODO: consider doubly linked list for organizing siblings. */ |
547 | /* TODO: consider doubly linked list for organizing siblings. */ |
546 | tmpfs_dentry_t *tmp = parentp->child; |
548 | tmpfs_dentry_t *tmp = parentp->child; |
547 | while (tmp->sibling != childp) |
549 | while (tmp->sibling != childp) |
548 | tmp = tmp->sibling; |
550 | tmp = tmp->sibling; |
549 | tmp->sibling = childp->sibling; |
551 | tmp->sibling = childp->sibling; |
550 | } |
552 | } |
551 | childp->sibling = NULL; |
553 | childp->sibling = NULL; |
552 | 554 | ||
553 | unsigned long key = (unsigned long) parentp; |
555 | unsigned long key = (unsigned long) parentp; |
554 | hash_table_remove(&childp->names, &key, 1); |
556 | hash_table_remove(&childp->names, &key, 1); |
555 | 557 | ||
556 | childp->lnkcnt--; |
558 | childp->lnkcnt--; |
557 | 559 | ||
558 | return EOK; |
560 | return EOK; |
559 | } |
561 | } |
560 | 562 | ||
561 | int tmpfs_destroy_node(void *nodep) |
563 | int tmpfs_destroy_node(void *nodep) |
562 | { |
564 | { |
563 | tmpfs_dentry_t *dentry = (tmpfs_dentry_t *) nodep; |
565 | tmpfs_dentry_t *dentry = (tmpfs_dentry_t *) nodep; |
564 | 566 | ||
565 | assert(!dentry->lnkcnt); |
567 | assert(!dentry->lnkcnt); |
566 | assert(!dentry->child); |
568 | assert(!dentry->child); |
567 | assert(!dentry->sibling); |
569 | assert(!dentry->sibling); |
568 | 570 | ||
569 | unsigned long key = dentry->index; |
571 | unsigned long key = dentry->index; |
570 | hash_table_remove(&dentries, &key, 1); |
572 | hash_table_remove(&dentries, &key, 1); |
571 | 573 | ||
572 | hash_table_destroy(&dentry->names); |
574 | hash_table_destroy(&dentry->names); |
573 | 575 | ||
574 | if (dentry->type == TMPFS_FILE) |
576 | if (dentry->type == TMPFS_FILE) |
575 | free(dentry->data); |
577 | free(dentry->data); |
576 | free(dentry); |
578 | free(dentry); |
577 | return EOK; |
579 | return EOK; |
578 | } |
580 | } |
579 | 581 | ||
580 | void tmpfs_mount(ipc_callid_t rid, ipc_call_t *request) |
582 | void tmpfs_mount(ipc_callid_t rid, ipc_call_t *request) |
581 | { |
583 | { |
582 | dev_handle_t mr_dev_handle = (dev_handle_t) IPC_GET_ARG1(*request); |
584 | dev_handle_t mr_dev_handle = (dev_handle_t) IPC_GET_ARG1(*request); |
583 | fs_index_t mr_index = (fs_index_t) IPC_GET_ARG2(*request); |
585 | fs_index_t mr_index = (fs_index_t) IPC_GET_ARG2(*request); |
584 | fs_handle_t mp_fs_handle = (fs_handle_t) IPC_GET_ARG3(*request); |
586 | fs_handle_t mp_fs_handle = (fs_handle_t) IPC_GET_ARG3(*request); |
585 | dev_handle_t mp_dev_handle = (dev_handle_t) IPC_GET_ARG4(*request); |
587 | dev_handle_t mp_dev_handle = (dev_handle_t) IPC_GET_ARG4(*request); |
586 | fs_index_t mp_index = (fs_index_t) IPC_GET_ARG5(*request); |
588 | fs_index_t mp_index = (fs_index_t) IPC_GET_ARG5(*request); |
587 | 589 | ||
588 | if ((mr_index == root->index) && |
590 | if ((mr_index == root->index) && |
589 | (mp_fs_handle == tmpfs_reg.fs_handle) && |
591 | (mp_fs_handle == tmpfs_reg.fs_handle) && |
590 | (mp_index == mr_index)) { |
592 | (mp_index == mr_index)) { |
591 | 593 | ||
592 | if (mr_dev_handle >= 0) { |
594 | if (mr_dev_handle >= 0) { |
593 | if (tmpfs_restore(mr_dev_handle)) |
595 | if (tmpfs_restore(mr_dev_handle)) |
594 | ipc_answer_0(rid, EOK); |
596 | ipc_answer_0(rid, EOK); |
595 | else |
597 | else |
596 | ipc_answer_0(rid, ELIMIT); |
598 | ipc_answer_0(rid, ELIMIT); |
597 | } else |
599 | } else |
598 | ipc_answer_0(rid, EOK); |
600 | ipc_answer_0(rid, EOK); |
599 | } else |
601 | } else |
600 | ipc_answer_0(rid, ENOTSUP); |
602 | ipc_answer_0(rid, ENOTSUP); |
601 | } |
603 | } |
602 | 604 | ||
603 | void tmpfs_lookup(ipc_callid_t rid, ipc_call_t *request) |
605 | void tmpfs_lookup(ipc_callid_t rid, ipc_call_t *request) |
604 | { |
606 | { |
605 | /* Initialize TMPFS. */ |
607 | /* Initialize TMPFS. */ |
606 | if (!root && !tmpfs_init()) { |
608 | if (!root && !tmpfs_init()) { |
607 | ipc_answer_0(rid, ENOMEM); |
609 | ipc_answer_0(rid, ENOMEM); |
608 | return; |
610 | return; |
609 | } |
611 | } |
610 | libfs_lookup(&tmpfs_libfs_ops, tmpfs_reg.fs_handle, rid, request); |
612 | libfs_lookup(&tmpfs_libfs_ops, tmpfs_reg.fs_handle, rid, request); |
611 | } |
613 | } |
612 | 614 | ||
613 | void tmpfs_read(ipc_callid_t rid, ipc_call_t *request) |
615 | void tmpfs_read(ipc_callid_t rid, ipc_call_t *request) |
614 | { |
616 | { |
615 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
617 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
616 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); |
618 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); |
617 | off_t pos = (off_t)IPC_GET_ARG3(*request); |
619 | off_t pos = (off_t)IPC_GET_ARG3(*request); |
618 | 620 | ||
619 | /* |
621 | /* |
620 | * Lookup the respective dentry. |
622 | * Lookup the respective dentry. |
621 | */ |
623 | */ |
622 | link_t *hlp; |
624 | link_t *hlp; |
623 | unsigned long key = index; |
625 | unsigned long key = index; |
624 | hlp = hash_table_find(&dentries, &key); |
626 | hlp = hash_table_find(&dentries, &key); |
625 | if (!hlp) { |
627 | if (!hlp) { |
626 | ipc_answer_0(rid, ENOENT); |
628 | ipc_answer_0(rid, ENOENT); |
627 | return; |
629 | return; |
628 | } |
630 | } |
629 | tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t, |
631 | tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t, |
630 | dh_link); |
632 | dh_link); |
631 | 633 | ||
632 | /* |
634 | /* |
633 | * Receive the read request. |
635 | * Receive the read request. |
634 | */ |
636 | */ |
635 | ipc_callid_t callid; |
637 | ipc_callid_t callid; |
636 | size_t len; |
638 | size_t len; |
637 | if (!ipc_data_read_receive(&callid, &len)) { |
639 | if (!ipc_data_read_receive(&callid, &len)) { |
638 | ipc_answer_0(callid, EINVAL); |
640 | ipc_answer_0(callid, EINVAL); |
639 | ipc_answer_0(rid, EINVAL); |
641 | ipc_answer_0(rid, EINVAL); |
640 | return; |
642 | return; |
641 | } |
643 | } |
642 | 644 | ||
643 | size_t bytes; |
645 | size_t bytes; |
644 | if (dentry->type == TMPFS_FILE) { |
646 | if (dentry->type == TMPFS_FILE) { |
645 | bytes = max(0, min(dentry->size - pos, len)); |
647 | bytes = max(0, min(dentry->size - pos, len)); |
646 | (void) ipc_data_read_finalize(callid, dentry->data + pos, |
648 | (void) ipc_data_read_finalize(callid, dentry->data + pos, |
647 | bytes); |
649 | bytes); |
648 | } else { |
650 | } else { |
649 | int i; |
651 | int i; |
650 | tmpfs_dentry_t *cur; |
652 | tmpfs_dentry_t *cur; |
651 | 653 | ||
652 | assert(dentry->type == TMPFS_DIRECTORY); |
654 | assert(dentry->type == TMPFS_DIRECTORY); |
653 | 655 | ||
654 | /* |
656 | /* |
655 | * Yes, we really use O(n) algorithm here. |
657 | * Yes, we really use O(n) algorithm here. |
656 | * If it bothers someone, it could be fixed by introducing a |
658 | * If it bothers someone, it could be fixed by introducing a |
657 | * hash table. |
659 | * hash table. |
658 | */ |
660 | */ |
659 | for (i = 0, cur = dentry->child; i < pos && cur; i++, |
661 | for (i = 0, cur = dentry->child; i < pos && cur; i++, |
660 | cur = cur->sibling) |
662 | cur = cur->sibling) |
661 | ; |
663 | ; |
662 | 664 | ||
663 | if (!cur) { |
665 | if (!cur) { |
664 | ipc_answer_0(callid, ENOENT); |
666 | ipc_answer_0(callid, ENOENT); |
665 | ipc_answer_1(rid, ENOENT, 0); |
667 | ipc_answer_1(rid, ENOENT, 0); |
666 | return; |
668 | return; |
667 | } |
669 | } |
668 | 670 | ||
669 | unsigned long key = (unsigned long) dentry; |
671 | unsigned long key = (unsigned long) dentry; |
670 | link_t *hlp = hash_table_find(&cur->names, &key); |
672 | link_t *hlp = hash_table_find(&cur->names, &key); |
671 | assert(hlp); |
673 | assert(hlp); |
672 | tmpfs_name_t *namep = hash_table_get_instance(hlp, tmpfs_name_t, |
674 | tmpfs_name_t *namep = hash_table_get_instance(hlp, tmpfs_name_t, |
673 | link); |
675 | link); |
674 | 676 | ||
675 | (void) ipc_data_read_finalize(callid, namep->name, |
677 | (void) ipc_data_read_finalize(callid, namep->name, |
676 | strlen(namep->name) + 1); |
678 | strlen(namep->name) + 1); |
677 | bytes = 1; |
679 | bytes = 1; |
678 | } |
680 | } |
679 | 681 | ||
680 | /* |
682 | /* |
681 | * Answer the VFS_READ call. |
683 | * Answer the VFS_READ call. |
682 | */ |
684 | */ |
683 | ipc_answer_1(rid, EOK, bytes); |
685 | ipc_answer_1(rid, EOK, bytes); |
684 | } |
686 | } |
685 | 687 | ||
686 | void tmpfs_write(ipc_callid_t rid, ipc_call_t *request) |
688 | void tmpfs_write(ipc_callid_t rid, ipc_call_t *request) |
687 | { |
689 | { |
688 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
690 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
689 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); |
691 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); |
690 | off_t pos = (off_t)IPC_GET_ARG3(*request); |
692 | off_t pos = (off_t)IPC_GET_ARG3(*request); |
691 | 693 | ||
692 | /* |
694 | /* |
693 | * Lookup the respective dentry. |
695 | * Lookup the respective dentry. |
694 | */ |
696 | */ |
695 | link_t *hlp; |
697 | link_t *hlp; |
696 | unsigned long key = index; |
698 | unsigned long key = index; |
697 | hlp = hash_table_find(&dentries, &key); |
699 | hlp = hash_table_find(&dentries, &key); |
698 | if (!hlp) { |
700 | if (!hlp) { |
699 | ipc_answer_0(rid, ENOENT); |
701 | ipc_answer_0(rid, ENOENT); |
700 | return; |
702 | return; |
701 | } |
703 | } |
702 | tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t, |
704 | tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t, |
703 | dh_link); |
705 | dh_link); |
704 | 706 | ||
705 | /* |
707 | /* |
706 | * Receive the write request. |
708 | * Receive the write request. |
707 | */ |
709 | */ |
708 | ipc_callid_t callid; |
710 | ipc_callid_t callid; |
709 | size_t len; |
711 | size_t len; |
710 | if (!ipc_data_write_receive(&callid, &len)) { |
712 | if (!ipc_data_write_receive(&callid, &len)) { |
711 | ipc_answer_0(callid, EINVAL); |
713 | ipc_answer_0(callid, EINVAL); |
712 | ipc_answer_0(rid, EINVAL); |
714 | ipc_answer_0(rid, EINVAL); |
713 | return; |
715 | return; |
714 | } |
716 | } |
715 | 717 | ||
716 | /* |
718 | /* |
717 | * Check whether the file needs to grow. |
719 | * Check whether the file needs to grow. |
718 | */ |
720 | */ |
719 | if (pos + len <= dentry->size) { |
721 | if (pos + len <= dentry->size) { |
720 | /* The file size is not changing. */ |
722 | /* The file size is not changing. */ |
721 | (void) ipc_data_write_finalize(callid, dentry->data + pos, len); |
723 | (void) ipc_data_write_finalize(callid, dentry->data + pos, len); |
722 | ipc_answer_2(rid, EOK, len, dentry->size); |
724 | ipc_answer_2(rid, EOK, len, dentry->size); |
723 | return; |
725 | return; |
724 | } |
726 | } |
725 | size_t delta = (pos + len) - dentry->size; |
727 | size_t delta = (pos + len) - dentry->size; |
726 | /* |
728 | /* |
727 | * At this point, we are deliberately extremely straightforward and |
729 | * At this point, we are deliberately extremely straightforward and |
728 | * simply realloc the contents of the file on every write that grows the |
730 | * simply realloc the contents of the file on every write that grows the |
729 | * file. In the end, the situation might not be as bad as it may look: |
731 | * file. In the end, the situation might not be as bad as it may look: |
730 | * our heap allocator can save us and just grow the block whenever |
732 | * our heap allocator can save us and just grow the block whenever |
731 | * possible. |
733 | * possible. |
732 | */ |
734 | */ |
733 | void *newdata = realloc(dentry->data, dentry->size + delta); |
735 | void *newdata = realloc(dentry->data, dentry->size + delta); |
734 | if (!newdata) { |
736 | if (!newdata) { |
735 | ipc_answer_0(callid, ENOMEM); |
737 | ipc_answer_0(callid, ENOMEM); |
736 | ipc_answer_2(rid, EOK, 0, dentry->size); |
738 | ipc_answer_2(rid, EOK, 0, dentry->size); |
737 | return; |
739 | return; |
738 | } |
740 | } |
739 | /* Clear any newly allocated memory in order to emulate gaps. */ |
741 | /* Clear any newly allocated memory in order to emulate gaps. */ |
740 | memset(newdata + dentry->size, 0, delta); |
742 | memset(newdata + dentry->size, 0, delta); |
741 | dentry->size += delta; |
743 | dentry->size += delta; |
742 | dentry->data = newdata; |
744 | dentry->data = newdata; |
743 | (void) ipc_data_write_finalize(callid, dentry->data + pos, len); |
745 | (void) ipc_data_write_finalize(callid, dentry->data + pos, len); |
744 | ipc_answer_2(rid, EOK, len, dentry->size); |
746 | ipc_answer_2(rid, EOK, len, dentry->size); |
745 | } |
747 | } |
746 | 748 | ||
747 | void tmpfs_truncate(ipc_callid_t rid, ipc_call_t *request) |
749 | void tmpfs_truncate(ipc_callid_t rid, ipc_call_t *request) |
748 | { |
750 | { |
749 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
751 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
750 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); |
752 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); |
751 | size_t size = (off_t)IPC_GET_ARG3(*request); |
753 | size_t size = (off_t)IPC_GET_ARG3(*request); |
752 | 754 | ||
753 | /* |
755 | /* |
754 | * Lookup the respective dentry. |
756 | * Lookup the respective dentry. |
755 | */ |
757 | */ |
756 | link_t *hlp; |
758 | link_t *hlp; |
757 | unsigned long key = index; |
759 | unsigned long key = index; |
758 | hlp = hash_table_find(&dentries, &key); |
760 | hlp = hash_table_find(&dentries, &key); |
759 | if (!hlp) { |
761 | if (!hlp) { |
760 | ipc_answer_0(rid, ENOENT); |
762 | ipc_answer_0(rid, ENOENT); |
761 | return; |
763 | return; |
762 | } |
764 | } |
763 | tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t, |
765 | tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t, |
764 | dh_link); |
766 | dh_link); |
765 | 767 | ||
766 | if (size == dentry->size) { |
768 | if (size == dentry->size) { |
767 | ipc_answer_0(rid, EOK); |
769 | ipc_answer_0(rid, EOK); |
768 | return; |
770 | return; |
769 | } |
771 | } |
770 | 772 | ||
771 | void *newdata = realloc(dentry->data, size); |
773 | void *newdata = realloc(dentry->data, size); |
772 | if (!newdata) { |
774 | if (!newdata) { |
773 | ipc_answer_0(rid, ENOMEM); |
775 | ipc_answer_0(rid, ENOMEM); |
774 | return; |
776 | return; |
775 | } |
777 | } |
776 | if (size > dentry->size) { |
778 | if (size > dentry->size) { |
777 | size_t delta = size - dentry->size; |
779 | size_t delta = size - dentry->size; |
778 | memset(newdata + dentry->size, 0, delta); |
780 | memset(newdata + dentry->size, 0, delta); |
779 | } |
781 | } |
780 | dentry->size = size; |
782 | dentry->size = size; |
781 | dentry->data = newdata; |
783 | dentry->data = newdata; |
782 | ipc_answer_0(rid, EOK); |
784 | ipc_answer_0(rid, EOK); |
783 | } |
785 | } |
784 | 786 | ||
785 | void tmpfs_destroy(ipc_callid_t rid, ipc_call_t *request) |
787 | void tmpfs_destroy(ipc_callid_t rid, ipc_call_t *request) |
786 | { |
788 | { |
787 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
789 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
788 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); |
790 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); |
789 | int rc; |
791 | int rc; |
790 | 792 | ||
791 | link_t *hlp; |
793 | link_t *hlp; |
792 | unsigned long key = index; |
794 | unsigned long key = index; |
793 | hlp = hash_table_find(&dentries, &key); |
795 | hlp = hash_table_find(&dentries, &key); |
794 | if (!hlp) { |
796 | if (!hlp) { |
795 | ipc_answer_0(rid, ENOENT); |
797 | ipc_answer_0(rid, ENOENT); |
796 | return; |
798 | return; |
797 | } |
799 | } |
798 | tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t, |
800 | tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t, |
799 | dh_link); |
801 | dh_link); |
800 | rc = tmpfs_destroy_node(dentry); |
802 | rc = tmpfs_destroy_node(dentry); |
801 | ipc_answer_0(rid, rc); |
803 | ipc_answer_0(rid, rc); |
802 | } |
804 | } |
803 | 805 | ||
804 | /** |
806 | /** |
805 | * @} |
807 | * @} |
806 | */ |
808 | */ |
807 | 809 |