Subversion Repositories HelenOS

Rev

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

Rev 2757 Rev 2758
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 *, 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 ((tmpfs_dentry_t *) nodep)->lnkcnt;
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;
-
 
174
    dentry->sibling = NULL;
173
    dentry->sibling = NULL;
175
    dentry->child = NULL;
174
    dentry->child = NULL;
176
    dentry->name = NULL;
175
    dentry->name = NULL;
177
    dentry->type = TMPFS_NONE;
176
    dentry->type = TMPFS_NONE;
178
    dentry->lnkcnt = 0;
177
    dentry->lnkcnt = 0;
179
    dentry->size = 0;
178
    dentry->size = 0;
180
    dentry->data = NULL;
179
    dentry->data = NULL;
181
    link_initialize(&dentry->dh_link);
180
    link_initialize(&dentry->dh_link);
182
}
181
}
183
 
182
 
184
static bool tmpfs_init(void)
183
static bool tmpfs_init(void)
185
{
184
{
186
    if (!hash_table_create(&dentries, DENTRIES_BUCKETS, 1, &dentries_ops))
185
    if (!hash_table_create(&dentries, DENTRIES_BUCKETS, 1, &dentries_ops))
187
        return false;
186
        return false;
188
    root = (tmpfs_dentry_t *) tmpfs_create_node(L_DIRECTORY);
187
    root = (tmpfs_dentry_t *) tmpfs_create_node(L_DIRECTORY);
189
    root->lnkcnt = 1;
188
    root->lnkcnt = 1;
190
    return root != NULL;
189
    return root != NULL;
191
}
190
}
192
 
191
 
193
/** Compare one component of path to a directory entry.
192
/** Compare one component of path to a directory entry.
194
 *
193
 *
195
 * @param nodep     Node to compare the path component with.
194
 * @param nodep     Node to compare the path component with.
196
 * @param component Array of characters holding component name.
195
 * @param component Array of characters holding component name.
197
 *
196
 *
198
 * @return      True on match, false otherwise.
197
 * @return      True on match, false otherwise.
199
 */
198
 */
200
bool tmpfs_match(void *nodep, const char *component)
199
bool tmpfs_match(void *nodep, const char *component)
201
{
200
{
202
    tmpfs_dentry_t *dentry = (tmpfs_dentry_t *) nodep;
201
    tmpfs_dentry_t *dentry = (tmpfs_dentry_t *) nodep;
203
 
202
 
204
    return !strcmp(dentry->name, component);
203
    return !strcmp(dentry->name, component);
205
}
204
}
206
 
205
 
207
void *tmpfs_create_node(int lflag)
206
void *tmpfs_create_node(int lflag)
208
{
207
{
209
    assert((lflag & L_FILE) ^ (lflag & L_DIRECTORY));
208
    assert((lflag & L_FILE) ^ (lflag & L_DIRECTORY));
210
 
209
 
211
    tmpfs_dentry_t *node = malloc(sizeof(tmpfs_dentry_t));
210
    tmpfs_dentry_t *node = malloc(sizeof(tmpfs_dentry_t));
212
    if (!node)
211
    if (!node)
213
        return NULL;
212
        return NULL;
214
 
213
 
215
    tmpfs_dentry_initialize(node);
214
    tmpfs_dentry_initialize(node);
216
    node->index = tmpfs_next_index++;
215
    node->index = tmpfs_next_index++;
217
    if (lflag & L_DIRECTORY)
216
    if (lflag & L_DIRECTORY)
218
        node->type = TMPFS_DIRECTORY;
217
        node->type = TMPFS_DIRECTORY;
219
    else
218
    else
220
        node->type = TMPFS_FILE;
219
        node->type = TMPFS_FILE;
221
 
220
 
222
    /* Insert the new node into the dentry hash table. */
221
    /* Insert the new node into the dentry hash table. */
223
    hash_table_insert(&dentries, &node->index, &node->dh_link);
222
    hash_table_insert(&dentries, &node->index, &node->dh_link);
224
    return (void *) node;
223
    return (void *) node;
225
}
224
}
226
 
225
 
227
bool tmpfs_link_node(void *prnt, void *chld, const char *nm)
226
bool tmpfs_link_node(void *prnt, void *chld, const char *nm)
228
{
227
{
229
    tmpfs_dentry_t *parentp = (tmpfs_dentry_t *) prnt;
228
    tmpfs_dentry_t *parentp = (tmpfs_dentry_t *) prnt;
230
    tmpfs_dentry_t *childp = (tmpfs_dentry_t *) chld;
229
    tmpfs_dentry_t *childp = (tmpfs_dentry_t *) chld;
231
 
230
 
232
    assert(parentp->type == TMPFS_DIRECTORY);
231
    assert(parentp->type == TMPFS_DIRECTORY);
233
 
232
 
234
    size_t len = strlen(nm);
233
    size_t len = strlen(nm);
235
    char *name = malloc(len + 1);
234
    char *name = malloc(len + 1);
236
    if (!name)
235
    if (!name)
237
        return false;
236
        return false;
238
   
237
   
239
    childp->lnkcnt++;
238
    childp->lnkcnt++;
240
   
239
   
241
    strcpy(name, nm);
240
    strcpy(name, nm);
242
    childp->name = name;
241
    childp->name = name;
243
 
242
 
244
    /* Insert the new node into the namespace. */
243
    /* Insert the new node into the namespace. */
245
    if (parentp->child) {
244
    if (parentp->child) {
246
        tmpfs_dentry_t *tmp = parentp->child;
245
        tmpfs_dentry_t *tmp = parentp->child;
247
        while (tmp->sibling)
246
        while (tmp->sibling)
248
            tmp = tmp->sibling;
247
            tmp = tmp->sibling;
249
        tmp->sibling = childp;
248
        tmp->sibling = childp;
250
    } else {
249
    } else {
251
        parentp->child = childp;
250
        parentp->child = childp;
252
    }
251
    }
253
    childp->parent = parentp;
-
 
254
 
252
 
255
    return true;
253
    return true;
256
}
254
}
257
 
255
 
258
int tmpfs_unlink_node(void *nodeptr)
256
int tmpfs_unlink_node(void *prnt, void *chld)
259
{
257
{
260
    tmpfs_dentry_t *dentry = (tmpfs_dentry_t *)nodeptr;
258
    tmpfs_dentry_t *parentp = (tmpfs_dentry_t *)prnt;
-
 
259
    tmpfs_dentry_t *childp = (tmpfs_dentry_t *)chld;
261
 
260
 
262
    if (dentry->child)
-
 
263
        return ENOTEMPTY;
-
 
264
 
-
 
265
    if (!dentry->parent)
261
    if (!parentp)
266
        return EBUSY;
262
        return EBUSY;
267
 
263
 
-
 
264
    if (childp->child)
-
 
265
        return ENOTEMPTY;
-
 
266
 
268
    if (dentry->parent->child == dentry) {
267
    if (parentp->child == childp) {
269
        dentry->parent->child = dentry->sibling;
268
        parentp->child = childp->sibling;
270
    } else {
269
    } else {
271
        /* TODO: consider doubly linked list for organizing siblings. */
270
        /* TODO: consider doubly linked list for organizing siblings. */
272
        tmpfs_dentry_t *tmp = dentry->parent->child;
271
        tmpfs_dentry_t *tmp = parentp->child;
273
        while (tmp->sibling != dentry)
272
        while (tmp->sibling != childp)
274
            tmp = tmp->sibling;
273
            tmp = tmp->sibling;
275
        tmp->sibling = dentry->sibling;
274
        tmp->sibling = childp->sibling;
276
    }
275
    }
277
    dentry->sibling = NULL;
276
    childp->sibling = NULL;
278
    dentry->parent = NULL;
-
 
279
 
277
 
280
    free(dentry->name);
278
    free(childp->name);
281
    dentry->name = NULL;
279
    childp->name = NULL;
282
 
280
 
283
    dentry->lnkcnt--;
281
    childp->lnkcnt--;
284
 
282
 
285
    return EOK;
283
    return EOK;
286
}
284
}
287
 
285
 
288
void tmpfs_destroy_node(void *nodep)
286
void tmpfs_destroy_node(void *nodep)
289
{
287
{
290
    tmpfs_dentry_t *dentry = (tmpfs_dentry_t *) nodep;
288
    tmpfs_dentry_t *dentry = (tmpfs_dentry_t *) nodep;
291
   
289
   
292
    assert(!dentry->lnkcnt);
290
    assert(!dentry->lnkcnt);
293
    assert(!dentry->child);
291
    assert(!dentry->child);
294
    assert(!dentry->sibling);
292
    assert(!dentry->sibling);
295
 
293
 
296
    unsigned long index = dentry->index;
294
    unsigned long index = dentry->index;
297
    hash_table_remove(&dentries, &index, 1);
295
    hash_table_remove(&dentries, &index, 1);
298
 
296
 
299
    if (dentry->type == TMPFS_FILE)
297
    if (dentry->type == TMPFS_FILE)
300
        free(dentry->data);
298
        free(dentry->data);
301
    free(dentry);
299
    free(dentry);
302
}
300
}
303
 
301
 
304
void tmpfs_lookup(ipc_callid_t rid, ipc_call_t *request)
302
void tmpfs_lookup(ipc_callid_t rid, ipc_call_t *request)
305
{
303
{
306
    /* Initialize TMPFS. */
304
    /* Initialize TMPFS. */
307
    if (!root && !tmpfs_init()) {
305
    if (!root && !tmpfs_init()) {
308
        ipc_answer_0(rid, ENOMEM);
306
        ipc_answer_0(rid, ENOMEM);
309
        return;
307
        return;
310
    }
308
    }
311
    libfs_lookup(&tmpfs_libfs_ops, tmpfs_reg.fs_handle, rid, request);
309
    libfs_lookup(&tmpfs_libfs_ops, tmpfs_reg.fs_handle, rid, request);
312
}
310
}
313
 
311
 
314
void tmpfs_read(ipc_callid_t rid, ipc_call_t *request)
312
void tmpfs_read(ipc_callid_t rid, ipc_call_t *request)
315
{
313
{
316
    int dev_handle = IPC_GET_ARG1(*request);
314
    int dev_handle = IPC_GET_ARG1(*request);
317
    unsigned long index = IPC_GET_ARG2(*request);
315
    unsigned long index = IPC_GET_ARG2(*request);
318
    off_t pos = IPC_GET_ARG3(*request);
316
    off_t pos = IPC_GET_ARG3(*request);
319
 
317
 
320
    /*
318
    /*
321
     * Lookup the respective dentry.
319
     * Lookup the respective dentry.
322
     */
320
     */
323
    link_t *hlp;
321
    link_t *hlp;
324
    hlp = hash_table_find(&dentries, &index);
322
    hlp = hash_table_find(&dentries, &index);
325
    if (!hlp) {
323
    if (!hlp) {
326
        ipc_answer_0(rid, ENOENT);
324
        ipc_answer_0(rid, ENOENT);
327
        return;
325
        return;
328
    }
326
    }
329
    tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t,
327
    tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t,
330
        dh_link);
328
        dh_link);
331
 
329
 
332
    /*
330
    /*
333
     * Receive the read request.
331
     * Receive the read request.
334
     */
332
     */
335
    ipc_callid_t callid;
333
    ipc_callid_t callid;
336
    size_t len;
334
    size_t len;
337
    if (!ipc_data_read_receive(&callid, &len)) {
335
    if (!ipc_data_read_receive(&callid, &len)) {
338
        ipc_answer_0(callid, EINVAL);  
336
        ipc_answer_0(callid, EINVAL);  
339
        ipc_answer_0(rid, EINVAL);
337
        ipc_answer_0(rid, EINVAL);
340
        return;
338
        return;
341
    }
339
    }
342
 
340
 
343
    size_t bytes;
341
    size_t bytes;
344
    if (dentry->type == TMPFS_FILE) {
342
    if (dentry->type == TMPFS_FILE) {
345
        bytes = max(0, min(dentry->size - pos, len));
343
        bytes = max(0, min(dentry->size - pos, len));
346
        (void) ipc_data_read_finalize(callid, dentry->data + pos,
344
        (void) ipc_data_read_finalize(callid, dentry->data + pos,
347
            bytes);
345
            bytes);
348
    } else {
346
    } else {
349
        int i;
347
        int i;
350
        tmpfs_dentry_t *cur;
348
        tmpfs_dentry_t *cur;
351
       
349
       
352
        assert(dentry->type == TMPFS_DIRECTORY);
350
        assert(dentry->type == TMPFS_DIRECTORY);
353
       
351
       
354
        /*
352
        /*
355
         * Yes, we really use O(n) algorithm here.
353
         * Yes, we really use O(n) algorithm here.
356
         * If it bothers someone, it could be fixed by introducing a
354
         * If it bothers someone, it could be fixed by introducing a
357
         * hash table.
355
         * hash table.
358
         */
356
         */
359
        for (i = 0, cur = dentry->child; i < pos && cur; i++,
357
        for (i = 0, cur = dentry->child; i < pos && cur; i++,
360
            cur = cur->sibling)
358
            cur = cur->sibling)
361
            ;
359
            ;
362
 
360
 
363
        if (!cur) {
361
        if (!cur) {
364
            ipc_answer_0(callid, ENOENT);
362
            ipc_answer_0(callid, ENOENT);
365
            ipc_answer_1(rid, ENOENT, 0);
363
            ipc_answer_1(rid, ENOENT, 0);
366
            return;
364
            return;
367
        }
365
        }
368
 
366
 
369
        (void) ipc_data_read_finalize(callid, cur->name,
367
        (void) ipc_data_read_finalize(callid, cur->name,
370
            strlen(cur->name) + 1);
368
            strlen(cur->name) + 1);
371
        bytes = 1;
369
        bytes = 1;
372
    }
370
    }
373
 
371
 
374
    /*
372
    /*
375
     * Answer the VFS_READ call.
373
     * Answer the VFS_READ call.
376
     */
374
     */
377
    ipc_answer_1(rid, EOK, bytes);
375
    ipc_answer_1(rid, EOK, bytes);
378
}
376
}
379
 
377
 
380
void tmpfs_write(ipc_callid_t rid, ipc_call_t *request)
378
void tmpfs_write(ipc_callid_t rid, ipc_call_t *request)
381
{
379
{
382
    int dev_handle = IPC_GET_ARG1(*request);
380
    int dev_handle = IPC_GET_ARG1(*request);
383
    unsigned long index = IPC_GET_ARG2(*request);
381
    unsigned long index = IPC_GET_ARG2(*request);
384
    off_t pos = IPC_GET_ARG3(*request);
382
    off_t pos = IPC_GET_ARG3(*request);
385
 
383
 
386
    /*
384
    /*
387
     * Lookup the respective dentry.
385
     * Lookup the respective dentry.
388
     */
386
     */
389
    link_t *hlp;
387
    link_t *hlp;
390
    hlp = hash_table_find(&dentries, &index);
388
    hlp = hash_table_find(&dentries, &index);
391
    if (!hlp) {
389
    if (!hlp) {
392
        ipc_answer_0(rid, ENOENT);
390
        ipc_answer_0(rid, ENOENT);
393
        return;
391
        return;
394
    }
392
    }
395
    tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t,
393
    tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t,
396
        dh_link);
394
        dh_link);
397
 
395
 
398
    /*
396
    /*
399
     * Receive the write request.
397
     * Receive the write request.
400
     */
398
     */
401
    ipc_callid_t callid;
399
    ipc_callid_t callid;
402
    size_t len;
400
    size_t len;
403
    if (!ipc_data_write_receive(&callid, &len)) {
401
    if (!ipc_data_write_receive(&callid, &len)) {
404
        ipc_answer_0(callid, EINVAL);  
402
        ipc_answer_0(callid, EINVAL);  
405
        ipc_answer_0(rid, EINVAL);
403
        ipc_answer_0(rid, EINVAL);
406
        return;
404
        return;
407
    }
405
    }
408
 
406
 
409
    /*
407
    /*
410
     * Check whether the file needs to grow.
408
     * Check whether the file needs to grow.
411
     */
409
     */
412
    if (pos + len <= dentry->size) {
410
    if (pos + len <= dentry->size) {
413
        /* The file size is not changing. */
411
        /* The file size is not changing. */
414
        (void) ipc_data_write_finalize(callid, dentry->data + pos, len);
412
        (void) ipc_data_write_finalize(callid, dentry->data + pos, len);
415
        ipc_answer_2(rid, EOK, len, dentry->size);
413
        ipc_answer_2(rid, EOK, len, dentry->size);
416
        return;
414
        return;
417
    }
415
    }
418
    size_t delta = (pos + len) - dentry->size;
416
    size_t delta = (pos + len) - dentry->size;
419
    /*
417
    /*
420
     * At this point, we are deliberately extremely straightforward and
418
     * At this point, we are deliberately extremely straightforward and
421
     * simply realloc the contents of the file on every write that grows the
419
     * simply realloc the contents of the file on every write that grows the
422
     * file. In the end, the situation might not be as bad as it may look:
420
     * file. In the end, the situation might not be as bad as it may look:
423
     * our heap allocator can save us and just grow the block whenever
421
     * our heap allocator can save us and just grow the block whenever
424
     * possible.
422
     * possible.
425
     */
423
     */
426
    void *newdata = realloc(dentry->data, dentry->size + delta);
424
    void *newdata = realloc(dentry->data, dentry->size + delta);
427
    if (!newdata) {
425
    if (!newdata) {
428
        ipc_answer_0(callid, ENOMEM);
426
        ipc_answer_0(callid, ENOMEM);
429
        ipc_answer_2(rid, EOK, 0, dentry->size);
427
        ipc_answer_2(rid, EOK, 0, dentry->size);
430
        return;
428
        return;
431
    }
429
    }
432
    /* Clear any newly allocated memory in order to emulate gaps. */
430
    /* Clear any newly allocated memory in order to emulate gaps. */
433
    memset(newdata + dentry->size, 0, delta);
431
    memset(newdata + dentry->size, 0, delta);
434
    dentry->size += delta;
432
    dentry->size += delta;
435
    dentry->data = newdata;
433
    dentry->data = newdata;
436
    (void) ipc_data_write_finalize(callid, dentry->data + pos, len);
434
    (void) ipc_data_write_finalize(callid, dentry->data + pos, len);
437
    ipc_answer_2(rid, EOK, len, dentry->size);
435
    ipc_answer_2(rid, EOK, len, dentry->size);
438
}
436
}
439
 
437
 
440
void tmpfs_truncate(ipc_callid_t rid, ipc_call_t *request)
438
void tmpfs_truncate(ipc_callid_t rid, ipc_call_t *request)
441
{
439
{
442
    int dev_handle = IPC_GET_ARG1(*request);
440
    int dev_handle = IPC_GET_ARG1(*request);
443
    unsigned long index = IPC_GET_ARG2(*request);
441
    unsigned long index = IPC_GET_ARG2(*request);
444
    size_t size = IPC_GET_ARG3(*request);
442
    size_t size = IPC_GET_ARG3(*request);
445
 
443
 
446
    /*
444
    /*
447
     * Lookup the respective dentry.
445
     * Lookup the respective dentry.
448
     */
446
     */
449
    link_t *hlp;
447
    link_t *hlp;
450
    hlp = hash_table_find(&dentries, &index);
448
    hlp = hash_table_find(&dentries, &index);
451
    if (!hlp) {
449
    if (!hlp) {
452
        ipc_answer_0(rid, ENOENT);
450
        ipc_answer_0(rid, ENOENT);
453
        return;
451
        return;
454
    }
452
    }
455
    tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t,
453
    tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t,
456
        dh_link);
454
        dh_link);
457
 
455
 
458
    if (size == dentry->size) {
456
    if (size == dentry->size) {
459
        ipc_answer_0(rid, EOK);
457
        ipc_answer_0(rid, EOK);
460
        return;
458
        return;
461
    }
459
    }
462
 
460
 
463
    void *newdata = realloc(dentry->data, size);
461
    void *newdata = realloc(dentry->data, size);
464
    if (!newdata) {
462
    if (!newdata) {
465
        ipc_answer_0(rid, ENOMEM);
463
        ipc_answer_0(rid, ENOMEM);
466
        return;
464
        return;
467
    }
465
    }
468
    if (size > dentry->size) {
466
    if (size > dentry->size) {
469
        size_t delta = size - dentry->size;
467
        size_t delta = size - dentry->size;
470
        memset(newdata + dentry->size, 0, delta);
468
        memset(newdata + dentry->size, 0, delta);
471
    }
469
    }
472
    dentry->size = size;
470
    dentry->size = size;
473
    dentry->data = newdata;
471
    dentry->data = newdata;
474
    ipc_answer_0(rid, EOK);
472
    ipc_answer_0(rid, EOK);
475
}
473
}
476
 
474
 
477
void tmpfs_destroy(ipc_callid_t rid, ipc_call_t *request)
475
void tmpfs_destroy(ipc_callid_t rid, ipc_call_t *request)
478
{
476
{
479
    int dev_handle = IPC_GET_ARG1(*request);
477
    int dev_handle = IPC_GET_ARG1(*request);
480
    unsigned long index = IPC_GET_ARG2(*request);
478
    unsigned long index = IPC_GET_ARG2(*request);
481
 
479
 
482
    link_t *hlp;
480
    link_t *hlp;
483
    hlp = hash_table_find(&dentries, &index);
481
    hlp = hash_table_find(&dentries, &index);
484
    if (!hlp) {
482
    if (!hlp) {
485
        ipc_answer_0(rid, ENOENT);
483
        ipc_answer_0(rid, ENOENT);
486
        return;
484
        return;
487
    }
485
    }
488
    tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t,
486
    tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t,
489
        dh_link);
487
        dh_link);
490
    tmpfs_destroy_node(dentry);
488
    tmpfs_destroy_node(dentry);
491
    ipc_answer_0(rid, EOK);
489
    ipc_answer_0(rid, EOK);
492
}
490
}
493
 
491
 
494
/**
492
/**
495
 * @}
493
 * @}
496
 */
494
 */
497
 
495