Subversion Repositories HelenOS

Rev

Rev 3009 | Rev 3153 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3009 Rev 3150
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
#include <libfs.h>
52
#include <libfs.h>
-
 
53
#include <ipc/services.h>
-
 
54
#include <ipc/devmap.h>
-
 
55
#include <sys/mman.h>
-
 
56
#include <byteorder.h>
53
 
57
 
54
#define min(a, b)       ((a) < (b) ? (a) : (b))
58
#define min(a, b)       ((a) < (b) ? (a) : (b))
55
#define max(a, b)       ((a) > (b) ? (a) : (b))
59
#define max(a, b)       ((a) > (b) ? (a) : (b))
56
 
60
 
57
#define DENTRIES_BUCKETS    256
61
#define DENTRIES_BUCKETS    256
58
 
62
 
59
#define NAMES_BUCKETS       4
63
#define NAMES_BUCKETS       4
60
 
64
 
-
 
65
#define BLOCK_SIZE          1024    // FIXME
-
 
66
#define RD_BASE             1024    // FIXME
-
 
67
#define RD_READ_BLOCK   (RD_BASE + 1)
-
 
68
 
61
/*
69
/*
62
 * For now, we don't distinguish between different dev_handles/instances. All
70
 * For now, we don't distinguish between different dev_handles/instances. All
63
 * requests resolve to the only instance, rooted in the following variable.
71
 * requests resolve to the only instance, rooted in the following variable.
64
 */
72
 */
65
static tmpfs_dentry_t *root;
73
static tmpfs_dentry_t *root;
66
 
74
 
67
/*
75
/*
68
 * Implementation of the libfs interface.
76
 * Implementation of the libfs interface.
69
 */
77
 */
70
 
78
 
71
/* Forward declarations of static functions. */
79
/* Forward declarations of static functions. */
72
static void *tmpfs_match(void *, const char *);
80
static void *tmpfs_match(void *, const char *);
73
static void *tmpfs_node_get(dev_handle_t, fs_index_t);
81
static void *tmpfs_node_get(dev_handle_t, fs_index_t);
74
static void tmpfs_node_put(void *);
82
static void tmpfs_node_put(void *);
75
static void *tmpfs_create_node(int);
83
static void *tmpfs_create_node(int);
76
static bool tmpfs_link_node(void *, void *, const char *);
84
static bool tmpfs_link_node(void *, void *, const char *);
77
static int tmpfs_unlink_node(void *, void *);
85
static int tmpfs_unlink_node(void *, void *);
78
static int tmpfs_destroy_node(void *);
86
static int tmpfs_destroy_node(void *);
79
 
87
 
80
/* Implementation of helper functions. */
88
/* Implementation of helper functions. */
81
static fs_index_t tmpfs_index_get(void *nodep)
89
static fs_index_t tmpfs_index_get(void *nodep)
82
{
90
{
83
    return ((tmpfs_dentry_t *) nodep)->index;
91
    return ((tmpfs_dentry_t *) nodep)->index;
84
}
92
}
85
 
93
 
86
static size_t tmpfs_size_get(void *nodep)
94
static size_t tmpfs_size_get(void *nodep)
87
{
95
{
88
    return ((tmpfs_dentry_t *) nodep)->size;
96
    return ((tmpfs_dentry_t *) nodep)->size;
89
}
97
}
90
 
98
 
91
static unsigned tmpfs_lnkcnt_get(void *nodep)
99
static unsigned tmpfs_lnkcnt_get(void *nodep)
92
{
100
{
93
    return ((tmpfs_dentry_t *) nodep)->lnkcnt;
101
    return ((tmpfs_dentry_t *) nodep)->lnkcnt;
94
}
102
}
95
 
103
 
96
static bool tmpfs_has_children(void *nodep)
104
static bool tmpfs_has_children(void *nodep)
97
{
105
{
98
    return ((tmpfs_dentry_t *) nodep)->child != NULL;
106
    return ((tmpfs_dentry_t *) nodep)->child != NULL;
99
}
107
}
100
 
108
 
101
static void *tmpfs_root_get(dev_handle_t dev_handle)
109
static void *tmpfs_root_get(dev_handle_t dev_handle)
102
{
110
{
103
    return root;
111
    return root;
104
}
112
}
105
 
113
 
106
static char tmpfs_plb_get_char(unsigned pos)
114
static char tmpfs_plb_get_char(unsigned pos)
107
{
115
{
108
    return tmpfs_reg.plb_ro[pos % PLB_SIZE];
116
    return tmpfs_reg.plb_ro[pos % PLB_SIZE];
109
}
117
}
110
 
118
 
111
static bool tmpfs_is_directory(void *nodep)
119
static bool tmpfs_is_directory(void *nodep)
112
{
120
{
113
    return ((tmpfs_dentry_t *) nodep)->type == TMPFS_DIRECTORY;
121
    return ((tmpfs_dentry_t *) nodep)->type == TMPFS_DIRECTORY;
114
}
122
}
115
 
123
 
116
static bool tmpfs_is_file(void *nodep)
124
static bool tmpfs_is_file(void *nodep)
117
{
125
{
118
    return ((tmpfs_dentry_t *) nodep)->type == TMPFS_FILE;
126
    return ((tmpfs_dentry_t *) nodep)->type == TMPFS_FILE;
119
}
127
}
120
 
128
 
121
/** libfs operations */
129
/** libfs operations */
122
libfs_ops_t tmpfs_libfs_ops = {
130
libfs_ops_t tmpfs_libfs_ops = {
123
    .match = tmpfs_match,
131
    .match = tmpfs_match,
124
    .node_get = tmpfs_node_get,
132
    .node_get = tmpfs_node_get,
125
    .node_put = tmpfs_node_put,
133
    .node_put = tmpfs_node_put,
126
    .create = tmpfs_create_node,
134
    .create = tmpfs_create_node,
127
    .destroy = tmpfs_destroy_node,
135
    .destroy = tmpfs_destroy_node,
128
    .link = tmpfs_link_node,
136
    .link = tmpfs_link_node,
129
    .unlink = tmpfs_unlink_node,
137
    .unlink = tmpfs_unlink_node,
130
    .index_get = tmpfs_index_get,
138
    .index_get = tmpfs_index_get,
131
    .size_get = tmpfs_size_get,
139
    .size_get = tmpfs_size_get,
132
    .lnkcnt_get = tmpfs_lnkcnt_get,
140
    .lnkcnt_get = tmpfs_lnkcnt_get,
133
    .has_children = tmpfs_has_children,
141
    .has_children = tmpfs_has_children,
134
    .root_get = tmpfs_root_get,
142
    .root_get = tmpfs_root_get,
135
    .plb_get_char = tmpfs_plb_get_char,
143
    .plb_get_char = tmpfs_plb_get_char,
136
    .is_directory = tmpfs_is_directory,
144
    .is_directory = tmpfs_is_directory,
137
    .is_file = tmpfs_is_file
145
    .is_file = tmpfs_is_file
138
};
146
};
139
 
147
 
140
/** Hash table of all directory entries. */
148
/** Hash table of all directory entries. */
141
hash_table_t dentries;
149
hash_table_t dentries;
142
 
150
 
-
 
151
struct rdentry {
-
 
152
    uint8_t type;
-
 
153
    uint32_t len;
-
 
154
} __attribute__((packed));
-
 
155
 
143
/* Implementation of hash table interface for the dentries hash table. */
156
/* Implementation of hash table interface for the dentries hash table. */
144
static hash_index_t dentries_hash(unsigned long *key)
157
static hash_index_t dentries_hash(unsigned long *key)
145
{
158
{
146
    return *key % DENTRIES_BUCKETS;
159
    return *key % DENTRIES_BUCKETS;
147
}
160
}
148
 
161
 
149
static int dentries_compare(unsigned long *key, hash_count_t keys,
162
static int dentries_compare(unsigned long *key, hash_count_t keys,
150
    link_t *item)
163
    link_t *item)
151
{
164
{
152
    tmpfs_dentry_t *dentry = hash_table_get_instance(item, tmpfs_dentry_t,
165
    tmpfs_dentry_t *dentry = hash_table_get_instance(item, tmpfs_dentry_t,
153
        dh_link);
166
        dh_link);
154
    return dentry->index == *key;
167
    return dentry->index == *key;
155
}
168
}
156
 
169
 
157
static void dentries_remove_callback(link_t *item)
170
static void dentries_remove_callback(link_t *item)
158
{
171
{
159
}
172
}
160
 
173
 
161
/** TMPFS dentries hash table operations. */
174
/** TMPFS dentries hash table operations. */
162
hash_table_operations_t dentries_ops = {
175
hash_table_operations_t dentries_ops = {
163
    .hash = dentries_hash,
176
    .hash = dentries_hash,
164
    .compare = dentries_compare,
177
    .compare = dentries_compare,
165
    .remove_callback = dentries_remove_callback
178
    .remove_callback = dentries_remove_callback
166
};
179
};
167
 
180
 
168
fs_index_t tmpfs_next_index = 1;
181
fs_index_t tmpfs_next_index = 1;
169
 
182
 
170
typedef struct {
183
typedef struct {
171
    char *name;
184
    char *name;
172
    tmpfs_dentry_t *parent;
185
    tmpfs_dentry_t *parent;
173
    link_t link;
186
    link_t link;
174
} tmpfs_name_t;
187
} tmpfs_name_t;
175
 
188
 
176
/* Implementation of hash table interface for the names hash table. */
189
/* Implementation of hash table interface for the names hash table. */
177
static hash_index_t names_hash(unsigned long *key)
190
static hash_index_t names_hash(unsigned long *key)
178
{
191
{
179
    tmpfs_dentry_t *dentry = (tmpfs_dentry_t *) *key;
192
    tmpfs_dentry_t *dentry = (tmpfs_dentry_t *) *key;
180
    return dentry->index % NAMES_BUCKETS;
193
    return dentry->index % NAMES_BUCKETS;
181
}
194
}
182
 
195
 
183
static int names_compare(unsigned long *key, hash_count_t keys, link_t *item)
196
static int names_compare(unsigned long *key, hash_count_t keys, link_t *item)
184
{
197
{
185
    tmpfs_dentry_t *dentry = (tmpfs_dentry_t *) *key;
198
    tmpfs_dentry_t *dentry = (tmpfs_dentry_t *) *key;
186
    tmpfs_name_t *namep = hash_table_get_instance(item, tmpfs_name_t,
199
    tmpfs_name_t *namep = hash_table_get_instance(item, tmpfs_name_t,
187
        link);
200
        link);
188
    return dentry == namep->parent;
201
    return dentry == namep->parent;
189
}
202
}
190
 
203
 
191
static void names_remove_callback(link_t *item)
204
static void names_remove_callback(link_t *item)
192
{
205
{
193
    tmpfs_name_t *namep = hash_table_get_instance(item, tmpfs_name_t,
206
    tmpfs_name_t *namep = hash_table_get_instance(item, tmpfs_name_t,
194
        link);
207
        link);
195
    free(namep->name);
208
    free(namep->name);
196
    free(namep);
209
    free(namep);
197
}
210
}
198
 
211
 
199
/** TMPFS node names hash table operations. */
212
/** TMPFS node names hash table operations. */
200
static hash_table_operations_t names_ops = {
213
static hash_table_operations_t names_ops = {
201
    .hash = names_hash,
214
    .hash = names_hash,
202
    .compare = names_compare,
215
    .compare = names_compare,
203
    .remove_callback = names_remove_callback
216
    .remove_callback = names_remove_callback
204
};
217
};
205
 
218
 
206
static void tmpfs_name_initialize(tmpfs_name_t *namep)
219
static void tmpfs_name_initialize(tmpfs_name_t *namep)
207
{
220
{
208
    namep->name = NULL;
221
    namep->name = NULL;
209
    namep->parent = NULL;
222
    namep->parent = NULL;
210
    link_initialize(&namep->link);
223
    link_initialize(&namep->link);
211
}
224
}
212
 
225
 
213
static bool tmpfs_dentry_initialize(tmpfs_dentry_t *dentry)
226
static bool tmpfs_dentry_initialize(tmpfs_dentry_t *dentry)
214
{
227
{
215
    dentry->index = 0;
228
    dentry->index = 0;
216
    dentry->sibling = NULL;
229
    dentry->sibling = NULL;
217
    dentry->child = NULL;
230
    dentry->child = NULL;
218
    dentry->type = TMPFS_NONE;
231
    dentry->type = TMPFS_NONE;
219
    dentry->lnkcnt = 0;
232
    dentry->lnkcnt = 0;
220
    dentry->size = 0;
233
    dentry->size = 0;
221
    dentry->data = NULL;
234
    dentry->data = NULL;
222
    link_initialize(&dentry->dh_link);
235
    link_initialize(&dentry->dh_link);
223
    return (bool)hash_table_create(&dentry->names, NAMES_BUCKETS, 1,
236
    return (bool)hash_table_create(&dentry->names, NAMES_BUCKETS, 1,
224
        &names_ops);
237
        &names_ops);
225
}
238
}
226
 
239
 
227
static bool tmpfs_init(void)
240
static bool tmpfs_init(void)
228
{
241
{
229
    if (!hash_table_create(&dentries, DENTRIES_BUCKETS, 1, &dentries_ops))
242
    if (!hash_table_create(&dentries, DENTRIES_BUCKETS, 1, &dentries_ops))
230
        return false;
243
        return false;
231
    root = (tmpfs_dentry_t *) tmpfs_create_node(L_DIRECTORY);
244
    root = (tmpfs_dentry_t *) tmpfs_create_node(L_DIRECTORY);
232
    if (!root) {
245
    if (!root) {
233
        hash_table_destroy(&dentries);
246
        hash_table_destroy(&dentries);
234
        return false;
247
        return false;
235
    }
248
    }
236
    root->lnkcnt = 1;
249
    root->lnkcnt = 1;
237
    return true;
250
    return true;
238
}
251
}
239
 
252
 
-
 
253
static bool tmpfs_blockread(int phone, void *buffer, size_t *bufpos, size_t *buflen, size_t *pos, void *dst, size_t size)
-
 
254
{
-
 
255
    size_t offset = 0;
-
 
256
    size_t left = size;
-
 
257
   
-
 
258
    while (left > 0) {
-
 
259
        size_t rd;
-
 
260
       
-
 
261
        if (*bufpos + left < *buflen)
-
 
262
            rd = left;
-
 
263
        else
-
 
264
            rd = *buflen - *bufpos;
-
 
265
       
-
 
266
        if (rd > 0) {
-
 
267
            memcpy(dst + offset, buffer + *bufpos, rd);
-
 
268
            offset += rd;
-
 
269
            *bufpos += rd;
-
 
270
            *pos += rd;
-
 
271
            left -= rd;
-
 
272
        }
-
 
273
       
-
 
274
        if (*bufpos == *buflen) {
-
 
275
            int retval;
-
 
276
            int rc = ipc_call_sync_2_1(phone, RD_READ_BLOCK, *pos / BLOCK_SIZE, BLOCK_SIZE, (sysarg_t *) &retval);
-
 
277
            if ((rc != EOK) || (retval != EOK))
-
 
278
                return false;
-
 
279
           
-
 
280
            *bufpos = 0;
-
 
281
            *buflen = BLOCK_SIZE;
-
 
282
        }
-
 
283
    }
-
 
284
   
-
 
285
    return true;
-
 
286
}
-
 
287
 
-
 
288
static bool tmpfs_restore_recursion(int phone, void *block, size_t *bufpos, size_t *buflen, size_t *pos, tmpfs_dentry_t *parent)
-
 
289
{
-
 
290
    struct rdentry entry;
-
 
291
   
-
 
292
    do {
-
 
293
        char *fname;
-
 
294
        tmpfs_dentry_t *node;
-
 
295
        uint32_t size;
-
 
296
       
-
 
297
        if (!tmpfs_blockread(phone, block, bufpos, buflen, pos, &entry, sizeof(entry)))
-
 
298
            return false;
-
 
299
       
-
 
300
        entry.len = uint32_t_le2host(entry.len);
-
 
301
       
-
 
302
        switch (entry.type) {
-
 
303
        case 0:
-
 
304
            break;
-
 
305
        case 1:
-
 
306
            fname = malloc(entry.len + 1);
-
 
307
            if (fname == NULL)
-
 
308
                return false;
-
 
309
           
-
 
310
            node = (tmpfs_dentry_t *) tmpfs_create_node(L_FILE);
-
 
311
            if (node == NULL) {
-
 
312
                free(fname);
-
 
313
                return false;
-
 
314
            }
-
 
315
           
-
 
316
            if (!tmpfs_blockread(phone, block, bufpos, buflen, pos, fname, entry.len)) {
-
 
317
                tmpfs_destroy_node((void *) node);
-
 
318
                free(fname);
-
 
319
                return false;
-
 
320
            }
-
 
321
            fname[entry.len] = 0;
-
 
322
           
-
 
323
            if (!tmpfs_link_node((void *) parent, (void *) node, fname)) {
-
 
324
                tmpfs_destroy_node((void *) node);
-
 
325
                free(fname);
-
 
326
                return false;
-
 
327
            }
-
 
328
            free(fname);
-
 
329
           
-
 
330
            if (!tmpfs_blockread(phone, block, bufpos, buflen, pos, &size, sizeof(size)))
-
 
331
                return false;
-
 
332
           
-
 
333
            size = uint32_t_le2host(size);
-
 
334
           
-
 
335
            node->data = malloc(size);
-
 
336
            if (node->data == NULL)
-
 
337
                return false;
-
 
338
           
-
 
339
            node->size = size;
-
 
340
            if (!tmpfs_blockread(phone, block, bufpos, buflen, pos, node->data, size))
-
 
341
                return false;
-
 
342
           
-
 
343
            break;
-
 
344
        case 2:
-
 
345
            fname = malloc(entry.len + 1);
-
 
346
            if (fname == NULL)
-
 
347
                return false;
-
 
348
           
-
 
349
            node = (tmpfs_dentry_t *) tmpfs_create_node(L_DIRECTORY);
-
 
350
            if (node == NULL) {
-
 
351
                free(fname);
-
 
352
                return false;
-
 
353
            }
-
 
354
           
-
 
355
            if (!tmpfs_blockread(phone, block, bufpos, buflen, pos, fname, entry.len)) {
-
 
356
                tmpfs_destroy_node((void *) node);
-
 
357
                free(fname);
-
 
358
                return false;
-
 
359
            }
-
 
360
            fname[entry.len] = 0;
-
 
361
           
-
 
362
            if (!tmpfs_link_node((void *) parent, (void *) node, fname)) {
-
 
363
                tmpfs_destroy_node((void *) node);
-
 
364
                free(fname);
-
 
365
                return false;
-
 
366
            }
-
 
367
            free(fname);
-
 
368
           
-
 
369
            if (!tmpfs_restore_recursion(phone, block, bufpos, buflen, pos, node))
-
 
370
                return false;
-
 
371
           
-
 
372
            break;
-
 
373
        default:
-
 
374
            return false;
-
 
375
        }
-
 
376
    } while (entry.type != 0);
-
 
377
   
-
 
378
    return true;
-
 
379
}
-
 
380
 
-
 
381
static bool tmpfs_restore(dev_handle_t dev)
-
 
382
{
-
 
383
    void *block = mmap(NULL, BLOCK_SIZE,
-
 
384
        PROTO_READ | PROTO_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
-
 
385
   
-
 
386
    if (block == NULL)
-
 
387
        return false;
-
 
388
   
-
 
389
    int phone = ipc_connect_me_to(PHONE_NS, SERVICE_DEVMAP, DEVMAP_CONNECT_TO_DEVICE, dev);
-
 
390
 
-
 
391
    if (phone < 0) {
-
 
392
        munmap(block, BLOCK_SIZE);
-
 
393
        return false;
-
 
394
    }
-
 
395
   
-
 
396
    if (ipc_share_out_start(phone, block, AS_AREA_READ | AS_AREA_WRITE) != EOK)
-
 
397
        goto error;
-
 
398
   
-
 
399
    size_t bufpos = 0;
-
 
400
    size_t buflen = 0;
-
 
401
    size_t pos = 0;
-
 
402
   
-
 
403
    char tag[6];
-
 
404
    if (!tmpfs_blockread(phone, block, &bufpos, &buflen, &pos, tag, 5))
-
 
405
        goto error;
-
 
406
   
-
 
407
    tag[5] = 0;
-
 
408
    if (strcmp(tag, "TMPFS") != 0)
-
 
409
        goto error;
-
 
410
   
-
 
411
    if (!tmpfs_restore_recursion(phone, block, &bufpos, &buflen, &pos, root))
-
 
412
        goto error;
-
 
413
       
-
 
414
    ipc_hangup(phone);
-
 
415
    munmap(block, BLOCK_SIZE);
-
 
416
    return true;
-
 
417
   
-
 
418
error:
-
 
419
    ipc_hangup(phone);
-
 
420
    munmap(block, BLOCK_SIZE);
-
 
421
    return false;
-
 
422
}
-
 
423
 
240
/** Compare one component of path to a directory entry.
424
/** Compare one component of path to a directory entry.
241
 *
425
 *
242
 * @param parentp   Pointer to node from which we descended.
426
 * @param parentp   Pointer to node from which we descended.
243
 * @param childp    Pointer to node to compare the path component with.
427
 * @param childp    Pointer to node to compare the path component with.
244
 * @param component Array of characters holding component name.
428
 * @param component Array of characters holding component name.
245
 *
429
 *
246
 * @return      True on match, false otherwise.
430
 * @return      True on match, false otherwise.
247
 */
431
 */
248
static bool
432
static bool
249
tmpfs_match_one(tmpfs_dentry_t *parentp, tmpfs_dentry_t *childp,
433
tmpfs_match_one(tmpfs_dentry_t *parentp, tmpfs_dentry_t *childp,
250
    const char *component)
434
    const char *component)
251
{
435
{
252
    unsigned long key = (unsigned long) parentp;
436
    unsigned long key = (unsigned long) parentp;
253
    link_t *hlp = hash_table_find(&childp->names, &key);
437
    link_t *hlp = hash_table_find(&childp->names, &key);
254
    assert(hlp);
438
    assert(hlp);
255
    tmpfs_name_t *namep = hash_table_get_instance(hlp, tmpfs_name_t, link);
439
    tmpfs_name_t *namep = hash_table_get_instance(hlp, tmpfs_name_t, link);
256
    return !strcmp(namep->name, component);
440
    return !strcmp(namep->name, component);
257
}
441
}
258
 
442
 
259
void *tmpfs_match(void *prnt, const char *component)
443
void *tmpfs_match(void *prnt, const char *component)
260
{
444
{
261
    tmpfs_dentry_t *parentp = (tmpfs_dentry_t *) prnt;
445
    tmpfs_dentry_t *parentp = (tmpfs_dentry_t *) prnt;
262
    tmpfs_dentry_t *childp = parentp->child;
446
    tmpfs_dentry_t *childp = parentp->child;
263
 
447
 
264
    while (childp && !tmpfs_match_one(parentp, childp, component))
448
    while (childp && !tmpfs_match_one(parentp, childp, component))
265
        childp = childp->sibling;
449
        childp = childp->sibling;
266
 
450
 
267
    return (void *) childp;
451
    return (void *) childp;
268
}
452
}
269
 
453
 
270
void *
454
void *
271
tmpfs_node_get(dev_handle_t dev_handle, fs_index_t index)
455
tmpfs_node_get(dev_handle_t dev_handle, fs_index_t index)
272
{
456
{
273
    unsigned long key = index;
457
    unsigned long key = index;
274
    link_t *lnk = hash_table_find(&dentries, &key);
458
    link_t *lnk = hash_table_find(&dentries, &key);
275
    if (!lnk)
459
    if (!lnk)
276
        return NULL;
460
        return NULL;
277
    return hash_table_get_instance(lnk, tmpfs_dentry_t, dh_link);
461
    return hash_table_get_instance(lnk, tmpfs_dentry_t, dh_link);
278
}
462
}
279
 
463
 
280
void tmpfs_node_put(void *node)
464
void tmpfs_node_put(void *node)
281
{
465
{
282
    /* nothing to do */
466
    /* nothing to do */
283
}
467
}
284
 
468
 
285
void *tmpfs_create_node(int lflag)
469
void *tmpfs_create_node(int lflag)
286
{
470
{
287
    assert((lflag & L_FILE) ^ (lflag & L_DIRECTORY));
471
    assert((lflag & L_FILE) ^ (lflag & L_DIRECTORY));
288
 
472
 
289
    tmpfs_dentry_t *node = malloc(sizeof(tmpfs_dentry_t));
473
    tmpfs_dentry_t *node = malloc(sizeof(tmpfs_dentry_t));
290
    if (!node)
474
    if (!node)
291
        return NULL;
475
        return NULL;
292
 
476
 
293
    if (!tmpfs_dentry_initialize(node)) {
477
    if (!tmpfs_dentry_initialize(node)) {
294
        free(node);
478
        free(node);
295
        return NULL;
479
        return NULL;
296
    }
480
    }
297
    node->index = tmpfs_next_index++;
481
    node->index = tmpfs_next_index++;
298
    if (lflag & L_DIRECTORY)
482
    if (lflag & L_DIRECTORY)
299
        node->type = TMPFS_DIRECTORY;
483
        node->type = TMPFS_DIRECTORY;
300
    else
484
    else
301
        node->type = TMPFS_FILE;
485
        node->type = TMPFS_FILE;
302
 
486
 
303
    /* Insert the new node into the dentry hash table. */
487
    /* Insert the new node into the dentry hash table. */
304
    unsigned long key = node->index;
488
    unsigned long key = node->index;
305
    hash_table_insert(&dentries, &key, &node->dh_link);
489
    hash_table_insert(&dentries, &key, &node->dh_link);
306
    return (void *) node;
490
    return (void *) node;
307
}
491
}
308
 
492
 
309
bool tmpfs_link_node(void *prnt, void *chld, const char *nm)
493
bool tmpfs_link_node(void *prnt, void *chld, const char *nm)
310
{
494
{
311
    tmpfs_dentry_t *parentp = (tmpfs_dentry_t *) prnt;
495
    tmpfs_dentry_t *parentp = (tmpfs_dentry_t *) prnt;
312
    tmpfs_dentry_t *childp = (tmpfs_dentry_t *) chld;
496
    tmpfs_dentry_t *childp = (tmpfs_dentry_t *) chld;
313
 
497
 
314
    assert(parentp->type == TMPFS_DIRECTORY);
498
    assert(parentp->type == TMPFS_DIRECTORY);
315
 
499
 
316
    tmpfs_name_t *namep = malloc(sizeof(tmpfs_name_t));
500
    tmpfs_name_t *namep = malloc(sizeof(tmpfs_name_t));
317
    if (!namep)
501
    if (!namep)
318
        return false;
502
        return false;
319
    tmpfs_name_initialize(namep);
503
    tmpfs_name_initialize(namep);
320
    size_t len = strlen(nm);
504
    size_t len = strlen(nm);
321
    namep->name = malloc(len + 1);
505
    namep->name = malloc(len + 1);
322
    if (!namep->name) {
506
    if (!namep->name) {
323
        free(namep);
507
        free(namep);
324
        return false;
508
        return false;
325
    }
509
    }
326
    strcpy(namep->name, nm);
510
    strcpy(namep->name, nm);
327
    namep->parent = parentp;
511
    namep->parent = parentp;
328
   
512
   
329
    childp->lnkcnt++;
513
    childp->lnkcnt++;
330
 
514
 
331
    unsigned long key = (unsigned long) parentp;
515
    unsigned long key = (unsigned long) parentp;
332
    hash_table_insert(&childp->names, &key, &namep->link);
516
    hash_table_insert(&childp->names, &key, &namep->link);
333
 
517
 
334
    /* Insert the new node into the namespace. */
518
    /* Insert the new node into the namespace. */
335
    if (parentp->child) {
519
    if (parentp->child) {
336
        tmpfs_dentry_t *tmp = parentp->child;
520
        tmpfs_dentry_t *tmp = parentp->child;
337
        while (tmp->sibling)
521
        while (tmp->sibling)
338
            tmp = tmp->sibling;
522
            tmp = tmp->sibling;
339
        tmp->sibling = childp;
523
        tmp->sibling = childp;
340
    } else {
524
    } else {
341
        parentp->child = childp;
525
        parentp->child = childp;
342
    }
526
    }
343
 
527
 
344
    return true;
528
    return true;
345
}
529
}
346
 
530
 
347
int tmpfs_unlink_node(void *prnt, void *chld)
531
int tmpfs_unlink_node(void *prnt, void *chld)
348
{
532
{
349
    tmpfs_dentry_t *parentp = (tmpfs_dentry_t *)prnt;
533
    tmpfs_dentry_t *parentp = (tmpfs_dentry_t *)prnt;
350
    tmpfs_dentry_t *childp = (tmpfs_dentry_t *)chld;
534
    tmpfs_dentry_t *childp = (tmpfs_dentry_t *)chld;
351
 
535
 
352
    if (!parentp)
536
    if (!parentp)
353
        return EBUSY;
537
        return EBUSY;
354
 
538
 
355
    if (childp->child)
539
    if (childp->child)
356
        return ENOTEMPTY;
540
        return ENOTEMPTY;
357
 
541
 
358
    if (parentp->child == childp) {
542
    if (parentp->child == childp) {
359
        parentp->child = childp->sibling;
543
        parentp->child = childp->sibling;
360
    } else {
544
    } else {
361
        /* TODO: consider doubly linked list for organizing siblings. */
545
        /* TODO: consider doubly linked list for organizing siblings. */
362
        tmpfs_dentry_t *tmp = parentp->child;
546
        tmpfs_dentry_t *tmp = parentp->child;
363
        while (tmp->sibling != childp)
547
        while (tmp->sibling != childp)
364
            tmp = tmp->sibling;
548
            tmp = tmp->sibling;
365
        tmp->sibling = childp->sibling;
549
        tmp->sibling = childp->sibling;
366
    }
550
    }
367
    childp->sibling = NULL;
551
    childp->sibling = NULL;
368
 
552
 
369
    unsigned long key = (unsigned long) parentp;
553
    unsigned long key = (unsigned long) parentp;
370
    hash_table_remove(&childp->names, &key, 1);
554
    hash_table_remove(&childp->names, &key, 1);
371
 
555
 
372
    childp->lnkcnt--;
556
    childp->lnkcnt--;
373
 
557
 
374
    return EOK;
558
    return EOK;
375
}
559
}
376
 
560
 
377
int tmpfs_destroy_node(void *nodep)
561
int tmpfs_destroy_node(void *nodep)
378
{
562
{
379
    tmpfs_dentry_t *dentry = (tmpfs_dentry_t *) nodep;
563
    tmpfs_dentry_t *dentry = (tmpfs_dentry_t *) nodep;
380
   
564
   
381
    assert(!dentry->lnkcnt);
565
    assert(!dentry->lnkcnt);
382
    assert(!dentry->child);
566
    assert(!dentry->child);
383
    assert(!dentry->sibling);
567
    assert(!dentry->sibling);
384
 
568
 
385
    unsigned long key = dentry->index;
569
    unsigned long key = dentry->index;
386
    hash_table_remove(&dentries, &key, 1);
570
    hash_table_remove(&dentries, &key, 1);
387
 
571
 
388
    hash_table_destroy(&dentry->names);
572
    hash_table_destroy(&dentry->names);
389
 
573
 
390
    if (dentry->type == TMPFS_FILE)
574
    if (dentry->type == TMPFS_FILE)
391
        free(dentry->data);
575
        free(dentry->data);
392
    free(dentry);
576
    free(dentry);
393
    return EOK;
577
    return EOK;
394
}
578
}
395
 
579
 
396
void tmpfs_mount(ipc_callid_t rid, ipc_call_t *request)
580
void tmpfs_mount(ipc_callid_t rid, ipc_call_t *request)
397
{
581
{
398
    dev_handle_t mr_dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
582
    dev_handle_t mr_dev_handle = (dev_handle_t) IPC_GET_ARG1(*request);
399
    fs_index_t mr_index = (fs_index_t)IPC_GET_ARG2(*request);
583
    fs_index_t mr_index = (fs_index_t) IPC_GET_ARG2(*request);
400
    fs_handle_t mp_fs_handle = (fs_handle_t)IPC_GET_ARG3(*request);
584
    fs_handle_t mp_fs_handle = (fs_handle_t) IPC_GET_ARG3(*request);
401
    dev_handle_t mp_dev_handle = (dev_handle_t)IPC_GET_ARG4(*request);
585
    dev_handle_t mp_dev_handle = (dev_handle_t) IPC_GET_ARG4(*request);
402
    fs_index_t mp_index = (fs_index_t)IPC_GET_ARG5(*request);
586
    fs_index_t mp_index = (fs_index_t) IPC_GET_ARG5(*request);
-
 
587
   
403
    if ((mr_index == root->index) &&
588
    if ((mr_index == root->index) &&
404
        (mp_fs_handle == tmpfs_reg.fs_handle) &&
589
        (mp_fs_handle == tmpfs_reg.fs_handle) &&
405
        (mp_index == mr_index))
590
        (mp_index == mr_index)) {
-
 
591
       
-
 
592
        if (mr_dev_handle >= 0) {
-
 
593
            if (tmpfs_restore(mr_dev_handle))
-
 
594
                ipc_answer_0(rid, EOK);
-
 
595
            else
-
 
596
                ipc_answer_0(rid, ELIMIT);
-
 
597
        } else
406
        ipc_answer_0(rid, EOK);
598
            ipc_answer_0(rid, EOK);
407
    else
599
    } else
408
        ipc_answer_0(rid, ENOTSUP);
600
        ipc_answer_0(rid, ENOTSUP);
409
}
601
}
410
 
602
 
411
void tmpfs_lookup(ipc_callid_t rid, ipc_call_t *request)
603
void tmpfs_lookup(ipc_callid_t rid, ipc_call_t *request)
412
{
604
{
413
    /* Initialize TMPFS. */
605
    /* Initialize TMPFS. */
414
    if (!root && !tmpfs_init()) {
606
    if (!root && !tmpfs_init()) {
415
        ipc_answer_0(rid, ENOMEM);
607
        ipc_answer_0(rid, ENOMEM);
416
        return;
608
        return;
417
    }
609
    }
418
    libfs_lookup(&tmpfs_libfs_ops, tmpfs_reg.fs_handle, rid, request);
610
    libfs_lookup(&tmpfs_libfs_ops, tmpfs_reg.fs_handle, rid, request);
419
}
611
}
420
 
612
 
421
void tmpfs_read(ipc_callid_t rid, ipc_call_t *request)
613
void tmpfs_read(ipc_callid_t rid, ipc_call_t *request)
422
{
614
{
423
    dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
615
    dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
424
    fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
616
    fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
425
    off_t pos = (off_t)IPC_GET_ARG3(*request);
617
    off_t pos = (off_t)IPC_GET_ARG3(*request);
426
 
618
 
427
    /*
619
    /*
428
     * Lookup the respective dentry.
620
     * Lookup the respective dentry.
429
     */
621
     */
430
    link_t *hlp;
622
    link_t *hlp;
431
    unsigned long key = index;
623
    unsigned long key = index;
432
    hlp = hash_table_find(&dentries, &key);
624
    hlp = hash_table_find(&dentries, &key);
433
    if (!hlp) {
625
    if (!hlp) {
434
        ipc_answer_0(rid, ENOENT);
626
        ipc_answer_0(rid, ENOENT);
435
        return;
627
        return;
436
    }
628
    }
437
    tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t,
629
    tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t,
438
        dh_link);
630
        dh_link);
439
 
631
 
440
    /*
632
    /*
441
     * Receive the read request.
633
     * Receive the read request.
442
     */
634
     */
443
    ipc_callid_t callid;
635
    ipc_callid_t callid;
444
    size_t len;
636
    size_t len;
445
    if (!ipc_data_read_receive(&callid, &len)) {
637
    if (!ipc_data_read_receive(&callid, &len)) {
446
        ipc_answer_0(callid, EINVAL);  
638
        ipc_answer_0(callid, EINVAL);  
447
        ipc_answer_0(rid, EINVAL);
639
        ipc_answer_0(rid, EINVAL);
448
        return;
640
        return;
449
    }
641
    }
450
 
642
 
451
    size_t bytes;
643
    size_t bytes;
452
    if (dentry->type == TMPFS_FILE) {
644
    if (dentry->type == TMPFS_FILE) {
453
        bytes = max(0, min(dentry->size - pos, len));
645
        bytes = max(0, min(dentry->size - pos, len));
454
        (void) ipc_data_read_finalize(callid, dentry->data + pos,
646
        (void) ipc_data_read_finalize(callid, dentry->data + pos,
455
            bytes);
647
            bytes);
456
    } else {
648
    } else {
457
        int i;
649
        int i;
458
        tmpfs_dentry_t *cur;
650
        tmpfs_dentry_t *cur;
459
       
651
       
460
        assert(dentry->type == TMPFS_DIRECTORY);
652
        assert(dentry->type == TMPFS_DIRECTORY);
461
       
653
       
462
        /*
654
        /*
463
         * Yes, we really use O(n) algorithm here.
655
         * Yes, we really use O(n) algorithm here.
464
         * If it bothers someone, it could be fixed by introducing a
656
         * If it bothers someone, it could be fixed by introducing a
465
         * hash table.
657
         * hash table.
466
         */
658
         */
467
        for (i = 0, cur = dentry->child; i < pos && cur; i++,
659
        for (i = 0, cur = dentry->child; i < pos && cur; i++,
468
            cur = cur->sibling)
660
            cur = cur->sibling)
469
            ;
661
            ;
470
 
662
 
471
        if (!cur) {
663
        if (!cur) {
472
            ipc_answer_0(callid, ENOENT);
664
            ipc_answer_0(callid, ENOENT);
473
            ipc_answer_1(rid, ENOENT, 0);
665
            ipc_answer_1(rid, ENOENT, 0);
474
            return;
666
            return;
475
        }
667
        }
476
 
668
 
477
        unsigned long key = (unsigned long) dentry;
669
        unsigned long key = (unsigned long) dentry;
478
        link_t *hlp = hash_table_find(&cur->names, &key);
670
        link_t *hlp = hash_table_find(&cur->names, &key);
479
        assert(hlp);
671
        assert(hlp);
480
        tmpfs_name_t *namep = hash_table_get_instance(hlp, tmpfs_name_t,
672
        tmpfs_name_t *namep = hash_table_get_instance(hlp, tmpfs_name_t,
481
            link);
673
            link);
482
 
674
 
483
        (void) ipc_data_read_finalize(callid, namep->name,
675
        (void) ipc_data_read_finalize(callid, namep->name,
484
            strlen(namep->name) + 1);
676
            strlen(namep->name) + 1);
485
        bytes = 1;
677
        bytes = 1;
486
    }
678
    }
487
 
679
 
488
    /*
680
    /*
489
     * Answer the VFS_READ call.
681
     * Answer the VFS_READ call.
490
     */
682
     */
491
    ipc_answer_1(rid, EOK, bytes);
683
    ipc_answer_1(rid, EOK, bytes);
492
}
684
}
493
 
685
 
494
void tmpfs_write(ipc_callid_t rid, ipc_call_t *request)
686
void tmpfs_write(ipc_callid_t rid, ipc_call_t *request)
495
{
687
{
496
    dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
688
    dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
497
    fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
689
    fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
498
    off_t pos = (off_t)IPC_GET_ARG3(*request);
690
    off_t pos = (off_t)IPC_GET_ARG3(*request);
499
 
691
 
500
    /*
692
    /*
501
     * Lookup the respective dentry.
693
     * Lookup the respective dentry.
502
     */
694
     */
503
    link_t *hlp;
695
    link_t *hlp;
504
    unsigned long key = index;
696
    unsigned long key = index;
505
    hlp = hash_table_find(&dentries, &key);
697
    hlp = hash_table_find(&dentries, &key);
506
    if (!hlp) {
698
    if (!hlp) {
507
        ipc_answer_0(rid, ENOENT);
699
        ipc_answer_0(rid, ENOENT);
508
        return;
700
        return;
509
    }
701
    }
510
    tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t,
702
    tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t,
511
        dh_link);
703
        dh_link);
512
 
704
 
513
    /*
705
    /*
514
     * Receive the write request.
706
     * Receive the write request.
515
     */
707
     */
516
    ipc_callid_t callid;
708
    ipc_callid_t callid;
517
    size_t len;
709
    size_t len;
518
    if (!ipc_data_write_receive(&callid, &len)) {
710
    if (!ipc_data_write_receive(&callid, &len)) {
519
        ipc_answer_0(callid, EINVAL);  
711
        ipc_answer_0(callid, EINVAL);  
520
        ipc_answer_0(rid, EINVAL);
712
        ipc_answer_0(rid, EINVAL);
521
        return;
713
        return;
522
    }
714
    }
523
 
715
 
524
    /*
716
    /*
525
     * Check whether the file needs to grow.
717
     * Check whether the file needs to grow.
526
     */
718
     */
527
    if (pos + len <= dentry->size) {
719
    if (pos + len <= dentry->size) {
528
        /* The file size is not changing. */
720
        /* The file size is not changing. */
529
        (void) ipc_data_write_finalize(callid, dentry->data + pos, len);
721
        (void) ipc_data_write_finalize(callid, dentry->data + pos, len);
530
        ipc_answer_2(rid, EOK, len, dentry->size);
722
        ipc_answer_2(rid, EOK, len, dentry->size);
531
        return;
723
        return;
532
    }
724
    }
533
    size_t delta = (pos + len) - dentry->size;
725
    size_t delta = (pos + len) - dentry->size;
534
    /*
726
    /*
535
     * At this point, we are deliberately extremely straightforward and
727
     * At this point, we are deliberately extremely straightforward and
536
     * simply realloc the contents of the file on every write that grows the
728
     * simply realloc the contents of the file on every write that grows the
537
     * file. In the end, the situation might not be as bad as it may look:
729
     * file. In the end, the situation might not be as bad as it may look:
538
     * our heap allocator can save us and just grow the block whenever
730
     * our heap allocator can save us and just grow the block whenever
539
     * possible.
731
     * possible.
540
     */
732
     */
541
    void *newdata = realloc(dentry->data, dentry->size + delta);
733
    void *newdata = realloc(dentry->data, dentry->size + delta);
542
    if (!newdata) {
734
    if (!newdata) {
543
        ipc_answer_0(callid, ENOMEM);
735
        ipc_answer_0(callid, ENOMEM);
544
        ipc_answer_2(rid, EOK, 0, dentry->size);
736
        ipc_answer_2(rid, EOK, 0, dentry->size);
545
        return;
737
        return;
546
    }
738
    }
547
    /* Clear any newly allocated memory in order to emulate gaps. */
739
    /* Clear any newly allocated memory in order to emulate gaps. */
548
    memset(newdata + dentry->size, 0, delta);
740
    memset(newdata + dentry->size, 0, delta);
549
    dentry->size += delta;
741
    dentry->size += delta;
550
    dentry->data = newdata;
742
    dentry->data = newdata;
551
    (void) ipc_data_write_finalize(callid, dentry->data + pos, len);
743
    (void) ipc_data_write_finalize(callid, dentry->data + pos, len);
552
    ipc_answer_2(rid, EOK, len, dentry->size);
744
    ipc_answer_2(rid, EOK, len, dentry->size);
553
}
745
}
554
 
746
 
555
void tmpfs_truncate(ipc_callid_t rid, ipc_call_t *request)
747
void tmpfs_truncate(ipc_callid_t rid, ipc_call_t *request)
556
{
748
{
557
    dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
749
    dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
558
    fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
750
    fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
559
    size_t size = (off_t)IPC_GET_ARG3(*request);
751
    size_t size = (off_t)IPC_GET_ARG3(*request);
560
 
752
 
561
    /*
753
    /*
562
     * Lookup the respective dentry.
754
     * Lookup the respective dentry.
563
     */
755
     */
564
    link_t *hlp;
756
    link_t *hlp;
565
    unsigned long key = index;
757
    unsigned long key = index;
566
    hlp = hash_table_find(&dentries, &key);
758
    hlp = hash_table_find(&dentries, &key);
567
    if (!hlp) {
759
    if (!hlp) {
568
        ipc_answer_0(rid, ENOENT);
760
        ipc_answer_0(rid, ENOENT);
569
        return;
761
        return;
570
    }
762
    }
571
    tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t,
763
    tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t,
572
        dh_link);
764
        dh_link);
573
 
765
 
574
    if (size == dentry->size) {
766
    if (size == dentry->size) {
575
        ipc_answer_0(rid, EOK);
767
        ipc_answer_0(rid, EOK);
576
        return;
768
        return;
577
    }
769
    }
578
 
770
 
579
    void *newdata = realloc(dentry->data, size);
771
    void *newdata = realloc(dentry->data, size);
580
    if (!newdata) {
772
    if (!newdata) {
581
        ipc_answer_0(rid, ENOMEM);
773
        ipc_answer_0(rid, ENOMEM);
582
        return;
774
        return;
583
    }
775
    }
584
    if (size > dentry->size) {
776
    if (size > dentry->size) {
585
        size_t delta = size - dentry->size;
777
        size_t delta = size - dentry->size;
586
        memset(newdata + dentry->size, 0, delta);
778
        memset(newdata + dentry->size, 0, delta);
587
    }
779
    }
588
    dentry->size = size;
780
    dentry->size = size;
589
    dentry->data = newdata;
781
    dentry->data = newdata;
590
    ipc_answer_0(rid, EOK);
782
    ipc_answer_0(rid, EOK);
591
}
783
}
592
 
784
 
593
void tmpfs_destroy(ipc_callid_t rid, ipc_call_t *request)
785
void tmpfs_destroy(ipc_callid_t rid, ipc_call_t *request)
594
{
786
{
595
    dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
787
    dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
596
    fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
788
    fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
597
    int rc;
789
    int rc;
598
 
790
 
599
    link_t *hlp;
791
    link_t *hlp;
600
    unsigned long key = index;
792
    unsigned long key = index;
601
    hlp = hash_table_find(&dentries, &key);
793
    hlp = hash_table_find(&dentries, &key);
602
    if (!hlp) {
794
    if (!hlp) {
603
        ipc_answer_0(rid, ENOENT);
795
        ipc_answer_0(rid, ENOENT);
604
        return;
796
        return;
605
    }
797
    }
606
    tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t,
798
    tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t,
607
        dh_link);
799
        dh_link);
608
    rc = tmpfs_destroy_node(dentry);
800
    rc = tmpfs_destroy_node(dentry);
609
    ipc_answer_0(rid, rc);
801
    ipc_answer_0(rid, rc);
610
}
802
}
611
 
803
 
612
/**
804
/**
613
 * @}
805
 * @}
614
 */
806
 */
615
 
807