Subversion Repositories HelenOS

Rev

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

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