Subversion Repositories HelenOS

Rev

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

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