Rev 2655 | Rev 2663 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 2655 | Rev 2658 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (c) 2007 Jakub Jermar |
2 | * Copyright (c) 2007 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 <sys/types.h> |
|
- | 49 | #include <libadt/hash_table.h> |
|
- | 50 | #include <as.h> |
|
- | 51 | ||
- | 52 | #define min(a, b) ((a) < (b) ? (a) : (b)) |
|
- | 53 | #define max(a, b) ((a) > (b) ? (a) : (b)) |
|
48 | 54 | ||
49 | #define PLB_GET_CHAR(i) (tmpfs_reg.plb_ro[(i) % PLB_SIZE]) |
55 | #define PLB_GET_CHAR(i) (tmpfs_reg.plb_ro[(i) % PLB_SIZE]) |
50 | 56 | ||
- | 57 | #define DENTRIES_BUCKETS 256 |
|
- | 58 | ||
- | 59 | /* |
|
- | 60 | * Hash table of all directory entries. |
|
- | 61 | */ |
|
- | 62 | hash_table_t dentries; |
|
- | 63 | ||
- | 64 | static hash_index_t dentries_hash(unsigned long *key) |
|
- | 65 | { |
|
- | 66 | return *key % DENTRIES_BUCKETS; |
|
- | 67 | } |
|
- | 68 | ||
- | 69 | static int dentries_compare(unsigned long *key, hash_count_t keys, |
|
- | 70 | link_t *item) |
|
- | 71 | { |
|
- | 72 | tmpfs_dentry_t *dentry = hash_table_get_instance(item, tmpfs_dentry_t, |
|
- | 73 | dh_link); |
|
- | 74 | return dentry->index == *key; |
|
- | 75 | } |
|
- | 76 | ||
- | 77 | static void dentries_remove_callback(link_t *item) |
|
- | 78 | { |
|
- | 79 | } |
|
- | 80 | ||
- | 81 | /** TMPFS dentries hash table operations. */ |
|
- | 82 | hash_table_operations_t dentries_ops = { |
|
- | 83 | .hash = dentries_hash, |
|
- | 84 | .compare = dentries_compare, |
|
- | 85 | .remove_callback = dentries_remove_callback |
|
- | 86 | }; |
|
- | 87 | ||
51 | unsigned tmpfs_next_index = 1; |
88 | unsigned tmpfs_next_index = 1; |
52 | 89 | ||
53 | static void tmpfs_dentry_initialize(tmpfs_dentry_t *dentry) |
90 | static void tmpfs_dentry_initialize(tmpfs_dentry_t *dentry) |
54 | { |
91 | { |
55 | dentry->index = 0; |
92 | dentry->index = 0; |
56 | dentry->parent = NULL; |
93 | dentry->parent = NULL; |
57 | dentry->sibling = NULL; |
94 | dentry->sibling = NULL; |
58 | dentry->child = NULL; |
95 | dentry->child = NULL; |
59 | dentry->name = NULL; |
96 | dentry->name = NULL; |
60 | dentry->type = TMPFS_NONE; |
97 | dentry->type = TMPFS_NONE; |
61 | dentry->size = 0; |
98 | dentry->size = 0; |
62 | dentry->data = NULL; |
99 | dentry->data = NULL; |
- | 100 | link_initialize(&dentry->dh_link); |
|
63 | } |
101 | } |
64 | 102 | ||
65 | /* |
103 | /* |
66 | * For now, we don't distinguish between different dev_handles/instances. All |
104 | * For now, we don't distinguish between different dev_handles/instances. All |
67 | * requests resolve to the only instance, rooted in the following variable. |
105 | * requests resolve to the only instance, rooted in the following variable. |
68 | */ |
106 | */ |
69 | static tmpfs_dentry_t *root; |
107 | static tmpfs_dentry_t *root; |
70 | 108 | ||
71 | static bool tmpfs_init(void) |
109 | static bool tmpfs_init(void) |
72 | { |
110 | { |
- | 111 | if (!hash_table_create(&dentries, DENTRIES_BUCKETS, 1, &dentries_ops)) |
|
- | 112 | return false; |
|
- | 113 | ||
73 | root = (tmpfs_dentry_t *) malloc(sizeof(tmpfs_dentry_t)); |
114 | root = (tmpfs_dentry_t *) malloc(sizeof(tmpfs_dentry_t)); |
74 | if (!root) { |
115 | if (!root) |
75 | return false; |
116 | return false; |
76 | } |
- | |
77 | tmpfs_dentry_initialize(root); |
117 | tmpfs_dentry_initialize(root); |
78 | root->index = tmpfs_next_index++; |
118 | root->index = tmpfs_next_index++; |
79 | root->name = ""; |
119 | root->name = ""; |
80 | root->type = TMPFS_DIRECTORY; |
120 | root->type = TMPFS_DIRECTORY; |
- | 121 | hash_table_insert(&dentries, &root->index, &root->dh_link); |
|
81 | 122 | ||
82 | /* |
123 | /* |
83 | * This is only for debugging. Once we can create files and directories |
124 | * This is only for debugging. Once we can create files and directories |
84 | * using VFS, we can get rid of this. |
125 | * using VFS, we can get rid of this. |
85 | */ |
126 | */ |
86 | tmpfs_dentry_t *d; |
127 | tmpfs_dentry_t *d; |
87 | d = (tmpfs_dentry_t *) malloc(sizeof(tmpfs_dentry_t)); |
128 | d = (tmpfs_dentry_t *) malloc(sizeof(tmpfs_dentry_t)); |
88 | if (!d) { |
129 | if (!d) { |
89 | free(root); |
130 | free(root); |
90 | root = NULL; |
131 | root = NULL; |
91 | return false; |
132 | return false; |
92 | } |
133 | } |
93 | tmpfs_dentry_initialize(d); |
134 | tmpfs_dentry_initialize(d); |
94 | d->index = tmpfs_next_index++; |
135 | d->index = tmpfs_next_index++; |
95 | root->child = d; |
136 | root->child = d; |
96 | d->parent = root; |
137 | d->parent = root; |
97 | d->type = TMPFS_DIRECTORY; |
138 | d->type = TMPFS_DIRECTORY; |
98 | d->name = "dir1"; |
139 | d->name = "dir1"; |
- | 140 | hash_table_insert(&dentries, &d->index, &d->dh_link); |
|
99 | 141 | ||
100 | d = (tmpfs_dentry_t *) malloc(sizeof(tmpfs_dentry_t)); |
142 | d = (tmpfs_dentry_t *) malloc(sizeof(tmpfs_dentry_t)); |
101 | if (!d) { |
143 | if (!d) { |
102 | free(root->child); |
144 | free(root->child); |
103 | free(root); |
145 | free(root); |
104 | root = NULL; |
146 | root = NULL; |
105 | return false; |
147 | return false; |
106 | } |
148 | } |
107 | tmpfs_dentry_initialize(d); |
149 | tmpfs_dentry_initialize(d); |
108 | d->index = tmpfs_next_index++; |
150 | d->index = tmpfs_next_index++; |
109 | root->child->sibling = d; |
151 | root->child->sibling = d; |
110 | d->parent = root; |
152 | d->parent = root; |
111 | d->type = TMPFS_DIRECTORY; |
153 | d->type = TMPFS_DIRECTORY; |
112 | d->name = "dir2"; |
154 | d->name = "dir2"; |
- | 155 | hash_table_insert(&dentries, &d->index, &d->dh_link); |
|
113 | 156 | ||
114 | d = (tmpfs_dentry_t *) malloc(sizeof(tmpfs_dentry_t)); |
157 | d = (tmpfs_dentry_t *) malloc(sizeof(tmpfs_dentry_t)); |
115 | if (!d) { |
158 | if (!d) { |
116 | free(root->child->sibling); |
159 | free(root->child->sibling); |
117 | free(root->child); |
160 | free(root->child); |
118 | free(root); |
161 | free(root); |
119 | root = NULL; |
162 | root = NULL; |
120 | return false; |
163 | return false; |
121 | } |
164 | } |
122 | tmpfs_dentry_initialize(d); |
165 | tmpfs_dentry_initialize(d); |
123 | d->index = tmpfs_next_index++; |
166 | d->index = tmpfs_next_index++; |
124 | root->child->child = d; |
167 | root->child->child = d; |
125 | d->parent = root->child; |
168 | d->parent = root->child; |
126 | d->type = TMPFS_FILE; |
169 | d->type = TMPFS_FILE; |
127 | d->name = "file1"; |
170 | d->name = "file1"; |
128 | d->data = "This is the contents of /dir1/file1.\n"; |
171 | d->data = "This is the contents of /dir1/file1.\n"; |
129 | d->size = strlen(d->data); |
172 | d->size = strlen(d->data); |
- | 173 | hash_table_insert(&dentries, &d->index, &d->dh_link); |
|
130 | 174 | ||
131 | d = (tmpfs_dentry_t *) malloc(sizeof(tmpfs_dentry_t)); |
175 | d = (tmpfs_dentry_t *) malloc(sizeof(tmpfs_dentry_t)); |
132 | if (!d) { |
176 | if (!d) { |
133 | free(root->child->sibling); |
177 | free(root->child->sibling); |
134 | free(root->child->child); |
178 | free(root->child->child); |
135 | free(root->child); |
179 | free(root->child); |
136 | free(root); |
180 | free(root); |
137 | root = NULL; |
181 | root = NULL; |
138 | return false; |
182 | return false; |
139 | } |
183 | } |
140 | tmpfs_dentry_initialize(d); |
184 | tmpfs_dentry_initialize(d); |
141 | d->index = tmpfs_next_index++; |
185 | d->index = tmpfs_next_index++; |
142 | root->child->sibling->child = d; |
186 | root->child->sibling->child = d; |
143 | d->parent = root->child->sibling; |
187 | d->parent = root->child->sibling; |
144 | d->type = TMPFS_FILE; |
188 | d->type = TMPFS_FILE; |
145 | d->name = "file2"; |
189 | d->name = "file2"; |
146 | d->data = "This is the contents of /dir2/file2.\n"; |
190 | d->data = "This is the contents of /dir2/file2.\n"; |
147 | d->size = strlen(d->data); |
191 | d->size = strlen(d->data); |
- | 192 | hash_table_insert(&dentries, &d->index, &d->dh_link); |
|
148 | 193 | ||
149 | return true; |
194 | return true; |
150 | } |
195 | } |
151 | 196 | ||
152 | /** Compare one component of path to a directory entry. |
197 | /** Compare one component of path to a directory entry. |
153 | * |
198 | * |
154 | * @param dentry Directory entry to compare the path component with. |
199 | * @param dentry Directory entry to compare the path component with. |
155 | * @param start Index into PLB where the path component starts. |
200 | * @param start Index into PLB where the path component starts. |
156 | * @param last Index of the last character of the path in PLB. |
201 | * @param last Index of the last character of the path in PLB. |
157 | * |
202 | * |
158 | * @return Zero on failure or delta such that (index + delta) % |
203 | * @return Zero on failure or delta such that (index + delta) % |
159 | * PLB_SIZE points to the first unprocessed character in |
204 | * PLB_SIZE points to the first unprocessed character in |
160 | * PLB which comprises the path. |
205 | * PLB which comprises the path. |
161 | */ |
206 | */ |
162 | static unsigned match_path_component(tmpfs_dentry_t *dentry, unsigned start, |
207 | static unsigned match_path_component(tmpfs_dentry_t *dentry, unsigned start, |
163 | unsigned last) |
208 | unsigned last) |
164 | { |
209 | { |
165 | int i, j; |
210 | int i, j; |
166 | size_t namelen; |
211 | size_t namelen; |
167 | 212 | ||
168 | namelen = strlen(dentry->name); |
213 | namelen = strlen(dentry->name); |
169 | 214 | ||
170 | if (last < start) |
215 | if (last < start) |
171 | last += PLB_SIZE; |
216 | last += PLB_SIZE; |
172 | 217 | ||
173 | for (i = 0, j = start; i < namelen && j <= last; i++, j++) { |
218 | for (i = 0, j = start; i < namelen && j <= last; i++, j++) { |
174 | if (dentry->name[i] != PLB_GET_CHAR(j)) |
219 | if (dentry->name[i] != PLB_GET_CHAR(j)) |
175 | return 0; |
220 | return 0; |
176 | } |
221 | } |
177 | 222 | ||
178 | if (i != namelen) |
223 | if (i != namelen) |
179 | return 0; |
224 | return 0; |
180 | if (j < last && PLB_GET_CHAR(j) != '/') |
225 | if (j < last && PLB_GET_CHAR(j) != '/') |
181 | return 0; |
226 | return 0; |
182 | if (j == last) |
227 | if (j == last) |
183 | return 0; |
228 | return 0; |
184 | 229 | ||
185 | return (j - start); |
230 | return (j - start); |
186 | } |
231 | } |
187 | 232 | ||
188 | void tmpfs_lookup(ipc_callid_t rid, ipc_call_t *request) |
233 | void tmpfs_lookup(ipc_callid_t rid, ipc_call_t *request) |
189 | { |
234 | { |
190 | unsigned next = IPC_GET_ARG1(*request); |
235 | unsigned next = IPC_GET_ARG1(*request); |
191 | unsigned last = IPC_GET_ARG2(*request); |
236 | unsigned last = IPC_GET_ARG2(*request); |
192 | int dev_handle = IPC_GET_ARG3(*request); |
237 | int dev_handle = IPC_GET_ARG3(*request); |
193 | 238 | ||
194 | if (last < next) |
239 | if (last < next) |
195 | last += PLB_SIZE; |
240 | last += PLB_SIZE; |
196 | 241 | ||
197 | if (!root && !tmpfs_init()) { |
242 | if (!root && !tmpfs_init()) { |
198 | ipc_answer_0(rid, ENOMEM); |
243 | ipc_answer_0(rid, ENOMEM); |
199 | return; |
244 | return; |
200 | } |
245 | } |
201 | 246 | ||
202 | tmpfs_dentry_t *dtmp = root->child; |
247 | tmpfs_dentry_t *dtmp = root->child; |
203 | tmpfs_dentry_t *dcur = root; |
248 | tmpfs_dentry_t *dcur = root; |
204 | 249 | ||
205 | bool hit = true; |
250 | bool hit = true; |
206 | 251 | ||
207 | if (PLB_GET_CHAR(next) == '/') |
252 | if (PLB_GET_CHAR(next) == '/') |
208 | next++; /* eat slash */ |
253 | next++; /* eat slash */ |
209 | 254 | ||
210 | while (next <= last) { |
255 | while (next <= last) { |
211 | unsigned delta; |
256 | unsigned delta; |
212 | hit = false; |
257 | hit = false; |
213 | do { |
258 | do { |
214 | delta = match_path_component(dtmp, next, last); |
259 | delta = match_path_component(dtmp, next, last); |
215 | if (!delta) { |
260 | if (!delta) { |
216 | dtmp = dtmp->sibling; |
261 | dtmp = dtmp->sibling; |
217 | } else { |
262 | } else { |
218 | hit = true; |
263 | hit = true; |
219 | next += delta; |
264 | next += delta; |
220 | next++; /* eat slash */ |
265 | next++; /* eat slash */ |
221 | dcur = dtmp; |
266 | dcur = dtmp; |
222 | dtmp = dtmp->child; |
267 | dtmp = dtmp->child; |
223 | } |
268 | } |
224 | } while (delta == 0 && dtmp); |
269 | } while (delta == 0 && dtmp); |
225 | if (!hit) { |
270 | if (!hit) { |
226 | ipc_answer_3(rid, ENOENT, tmpfs_reg.fs_handle, |
271 | ipc_answer_3(rid, ENOENT, tmpfs_reg.fs_handle, |
227 | dev_handle, dcur->index); |
272 | dev_handle, dcur->index); |
228 | return; |
273 | return; |
229 | } |
274 | } |
230 | } |
275 | } |
231 | 276 | ||
232 | ipc_answer_3(rid, EOK, tmpfs_reg.fs_handle, dev_handle, dcur->index); |
277 | ipc_answer_3(rid, EOK, tmpfs_reg.fs_handle, dev_handle, dcur->index); |
233 | } |
278 | } |
- | 279 | ||
- | 280 | void tmpfs_read(ipc_callid_t rid, ipc_call_t *request) |
|
- | 281 | { |
|
- | 282 | int dev_handle = IPC_GET_ARG1(*request); |
|
- | 283 | unsigned long index = IPC_GET_ARG2(*request); |
|
- | 284 | off_t pos = IPC_GET_ARG3(*request); |
|
- | 285 | size_t size = IPC_GET_ARG4(*request); |
|
- | 286 | ||
- | 287 | /* |
|
- | 288 | * Lookup the respective dentry. |
|
- | 289 | */ |
|
- | 290 | link_t *hlp; |
|
- | 291 | hlp = hash_table_find(&dentries, &index); |
|
- | 292 | if (!hlp) { |
|
- | 293 | ipc_answer_0(rid, ENOENT); |
|
- | 294 | return; |
|
- | 295 | } |
|
- | 296 | tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t, |
|
- | 297 | dh_link); |
|
- | 298 | ||
- | 299 | /* |
|
- | 300 | * Receive the communication area. |
|
- | 301 | */ |
|
- | 302 | ipc_callid_t callid; |
|
- | 303 | ipc_call_t call; |
|
- | 304 | callid = async_get_call(&call); |
|
- | 305 | if (IPC_GET_METHOD(call) != IPC_M_AS_AREA_SEND) { |
|
- | 306 | ipc_answer_0(callid, EINVAL); |
|
- | 307 | ipc_answer_0(rid, EINVAL); |
|
- | 308 | return; |
|
- | 309 | } |
|
- | 310 | ||
- | 311 | int flags = IPC_GET_ARG3(call); |
|
- | 312 | if (!(flags & AS_AREA_WRITE)) { |
|
- | 313 | ipc_answer_0(callid, EINVAL); |
|
- | 314 | ipc_answer_0(rid, EINVAL); |
|
- | 315 | return; |
|
- | 316 | } |
|
- | 317 | size_t sz = IPC_GET_ARG2(call); |
|
- | 318 | uint8_t *buf = as_get_mappable_page(sz); |
|
- | 319 | if (!buf) { |
|
- | 320 | ipc_answer_0(callid, ENOMEM); |
|
- | 321 | ipc_answer_0(rid, ENOMEM); |
|
- | 322 | return; |
|
- | 323 | } |
|
- | 324 | ipc_answer_1(callid, EOK, buf); /* commit to share the area */ |
|
- | 325 | ||
- | 326 | size_t bytes = max(0, min(dentry->size - pos, size)); |
|
- | 327 | memcpy(buf, dentry->data + pos, bytes); |
|
- | 328 | ||
- | 329 | (void) as_area_destroy(buf); |
|
- | 330 | ||
- | 331 | ipc_answer_1(rid, EOK, bytes); |
|
- | 332 | } |
|
234 | 333 | ||
235 | /** |
334 | /** |
236 | * @} |
335 | * @} |
237 | */ |
336 | */ |
238 | 337 |