Subversion Repositories HelenOS

Rev

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

Rev 2844 Rev 2852
1
/*
1
/*
2
 * Copyright (c) 2008 Jakub Jermar
2
 * Copyright (c) 2008 Jakub Jermar
3
 * All rights reserved.
3
 * All rights reserved.
4
 *
4
 *
5
 * Redistribution and use in source and binary forms, with or without
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
6
 * modification, are permitted provided that the following conditions
7
 * are met:
7
 * are met:
8
 *
8
 *
9
 * - Redistributions of source code must retain the above copyright
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
13
 *   documentation and/or other materials provided with the distribution.
14
 * - The name of the author may not be used to endorse or promote products
14
 * - The name of the author may not be used to endorse or promote products
15
 *   derived from this software without specific prior written permission.
15
 *   derived from this software without specific prior written permission.
16
 *
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
27
 */
28
 
28
 
29
/** @addtogroup fs
29
/** @addtogroup fs
30
 * @{
30
 * @{
31
 */
31
 */
32
 
32
 
33
/**
33
/**
34
 * @file    tmpfs_ops.c
34
 * @file    tmpfs_ops.c
35
 * @brief   Implementation of VFS operations for the TMPFS file system
35
 * @brief   Implementation of VFS operations for the TMPFS file system
36
 *      server.
36
 *      server.
37
 */
37
 */
38
 
38
 
39
#include "tmpfs.h"
39
#include "tmpfs.h"
40
#include "../../vfs/vfs.h"
40
#include "../../vfs/vfs.h"
41
#include <ipc/ipc.h>
41
#include <ipc/ipc.h>
42
#include <async.h>
42
#include <async.h>
43
#include <errno.h>
43
#include <errno.h>
44
#include <atomic.h>
44
#include <atomic.h>
45
#include <stdlib.h>
45
#include <stdlib.h>
46
#include <string.h>
46
#include <string.h>
47
#include <stdio.h>
47
#include <stdio.h>
48
#include <assert.h>
48
#include <assert.h>
49
#include <sys/types.h>
49
#include <sys/types.h>
50
#include <libadt/hash_table.h>
50
#include <libadt/hash_table.h>
51
#include <as.h>
51
#include <as.h>
52
#include <libfs.h>
52
#include <libfs.h>
53
 
53
 
54
#define min(a, b)       ((a) < (b) ? (a) : (b))
54
#define min(a, b)       ((a) < (b) ? (a) : (b))
55
#define max(a, b)       ((a) > (b) ? (a) : (b))
55
#define max(a, b)       ((a) > (b) ? (a) : (b))
56
 
56
 
57
#define DENTRIES_BUCKETS    256
57
#define DENTRIES_BUCKETS    256
58
 
58
 
59
#define NAMES_BUCKETS       4
59
#define NAMES_BUCKETS       4
60
 
60
 
61
/*
61
/*
62
 * For now, we don't distinguish between different dev_handles/instances. All
62
 * For now, we don't distinguish between different dev_handles/instances. All
63
 * requests resolve to the only instance, rooted in the following variable.
63
 * requests resolve to the only instance, rooted in the following variable.
64
 */
64
 */
65
static tmpfs_dentry_t *root;
65
static tmpfs_dentry_t *root;
66
 
66
 
67
/*
67
/*
68
 * Implementation of the libfs interface.
68
 * Implementation of the libfs interface.
69
 */
69
 */
70
 
70
 
71
/* Forward declarations of static functions. */
71
/* Forward declarations of static functions. */
72
static void *tmpfs_match(void *, const char *);
72
static void *tmpfs_match(void *, const char *);
73
static void *tmpfs_node_get(dev_handle_t, fs_index_t, fs_index_t);
73
static void *tmpfs_node_get(dev_handle_t, fs_index_t, fs_index_t);
-
 
74
static void tmpfs_node_put(void *);
74
static void *tmpfs_create_node(int);
75
static void *tmpfs_create_node(int);
75
static bool tmpfs_link_node(void *, void *, const char *);
76
static bool tmpfs_link_node(void *, void *, const char *);
76
static int tmpfs_unlink_node(void *, void *);
77
static int tmpfs_unlink_node(void *, void *);
77
static void tmpfs_destroy_node(void *);
78
static void tmpfs_destroy_node(void *);
78
 
79
 
79
/* Implementation of helper functions. */
80
/* Implementation of helper functions. */
80
static fs_index_t tmpfs_index_get(void *nodep)
81
static fs_index_t tmpfs_index_get(void *nodep)
81
{
82
{
82
    return ((tmpfs_dentry_t *) nodep)->index;
83
    return ((tmpfs_dentry_t *) nodep)->index;
83
}
84
}
84
 
85
 
85
static size_t tmpfs_size_get(void *nodep)
86
static size_t tmpfs_size_get(void *nodep)
86
{
87
{
87
    return ((tmpfs_dentry_t *) nodep)->size;
88
    return ((tmpfs_dentry_t *) nodep)->size;
88
}
89
}
89
 
90
 
90
static unsigned tmpfs_lnkcnt_get(void *nodep)
91
static unsigned tmpfs_lnkcnt_get(void *nodep)
91
{
92
{
92
    return ((tmpfs_dentry_t *) nodep)->lnkcnt;
93
    return ((tmpfs_dentry_t *) nodep)->lnkcnt;
93
}
94
}
94
 
95
 
95
static bool tmpfs_has_children(void *nodep)
96
static bool tmpfs_has_children(void *nodep)
96
{
97
{
97
    return ((tmpfs_dentry_t *) nodep)->child != NULL;
98
    return ((tmpfs_dentry_t *) nodep)->child != NULL;
98
}
99
}
99
 
100
 
100
static void *tmpfs_root_get(dev_handle_t dev_handle)
101
static void *tmpfs_root_get(dev_handle_t dev_handle)
101
{
102
{
102
    return root;
103
    return root;
103
}
104
}
104
 
105
 
105
static char tmpfs_plb_get_char(unsigned pos)
106
static char tmpfs_plb_get_char(unsigned pos)
106
{
107
{
107
    return tmpfs_reg.plb_ro[pos % PLB_SIZE];
108
    return tmpfs_reg.plb_ro[pos % PLB_SIZE];
108
}
109
}
109
 
110
 
110
static bool tmpfs_is_directory(void *nodep)
111
static bool tmpfs_is_directory(void *nodep)
111
{
112
{
112
    return ((tmpfs_dentry_t *) nodep)->type == TMPFS_DIRECTORY;
113
    return ((tmpfs_dentry_t *) nodep)->type == TMPFS_DIRECTORY;
113
}
114
}
114
 
115
 
115
static bool tmpfs_is_file(void *nodep)
116
static bool tmpfs_is_file(void *nodep)
116
{
117
{
117
    return ((tmpfs_dentry_t *) nodep)->type == TMPFS_FILE;
118
    return ((tmpfs_dentry_t *) nodep)->type == TMPFS_FILE;
118
}
119
}
119
 
120
 
120
/** libfs operations */
121
/** libfs operations */
121
libfs_ops_t tmpfs_libfs_ops = {
122
libfs_ops_t tmpfs_libfs_ops = {
122
    .match = tmpfs_match,
123
    .match = tmpfs_match,
123
    .node_get = tmpfs_node_get,
124
    .node_get = tmpfs_node_get,
-
 
125
    .node_put = tmpfs_node_put,
124
    .create = tmpfs_create_node,
126
    .create = tmpfs_create_node,
125
    .destroy = tmpfs_destroy_node,
127
    .destroy = tmpfs_destroy_node,
126
    .link = tmpfs_link_node,
128
    .link = tmpfs_link_node,
127
    .unlink = tmpfs_unlink_node,
129
    .unlink = tmpfs_unlink_node,
128
    .index_get = tmpfs_index_get,
130
    .index_get = tmpfs_index_get,
129
    .size_get = tmpfs_size_get,
131
    .size_get = tmpfs_size_get,
130
    .lnkcnt_get = tmpfs_lnkcnt_get,
132
    .lnkcnt_get = tmpfs_lnkcnt_get,
131
    .has_children = tmpfs_has_children,
133
    .has_children = tmpfs_has_children,
132
    .root_get = tmpfs_root_get,
134
    .root_get = tmpfs_root_get,
133
    .plb_get_char = tmpfs_plb_get_char,
135
    .plb_get_char = tmpfs_plb_get_char,
134
    .is_directory = tmpfs_is_directory,
136
    .is_directory = tmpfs_is_directory,
135
    .is_file = tmpfs_is_file
137
    .is_file = tmpfs_is_file
136
};
138
};
137
 
139
 
138
/** Hash table of all directory entries. */
140
/** Hash table of all directory entries. */
139
hash_table_t dentries;
141
hash_table_t dentries;
140
 
142
 
141
/* Implementation of hash table interface for the dentries hash table. */
143
/* Implementation of hash table interface for the dentries hash table. */
142
static hash_index_t dentries_hash(unsigned long *key)
144
static hash_index_t dentries_hash(unsigned long *key)
143
{
145
{
144
    return *key % DENTRIES_BUCKETS;
146
    return *key % DENTRIES_BUCKETS;
145
}
147
}
146
 
148
 
147
static int dentries_compare(unsigned long *key, hash_count_t keys,
149
static int dentries_compare(unsigned long *key, hash_count_t keys,
148
    link_t *item)
150
    link_t *item)
149
{
151
{
150
    tmpfs_dentry_t *dentry = hash_table_get_instance(item, tmpfs_dentry_t,
152
    tmpfs_dentry_t *dentry = hash_table_get_instance(item, tmpfs_dentry_t,
151
        dh_link);
153
        dh_link);
152
    return dentry->index == *key;
154
    return dentry->index == *key;
153
}
155
}
154
 
156
 
155
static void dentries_remove_callback(link_t *item)
157
static void dentries_remove_callback(link_t *item)
156
{
158
{
157
}
159
}
158
 
160
 
159
/** TMPFS dentries hash table operations. */
161
/** TMPFS dentries hash table operations. */
160
hash_table_operations_t dentries_ops = {
162
hash_table_operations_t dentries_ops = {
161
    .hash = dentries_hash,
163
    .hash = dentries_hash,
162
    .compare = dentries_compare,
164
    .compare = dentries_compare,
163
    .remove_callback = dentries_remove_callback
165
    .remove_callback = dentries_remove_callback
164
};
166
};
165
 
167
 
166
fs_index_t tmpfs_next_index = 1;
168
fs_index_t tmpfs_next_index = 1;
167
 
169
 
168
typedef struct {
170
typedef struct {
169
    char *name;
171
    char *name;
170
    tmpfs_dentry_t *parent;
172
    tmpfs_dentry_t *parent;
171
    link_t link;
173
    link_t link;
172
} tmpfs_name_t;
174
} tmpfs_name_t;
173
 
175
 
174
/* Implementation of hash table interface for the names hash table. */
176
/* Implementation of hash table interface for the names hash table. */
175
static hash_index_t names_hash(unsigned long *key)
177
static hash_index_t names_hash(unsigned long *key)
176
{
178
{
177
    tmpfs_dentry_t *dentry = (tmpfs_dentry_t *) *key;
179
    tmpfs_dentry_t *dentry = (tmpfs_dentry_t *) *key;
178
    return dentry->index % NAMES_BUCKETS;
180
    return dentry->index % NAMES_BUCKETS;
179
}
181
}
180
 
182
 
181
static int names_compare(unsigned long *key, hash_count_t keys, link_t *item)
183
static int names_compare(unsigned long *key, hash_count_t keys, link_t *item)
182
{
184
{
183
    tmpfs_dentry_t *dentry = (tmpfs_dentry_t *) *key;
185
    tmpfs_dentry_t *dentry = (tmpfs_dentry_t *) *key;
184
    tmpfs_name_t *namep = hash_table_get_instance(item, tmpfs_name_t,
186
    tmpfs_name_t *namep = hash_table_get_instance(item, tmpfs_name_t,
185
        link);
187
        link);
186
    return dentry == namep->parent;
188
    return dentry == namep->parent;
187
}
189
}
188
 
190
 
189
static void names_remove_callback(link_t *item)
191
static void names_remove_callback(link_t *item)
190
{
192
{
191
    tmpfs_name_t *namep = hash_table_get_instance(item, tmpfs_name_t,
193
    tmpfs_name_t *namep = hash_table_get_instance(item, tmpfs_name_t,
192
        link);
194
        link);
193
    free(namep->name);
195
    free(namep->name);
194
    free(namep);
196
    free(namep);
195
}
197
}
196
 
198
 
197
/** TMPFS node names hash table operations. */
199
/** TMPFS node names hash table operations. */
198
static hash_table_operations_t names_ops = {
200
static hash_table_operations_t names_ops = {
199
    .hash = names_hash,
201
    .hash = names_hash,
200
    .compare = names_compare,
202
    .compare = names_compare,
201
    .remove_callback = names_remove_callback
203
    .remove_callback = names_remove_callback
202
};
204
};
203
 
205
 
204
static void tmpfs_name_initialize(tmpfs_name_t *namep)
206
static void tmpfs_name_initialize(tmpfs_name_t *namep)
205
{
207
{
206
    namep->name = NULL;
208
    namep->name = NULL;
207
    namep->parent = NULL;
209
    namep->parent = NULL;
208
    link_initialize(&namep->link);
210
    link_initialize(&namep->link);
209
}
211
}
210
 
212
 
211
static bool tmpfs_dentry_initialize(tmpfs_dentry_t *dentry)
213
static bool tmpfs_dentry_initialize(tmpfs_dentry_t *dentry)
212
{
214
{
213
    dentry->index = 0;
215
    dentry->index = 0;
214
    dentry->sibling = NULL;
216
    dentry->sibling = NULL;
215
    dentry->child = NULL;
217
    dentry->child = NULL;
216
    dentry->type = TMPFS_NONE;
218
    dentry->type = TMPFS_NONE;
217
    dentry->lnkcnt = 0;
219
    dentry->lnkcnt = 0;
218
    dentry->size = 0;
220
    dentry->size = 0;
219
    dentry->data = NULL;
221
    dentry->data = NULL;
220
    link_initialize(&dentry->dh_link);
222
    link_initialize(&dentry->dh_link);
221
    return (bool)hash_table_create(&dentry->names, NAMES_BUCKETS, 1,
223
    return (bool)hash_table_create(&dentry->names, NAMES_BUCKETS, 1,
222
        &names_ops);
224
        &names_ops);
223
}
225
}
224
 
226
 
225
static bool tmpfs_init(void)
227
static bool tmpfs_init(void)
226
{
228
{
227
    if (!hash_table_create(&dentries, DENTRIES_BUCKETS, 1, &dentries_ops))
229
    if (!hash_table_create(&dentries, DENTRIES_BUCKETS, 1, &dentries_ops))
228
        return false;
230
        return false;
229
    root = (tmpfs_dentry_t *) tmpfs_create_node(L_DIRECTORY);
231
    root = (tmpfs_dentry_t *) tmpfs_create_node(L_DIRECTORY);
230
    if (!root) {
232
    if (!root) {
231
        hash_table_destroy(&dentries);
233
        hash_table_destroy(&dentries);
232
        return false;
234
        return false;
233
    }
235
    }
234
    root->lnkcnt = 1;
236
    root->lnkcnt = 1;
235
    return true;
237
    return true;
236
}
238
}
237
 
239
 
238
/** Compare one component of path to a directory entry.
240
/** Compare one component of path to a directory entry.
239
 *
241
 *
240
 * @param parentp   Pointer to node from which we descended.
242
 * @param parentp   Pointer to node from which we descended.
241
 * @param childp    Pointer to node to compare the path component with.
243
 * @param childp    Pointer to node to compare the path component with.
242
 * @param component Array of characters holding component name.
244
 * @param component Array of characters holding component name.
243
 *
245
 *
244
 * @return      True on match, false otherwise.
246
 * @return      True on match, false otherwise.
245
 */
247
 */
246
static bool
248
static bool
247
tmpfs_match_one(tmpfs_dentry_t *parentp, tmpfs_dentry_t *childp,
249
tmpfs_match_one(tmpfs_dentry_t *parentp, tmpfs_dentry_t *childp,
248
    const char *component)
250
    const char *component)
249
{
251
{
250
    unsigned long key = (unsigned long) parentp;
252
    unsigned long key = (unsigned long) parentp;
251
    link_t *hlp = hash_table_find(&childp->names, &key);
253
    link_t *hlp = hash_table_find(&childp->names, &key);
252
    assert(hlp);
254
    assert(hlp);
253
    tmpfs_name_t *namep = hash_table_get_instance(hlp, tmpfs_name_t, link);
255
    tmpfs_name_t *namep = hash_table_get_instance(hlp, tmpfs_name_t, link);
254
    return !strcmp(namep->name, component);
256
    return !strcmp(namep->name, component);
255
}
257
}
256
 
258
 
257
void *tmpfs_match(void *prnt, const char *component)
259
void *tmpfs_match(void *prnt, const char *component)
258
{
260
{
259
    tmpfs_dentry_t *parentp = (tmpfs_dentry_t *) prnt;
261
    tmpfs_dentry_t *parentp = (tmpfs_dentry_t *) prnt;
260
    tmpfs_dentry_t *childp = parentp->child;
262
    tmpfs_dentry_t *childp = parentp->child;
261
 
263
 
262
    while (childp && !tmpfs_match_one(parentp, childp, component))
264
    while (childp && !tmpfs_match_one(parentp, childp, component))
263
        childp = childp->sibling;
265
        childp = childp->sibling;
264
 
266
 
265
    return (void *) childp;
267
    return (void *) childp;
266
}
268
}
267
 
269
 
268
void *
270
void *
269
tmpfs_node_get(dev_handle_t dev_handle, fs_index_t index, fs_index_t pindex)
271
tmpfs_node_get(dev_handle_t dev_handle, fs_index_t index, fs_index_t pindex)
270
{
272
{
271
    unsigned long key = index;
273
    unsigned long key = index;
272
    link_t *lnk = hash_table_find(&dentries, &key);
274
    link_t *lnk = hash_table_find(&dentries, &key);
273
    if (!lnk)
275
    if (!lnk)
274
        return NULL;
276
        return NULL;
275
    return hash_table_get_instance(lnk, tmpfs_dentry_t, dh_link);
277
    return hash_table_get_instance(lnk, tmpfs_dentry_t, dh_link);
276
}
278
}
-
 
279
 
-
 
280
void tmpfs_node_put(void *node)
-
 
281
{
-
 
282
    /* nothing to do */
-
 
283
}
277
 
284
 
278
void *tmpfs_create_node(int lflag)
285
void *tmpfs_create_node(int lflag)
279
{
286
{
280
    assert((lflag & L_FILE) ^ (lflag & L_DIRECTORY));
287
    assert((lflag & L_FILE) ^ (lflag & L_DIRECTORY));
281
 
288
 
282
    tmpfs_dentry_t *node = malloc(sizeof(tmpfs_dentry_t));
289
    tmpfs_dentry_t *node = malloc(sizeof(tmpfs_dentry_t));
283
    if (!node)
290
    if (!node)
284
        return NULL;
291
        return NULL;
285
 
292
 
286
    if (!tmpfs_dentry_initialize(node)) {
293
    if (!tmpfs_dentry_initialize(node)) {
287
        free(node);
294
        free(node);
288
        return NULL;
295
        return NULL;
289
    }
296
    }
290
    node->index = tmpfs_next_index++;
297
    node->index = tmpfs_next_index++;
291
    if (lflag & L_DIRECTORY)
298
    if (lflag & L_DIRECTORY)
292
        node->type = TMPFS_DIRECTORY;
299
        node->type = TMPFS_DIRECTORY;
293
    else
300
    else
294
        node->type = TMPFS_FILE;
301
        node->type = TMPFS_FILE;
295
 
302
 
296
    /* Insert the new node into the dentry hash table. */
303
    /* Insert the new node into the dentry hash table. */
297
    unsigned long key = node->index;
304
    unsigned long key = node->index;
298
    hash_table_insert(&dentries, &key, &node->dh_link);
305
    hash_table_insert(&dentries, &key, &node->dh_link);
299
    return (void *) node;
306
    return (void *) node;
300
}
307
}
301
 
308
 
302
bool tmpfs_link_node(void *prnt, void *chld, const char *nm)
309
bool tmpfs_link_node(void *prnt, void *chld, const char *nm)
303
{
310
{
304
    tmpfs_dentry_t *parentp = (tmpfs_dentry_t *) prnt;
311
    tmpfs_dentry_t *parentp = (tmpfs_dentry_t *) prnt;
305
    tmpfs_dentry_t *childp = (tmpfs_dentry_t *) chld;
312
    tmpfs_dentry_t *childp = (tmpfs_dentry_t *) chld;
306
 
313
 
307
    assert(parentp->type == TMPFS_DIRECTORY);
314
    assert(parentp->type == TMPFS_DIRECTORY);
308
 
315
 
309
    tmpfs_name_t *namep = malloc(sizeof(tmpfs_name_t));
316
    tmpfs_name_t *namep = malloc(sizeof(tmpfs_name_t));
310
    if (!namep)
317
    if (!namep)
311
        return false;
318
        return false;
312
    tmpfs_name_initialize(namep);
319
    tmpfs_name_initialize(namep);
313
    size_t len = strlen(nm);
320
    size_t len = strlen(nm);
314
    namep->name = malloc(len + 1);
321
    namep->name = malloc(len + 1);
315
    if (!namep->name) {
322
    if (!namep->name) {
316
        free(namep);
323
        free(namep);
317
        return false;
324
        return false;
318
    }
325
    }
319
    strcpy(namep->name, nm);
326
    strcpy(namep->name, nm);
320
    namep->parent = parentp;
327
    namep->parent = parentp;
321
   
328
   
322
    childp->lnkcnt++;
329
    childp->lnkcnt++;
323
 
330
 
324
    unsigned long key = (unsigned long) parentp;
331
    unsigned long key = (unsigned long) parentp;
325
    hash_table_insert(&childp->names, &key, &namep->link);
332
    hash_table_insert(&childp->names, &key, &namep->link);
326
 
333
 
327
    /* Insert the new node into the namespace. */
334
    /* Insert the new node into the namespace. */
328
    if (parentp->child) {
335
    if (parentp->child) {
329
        tmpfs_dentry_t *tmp = parentp->child;
336
        tmpfs_dentry_t *tmp = parentp->child;
330
        while (tmp->sibling)
337
        while (tmp->sibling)
331
            tmp = tmp->sibling;
338
            tmp = tmp->sibling;
332
        tmp->sibling = childp;
339
        tmp->sibling = childp;
333
    } else {
340
    } else {
334
        parentp->child = childp;
341
        parentp->child = childp;
335
    }
342
    }
336
 
343
 
337
    return true;
344
    return true;
338
}
345
}
339
 
346
 
340
int tmpfs_unlink_node(void *prnt, void *chld)
347
int tmpfs_unlink_node(void *prnt, void *chld)
341
{
348
{
342
    tmpfs_dentry_t *parentp = (tmpfs_dentry_t *)prnt;
349
    tmpfs_dentry_t *parentp = (tmpfs_dentry_t *)prnt;
343
    tmpfs_dentry_t *childp = (tmpfs_dentry_t *)chld;
350
    tmpfs_dentry_t *childp = (tmpfs_dentry_t *)chld;
344
 
351
 
345
    if (!parentp)
352
    if (!parentp)
346
        return EBUSY;
353
        return EBUSY;
347
 
354
 
348
    if (childp->child)
355
    if (childp->child)
349
        return ENOTEMPTY;
356
        return ENOTEMPTY;
350
 
357
 
351
    if (parentp->child == childp) {
358
    if (parentp->child == childp) {
352
        parentp->child = childp->sibling;
359
        parentp->child = childp->sibling;
353
    } else {
360
    } else {
354
        /* TODO: consider doubly linked list for organizing siblings. */
361
        /* TODO: consider doubly linked list for organizing siblings. */
355
        tmpfs_dentry_t *tmp = parentp->child;
362
        tmpfs_dentry_t *tmp = parentp->child;
356
        while (tmp->sibling != childp)
363
        while (tmp->sibling != childp)
357
            tmp = tmp->sibling;
364
            tmp = tmp->sibling;
358
        tmp->sibling = childp->sibling;
365
        tmp->sibling = childp->sibling;
359
    }
366
    }
360
    childp->sibling = NULL;
367
    childp->sibling = NULL;
361
 
368
 
362
    unsigned long key = (unsigned long) parentp;
369
    unsigned long key = (unsigned long) parentp;
363
    hash_table_remove(&childp->names, &key, 1);
370
    hash_table_remove(&childp->names, &key, 1);
364
 
371
 
365
    childp->lnkcnt--;
372
    childp->lnkcnt--;
366
 
373
 
367
    return EOK;
374
    return EOK;
368
}
375
}
369
 
376
 
370
void tmpfs_destroy_node(void *nodep)
377
void tmpfs_destroy_node(void *nodep)
371
{
378
{
372
    tmpfs_dentry_t *dentry = (tmpfs_dentry_t *) nodep;
379
    tmpfs_dentry_t *dentry = (tmpfs_dentry_t *) nodep;
373
   
380
   
374
    assert(!dentry->lnkcnt);
381
    assert(!dentry->lnkcnt);
375
    assert(!dentry->child);
382
    assert(!dentry->child);
376
    assert(!dentry->sibling);
383
    assert(!dentry->sibling);
377
 
384
 
378
    unsigned long key = dentry->index;
385
    unsigned long key = dentry->index;
379
    hash_table_remove(&dentries, &key, 1);
386
    hash_table_remove(&dentries, &key, 1);
380
 
387
 
381
    hash_table_destroy(&dentry->names);
388
    hash_table_destroy(&dentry->names);
382
 
389
 
383
    if (dentry->type == TMPFS_FILE)
390
    if (dentry->type == TMPFS_FILE)
384
        free(dentry->data);
391
        free(dentry->data);
385
    free(dentry);
392
    free(dentry);
386
}
393
}
387
 
394
 
388
void tmpfs_lookup(ipc_callid_t rid, ipc_call_t *request)
395
void tmpfs_lookup(ipc_callid_t rid, ipc_call_t *request)
389
{
396
{
390
    /* Initialize TMPFS. */
397
    /* Initialize TMPFS. */
391
    if (!root && !tmpfs_init()) {
398
    if (!root && !tmpfs_init()) {
392
        ipc_answer_0(rid, ENOMEM);
399
        ipc_answer_0(rid, ENOMEM);
393
        return;
400
        return;
394
    }
401
    }
395
    libfs_lookup(&tmpfs_libfs_ops, tmpfs_reg.fs_handle, rid, request);
402
    libfs_lookup(&tmpfs_libfs_ops, tmpfs_reg.fs_handle, rid, request);
396
}
403
}
397
 
404
 
398
void tmpfs_read(ipc_callid_t rid, ipc_call_t *request)
405
void tmpfs_read(ipc_callid_t rid, ipc_call_t *request)
399
{
406
{
400
    dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
407
    dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
401
    fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
408
    fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
402
    off_t pos = (off_t)IPC_GET_ARG3(*request);
409
    off_t pos = (off_t)IPC_GET_ARG3(*request);
403
 
410
 
404
    /*
411
    /*
405
     * Lookup the respective dentry.
412
     * Lookup the respective dentry.
406
     */
413
     */
407
    link_t *hlp;
414
    link_t *hlp;
408
    unsigned long key = index;
415
    unsigned long key = index;
409
    hlp = hash_table_find(&dentries, &key);
416
    hlp = hash_table_find(&dentries, &key);
410
    if (!hlp) {
417
    if (!hlp) {
411
        ipc_answer_0(rid, ENOENT);
418
        ipc_answer_0(rid, ENOENT);
412
        return;
419
        return;
413
    }
420
    }
414
    tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t,
421
    tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t,
415
        dh_link);
422
        dh_link);
416
 
423
 
417
    /*
424
    /*
418
     * Receive the read request.
425
     * Receive the read request.
419
     */
426
     */
420
    ipc_callid_t callid;
427
    ipc_callid_t callid;
421
    size_t len;
428
    size_t len;
422
    if (!ipc_data_read_receive(&callid, &len)) {
429
    if (!ipc_data_read_receive(&callid, &len)) {
423
        ipc_answer_0(callid, EINVAL);  
430
        ipc_answer_0(callid, EINVAL);  
424
        ipc_answer_0(rid, EINVAL);
431
        ipc_answer_0(rid, EINVAL);
425
        return;
432
        return;
426
    }
433
    }
427
 
434
 
428
    size_t bytes;
435
    size_t bytes;
429
    if (dentry->type == TMPFS_FILE) {
436
    if (dentry->type == TMPFS_FILE) {
430
        bytes = max(0, min(dentry->size - pos, len));
437
        bytes = max(0, min(dentry->size - pos, len));
431
        (void) ipc_data_read_finalize(callid, dentry->data + pos,
438
        (void) ipc_data_read_finalize(callid, dentry->data + pos,
432
            bytes);
439
            bytes);
433
    } else {
440
    } else {
434
        int i;
441
        int i;
435
        tmpfs_dentry_t *cur;
442
        tmpfs_dentry_t *cur;
436
       
443
       
437
        assert(dentry->type == TMPFS_DIRECTORY);
444
        assert(dentry->type == TMPFS_DIRECTORY);
438
       
445
       
439
        /*
446
        /*
440
         * Yes, we really use O(n) algorithm here.
447
         * Yes, we really use O(n) algorithm here.
441
         * If it bothers someone, it could be fixed by introducing a
448
         * If it bothers someone, it could be fixed by introducing a
442
         * hash table.
449
         * hash table.
443
         */
450
         */
444
        for (i = 0, cur = dentry->child; i < pos && cur; i++,
451
        for (i = 0, cur = dentry->child; i < pos && cur; i++,
445
            cur = cur->sibling)
452
            cur = cur->sibling)
446
            ;
453
            ;
447
 
454
 
448
        if (!cur) {
455
        if (!cur) {
449
            ipc_answer_0(callid, ENOENT);
456
            ipc_answer_0(callid, ENOENT);
450
            ipc_answer_1(rid, ENOENT, 0);
457
            ipc_answer_1(rid, ENOENT, 0);
451
            return;
458
            return;
452
        }
459
        }
453
 
460
 
454
        unsigned long key = (unsigned long) dentry;
461
        unsigned long key = (unsigned long) dentry;
455
        link_t *hlp = hash_table_find(&cur->names, &key);
462
        link_t *hlp = hash_table_find(&cur->names, &key);
456
        assert(hlp);
463
        assert(hlp);
457
        tmpfs_name_t *namep = hash_table_get_instance(hlp, tmpfs_name_t,
464
        tmpfs_name_t *namep = hash_table_get_instance(hlp, tmpfs_name_t,
458
            link);
465
            link);
459
 
466
 
460
        (void) ipc_data_read_finalize(callid, namep->name,
467
        (void) ipc_data_read_finalize(callid, namep->name,
461
            strlen(namep->name) + 1);
468
            strlen(namep->name) + 1);
462
        bytes = 1;
469
        bytes = 1;
463
    }
470
    }
464
 
471
 
465
    /*
472
    /*
466
     * Answer the VFS_READ call.
473
     * Answer the VFS_READ call.
467
     */
474
     */
468
    ipc_answer_1(rid, EOK, bytes);
475
    ipc_answer_1(rid, EOK, bytes);
469
}
476
}
470
 
477
 
471
void tmpfs_write(ipc_callid_t rid, ipc_call_t *request)
478
void tmpfs_write(ipc_callid_t rid, ipc_call_t *request)
472
{
479
{
473
    dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
480
    dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
474
    fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
481
    fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
475
    off_t pos = (off_t)IPC_GET_ARG3(*request);
482
    off_t pos = (off_t)IPC_GET_ARG3(*request);
476
 
483
 
477
    /*
484
    /*
478
     * Lookup the respective dentry.
485
     * Lookup the respective dentry.
479
     */
486
     */
480
    link_t *hlp;
487
    link_t *hlp;
481
    unsigned long key = index;
488
    unsigned long key = index;
482
    hlp = hash_table_find(&dentries, &key);
489
    hlp = hash_table_find(&dentries, &key);
483
    if (!hlp) {
490
    if (!hlp) {
484
        ipc_answer_0(rid, ENOENT);
491
        ipc_answer_0(rid, ENOENT);
485
        return;
492
        return;
486
    }
493
    }
487
    tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t,
494
    tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t,
488
        dh_link);
495
        dh_link);
489
 
496
 
490
    /*
497
    /*
491
     * Receive the write request.
498
     * Receive the write request.
492
     */
499
     */
493
    ipc_callid_t callid;
500
    ipc_callid_t callid;
494
    size_t len;
501
    size_t len;
495
    if (!ipc_data_write_receive(&callid, &len)) {
502
    if (!ipc_data_write_receive(&callid, &len)) {
496
        ipc_answer_0(callid, EINVAL);  
503
        ipc_answer_0(callid, EINVAL);  
497
        ipc_answer_0(rid, EINVAL);
504
        ipc_answer_0(rid, EINVAL);
498
        return;
505
        return;
499
    }
506
    }
500
 
507
 
501
    /*
508
    /*
502
     * Check whether the file needs to grow.
509
     * Check whether the file needs to grow.
503
     */
510
     */
504
    if (pos + len <= dentry->size) {
511
    if (pos + len <= dentry->size) {
505
        /* The file size is not changing. */
512
        /* The file size is not changing. */
506
        (void) ipc_data_write_finalize(callid, dentry->data + pos, len);
513
        (void) ipc_data_write_finalize(callid, dentry->data + pos, len);
507
        ipc_answer_2(rid, EOK, len, dentry->size);
514
        ipc_answer_2(rid, EOK, len, dentry->size);
508
        return;
515
        return;
509
    }
516
    }
510
    size_t delta = (pos + len) - dentry->size;
517
    size_t delta = (pos + len) - dentry->size;
511
    /*
518
    /*
512
     * At this point, we are deliberately extremely straightforward and
519
     * At this point, we are deliberately extremely straightforward and
513
     * simply realloc the contents of the file on every write that grows the
520
     * simply realloc the contents of the file on every write that grows the
514
     * file. In the end, the situation might not be as bad as it may look:
521
     * file. In the end, the situation might not be as bad as it may look:
515
     * our heap allocator can save us and just grow the block whenever
522
     * our heap allocator can save us and just grow the block whenever
516
     * possible.
523
     * possible.
517
     */
524
     */
518
    void *newdata = realloc(dentry->data, dentry->size + delta);
525
    void *newdata = realloc(dentry->data, dentry->size + delta);
519
    if (!newdata) {
526
    if (!newdata) {
520
        ipc_answer_0(callid, ENOMEM);
527
        ipc_answer_0(callid, ENOMEM);
521
        ipc_answer_2(rid, EOK, 0, dentry->size);
528
        ipc_answer_2(rid, EOK, 0, dentry->size);
522
        return;
529
        return;
523
    }
530
    }
524
    /* Clear any newly allocated memory in order to emulate gaps. */
531
    /* Clear any newly allocated memory in order to emulate gaps. */
525
    memset(newdata + dentry->size, 0, delta);
532
    memset(newdata + dentry->size, 0, delta);
526
    dentry->size += delta;
533
    dentry->size += delta;
527
    dentry->data = newdata;
534
    dentry->data = newdata;
528
    (void) ipc_data_write_finalize(callid, dentry->data + pos, len);
535
    (void) ipc_data_write_finalize(callid, dentry->data + pos, len);
529
    ipc_answer_2(rid, EOK, len, dentry->size);
536
    ipc_answer_2(rid, EOK, len, dentry->size);
530
}
537
}
531
 
538
 
532
void tmpfs_truncate(ipc_callid_t rid, ipc_call_t *request)
539
void tmpfs_truncate(ipc_callid_t rid, ipc_call_t *request)
533
{
540
{
534
    dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
541
    dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
535
    fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
542
    fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
536
    size_t size = (off_t)IPC_GET_ARG3(*request);
543
    size_t size = (off_t)IPC_GET_ARG3(*request);
537
 
544
 
538
    /*
545
    /*
539
     * Lookup the respective dentry.
546
     * Lookup the respective dentry.
540
     */
547
     */
541
    link_t *hlp;
548
    link_t *hlp;
542
    unsigned long key = index;
549
    unsigned long key = index;
543
    hlp = hash_table_find(&dentries, &key);
550
    hlp = hash_table_find(&dentries, &key);
544
    if (!hlp) {
551
    if (!hlp) {
545
        ipc_answer_0(rid, ENOENT);
552
        ipc_answer_0(rid, ENOENT);
546
        return;
553
        return;
547
    }
554
    }
548
    tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t,
555
    tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t,
549
        dh_link);
556
        dh_link);
550
 
557
 
551
    if (size == dentry->size) {
558
    if (size == dentry->size) {
552
        ipc_answer_0(rid, EOK);
559
        ipc_answer_0(rid, EOK);
553
        return;
560
        return;
554
    }
561
    }
555
 
562
 
556
    void *newdata = realloc(dentry->data, size);
563
    void *newdata = realloc(dentry->data, size);
557
    if (!newdata) {
564
    if (!newdata) {
558
        ipc_answer_0(rid, ENOMEM);
565
        ipc_answer_0(rid, ENOMEM);
559
        return;
566
        return;
560
    }
567
    }
561
    if (size > dentry->size) {
568
    if (size > dentry->size) {
562
        size_t delta = size - dentry->size;
569
        size_t delta = size - dentry->size;
563
        memset(newdata + dentry->size, 0, delta);
570
        memset(newdata + dentry->size, 0, delta);
564
    }
571
    }
565
    dentry->size = size;
572
    dentry->size = size;
566
    dentry->data = newdata;
573
    dentry->data = newdata;
567
    ipc_answer_0(rid, EOK);
574
    ipc_answer_0(rid, EOK);
568
}
575
}
569
 
576
 
570
void tmpfs_destroy(ipc_callid_t rid, ipc_call_t *request)
577
void tmpfs_destroy(ipc_callid_t rid, ipc_call_t *request)
571
{
578
{
572
    dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
579
    dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
573
    fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
580
    fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
574
 
581
 
575
    link_t *hlp;
582
    link_t *hlp;
576
    unsigned long key = index;
583
    unsigned long key = index;
577
    hlp = hash_table_find(&dentries, &key);
584
    hlp = hash_table_find(&dentries, &key);
578
    if (!hlp) {
585
    if (!hlp) {
579
        ipc_answer_0(rid, ENOENT);
586
        ipc_answer_0(rid, ENOENT);
580
        return;
587
        return;
581
    }
588
    }
582
    tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t,
589
    tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t,
583
        dh_link);
590
        dh_link);
584
    tmpfs_destroy_node(dentry);
591
    tmpfs_destroy_node(dentry);
585
    ipc_answer_0(rid, EOK);
592
    ipc_answer_0(rid, EOK);
586
}
593
}
587
 
594
 
588
/**
595
/**
589
 * @}
596
 * @}
590
 */
597
 */
591
 
598