Subversion Repositories HelenOS

Rev

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

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