Subversion Repositories HelenOS

Rev

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

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