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