Rev 4419 | Rev 4439 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
4419 | trochtova | 1 | /* |
2 | * Copyright (c) 2008 Jakub Jermar |
||
3 | * All rights reserved. |
||
4 | * |
||
5 | * Redistribution and use in source and binary forms, with or without |
||
6 | * modification, are permitted provided that the following conditions |
||
7 | * are met: |
||
8 | * |
||
9 | * - Redistributions of source code must retain the above copyright |
||
10 | * notice, this list of conditions and the following disclaimer. |
||
11 | * - Redistributions in binary form must reproduce the above copyright |
||
12 | * notice, this list of conditions and the following disclaimer in the |
||
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 |
||
15 | * derived from this software without specific prior written permission. |
||
16 | * |
||
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 |
||
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
||
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
||
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
||
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 |
||
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 |
||
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
27 | */ |
||
28 | |||
29 | /** @addtogroup fs |
||
30 | * @{ |
||
31 | */ |
||
32 | |||
33 | /** |
||
34 | * @file tmpfs_ops.c |
||
35 | * @brief Implementation of VFS operations for the TMPFS file system |
||
36 | * server. |
||
37 | */ |
||
38 | |||
39 | #include "tmpfs.h" |
||
40 | #include "../../vfs/vfs.h" |
||
41 | #include <ipc/ipc.h> |
||
42 | #include <async.h> |
||
43 | #include <errno.h> |
||
44 | #include <atomic.h> |
||
45 | #include <stdlib.h> |
||
46 | #include <string.h> |
||
47 | #include <stdio.h> |
||
48 | #include <assert.h> |
||
49 | #include <sys/types.h> |
||
50 | #include <libadt/hash_table.h> |
||
51 | #include <as.h> |
||
52 | #include <libfs.h> |
||
53 | |||
54 | #define min(a, b) ((a) < (b) ? (a) : (b)) |
||
55 | #define max(a, b) ((a) > (b) ? (a) : (b)) |
||
56 | |||
4420 | trochtova | 57 | #define NODES_BUCKETS 256 |
4419 | trochtova | 58 | |
4420 | trochtova | 59 | /** All root nodes have index 0. */ |
60 | #define TMPFS_SOME_ROOT 0 |
||
61 | /** Global counter for assigning node indices. Shared by all instances. */ |
||
62 | fs_index_t tmpfs_next_index = 1; |
||
4419 | trochtova | 63 | |
64 | /* |
||
65 | * Implementation of the libfs interface. |
||
66 | */ |
||
67 | |||
68 | /* Forward declarations of static functions. */ |
||
4420 | trochtova | 69 | static fs_node_t *tmpfs_match(fs_node_t *, const char *); |
70 | static fs_node_t *tmpfs_node_get(dev_handle_t, fs_index_t); |
||
71 | static void tmpfs_node_put(fs_node_t *); |
||
72 | static fs_node_t *tmpfs_create_node(dev_handle_t, int); |
||
73 | static int tmpfs_link_node(fs_node_t *, fs_node_t *, const char *); |
||
74 | static int tmpfs_unlink_node(fs_node_t *, fs_node_t *, const char *); |
||
75 | static int tmpfs_destroy_node(fs_node_t *); |
||
4419 | trochtova | 76 | |
77 | /* Implementation of helper functions. */ |
||
4420 | trochtova | 78 | static fs_index_t tmpfs_index_get(fs_node_t *fn) |
4419 | trochtova | 79 | { |
4420 | trochtova | 80 | return TMPFS_NODE(fn)->index; |
4419 | trochtova | 81 | } |
82 | |||
4420 | trochtova | 83 | static size_t tmpfs_size_get(fs_node_t *fn) |
4419 | trochtova | 84 | { |
4420 | trochtova | 85 | return TMPFS_NODE(fn)->size; |
4419 | trochtova | 86 | } |
87 | |||
4420 | trochtova | 88 | static unsigned tmpfs_lnkcnt_get(fs_node_t *fn) |
4419 | trochtova | 89 | { |
4420 | trochtova | 90 | return TMPFS_NODE(fn)->lnkcnt; |
4419 | trochtova | 91 | } |
92 | |||
4420 | trochtova | 93 | static bool tmpfs_has_children(fs_node_t *fn) |
4419 | trochtova | 94 | { |
4420 | trochtova | 95 | return !list_empty(&TMPFS_NODE(fn)->cs_head); |
4419 | trochtova | 96 | } |
97 | |||
4420 | trochtova | 98 | static fs_node_t *tmpfs_root_get(dev_handle_t dev_handle) |
4419 | trochtova | 99 | { |
4420 | trochtova | 100 | return tmpfs_node_get(dev_handle, TMPFS_SOME_ROOT); |
4419 | trochtova | 101 | } |
102 | |||
103 | static char tmpfs_plb_get_char(unsigned pos) |
||
104 | { |
||
105 | return tmpfs_reg.plb_ro[pos % PLB_SIZE]; |
||
106 | } |
||
107 | |||
4420 | trochtova | 108 | static bool tmpfs_is_directory(fs_node_t *fn) |
4419 | trochtova | 109 | { |
4420 | trochtova | 110 | return TMPFS_NODE(fn)->type == TMPFS_DIRECTORY; |
4419 | trochtova | 111 | } |
112 | |||
4420 | trochtova | 113 | static bool tmpfs_is_file(fs_node_t *fn) |
4419 | trochtova | 114 | { |
4420 | trochtova | 115 | return TMPFS_NODE(fn)->type == TMPFS_FILE; |
4419 | trochtova | 116 | } |
117 | |||
118 | /** libfs operations */ |
||
119 | libfs_ops_t tmpfs_libfs_ops = { |
||
120 | .match = tmpfs_match, |
||
121 | .node_get = tmpfs_node_get, |
||
122 | .node_put = tmpfs_node_put, |
||
123 | .create = tmpfs_create_node, |
||
124 | .destroy = tmpfs_destroy_node, |
||
125 | .link = tmpfs_link_node, |
||
126 | .unlink = tmpfs_unlink_node, |
||
127 | .index_get = tmpfs_index_get, |
||
128 | .size_get = tmpfs_size_get, |
||
129 | .lnkcnt_get = tmpfs_lnkcnt_get, |
||
130 | .has_children = tmpfs_has_children, |
||
131 | .root_get = tmpfs_root_get, |
||
132 | .plb_get_char = tmpfs_plb_get_char, |
||
133 | .is_directory = tmpfs_is_directory, |
||
134 | .is_file = tmpfs_is_file |
||
135 | }; |
||
136 | |||
4420 | trochtova | 137 | /** Hash table of all TMPFS nodes. */ |
138 | hash_table_t nodes; |
||
4419 | trochtova | 139 | |
4420 | trochtova | 140 | #define NODES_KEY_INDEX 0 |
141 | #define NODES_KEY_DEV 1 |
||
4419 | trochtova | 142 | |
4420 | trochtova | 143 | /* Implementation of hash table interface for the nodes hash table. */ |
144 | static hash_index_t nodes_hash(unsigned long key[]) |
||
4419 | trochtova | 145 | { |
4420 | trochtova | 146 | return key[NODES_KEY_INDEX] % NODES_BUCKETS; |
4419 | trochtova | 147 | } |
148 | |||
4420 | trochtova | 149 | static int nodes_compare(unsigned long key[], hash_count_t keys, link_t *item) |
4419 | trochtova | 150 | { |
4420 | trochtova | 151 | tmpfs_node_t *nodep = hash_table_get_instance(item, tmpfs_node_t, |
152 | nh_link); |
||
153 | return (nodep->index == key[NODES_KEY_INDEX] && |
||
154 | nodep->dev_handle == key[NODES_KEY_DEV]); |
||
4419 | trochtova | 155 | } |
156 | |||
4420 | trochtova | 157 | static void nodes_remove_callback(link_t *item) |
4419 | trochtova | 158 | { |
159 | } |
||
160 | |||
4420 | trochtova | 161 | /** TMPFS nodes hash table operations. */ |
162 | hash_table_operations_t nodes_ops = { |
||
163 | .hash = nodes_hash, |
||
164 | .compare = nodes_compare, |
||
165 | .remove_callback = nodes_remove_callback |
||
4419 | trochtova | 166 | }; |
167 | |||
4420 | trochtova | 168 | static void tmpfs_node_initialize(tmpfs_node_t *nodep) |
4419 | trochtova | 169 | { |
4420 | trochtova | 170 | nodep->bp = NULL; |
171 | nodep->index = 0; |
||
172 | nodep->dev_handle = 0; |
||
173 | nodep->type = TMPFS_NONE; |
||
174 | nodep->lnkcnt = 0; |
||
175 | nodep->size = 0; |
||
176 | nodep->data = NULL; |
||
177 | link_initialize(&nodep->nh_link); |
||
178 | list_initialize(&nodep->cs_head); |
||
4419 | trochtova | 179 | } |
180 | |||
4420 | trochtova | 181 | static void tmpfs_dentry_initialize(tmpfs_dentry_t *dentryp) |
4419 | trochtova | 182 | { |
4420 | trochtova | 183 | link_initialize(&dentryp->link); |
184 | dentryp->name = NULL; |
||
185 | dentryp->node = NULL; |
||
4419 | trochtova | 186 | } |
187 | |||
4420 | trochtova | 188 | bool tmpfs_init(void) |
4419 | trochtova | 189 | { |
4420 | trochtova | 190 | if (!hash_table_create(&nodes, NODES_BUCKETS, 2, &nodes_ops)) |
4419 | trochtova | 191 | return false; |
4420 | trochtova | 192 | |
4419 | trochtova | 193 | return true; |
194 | } |
||
195 | |||
4420 | trochtova | 196 | static bool tmpfs_instance_init(dev_handle_t dev_handle) |
4419 | trochtova | 197 | { |
4420 | trochtova | 198 | fs_node_t *rfn; |
199 | |||
200 | rfn = tmpfs_create_node(dev_handle, L_DIRECTORY); |
||
201 | if (!rfn) |
||
202 | return false; |
||
203 | TMPFS_NODE(rfn)->lnkcnt = 0; /* FS root is not linked */ |
||
204 | return true; |
||
4419 | trochtova | 205 | } |
206 | |||
4420 | trochtova | 207 | fs_node_t *tmpfs_match(fs_node_t *pfn, const char *component) |
4419 | trochtova | 208 | { |
4420 | trochtova | 209 | tmpfs_node_t *parentp = TMPFS_NODE(pfn); |
210 | link_t *lnk; |
||
4419 | trochtova | 211 | |
4420 | trochtova | 212 | for (lnk = parentp->cs_head.next; lnk != &parentp->cs_head; |
213 | lnk = lnk->next) { |
||
214 | tmpfs_dentry_t *dentryp = list_get_instance(lnk, tmpfs_dentry_t, |
||
215 | link); |
||
216 | if (!str_cmp(dentryp->name, component)) |
||
217 | return FS_NODE(dentryp->node); |
||
218 | } |
||
4419 | trochtova | 219 | |
4420 | trochtova | 220 | return NULL; |
4419 | trochtova | 221 | } |
222 | |||
4420 | trochtova | 223 | fs_node_t *tmpfs_node_get(dev_handle_t dev_handle, fs_index_t index) |
4419 | trochtova | 224 | { |
4420 | trochtova | 225 | unsigned long key[] = { |
226 | [NODES_KEY_INDEX] = index, |
||
227 | [NODES_KEY_DEV] = dev_handle |
||
228 | }; |
||
229 | link_t *lnk = hash_table_find(&nodes, key); |
||
4419 | trochtova | 230 | if (!lnk) |
231 | return NULL; |
||
4420 | trochtova | 232 | return FS_NODE(hash_table_get_instance(lnk, tmpfs_node_t, nh_link)); |
4419 | trochtova | 233 | } |
234 | |||
4420 | trochtova | 235 | void tmpfs_node_put(fs_node_t *fn) |
4419 | trochtova | 236 | { |
237 | /* nothing to do */ |
||
238 | } |
||
239 | |||
4420 | trochtova | 240 | fs_node_t *tmpfs_create_node(dev_handle_t dev_handle, int lflag) |
4419 | trochtova | 241 | { |
242 | assert((lflag & L_FILE) ^ (lflag & L_DIRECTORY)); |
||
243 | |||
4420 | trochtova | 244 | tmpfs_node_t *nodep = malloc(sizeof(tmpfs_node_t)); |
245 | if (!nodep) |
||
4419 | trochtova | 246 | return NULL; |
4420 | trochtova | 247 | tmpfs_node_initialize(nodep); |
248 | nodep->bp = malloc(sizeof(fs_node_t)); |
||
249 | if (!nodep->bp) { |
||
250 | free(nodep); |
||
4419 | trochtova | 251 | return NULL; |
252 | } |
||
4420 | trochtova | 253 | fs_node_initialize(nodep->bp); |
254 | nodep->bp->data = nodep; /* link the FS and TMPFS nodes */ |
||
255 | if (!tmpfs_root_get(dev_handle)) |
||
256 | nodep->index = TMPFS_SOME_ROOT; |
||
257 | else |
||
258 | nodep->index = tmpfs_next_index++; |
||
259 | nodep->dev_handle = dev_handle; |
||
4419 | trochtova | 260 | if (lflag & L_DIRECTORY) |
4420 | trochtova | 261 | nodep->type = TMPFS_DIRECTORY; |
4419 | trochtova | 262 | else |
4420 | trochtova | 263 | nodep->type = TMPFS_FILE; |
4419 | trochtova | 264 | |
4420 | trochtova | 265 | /* Insert the new node into the nodes hash table. */ |
266 | unsigned long key[] = { |
||
267 | [NODES_KEY_INDEX] = nodep->index, |
||
268 | [NODES_KEY_DEV] = nodep->dev_handle |
||
269 | }; |
||
270 | hash_table_insert(&nodes, key, &nodep->nh_link); |
||
271 | return FS_NODE(nodep); |
||
4419 | trochtova | 272 | } |
273 | |||
4420 | trochtova | 274 | int tmpfs_link_node(fs_node_t *pfn, fs_node_t *cfn, const char *nm) |
4419 | trochtova | 275 | { |
4420 | trochtova | 276 | tmpfs_node_t *parentp = TMPFS_NODE(pfn); |
277 | tmpfs_node_t *childp = TMPFS_NODE(cfn); |
||
278 | tmpfs_dentry_t *dentryp; |
||
279 | link_t *lnk; |
||
4419 | trochtova | 280 | |
281 | assert(parentp->type == TMPFS_DIRECTORY); |
||
282 | |||
4420 | trochtova | 283 | /* Check for duplicit entries. */ |
284 | for (lnk = parentp->cs_head.next; lnk != &parentp->cs_head; |
||
285 | lnk = lnk->next) { |
||
286 | dentryp = list_get_instance(lnk, tmpfs_dentry_t, link); |
||
287 | if (!str_cmp(dentryp->name, nm)) |
||
288 | return EEXIST; |
||
289 | } |
||
290 | |||
291 | /* Allocate and initialize the dentry. */ |
||
292 | dentryp = malloc(sizeof(tmpfs_dentry_t)); |
||
293 | if (!dentryp) |
||
4419 | trochtova | 294 | return ENOMEM; |
4420 | trochtova | 295 | tmpfs_dentry_initialize(dentryp); |
296 | |||
297 | /* Populate and link the new dentry. */ |
||
4419 | trochtova | 298 | size_t size = str_size(nm); |
4420 | trochtova | 299 | dentryp->name = malloc(size + 1); |
300 | if (!dentryp->name) { |
||
301 | free(dentryp); |
||
4419 | trochtova | 302 | return ENOMEM; |
303 | } |
||
4420 | trochtova | 304 | str_cpy(dentryp->name, size + 1, nm); |
305 | dentryp->node = childp; |
||
4419 | trochtova | 306 | childp->lnkcnt++; |
4420 | trochtova | 307 | list_append(&dentryp->link, &parentp->cs_head); |
4419 | trochtova | 308 | |
309 | return EOK; |
||
310 | } |
||
311 | |||
4420 | trochtova | 312 | int tmpfs_unlink_node(fs_node_t *pfn, fs_node_t *cfn, const char *nm) |
4419 | trochtova | 313 | { |
4420 | trochtova | 314 | tmpfs_node_t *parentp = TMPFS_NODE(pfn); |
315 | tmpfs_node_t *childp = NULL; |
||
316 | tmpfs_dentry_t *dentryp; |
||
317 | link_t *lnk; |
||
4419 | trochtova | 318 | |
319 | if (!parentp) |
||
320 | return EBUSY; |
||
4420 | trochtova | 321 | |
322 | for (lnk = parentp->cs_head.next; lnk != &parentp->cs_head; |
||
323 | lnk = lnk->next) { |
||
324 | dentryp = list_get_instance(lnk, tmpfs_dentry_t, link); |
||
325 | if (!str_cmp(dentryp->name, nm)) { |
||
326 | childp = dentryp->node; |
||
327 | assert(FS_NODE(childp) == cfn); |
||
328 | break; |
||
329 | } |
||
330 | } |
||
4419 | trochtova | 331 | |
4420 | trochtova | 332 | if (!childp) |
333 | return ENOENT; |
||
334 | |||
335 | if ((childp->lnkcnt == 1) && !list_empty(&childp->cs_head)) |
||
4419 | trochtova | 336 | return ENOTEMPTY; |
337 | |||
4420 | trochtova | 338 | list_remove(&dentryp->link); |
339 | free(dentryp); |
||
4419 | trochtova | 340 | childp->lnkcnt--; |
341 | |||
342 | return EOK; |
||
343 | } |
||
344 | |||
4420 | trochtova | 345 | int tmpfs_destroy_node(fs_node_t *fn) |
4419 | trochtova | 346 | { |
4420 | trochtova | 347 | tmpfs_node_t *nodep = TMPFS_NODE(fn); |
4419 | trochtova | 348 | |
4420 | trochtova | 349 | assert(!nodep->lnkcnt); |
350 | assert(list_empty(&nodep->cs_head)); |
||
4419 | trochtova | 351 | |
4420 | trochtova | 352 | unsigned long key[] = { |
353 | [NODES_KEY_INDEX] = nodep->index, |
||
354 | [NODES_KEY_DEV] = nodep->dev_handle |
||
355 | }; |
||
356 | hash_table_remove(&nodes, key, 2); |
||
4419 | trochtova | 357 | |
4420 | trochtova | 358 | if (nodep->type == TMPFS_FILE) |
359 | free(nodep->data); |
||
360 | free(nodep->bp); |
||
361 | free(nodep); |
||
4419 | trochtova | 362 | return EOK; |
363 | } |
||
364 | |||
365 | void tmpfs_mounted(ipc_callid_t rid, ipc_call_t *request) |
||
366 | { |
||
367 | dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request); |
||
368 | |||
4420 | trochtova | 369 | /* accept the mount options */ |
370 | ipc_callid_t callid; |
||
371 | size_t size; |
||
372 | if (!ipc_data_write_receive(&callid, &size)) { |
||
373 | ipc_answer_0(callid, EINVAL); |
||
374 | ipc_answer_0(rid, EINVAL); |
||
375 | return; |
||
376 | } |
||
377 | char *opts = malloc(size + 1); |
||
378 | if (!opts) { |
||
379 | ipc_answer_0(callid, ENOMEM); |
||
4419 | trochtova | 380 | ipc_answer_0(rid, ENOMEM); |
381 | return; |
||
382 | } |
||
4420 | trochtova | 383 | ipcarg_t retval = ipc_data_write_finalize(callid, opts, size); |
384 | if (retval != EOK) { |
||
385 | ipc_answer_0(rid, retval); |
||
386 | free(opts); |
||
387 | return; |
||
388 | } |
||
389 | opts[size] = '\0'; |
||
4419 | trochtova | 390 | |
4420 | trochtova | 391 | /* Initialize TMPFS instance. */ |
392 | if (!tmpfs_instance_init(dev_handle)) { |
||
393 | ipc_answer_0(rid, ENOMEM); |
||
394 | return; |
||
395 | } |
||
396 | |||
397 | tmpfs_node_t *rootp = TMPFS_NODE(tmpfs_root_get(dev_handle)); |
||
398 | if (str_cmp(opts, "restore") == 0) { |
||
4419 | trochtova | 399 | if (tmpfs_restore(dev_handle)) |
4420 | trochtova | 400 | ipc_answer_3(rid, EOK, rootp->index, rootp->size, |
401 | rootp->lnkcnt); |
||
4419 | trochtova | 402 | else |
403 | ipc_answer_0(rid, ELIMIT); |
||
404 | } else { |
||
4420 | trochtova | 405 | ipc_answer_3(rid, EOK, rootp->index, rootp->size, |
406 | rootp->lnkcnt); |
||
4419 | trochtova | 407 | } |
408 | } |
||
409 | |||
410 | void tmpfs_mount(ipc_callid_t rid, ipc_call_t *request) |
||
411 | { |
||
412 | dev_handle_t mp_dev_handle = (dev_handle_t) IPC_GET_ARG1(*request); |
||
413 | fs_index_t mp_index = (fs_index_t) IPC_GET_ARG2(*request); |
||
414 | fs_handle_t mr_fs_handle = (fs_handle_t) IPC_GET_ARG3(*request); |
||
415 | dev_handle_t mr_dev_handle = (dev_handle_t) IPC_GET_ARG4(*request); |
||
416 | |||
417 | ipc_answer_0(rid, ENOTSUP); |
||
418 | } |
||
419 | |||
420 | void tmpfs_lookup(ipc_callid_t rid, ipc_call_t *request) |
||
421 | { |
||
422 | libfs_lookup(&tmpfs_libfs_ops, tmpfs_reg.fs_handle, rid, request); |
||
423 | } |
||
424 | |||
425 | void tmpfs_read(ipc_callid_t rid, ipc_call_t *request) |
||
426 | { |
||
427 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
||
428 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); |
||
429 | off_t pos = (off_t)IPC_GET_ARG3(*request); |
||
430 | |||
431 | /* |
||
4420 | trochtova | 432 | * Lookup the respective TMPFS node. |
4419 | trochtova | 433 | */ |
434 | link_t *hlp; |
||
4420 | trochtova | 435 | unsigned long key[] = { |
436 | [NODES_KEY_INDEX] = index, |
||
437 | [NODES_KEY_DEV] = dev_handle, |
||
438 | }; |
||
439 | hlp = hash_table_find(&nodes, key); |
||
4419 | trochtova | 440 | if (!hlp) { |
441 | ipc_answer_0(rid, ENOENT); |
||
442 | return; |
||
443 | } |
||
4420 | trochtova | 444 | tmpfs_node_t *nodep = hash_table_get_instance(hlp, tmpfs_node_t, |
445 | nh_link); |
||
4419 | trochtova | 446 | |
447 | /* |
||
448 | * Receive the read request. |
||
449 | */ |
||
450 | ipc_callid_t callid; |
||
451 | size_t size; |
||
452 | if (!ipc_data_read_receive(&callid, &size)) { |
||
453 | ipc_answer_0(callid, EINVAL); |
||
454 | ipc_answer_0(rid, EINVAL); |
||
455 | return; |
||
456 | } |
||
457 | |||
458 | size_t bytes; |
||
4420 | trochtova | 459 | if (nodep->type == TMPFS_FILE) { |
460 | bytes = max(0, min(nodep->size - pos, size)); |
||
461 | (void) ipc_data_read_finalize(callid, nodep->data + pos, |
||
4419 | trochtova | 462 | bytes); |
463 | } else { |
||
4420 | trochtova | 464 | tmpfs_dentry_t *dentryp; |
465 | link_t *lnk; |
||
4419 | trochtova | 466 | int i; |
467 | |||
4420 | trochtova | 468 | assert(nodep->type == TMPFS_DIRECTORY); |
4419 | trochtova | 469 | |
470 | /* |
||
471 | * Yes, we really use O(n) algorithm here. |
||
472 | * If it bothers someone, it could be fixed by introducing a |
||
473 | * hash table. |
||
474 | */ |
||
4420 | trochtova | 475 | for (i = 0, lnk = nodep->cs_head.next; |
476 | i < pos && lnk != &nodep->cs_head; |
||
477 | i++, lnk = lnk->next) |
||
4419 | trochtova | 478 | ; |
479 | |||
4420 | trochtova | 480 | if (lnk == &nodep->cs_head) { |
4419 | trochtova | 481 | ipc_answer_0(callid, ENOENT); |
482 | ipc_answer_1(rid, ENOENT, 0); |
||
483 | return; |
||
484 | } |
||
485 | |||
4420 | trochtova | 486 | dentryp = list_get_instance(lnk, tmpfs_dentry_t, link); |
4419 | trochtova | 487 | |
4420 | trochtova | 488 | (void) ipc_data_read_finalize(callid, dentryp->name, |
489 | str_size(dentryp->name) + 1); |
||
4419 | trochtova | 490 | bytes = 1; |
491 | } |
||
492 | |||
493 | /* |
||
494 | * Answer the VFS_READ call. |
||
495 | */ |
||
496 | ipc_answer_1(rid, EOK, bytes); |
||
497 | } |
||
498 | |||
499 | void tmpfs_write(ipc_callid_t rid, ipc_call_t *request) |
||
500 | { |
||
501 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
||
502 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); |
||
503 | off_t pos = (off_t)IPC_GET_ARG3(*request); |
||
504 | |||
505 | /* |
||
4420 | trochtova | 506 | * Lookup the respective TMPFS node. |
4419 | trochtova | 507 | */ |
508 | link_t *hlp; |
||
4420 | trochtova | 509 | unsigned long key[] = { |
510 | [NODES_KEY_INDEX] = index, |
||
511 | [NODES_KEY_DEV] = dev_handle |
||
512 | }; |
||
513 | hlp = hash_table_find(&nodes, key); |
||
4419 | trochtova | 514 | if (!hlp) { |
515 | ipc_answer_0(rid, ENOENT); |
||
516 | return; |
||
517 | } |
||
4420 | trochtova | 518 | tmpfs_node_t *nodep = hash_table_get_instance(hlp, tmpfs_node_t, |
519 | nh_link); |
||
4419 | trochtova | 520 | |
521 | /* |
||
522 | * Receive the write request. |
||
523 | */ |
||
524 | ipc_callid_t callid; |
||
525 | size_t size; |
||
526 | if (!ipc_data_write_receive(&callid, &size)) { |
||
527 | ipc_answer_0(callid, EINVAL); |
||
528 | ipc_answer_0(rid, EINVAL); |
||
529 | return; |
||
530 | } |
||
531 | |||
532 | /* |
||
533 | * Check whether the file needs to grow. |
||
534 | */ |
||
4420 | trochtova | 535 | if (pos + size <= nodep->size) { |
4419 | trochtova | 536 | /* The file size is not changing. */ |
4420 | trochtova | 537 | (void) ipc_data_write_finalize(callid, nodep->data + pos, size); |
538 | ipc_answer_2(rid, EOK, size, nodep->size); |
||
4419 | trochtova | 539 | return; |
540 | } |
||
4420 | trochtova | 541 | size_t delta = (pos + size) - nodep->size; |
4419 | trochtova | 542 | /* |
543 | * At this point, we are deliberately extremely straightforward and |
||
544 | * simply realloc the contents of the file on every write that grows the |
||
545 | * file. In the end, the situation might not be as bad as it may look: |
||
546 | * our heap allocator can save us and just grow the block whenever |
||
547 | * possible. |
||
548 | */ |
||
4420 | trochtova | 549 | void *newdata = realloc(nodep->data, nodep->size + delta); |
4419 | trochtova | 550 | if (!newdata) { |
551 | ipc_answer_0(callid, ENOMEM); |
||
4420 | trochtova | 552 | ipc_answer_2(rid, EOK, 0, nodep->size); |
4419 | trochtova | 553 | return; |
554 | } |
||
555 | /* Clear any newly allocated memory in order to emulate gaps. */ |
||
4420 | trochtova | 556 | memset(newdata + nodep->size, 0, delta); |
557 | nodep->size += delta; |
||
558 | nodep->data = newdata; |
||
559 | (void) ipc_data_write_finalize(callid, nodep->data + pos, size); |
||
560 | ipc_answer_2(rid, EOK, size, nodep->size); |
||
4419 | trochtova | 561 | } |
562 | |||
563 | void tmpfs_truncate(ipc_callid_t rid, ipc_call_t *request) |
||
564 | { |
||
565 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
||
566 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); |
||
567 | size_t size = (off_t)IPC_GET_ARG3(*request); |
||
568 | |||
569 | /* |
||
4420 | trochtova | 570 | * Lookup the respective TMPFS node. |
4419 | trochtova | 571 | */ |
572 | link_t *hlp; |
||
4420 | trochtova | 573 | unsigned long key[] = { |
574 | [NODES_KEY_INDEX] = index, |
||
575 | [NODES_KEY_DEV] = dev_handle |
||
576 | }; |
||
577 | hlp = hash_table_find(&nodes, key); |
||
4419 | trochtova | 578 | if (!hlp) { |
579 | ipc_answer_0(rid, ENOENT); |
||
580 | return; |
||
581 | } |
||
4420 | trochtova | 582 | tmpfs_node_t *nodep = hash_table_get_instance(hlp, tmpfs_node_t, |
583 | nh_link); |
||
4419 | trochtova | 584 | |
4420 | trochtova | 585 | if (size == nodep->size) { |
4419 | trochtova | 586 | ipc_answer_0(rid, EOK); |
587 | return; |
||
588 | } |
||
589 | |||
4420 | trochtova | 590 | void *newdata = realloc(nodep->data, size); |
4419 | trochtova | 591 | if (!newdata) { |
592 | ipc_answer_0(rid, ENOMEM); |
||
593 | return; |
||
594 | } |
||
4420 | trochtova | 595 | if (size > nodep->size) { |
596 | size_t delta = size - nodep->size; |
||
597 | memset(newdata + nodep->size, 0, delta); |
||
4419 | trochtova | 598 | } |
4420 | trochtova | 599 | nodep->size = size; |
600 | nodep->data = newdata; |
||
4419 | trochtova | 601 | ipc_answer_0(rid, EOK); |
602 | } |
||
603 | |||
604 | void tmpfs_destroy(ipc_callid_t rid, ipc_call_t *request) |
||
605 | { |
||
606 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
||
607 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); |
||
608 | int rc; |
||
609 | |||
610 | link_t *hlp; |
||
4420 | trochtova | 611 | unsigned long key[] = { |
612 | [NODES_KEY_INDEX] = index, |
||
613 | [NODES_KEY_DEV] = dev_handle |
||
614 | }; |
||
615 | hlp = hash_table_find(&nodes, key); |
||
4419 | trochtova | 616 | if (!hlp) { |
617 | ipc_answer_0(rid, ENOENT); |
||
618 | return; |
||
619 | } |
||
4420 | trochtova | 620 | tmpfs_node_t *nodep = hash_table_get_instance(hlp, tmpfs_node_t, |
621 | nh_link); |
||
622 | rc = tmpfs_destroy_node(FS_NODE(nodep)); |
||
4419 | trochtova | 623 | ipc_answer_0(rid, rc); |
624 | } |
||
625 | |||
626 | /** |
||
627 | * @} |
||
628 | */ |