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