Subversion Repositories HelenOS

Rev

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

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