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