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 |
|
---|
57 | #define DENTRIES_BUCKETS 256
|
---|
58 |
|
---|
59 | #define NAMES_BUCKETS 4
|
---|
60 |
|
---|
61 | /*
|
---|
62 | * For now, we don't distinguish between different dev_handles/instances. All
|
---|
63 | * requests resolve to the only instance, rooted in the following variable.
|
---|
64 | */
|
---|
65 | static tmpfs_dentry_t *root;
|
---|
66 |
|
---|
67 | /*
|
---|
68 | * Implementation of the libfs interface.
|
---|
69 | */
|
---|
70 |
|
---|
71 | /* Forward declarations of static functions. */
|
---|
72 | static bool tmpfs_match(void *, void *, const char *);
|
---|
73 | static void *tmpfs_create_node(int);
|
---|
74 | static bool tmpfs_link_node(void *, void *, const char *);
|
---|
75 | static int tmpfs_unlink_node(void *, void *);
|
---|
76 | static void tmpfs_destroy_node(void *);
|
---|
77 |
|
---|
78 | /* Implementation of helper functions. */
|
---|
79 | static unsigned long tmpfs_index_get(void *nodep)
|
---|
80 | {
|
---|
81 | return ((tmpfs_dentry_t *) nodep)->index;
|
---|
82 | }
|
---|
83 |
|
---|
84 | static unsigned long tmpfs_size_get(void *nodep)
|
---|
85 | {
|
---|
86 | return ((tmpfs_dentry_t *) nodep)->size;
|
---|
87 | }
|
---|
88 |
|
---|
89 | static unsigned tmpfs_lnkcnt_get(void *nodep)
|
---|
90 | {
|
---|
91 | return ((tmpfs_dentry_t *) nodep)->lnkcnt;
|
---|
92 | }
|
---|
93 |
|
---|
94 | static void *tmpfs_child_get(void *nodep)
|
---|
95 | {
|
---|
96 | return ((tmpfs_dentry_t *) nodep)->child;
|
---|
97 | }
|
---|
98 |
|
---|
99 | static void *tmpfs_sibling_get(void *nodep)
|
---|
100 | {
|
---|
101 | return ((tmpfs_dentry_t *) nodep)->sibling;
|
---|
102 | }
|
---|
103 |
|
---|
104 | static void *tmpfs_root_get(void)
|
---|
105 | {
|
---|
106 | return root;
|
---|
107 | }
|
---|
108 |
|
---|
109 | static char tmpfs_plb_get_char(unsigned pos)
|
---|
110 | {
|
---|
111 | return tmpfs_reg.plb_ro[pos % PLB_SIZE];
|
---|
112 | }
|
---|
113 |
|
---|
114 | static bool tmpfs_is_directory(void *nodep)
|
---|
115 | {
|
---|
116 | return ((tmpfs_dentry_t *) nodep)->type == TMPFS_DIRECTORY;
|
---|
117 | }
|
---|
118 |
|
---|
119 | static bool tmpfs_is_file(void *nodep)
|
---|
120 | {
|
---|
121 | return ((tmpfs_dentry_t *) nodep)->type == TMPFS_FILE;
|
---|
122 | }
|
---|
123 |
|
---|
124 | /** libfs operations */
|
---|
125 | libfs_ops_t tmpfs_libfs_ops = {
|
---|
126 | .match = tmpfs_match,
|
---|
127 | .create = tmpfs_create_node,
|
---|
128 | .destroy = tmpfs_destroy_node,
|
---|
129 | .link = tmpfs_link_node,
|
---|
130 | .unlink = tmpfs_unlink_node,
|
---|
131 | .index_get = tmpfs_index_get,
|
---|
132 | .size_get = tmpfs_size_get,
|
---|
133 | .lnkcnt_get = tmpfs_lnkcnt_get,
|
---|
134 | .child_get = tmpfs_child_get,
|
---|
135 | .sibling_get = tmpfs_sibling_get,
|
---|
136 | .root_get = tmpfs_root_get,
|
---|
137 | .plb_get_char = tmpfs_plb_get_char,
|
---|
138 | .is_directory = tmpfs_is_directory,
|
---|
139 | .is_file = tmpfs_is_file
|
---|
140 | };
|
---|
141 |
|
---|
142 | /** Hash table of all directory entries. */
|
---|
143 | hash_table_t dentries;
|
---|
144 |
|
---|
145 | /* Implementation of hash table interface for the dentries hash table. */
|
---|
146 | static hash_index_t dentries_hash(unsigned long *key)
|
---|
147 | {
|
---|
148 | return *key % DENTRIES_BUCKETS;
|
---|
149 | }
|
---|
150 |
|
---|
151 | static int dentries_compare(unsigned long *key, hash_count_t keys,
|
---|
152 | link_t *item)
|
---|
153 | {
|
---|
154 | tmpfs_dentry_t *dentry = hash_table_get_instance(item, tmpfs_dentry_t,
|
---|
155 | dh_link);
|
---|
156 | return dentry->index == *key;
|
---|
157 | }
|
---|
158 |
|
---|
159 | static void dentries_remove_callback(link_t *item)
|
---|
160 | {
|
---|
161 | }
|
---|
162 |
|
---|
163 | /** TMPFS dentries hash table operations. */
|
---|
164 | hash_table_operations_t dentries_ops = {
|
---|
165 | .hash = dentries_hash,
|
---|
166 | .compare = dentries_compare,
|
---|
167 | .remove_callback = dentries_remove_callback
|
---|
168 | };
|
---|
169 |
|
---|
170 | unsigned tmpfs_next_index = 1;
|
---|
171 |
|
---|
172 | typedef struct {
|
---|
173 | char *name;
|
---|
174 | tmpfs_dentry_t *parent;
|
---|
175 | link_t link;
|
---|
176 | } tmpfs_name_t;
|
---|
177 |
|
---|
178 | /* Implementation of hash table interface for the names hash table. */
|
---|
179 | static hash_index_t names_hash(unsigned long *key)
|
---|
180 | {
|
---|
181 | tmpfs_dentry_t *dentry = (tmpfs_dentry_t *) *key;
|
---|
182 | return dentry->index % NAMES_BUCKETS;
|
---|
183 | }
|
---|
184 |
|
---|
185 | static int names_compare(unsigned long *key, hash_count_t keys, link_t *item)
|
---|
186 | {
|
---|
187 | tmpfs_dentry_t *dentry = (tmpfs_dentry_t *) *key;
|
---|
188 | tmpfs_name_t *namep = hash_table_get_instance(item, tmpfs_name_t,
|
---|
189 | link);
|
---|
190 | return dentry == namep->parent;
|
---|
191 | }
|
---|
192 |
|
---|
193 | static void names_remove_callback(link_t *item)
|
---|
194 | {
|
---|
195 | tmpfs_name_t *namep = hash_table_get_instance(item, tmpfs_name_t,
|
---|
196 | link);
|
---|
197 | free(namep->name);
|
---|
198 | free(namep);
|
---|
199 | }
|
---|
200 |
|
---|
201 | /** TMPFS node names hash table operations. */
|
---|
202 | static hash_table_operations_t names_ops = {
|
---|
203 | .hash = names_hash,
|
---|
204 | .compare = names_compare,
|
---|
205 | .remove_callback = names_remove_callback
|
---|
206 | };
|
---|
207 |
|
---|
208 | static void tmpfs_name_initialize(tmpfs_name_t *namep)
|
---|
209 | {
|
---|
210 | namep->name = NULL;
|
---|
211 | namep->parent = NULL;
|
---|
212 | link_initialize(&namep->link);
|
---|
213 | }
|
---|
214 |
|
---|
215 | static bool tmpfs_dentry_initialize(tmpfs_dentry_t *dentry)
|
---|
216 | {
|
---|
217 | dentry->index = 0;
|
---|
218 | dentry->sibling = NULL;
|
---|
219 | dentry->child = NULL;
|
---|
220 | dentry->type = TMPFS_NONE;
|
---|
221 | dentry->lnkcnt = 0;
|
---|
222 | dentry->size = 0;
|
---|
223 | dentry->data = NULL;
|
---|
224 | link_initialize(&dentry->dh_link);
|
---|
225 | return (bool)hash_table_create(&dentry->names, NAMES_BUCKETS, 1,
|
---|
226 | &names_ops);
|
---|
227 | }
|
---|
228 |
|
---|
229 | static bool tmpfs_init(void)
|
---|
230 | {
|
---|
231 | if (!hash_table_create(&dentries, DENTRIES_BUCKETS, 1, &dentries_ops))
|
---|
232 | return false;
|
---|
233 | root = (tmpfs_dentry_t *) tmpfs_create_node(L_DIRECTORY);
|
---|
234 | if (!root) {
|
---|
235 | hash_table_destroy(&dentries);
|
---|
236 | return false;
|
---|
237 | }
|
---|
238 | root->lnkcnt = 1;
|
---|
239 | return true;
|
---|
240 | }
|
---|
241 |
|
---|
242 | /** Compare one component of path to a directory entry.
|
---|
243 | *
|
---|
244 | * @param prnt Node from which we descended.
|
---|
245 | * @param chld Node to compare the path component with.
|
---|
246 | * @param component Array of characters holding component name.
|
---|
247 | *
|
---|
248 | * @return True on match, false otherwise.
|
---|
249 | */
|
---|
250 | bool tmpfs_match(void *prnt, void *chld, const char *component)
|
---|
251 | {
|
---|
252 | tmpfs_dentry_t *parentp = (tmpfs_dentry_t *) prnt;
|
---|
253 | tmpfs_dentry_t *childp = (tmpfs_dentry_t *) chld;
|
---|
254 |
|
---|
255 | unsigned long key = (unsigned long) parentp;
|
---|
256 | link_t *hlp = hash_table_find(&childp->names, &key);
|
---|
257 | assert(hlp);
|
---|
258 | tmpfs_name_t *namep = hash_table_get_instance(hlp, tmpfs_name_t, link);
|
---|
259 |
|
---|
260 | return !strcmp(namep->name, component);
|
---|
261 | }
|
---|
262 |
|
---|
263 | void *tmpfs_create_node(int lflag)
|
---|
264 | {
|
---|
265 | assert((lflag & L_FILE) ^ (lflag & L_DIRECTORY));
|
---|
266 |
|
---|
267 | tmpfs_dentry_t *node = malloc(sizeof(tmpfs_dentry_t));
|
---|
268 | if (!node)
|
---|
269 | return NULL;
|
---|
270 |
|
---|
271 | if (!tmpfs_dentry_initialize(node)) {
|
---|
272 | free(node);
|
---|
273 | return NULL;
|
---|
274 | }
|
---|
275 | node->index = tmpfs_next_index++;
|
---|
276 | if (lflag & L_DIRECTORY)
|
---|
277 | node->type = TMPFS_DIRECTORY;
|
---|
278 | else
|
---|
279 | node->type = TMPFS_FILE;
|
---|
280 |
|
---|
281 | /* Insert the new node into the dentry hash table. */
|
---|
282 | hash_table_insert(&dentries, &node->index, &node->dh_link);
|
---|
283 | return (void *) node;
|
---|
284 | }
|
---|
285 |
|
---|
286 | bool tmpfs_link_node(void *prnt, void *chld, const char *nm)
|
---|
287 | {
|
---|
288 | tmpfs_dentry_t *parentp = (tmpfs_dentry_t *) prnt;
|
---|
289 | tmpfs_dentry_t *childp = (tmpfs_dentry_t *) chld;
|
---|
290 |
|
---|
291 | assert(parentp->type == TMPFS_DIRECTORY);
|
---|
292 |
|
---|
293 | tmpfs_name_t *namep = malloc(sizeof(tmpfs_name_t));
|
---|
294 | if (!namep)
|
---|
295 | return false;
|
---|
296 | tmpfs_name_initialize(namep);
|
---|
297 | size_t len = strlen(nm);
|
---|
298 | namep->name = malloc(len + 1);
|
---|
299 | if (!namep->name) {
|
---|
300 | free(namep);
|
---|
301 | return false;
|
---|
302 | }
|
---|
303 | strcpy(namep->name, nm);
|
---|
304 | namep->parent = parentp;
|
---|
305 |
|
---|
306 | childp->lnkcnt++;
|
---|
307 |
|
---|
308 | unsigned long key = (unsigned long) parentp;
|
---|
309 | hash_table_insert(&childp->names, &key, &namep->link);
|
---|
310 |
|
---|
311 | /* Insert the new node into the namespace. */
|
---|
312 | if (parentp->child) {
|
---|
313 | tmpfs_dentry_t *tmp = parentp->child;
|
---|
314 | while (tmp->sibling)
|
---|
315 | tmp = tmp->sibling;
|
---|
316 | tmp->sibling = childp;
|
---|
317 | } else {
|
---|
318 | parentp->child = childp;
|
---|
319 | }
|
---|
320 |
|
---|
321 | return true;
|
---|
322 | }
|
---|
323 |
|
---|
324 | int tmpfs_unlink_node(void *prnt, void *chld)
|
---|
325 | {
|
---|
326 | tmpfs_dentry_t *parentp = (tmpfs_dentry_t *)prnt;
|
---|
327 | tmpfs_dentry_t *childp = (tmpfs_dentry_t *)chld;
|
---|
328 |
|
---|
329 | if (!parentp)
|
---|
330 | return EBUSY;
|
---|
331 |
|
---|
332 | if (childp->child)
|
---|
333 | return ENOTEMPTY;
|
---|
334 |
|
---|
335 | if (parentp->child == childp) {
|
---|
336 | parentp->child = childp->sibling;
|
---|
337 | } else {
|
---|
338 | /* TODO: consider doubly linked list for organizing siblings. */
|
---|
339 | tmpfs_dentry_t *tmp = parentp->child;
|
---|
340 | while (tmp->sibling != childp)
|
---|
341 | tmp = tmp->sibling;
|
---|
342 | tmp->sibling = childp->sibling;
|
---|
343 | }
|
---|
344 | childp->sibling = NULL;
|
---|
345 |
|
---|
346 | unsigned long key = (unsigned long) parentp;
|
---|
347 | hash_table_remove(&childp->names, &key, 1);
|
---|
348 |
|
---|
349 | childp->lnkcnt--;
|
---|
350 |
|
---|
351 | return EOK;
|
---|
352 | }
|
---|
353 |
|
---|
354 | void tmpfs_destroy_node(void *nodep)
|
---|
355 | {
|
---|
356 | tmpfs_dentry_t *dentry = (tmpfs_dentry_t *) nodep;
|
---|
357 |
|
---|
358 | assert(!dentry->lnkcnt);
|
---|
359 | assert(!dentry->child);
|
---|
360 | assert(!dentry->sibling);
|
---|
361 |
|
---|
362 | unsigned long index = dentry->index;
|
---|
363 | hash_table_remove(&dentries, &index, 1);
|
---|
364 |
|
---|
365 | hash_table_destroy(&dentry->names);
|
---|
366 |
|
---|
367 | if (dentry->type == TMPFS_FILE)
|
---|
368 | free(dentry->data);
|
---|
369 | free(dentry);
|
---|
370 | }
|
---|
371 |
|
---|
372 | void tmpfs_lookup(ipc_callid_t rid, ipc_call_t *request)
|
---|
373 | {
|
---|
374 | /* Initialize TMPFS. */
|
---|
375 | if (!root && !tmpfs_init()) {
|
---|
376 | ipc_answer_0(rid, ENOMEM);
|
---|
377 | return;
|
---|
378 | }
|
---|
379 | libfs_lookup(&tmpfs_libfs_ops, tmpfs_reg.fs_handle, rid, request);
|
---|
380 | }
|
---|
381 |
|
---|
382 | void tmpfs_read(ipc_callid_t rid, ipc_call_t *request)
|
---|
383 | {
|
---|
384 | int dev_handle = IPC_GET_ARG1(*request);
|
---|
385 | unsigned long index = IPC_GET_ARG2(*request);
|
---|
386 | off_t pos = IPC_GET_ARG3(*request);
|
---|
387 |
|
---|
388 | /*
|
---|
389 | * Lookup the respective dentry.
|
---|
390 | */
|
---|
391 | link_t *hlp;
|
---|
392 | hlp = hash_table_find(&dentries, &index);
|
---|
393 | if (!hlp) {
|
---|
394 | ipc_answer_0(rid, ENOENT);
|
---|
395 | return;
|
---|
396 | }
|
---|
397 | tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t,
|
---|
398 | dh_link);
|
---|
399 |
|
---|
400 | /*
|
---|
401 | * Receive the read request.
|
---|
402 | */
|
---|
403 | ipc_callid_t callid;
|
---|
404 | size_t len;
|
---|
405 | if (!ipc_data_read_receive(&callid, &len)) {
|
---|
406 | ipc_answer_0(callid, EINVAL);
|
---|
407 | ipc_answer_0(rid, EINVAL);
|
---|
408 | return;
|
---|
409 | }
|
---|
410 |
|
---|
411 | size_t bytes;
|
---|
412 | if (dentry->type == TMPFS_FILE) {
|
---|
413 | bytes = max(0, min(dentry->size - pos, len));
|
---|
414 | (void) ipc_data_read_finalize(callid, dentry->data + pos,
|
---|
415 | bytes);
|
---|
416 | } else {
|
---|
417 | int i;
|
---|
418 | tmpfs_dentry_t *cur;
|
---|
419 |
|
---|
420 | assert(dentry->type == TMPFS_DIRECTORY);
|
---|
421 |
|
---|
422 | /*
|
---|
423 | * Yes, we really use O(n) algorithm here.
|
---|
424 | * If it bothers someone, it could be fixed by introducing a
|
---|
425 | * hash table.
|
---|
426 | */
|
---|
427 | for (i = 0, cur = dentry->child; i < pos && cur; i++,
|
---|
428 | cur = cur->sibling)
|
---|
429 | ;
|
---|
430 |
|
---|
431 | if (!cur) {
|
---|
432 | ipc_answer_0(callid, ENOENT);
|
---|
433 | ipc_answer_1(rid, ENOENT, 0);
|
---|
434 | return;
|
---|
435 | }
|
---|
436 |
|
---|
437 | unsigned long key = (unsigned long) dentry;
|
---|
438 | link_t *hlp = hash_table_find(&cur->names, &key);
|
---|
439 | assert(hlp);
|
---|
440 | tmpfs_name_t *namep = hash_table_get_instance(hlp, tmpfs_name_t,
|
---|
441 | link);
|
---|
442 |
|
---|
443 | (void) ipc_data_read_finalize(callid, namep->name,
|
---|
444 | strlen(namep->name) + 1);
|
---|
445 | bytes = 1;
|
---|
446 | }
|
---|
447 |
|
---|
448 | /*
|
---|
449 | * Answer the VFS_READ call.
|
---|
450 | */
|
---|
451 | ipc_answer_1(rid, EOK, bytes);
|
---|
452 | }
|
---|
453 |
|
---|
454 | void tmpfs_write(ipc_callid_t rid, ipc_call_t *request)
|
---|
455 | {
|
---|
456 | int dev_handle = IPC_GET_ARG1(*request);
|
---|
457 | unsigned long index = IPC_GET_ARG2(*request);
|
---|
458 | off_t pos = IPC_GET_ARG3(*request);
|
---|
459 |
|
---|
460 | /*
|
---|
461 | * Lookup the respective dentry.
|
---|
462 | */
|
---|
463 | link_t *hlp;
|
---|
464 | hlp = hash_table_find(&dentries, &index);
|
---|
465 | if (!hlp) {
|
---|
466 | ipc_answer_0(rid, ENOENT);
|
---|
467 | return;
|
---|
468 | }
|
---|
469 | tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t,
|
---|
470 | dh_link);
|
---|
471 |
|
---|
472 | /*
|
---|
473 | * Receive the write request.
|
---|
474 | */
|
---|
475 | ipc_callid_t callid;
|
---|
476 | size_t len;
|
---|
477 | if (!ipc_data_write_receive(&callid, &len)) {
|
---|
478 | ipc_answer_0(callid, EINVAL);
|
---|
479 | ipc_answer_0(rid, EINVAL);
|
---|
480 | return;
|
---|
481 | }
|
---|
482 |
|
---|
483 | /*
|
---|
484 | * Check whether the file needs to grow.
|
---|
485 | */
|
---|
486 | if (pos + len <= dentry->size) {
|
---|
487 | /* The file size is not changing. */
|
---|
488 | (void) ipc_data_write_finalize(callid, dentry->data + pos, len);
|
---|
489 | ipc_answer_2(rid, EOK, len, dentry->size);
|
---|
490 | return;
|
---|
491 | }
|
---|
492 | size_t delta = (pos + len) - dentry->size;
|
---|
493 | /*
|
---|
494 | * At this point, we are deliberately extremely straightforward and
|
---|
495 | * simply realloc the contents of the file on every write that grows the
|
---|
496 | * file. In the end, the situation might not be as bad as it may look:
|
---|
497 | * our heap allocator can save us and just grow the block whenever
|
---|
498 | * possible.
|
---|
499 | */
|
---|
500 | void *newdata = realloc(dentry->data, dentry->size + delta);
|
---|
501 | if (!newdata) {
|
---|
502 | ipc_answer_0(callid, ENOMEM);
|
---|
503 | ipc_answer_2(rid, EOK, 0, dentry->size);
|
---|
504 | return;
|
---|
505 | }
|
---|
506 | /* Clear any newly allocated memory in order to emulate gaps. */
|
---|
507 | memset(newdata + dentry->size, 0, delta);
|
---|
508 | dentry->size += delta;
|
---|
509 | dentry->data = newdata;
|
---|
510 | (void) ipc_data_write_finalize(callid, dentry->data + pos, len);
|
---|
511 | ipc_answer_2(rid, EOK, len, dentry->size);
|
---|
512 | }
|
---|
513 |
|
---|
514 | void tmpfs_truncate(ipc_callid_t rid, ipc_call_t *request)
|
---|
515 | {
|
---|
516 | int dev_handle = IPC_GET_ARG1(*request);
|
---|
517 | unsigned long index = IPC_GET_ARG2(*request);
|
---|
518 | size_t size = IPC_GET_ARG3(*request);
|
---|
519 |
|
---|
520 | /*
|
---|
521 | * Lookup the respective dentry.
|
---|
522 | */
|
---|
523 | link_t *hlp;
|
---|
524 | hlp = hash_table_find(&dentries, &index);
|
---|
525 | if (!hlp) {
|
---|
526 | ipc_answer_0(rid, ENOENT);
|
---|
527 | return;
|
---|
528 | }
|
---|
529 | tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t,
|
---|
530 | dh_link);
|
---|
531 |
|
---|
532 | if (size == dentry->size) {
|
---|
533 | ipc_answer_0(rid, EOK);
|
---|
534 | return;
|
---|
535 | }
|
---|
536 |
|
---|
537 | void *newdata = realloc(dentry->data, size);
|
---|
538 | if (!newdata) {
|
---|
539 | ipc_answer_0(rid, ENOMEM);
|
---|
540 | return;
|
---|
541 | }
|
---|
542 | if (size > dentry->size) {
|
---|
543 | size_t delta = size - dentry->size;
|
---|
544 | memset(newdata + dentry->size, 0, delta);
|
---|
545 | }
|
---|
546 | dentry->size = size;
|
---|
547 | dentry->data = newdata;
|
---|
548 | ipc_answer_0(rid, EOK);
|
---|
549 | }
|
---|
550 |
|
---|
551 | void tmpfs_destroy(ipc_callid_t rid, ipc_call_t *request)
|
---|
552 | {
|
---|
553 | int dev_handle = IPC_GET_ARG1(*request);
|
---|
554 | unsigned long index = IPC_GET_ARG2(*request);
|
---|
555 |
|
---|
556 | link_t *hlp;
|
---|
557 | hlp = hash_table_find(&dentries, &index);
|
---|
558 | if (!hlp) {
|
---|
559 | ipc_answer_0(rid, ENOENT);
|
---|
560 | return;
|
---|
561 | }
|
---|
562 | tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t,
|
---|
563 | dh_link);
|
---|
564 | tmpfs_destroy_node(dentry);
|
---|
565 | ipc_answer_0(rid, EOK);
|
---|
566 | }
|
---|
567 |
|
---|
568 | /**
|
---|
569 | * @}
|
---|
570 | */
|
---|