Rev 2730 | Rev 2732 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 2730 | Rev 2731 | ||
---|---|---|---|
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 <dirent.h> |
48 | #include <dirent.h> |
49 | #include <assert.h> |
49 | #include <assert.h> |
50 | #include <sys/types.h> |
50 | #include <sys/types.h> |
51 | #include <libadt/hash_table.h> |
51 | #include <libadt/hash_table.h> |
52 | #include <as.h> |
52 | #include <as.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 PLB_GET_CHAR(i) (tmpfs_reg.plb_ro[(i) % PLB_SIZE]) |
57 | #define PLB_GET_CHAR(i) (tmpfs_reg.plb_ro[(i) % PLB_SIZE]) |
58 | 58 | ||
59 | #define DENTRIES_BUCKETS 256 |
59 | #define DENTRIES_BUCKETS 256 |
60 | 60 | ||
61 | #define TMPFS_GET_INDEX(x) (((tmpfs_dentry_t *)(x))->index) |
61 | #define TMPFS_GET_INDEX(x) (((tmpfs_dentry_t *)(x))->index) |
62 | #define TMPFS_GET_LNKCNT(x) 1 |
62 | #define TMPFS_GET_LNKCNT(x) 1 |
63 | 63 | ||
64 | /* |
64 | /* |
65 | * Hash table of all directory entries. |
65 | * Hash table of all directory entries. |
66 | */ |
66 | */ |
67 | hash_table_t dentries; |
67 | hash_table_t dentries; |
68 | 68 | ||
69 | static hash_index_t dentries_hash(unsigned long *key) |
69 | static hash_index_t dentries_hash(unsigned long *key) |
70 | { |
70 | { |
71 | return *key % DENTRIES_BUCKETS; |
71 | return *key % DENTRIES_BUCKETS; |
72 | } |
72 | } |
73 | 73 | ||
74 | static int dentries_compare(unsigned long *key, hash_count_t keys, |
74 | static int dentries_compare(unsigned long *key, hash_count_t keys, |
75 | link_t *item) |
75 | link_t *item) |
76 | { |
76 | { |
77 | tmpfs_dentry_t *dentry = hash_table_get_instance(item, tmpfs_dentry_t, |
77 | tmpfs_dentry_t *dentry = hash_table_get_instance(item, tmpfs_dentry_t, |
78 | dh_link); |
78 | dh_link); |
79 | return dentry->index == *key; |
79 | return dentry->index == *key; |
80 | } |
80 | } |
81 | 81 | ||
82 | static void dentries_remove_callback(link_t *item) |
82 | static void dentries_remove_callback(link_t *item) |
83 | { |
83 | { |
84 | } |
84 | } |
85 | 85 | ||
86 | /** TMPFS dentries hash table operations. */ |
86 | /** TMPFS dentries hash table operations. */ |
87 | hash_table_operations_t dentries_ops = { |
87 | hash_table_operations_t dentries_ops = { |
88 | .hash = dentries_hash, |
88 | .hash = dentries_hash, |
89 | .compare = dentries_compare, |
89 | .compare = dentries_compare, |
90 | .remove_callback = dentries_remove_callback |
90 | .remove_callback = dentries_remove_callback |
91 | }; |
91 | }; |
92 | 92 | ||
93 | unsigned tmpfs_next_index = 1; |
93 | unsigned tmpfs_next_index = 1; |
94 | 94 | ||
95 | static void tmpfs_dentry_initialize(tmpfs_dentry_t *dentry) |
95 | static void tmpfs_dentry_initialize(tmpfs_dentry_t *dentry) |
96 | { |
96 | { |
97 | dentry->index = 0; |
97 | dentry->index = 0; |
98 | dentry->parent = NULL; |
98 | dentry->parent = NULL; |
99 | dentry->sibling = NULL; |
99 | dentry->sibling = NULL; |
100 | dentry->child = NULL; |
100 | dentry->child = NULL; |
101 | dentry->name = NULL; |
101 | dentry->name = NULL; |
102 | dentry->type = TMPFS_NONE; |
102 | dentry->type = TMPFS_NONE; |
103 | dentry->size = 0; |
103 | dentry->size = 0; |
104 | dentry->data = NULL; |
104 | dentry->data = NULL; |
105 | link_initialize(&dentry->dh_link); |
105 | link_initialize(&dentry->dh_link); |
106 | } |
106 | } |
107 | 107 | ||
108 | /* |
108 | /* |
109 | * For now, we don't distinguish between different dev_handles/instances. All |
109 | * For now, we don't distinguish between different dev_handles/instances. All |
110 | * requests resolve to the only instance, rooted in the following variable. |
110 | * requests resolve to the only instance, rooted in the following variable. |
111 | */ |
111 | */ |
112 | static tmpfs_dentry_t *root; |
112 | static tmpfs_dentry_t *root; |
113 | 113 | ||
114 | static bool tmpfs_init(void) |
114 | static bool tmpfs_init(void) |
115 | { |
115 | { |
116 | if (!hash_table_create(&dentries, DENTRIES_BUCKETS, 1, &dentries_ops)) |
116 | if (!hash_table_create(&dentries, DENTRIES_BUCKETS, 1, &dentries_ops)) |
117 | return false; |
117 | return false; |
118 | 118 | ||
119 | root = (tmpfs_dentry_t *) malloc(sizeof(tmpfs_dentry_t)); |
119 | root = (tmpfs_dentry_t *) malloc(sizeof(tmpfs_dentry_t)); |
120 | if (!root) |
120 | if (!root) |
121 | return false; |
121 | return false; |
122 | tmpfs_dentry_initialize(root); |
122 | tmpfs_dentry_initialize(root); |
123 | root->index = tmpfs_next_index++; |
123 | root->index = tmpfs_next_index++; |
124 | root->name = ""; |
124 | root->name = ""; |
125 | root->type = TMPFS_DIRECTORY; |
125 | root->type = TMPFS_DIRECTORY; |
126 | hash_table_insert(&dentries, &root->index, &root->dh_link); |
126 | hash_table_insert(&dentries, &root->index, &root->dh_link); |
127 | 127 | ||
128 | return true; |
128 | return true; |
129 | } |
129 | } |
130 | 130 | ||
131 | /** Compare one component of path to a directory entry. |
131 | /** Compare one component of path to a directory entry. |
132 | * |
132 | * |
133 | * @param nodep Node to compare the path component with. |
133 | * @param nodep Node to compare the path component with. |
134 | * @param component Array of characters holding component name. |
134 | * @param component Array of characters holding component name. |
135 | * |
135 | * |
136 | * @return True on match, false otherwise. |
136 | * @return True on match, false otherwise. |
137 | */ |
137 | */ |
138 | static bool match_component(void *nodep, const char *component) |
138 | static bool match_component(void *nodep, const char *component) |
139 | { |
139 | { |
140 | tmpfs_dentry_t *dentry = (tmpfs_dentry_t *) nodep; |
140 | tmpfs_dentry_t *dentry = (tmpfs_dentry_t *) nodep; |
141 | 141 | ||
142 | return !strcmp(dentry->name, component); |
142 | return !strcmp(dentry->name, component); |
143 | } |
143 | } |
144 | 144 | ||
145 | static void *create_node(void *nodep, |
145 | static void *create_node(void *nodep, |
146 | const char *component, int lflag) |
146 | const char *component, int lflag) |
147 | { |
147 | { |
148 | tmpfs_dentry_t *dentry = (tmpfs_dentry_t *) nodep; |
148 | tmpfs_dentry_t *dentry = (tmpfs_dentry_t *) nodep; |
149 | 149 | ||
150 | assert(dentry->type == TMPFS_DIRECTORY); |
150 | assert(dentry->type == TMPFS_DIRECTORY); |
151 | assert((lflag & L_FILE) ^ (lflag & L_DIRECTORY)); |
151 | assert((lflag & L_FILE) ^ (lflag & L_DIRECTORY)); |
152 | 152 | ||
153 | tmpfs_dentry_t *node = malloc(sizeof(tmpfs_dentry_t)); |
153 | tmpfs_dentry_t *node = malloc(sizeof(tmpfs_dentry_t)); |
154 | if (!node) |
154 | if (!node) |
155 | return NULL; |
155 | return NULL; |
156 | size_t len = strlen(component); |
156 | size_t len = strlen(component); |
157 | char *name = malloc(len + 1); |
157 | char *name = malloc(len + 1); |
158 | if (!name) { |
158 | if (!name) { |
159 | free(node); |
159 | free(node); |
160 | return NULL; |
160 | return NULL; |
161 | } |
161 | } |
162 | strcpy(name, component); |
162 | strcpy(name, component); |
163 | 163 | ||
164 | tmpfs_dentry_initialize(node); |
164 | tmpfs_dentry_initialize(node); |
165 | node->index = tmpfs_next_index++; |
165 | node->index = tmpfs_next_index++; |
166 | node->name = name; |
166 | node->name = name; |
167 | node->parent = dentry; |
167 | node->parent = dentry; |
168 | if (lflag & L_DIRECTORY) |
168 | if (lflag & L_DIRECTORY) |
169 | node->type = TMPFS_DIRECTORY; |
169 | node->type = TMPFS_DIRECTORY; |
170 | else |
170 | else |
171 | node->type = TMPFS_FILE; |
171 | node->type = TMPFS_FILE; |
172 | 172 | ||
173 | /* Insert the new node into the namespace. */ |
173 | /* Insert the new node into the namespace. */ |
174 | if (dentry->child) { |
174 | if (dentry->child) { |
175 | tmpfs_dentry_t *tmp = dentry->child; |
175 | tmpfs_dentry_t *tmp = dentry->child; |
176 | while (tmp->sibling) |
176 | while (tmp->sibling) |
177 | tmp = tmp->sibling; |
177 | tmp = tmp->sibling; |
178 | tmp->sibling = node; |
178 | tmp->sibling = node; |
179 | } else { |
179 | } else { |
180 | dentry->child = node; |
180 | dentry->child = node; |
181 | } |
181 | } |
182 | 182 | ||
183 | /* Insert the new node into the dentry hash table. */ |
183 | /* Insert the new node into the dentry hash table. */ |
184 | hash_table_insert(&dentries, &node->index, &node->dh_link); |
184 | hash_table_insert(&dentries, &node->index, &node->dh_link); |
185 | return (void *) node; |
185 | return (void *) node; |
186 | } |
186 | } |
187 | 187 | ||
188 | static int destroy_component(void *nodeptr) |
188 | static int destroy_component(void *nodeptr) |
189 | { |
189 | { |
190 | return EPERM; |
190 | return EPERM; |
191 | } |
191 | } |
192 | 192 | ||
193 | void tmpfs_lookup(ipc_callid_t rid, ipc_call_t *request) |
193 | void tmpfs_lookup(ipc_callid_t rid, ipc_call_t *request) |
194 | { |
194 | { |
195 | unsigned next = IPC_GET_ARG1(*request); |
195 | unsigned next = IPC_GET_ARG1(*request); |
196 | unsigned last = IPC_GET_ARG2(*request); |
196 | unsigned last = IPC_GET_ARG2(*request); |
197 | int dev_handle = IPC_GET_ARG3(*request); |
197 | int dev_handle = IPC_GET_ARG3(*request); |
198 | int lflag = IPC_GET_ARG4(*request); |
198 | int lflag = IPC_GET_ARG4(*request); |
199 | 199 | ||
200 | if (last < next) |
200 | if (last < next) |
201 | last += PLB_SIZE; |
201 | last += PLB_SIZE; |
202 | 202 | ||
203 | /* |
203 | /* |
204 | * Initialize TMPFS. |
204 | * Initialize TMPFS. |
205 | */ |
205 | */ |
206 | if (!root && !tmpfs_init()) { |
206 | if (!root && !tmpfs_init()) { |
207 | ipc_answer_0(rid, ENOMEM); |
207 | ipc_answer_0(rid, ENOMEM); |
208 | return; |
208 | return; |
209 | } |
209 | } |
210 | 210 | ||
211 | tmpfs_dentry_t *dtmp = root->child; |
211 | tmpfs_dentry_t *dtmp = root->child; |
212 | tmpfs_dentry_t *dcur = root; |
212 | tmpfs_dentry_t *dcur = root; |
213 | 213 | ||
214 | if (PLB_GET_CHAR(next) == '/') |
214 | if (PLB_GET_CHAR(next) == '/') |
215 | next++; /* eat slash */ |
215 | next++; /* eat slash */ |
216 | 216 | ||
217 | char component[NAME_MAX + 1]; |
217 | char component[NAME_MAX + 1]; |
218 | int len = 0; |
218 | int len = 0; |
219 | while (dtmp && next <= last) { |
219 | while (dtmp && next <= last) { |
220 | 220 | ||
221 | /* collect the component */ |
221 | /* collect the component */ |
222 | if (PLB_GET_CHAR(next) != '/') { |
222 | if (PLB_GET_CHAR(next) != '/') { |
223 | if (len + 1 == NAME_MAX) { |
223 | if (len + 1 == NAME_MAX) { |
224 | /* comopnent length overflow */ |
224 | /* comopnent length overflow */ |
225 | ipc_answer_0(rid, ENAMETOOLONG); |
225 | ipc_answer_0(rid, ENAMETOOLONG); |
226 | return; |
226 | return; |
227 | } |
227 | } |
228 | component[len++] = PLB_GET_CHAR(next); |
228 | component[len++] = PLB_GET_CHAR(next); |
229 | next++; /* process next character */ |
229 | next++; /* process next character */ |
230 | if (next <= last) |
230 | if (next <= last) |
231 | continue; |
231 | continue; |
232 | } |
232 | } |
233 | 233 | ||
234 | assert(len); |
234 | assert(len); |
235 | component[len] = '\0'; |
235 | component[len] = '\0'; |
236 | next++; /* eat slash */ |
236 | next++; /* eat slash */ |
237 | len = 0; |
237 | len = 0; |
238 | 238 | ||
239 | /* match the component */ |
239 | /* match the component */ |
240 | while (dtmp && !match_component(dtmp, component)) |
240 | while (dtmp && !match_component(dtmp, component)) |
241 | dtmp = dtmp->sibling; |
241 | dtmp = dtmp->sibling; |
242 | 242 | ||
243 | /* handle miss: match amongst siblings */ |
243 | /* handle miss: match amongst siblings */ |
244 | if (!dtmp) { |
244 | if (!dtmp) { |
245 | if ((next > last) && (lflag & L_CREATE)) { |
245 | if ((next > last) && (lflag & L_CREATE)) { |
246 | /* no components left and L_CREATE specified */ |
246 | /* no components left and L_CREATE specified */ |
247 | if (dcur->type != TMPFS_DIRECTORY) { |
247 | if (dcur->type != TMPFS_DIRECTORY) { |
248 | ipc_answer_0(rid, ENOTDIR); |
248 | ipc_answer_0(rid, ENOTDIR); |
249 | return; |
249 | return; |
250 | } |
250 | } |
251 | void *nodep = create_node(dcur, |
251 | void *nodep = create_node(dcur, |
252 | component, lflag); |
252 | component, lflag); |
253 | if (nodep) { |
253 | if (nodep) { |
254 | ipc_answer_5(rid, EOK, |
254 | ipc_answer_5(rid, EOK, |
255 | tmpfs_reg.fs_handle, dev_handle, |
255 | tmpfs_reg.fs_handle, dev_handle, |
256 | TMPFS_GET_INDEX(nodep), 0, |
256 | TMPFS_GET_INDEX(nodep), 0, |
257 | TMPFS_GET_LNKCNT(nodep)); |
257 | TMPFS_GET_LNKCNT(nodep)); |
258 | } else { |
258 | } else { |
259 | ipc_answer_0(rid, ENOSPC); |
259 | ipc_answer_0(rid, ENOSPC); |
260 | } |
260 | } |
261 | return; |
261 | return; |
262 | } |
262 | } |
263 | ipc_answer_0(rid, ENOENT); |
263 | ipc_answer_0(rid, ENOENT); |
264 | return; |
264 | return; |
265 | } |
265 | } |
266 | 266 | ||
267 | /* descend one level */ |
267 | /* descend one level */ |
268 | dcur = dtmp; |
268 | dcur = dtmp; |
269 | dtmp = dtmp->child; |
269 | dtmp = dtmp->child; |
270 | } |
270 | } |
271 | 271 | ||
272 | /* handle miss: excessive components */ |
272 | /* handle miss: excessive components */ |
273 | if (!dtmp && next <= last) { |
273 | if (!dtmp && next <= last) { |
274 | if (lflag & L_CREATE) { |
274 | if (lflag & L_CREATE) { |
275 | if (dcur->type != TMPFS_DIRECTORY) { |
275 | if (dcur->type != TMPFS_DIRECTORY) { |
276 | ipc_answer_0(rid, ENOTDIR); |
276 | ipc_answer_0(rid, ENOTDIR); |
277 | return; |
277 | return; |
278 | } |
278 | } |
279 | 279 | ||
280 | /* collect next component */ |
280 | /* collect next component */ |
281 | while (next <= last) { |
281 | while (next <= last) { |
282 | if (PLB_GET_CHAR(next) == '/') { |
282 | if (PLB_GET_CHAR(next) == '/') { |
283 | /* more than one component */ |
283 | /* more than one component */ |
284 | ipc_answer_0(rid, ENOENT); |
284 | ipc_answer_0(rid, ENOENT); |
285 | return; |
285 | return; |
286 | } |
286 | } |
287 | if (len + 1 == NAME_MAX) { |
287 | if (len + 1 == NAME_MAX) { |
288 | /* component length overflow */ |
288 | /* component length overflow */ |
289 | ipc_answer_0(rid, ENAMETOOLONG); |
289 | ipc_answer_0(rid, ENAMETOOLONG); |
290 | return; |
290 | return; |
291 | } |
291 | } |
292 | component[len++] = PLB_GET_CHAR(next); |
292 | component[len++] = PLB_GET_CHAR(next); |
293 | next++; /* process next character */ |
293 | next++; /* process next character */ |
294 | } |
294 | } |
295 | assert(len); |
295 | assert(len); |
296 | component[len] = '\0'; |
296 | component[len] = '\0'; |
297 | len = 0; |
297 | len = 0; |
298 | 298 | ||
299 | void *nodep = create_node(dcur, component, lflag); |
299 | void *nodep = create_node(dcur, component, lflag); |
300 | if (nodep) { |
300 | if (nodep) { |
301 | ipc_answer_5(rid, EOK, tmpfs_reg.fs_handle, |
301 | ipc_answer_5(rid, EOK, tmpfs_reg.fs_handle, |
302 | dev_handle, TMPFS_GET_INDEX(nodep), 0, |
302 | dev_handle, TMPFS_GET_INDEX(nodep), 0, |
303 | TMPFS_GET_LNKCNT(nodep)); |
303 | TMPFS_GET_LNKCNT(nodep)); |
304 | } else { |
304 | } else { |
305 | ipc_answer_0(rid, ENOSPC); |
305 | ipc_answer_0(rid, ENOSPC); |
306 | } |
306 | } |
307 | return; |
307 | return; |
308 | } |
308 | } |
309 | ipc_answer_0(rid, ENOENT); |
309 | ipc_answer_0(rid, ENOENT); |
310 | return; |
310 | return; |
311 | } |
311 | } |
312 | 312 | ||
313 | /* handle hit */ |
313 | /* handle hit */ |
314 | if (lflag & L_DESTROY) { |
314 | if (lflag & L_DESTROY) { |
315 | int res = destroy_component(dcur); |
315 | int res = destroy_component(dcur); |
316 | ipc_answer_0(rid, res); |
316 | ipc_answer_0(rid, res); |
317 | return; |
317 | return; |
318 | } |
318 | } |
319 | if ((lflag & (L_CREATE | L_EXCLUSIVE)) == (L_CREATE | L_EXCLUSIVE)) { |
319 | if ((lflag & (L_CREATE | L_EXCLUSIVE)) == (L_CREATE | L_EXCLUSIVE)) { |
320 | ipc_answer_0(rid, EEXIST); |
320 | ipc_answer_0(rid, EEXIST); |
321 | return; |
321 | return; |
322 | } |
322 | } |
323 | if ((lflag & L_FILE) && (dcur->type != TMPFS_FILE)) { |
323 | if ((lflag & L_FILE) && (dcur->type != TMPFS_FILE)) { |
324 | ipc_answer_0(rid, EISDIR); |
324 | ipc_answer_0(rid, EISDIR); |
325 | return; |
325 | return; |
326 | } |
326 | } |
327 | if ((lflag & L_DIRECTORY) && (dcur->type != TMPFS_DIRECTORY)) { |
327 | if ((lflag & L_DIRECTORY) && (dcur->type != TMPFS_DIRECTORY)) { |
328 | ipc_answer_0(rid, ENOTDIR); |
328 | ipc_answer_0(rid, ENOTDIR); |
329 | return; |
329 | return; |
330 | } |
330 | } |
331 | 331 | ||
332 | ipc_answer_5(rid, EOK, tmpfs_reg.fs_handle, dev_handle, dcur->index, |
332 | ipc_answer_5(rid, EOK, tmpfs_reg.fs_handle, dev_handle, dcur->index, |
333 | dcur->size, TMPFS_GET_LNKCNT(dcur)); |
333 | dcur->size, TMPFS_GET_LNKCNT(dcur)); |
334 | } |
334 | } |
335 | 335 | ||
336 | void tmpfs_read(ipc_callid_t rid, ipc_call_t *request) |
336 | void tmpfs_read(ipc_callid_t rid, ipc_call_t *request) |
337 | { |
337 | { |
338 | int dev_handle = IPC_GET_ARG1(*request); |
338 | int dev_handle = IPC_GET_ARG1(*request); |
339 | unsigned long index = IPC_GET_ARG2(*request); |
339 | unsigned long index = IPC_GET_ARG2(*request); |
340 | off_t pos = IPC_GET_ARG3(*request); |
340 | off_t pos = IPC_GET_ARG3(*request); |
341 | 341 | ||
342 | /* |
342 | /* |
343 | * Lookup the respective dentry. |
343 | * Lookup the respective dentry. |
344 | */ |
344 | */ |
345 | link_t *hlp; |
345 | link_t *hlp; |
346 | hlp = hash_table_find(&dentries, &index); |
346 | hlp = hash_table_find(&dentries, &index); |
347 | if (!hlp) { |
347 | if (!hlp) { |
348 | ipc_answer_0(rid, ENOENT); |
348 | ipc_answer_0(rid, ENOENT); |
349 | return; |
349 | return; |
350 | } |
350 | } |
351 | tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t, |
351 | tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t, |
352 | dh_link); |
352 | dh_link); |
353 | 353 | ||
354 | /* |
354 | /* |
355 | * Receive the read request. |
355 | * Receive the read request. |
356 | */ |
356 | */ |
357 | ipc_callid_t callid; |
357 | ipc_callid_t callid; |
358 | size_t len; |
358 | size_t len; |
359 | if (!ipc_data_read_receive(&callid, &len)) { |
359 | if (!ipc_data_read_receive(&callid, &len)) { |
360 | ipc_answer_0(callid, EINVAL); |
360 | ipc_answer_0(callid, EINVAL); |
361 | ipc_answer_0(rid, EINVAL); |
361 | ipc_answer_0(rid, EINVAL); |
362 | return; |
362 | return; |
363 | } |
363 | } |
364 | 364 | ||
365 | size_t bytes; |
365 | size_t bytes; |
366 | if (dentry->type == TMPFS_FILE) { |
366 | if (dentry->type == TMPFS_FILE) { |
367 | bytes = max(0, min(dentry->size - pos, len)); |
367 | bytes = max(0, min(dentry->size - pos, len)); |
368 | (void) ipc_data_read_finalize(callid, dentry->data + pos, |
368 | (void) ipc_data_read_finalize(callid, dentry->data + pos, |
369 | bytes); |
369 | bytes); |
370 | } else { |
370 | } else { |
371 | int i; |
371 | int i; |
372 | tmpfs_dentry_t *cur = dentry->child; |
372 | tmpfs_dentry_t *cur = dentry->child; |
373 | 373 | ||
374 | assert(dentry->type == TMPFS_DIRECTORY); |
374 | assert(dentry->type == TMPFS_DIRECTORY); |
375 | 375 | ||
376 | /* |
376 | /* |
377 | * Yes, we really use O(n) algorithm here. |
377 | * Yes, we really use O(n) algorithm here. |
378 | * If it bothers someone, it could be fixed by introducing a |
378 | * If it bothers someone, it could be fixed by introducing a |
379 | * hash table. |
379 | * hash table. |
380 | */ |
380 | */ |
381 | for (i = 0, cur = dentry->child; i < pos && cur; i++, |
381 | for (i = 0, cur = dentry->child; i < pos && cur; i++, |
382 | cur = cur->sibling) |
382 | cur = cur->sibling) |
383 | ; |
383 | ; |
384 | 384 | ||
385 | if (!cur) { |
385 | if (!cur) { |
386 | ipc_answer_0(callid, ENOENT); |
386 | ipc_answer_0(callid, ENOENT); |
387 | ipc_answer_1(rid, ENOENT, 0); |
387 | ipc_answer_1(rid, ENOENT, 0); |
388 | return; |
388 | return; |
389 | } |
389 | } |
390 | 390 | ||
391 | (void) ipc_data_read_finalize(callid, cur->name, |
391 | (void) ipc_data_read_finalize(callid, cur->name, |
392 | strlen(cur->name) + 1); |
392 | strlen(cur->name) + 1); |
393 | bytes = 1; |
393 | bytes = 1; |
394 | } |
394 | } |
395 | 395 | ||
396 | /* |
396 | /* |
397 | * Answer the VFS_READ call. |
397 | * Answer the VFS_READ call. |
398 | */ |
398 | */ |
399 | ipc_answer_1(rid, EOK, bytes); |
399 | ipc_answer_1(rid, EOK, bytes); |
400 | } |
400 | } |
401 | 401 | ||
402 | void tmpfs_write(ipc_callid_t rid, ipc_call_t *request) |
402 | void tmpfs_write(ipc_callid_t rid, ipc_call_t *request) |
403 | { |
403 | { |
404 | int dev_handle = IPC_GET_ARG1(*request); |
404 | int dev_handle = IPC_GET_ARG1(*request); |
405 | unsigned long index = IPC_GET_ARG2(*request); |
405 | unsigned long index = IPC_GET_ARG2(*request); |
406 | off_t pos = IPC_GET_ARG3(*request); |
406 | off_t pos = IPC_GET_ARG3(*request); |
407 | 407 | ||
408 | /* |
408 | /* |
409 | * Lookup the respective dentry. |
409 | * Lookup the respective dentry. |
410 | */ |
410 | */ |
411 | link_t *hlp; |
411 | link_t *hlp; |
412 | hlp = hash_table_find(&dentries, &index); |
412 | hlp = hash_table_find(&dentries, &index); |
413 | if (!hlp) { |
413 | if (!hlp) { |
414 | ipc_answer_0(rid, ENOENT); |
414 | ipc_answer_0(rid, ENOENT); |
415 | return; |
415 | return; |
416 | } |
416 | } |
417 | tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t, |
417 | tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t, |
418 | dh_link); |
418 | dh_link); |
419 | 419 | ||
420 | /* |
420 | /* |
421 | * Receive the write request. |
421 | * Receive the write request. |
422 | */ |
422 | */ |
423 | ipc_callid_t callid; |
423 | ipc_callid_t callid; |
424 | size_t len; |
424 | size_t len; |
425 | if (!ipc_data_write_receive(&callid, &len)) { |
425 | if (!ipc_data_write_receive(&callid, &len)) { |
426 | ipc_answer_0(callid, EINVAL); |
426 | ipc_answer_0(callid, EINVAL); |
427 | ipc_answer_0(rid, EINVAL); |
427 | ipc_answer_0(rid, EINVAL); |
428 | return; |
428 | return; |
429 | } |
429 | } |
430 | 430 | ||
431 | /* |
431 | /* |
432 | * Check whether the file needs to grow. |
432 | * Check whether the file needs to grow. |
433 | */ |
433 | */ |
434 | if (pos + len <= dentry->size) { |
434 | if (pos + len <= dentry->size) { |
435 | /* The file size is not changing. */ |
435 | /* The file size is not changing. */ |
436 | (void) ipc_data_write_finalize(callid, dentry->data + pos, len); |
436 | (void) ipc_data_write_finalize(callid, dentry->data + pos, len); |
437 | ipc_answer_2(rid, EOK, len, dentry->size); |
437 | ipc_answer_2(rid, EOK, len, dentry->size); |
438 | return; |
438 | return; |
439 | } |
439 | } |
440 | size_t delta = (pos + len) - dentry->size; |
440 | size_t delta = (pos + len) - dentry->size; |
441 | /* |
441 | /* |
442 | * At this point, we are deliberately extremely straightforward and |
442 | * At this point, we are deliberately extremely straightforward and |
443 | * simply realloc the contents of the file on every write that grows the |
443 | * simply realloc the contents of the file on every write that grows the |
444 | * file. In the end, the situation might not be as bad as it may look: |
444 | * file. In the end, the situation might not be as bad as it may look: |
445 | * our heap allocator can save us and just grow the block whenever |
445 | * our heap allocator can save us and just grow the block whenever |
446 | * possible. |
446 | * possible. |
447 | */ |
447 | */ |
448 | void *newdata = realloc(dentry->data, dentry->size + delta); |
448 | void *newdata = realloc(dentry->data, dentry->size + delta); |
449 | if (!newdata) { |
449 | if (!newdata) { |
450 | ipc_answer_0(callid, ENOMEM); |
450 | ipc_answer_0(callid, ENOMEM); |
451 | ipc_answer_2(rid, EOK, 0, dentry->size); |
451 | ipc_answer_2(rid, EOK, 0, dentry->size); |
452 | return; |
452 | return; |
453 | } |
453 | } |
454 | /* Clear any newly allocated memory in order to emulate gaps. */ |
454 | /* Clear any newly allocated memory in order to emulate gaps. */ |
455 | memset(newdata + dentry->size, 0, delta); |
455 | memset(newdata + dentry->size, 0, delta); |
456 | dentry->size += delta; |
456 | dentry->size += delta; |
457 | dentry->data = newdata; |
457 | dentry->data = newdata; |
458 | (void) ipc_data_write_finalize(callid, dentry->data + pos, len); |
458 | (void) ipc_data_write_finalize(callid, dentry->data + pos, len); |
459 | ipc_answer_2(rid, EOK, len, dentry->size); |
459 | ipc_answer_2(rid, EOK, len, dentry->size); |
460 | } |
460 | } |
461 | 461 | ||
462 | void tmpfs_truncate(ipc_callid_t rid, ipc_call_t *request) |
462 | void tmpfs_truncate(ipc_callid_t rid, ipc_call_t *request) |
463 | { |
463 | { |
464 | int dev_handle = IPC_GET_ARG1(*request); |
464 | int dev_handle = IPC_GET_ARG1(*request); |
465 | unsigned long index = IPC_GET_ARG2(*request); |
465 | unsigned long index = IPC_GET_ARG2(*request); |
466 | size_t size = IPC_GET_ARG3(*request); |
466 | size_t size = IPC_GET_ARG3(*request); |
467 | 467 | ||
468 | /* |
468 | /* |
469 | * Lookup the respective dentry. |
469 | * Lookup the respective dentry. |
470 | */ |
470 | */ |
471 | link_t *hlp; |
471 | link_t *hlp; |
472 | hlp = hash_table_find(&dentries, &index); |
472 | hlp = hash_table_find(&dentries, &index); |
473 | if (!hlp) { |
473 | if (!hlp) { |
474 | ipc_answer_0(rid, ENOENT); |
474 | ipc_answer_0(rid, ENOENT); |
475 | return; |
475 | return; |
476 | } |
476 | } |
477 | tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t, |
477 | tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t, |
478 | dh_link); |
478 | dh_link); |
479 | 479 | ||
480 | if (size == dentry->size) { |
480 | if (size == dentry->size) { |
481 | ipc_answer_0(rid, EOK); |
481 | ipc_answer_0(rid, EOK); |
482 | return; |
482 | return; |
483 | } |
483 | } |
484 | 484 | ||
485 | void *newdata = realloc(dentry->data, size); |
485 | void *newdata = realloc(dentry->data, size); |
486 | if (!newdata) { |
486 | if (!newdata) { |
487 | ipc_answer_0(rid, ENOMEM); |
487 | ipc_answer_0(rid, ENOMEM); |
488 | return; |
488 | return; |
489 | } |
489 | } |
490 | if (size > dentry->size) { |
490 | if (size > dentry->size) { |
491 | size_t delta = size - dentry->size; |
491 | size_t delta = size - dentry->size; |
492 | memset(newdata + dentry->size, 0, delta); |
492 | memset(newdata + dentry->size, 0, delta); |
493 | } |
493 | } |
494 | dentry->size = size; |
494 | dentry->size = size; |
495 | dentry->data = newdata; |
495 | dentry->data = newdata; |
496 | ipc_answer_0(rid, EOK); |
496 | ipc_answer_0(rid, EOK); |
497 | } |
497 | } |
498 | 498 | ||
- | 499 | void tmpfs_free(ipc_callid_t rid, ipc_call_t *request) |
|
- | 500 | { |
|
- | 501 | int dev_handle = IPC_GET_ARG1(*request); |
|
- | 502 | unsigned long index = IPC_GET_ARG2(*request); |
|
- | 503 | ||
- | 504 | link_t *hlp; |
|
- | 505 | hlp = hash_table_find(&dentries, &index); |
|
- | 506 | if (!hlp) { |
|
- | 507 | ipc_answer_0(rid, ENOENT); |
|
- | 508 | return; |
|
- | 509 | } |
|
- | 510 | tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t, |
|
- | 511 | dh_link); |
|
- | 512 | ||
- | 513 | assert(!dentry->parent); |
|
- | 514 | assert(!dentry->child); |
|
- | 515 | assert(!dentry->sibling); |
|
- | 516 | ||
- | 517 | if (dentry->type == TMPFS_FILE) |
|
- | 518 | free(dentry->data); |
|
- | 519 | free(dentry->name); |
|
- | 520 | free(dentry); |
|
- | 521 | ||
- | 522 | ipc_answer_0(rid, EOK); |
|
- | 523 | } |
|
- | 524 | ||
499 | /** |
525 | /** |
500 | * @} |
526 | * @} |
501 | */ |
527 | */ |
502 | 528 |