Subversion Repositories HelenOS

Rev

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

Rev 3103 Rev 3109
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);
73
static void *tmpfs_node_get(dev_handle_t, fs_index_t);
74
static void tmpfs_node_put(void *);
74
static void tmpfs_node_put(void *);
75
static void *tmpfs_create_node(int);
75
static void *tmpfs_create_node(int);
76
static bool tmpfs_link_node(void *, void *, const char *);
76
static bool tmpfs_link_node(void *, void *, const char *);
77
static int tmpfs_unlink_node(void *, void *);
77
static int tmpfs_unlink_node(void *, void *);
78
static int tmpfs_destroy_node(void *);
78
static int tmpfs_destroy_node(void *);
79
 
79
 
80
/* Implementation of helper functions. */
80
/* Implementation of helper functions. */
81
static fs_index_t tmpfs_index_get(void *nodep)
81
static fs_index_t tmpfs_index_get(void *nodep)
82
{
82
{
83
    return ((tmpfs_dentry_t *) nodep)->index;
83
    return ((tmpfs_dentry_t *) nodep)->index;
84
}
84
}
85
 
85
 
86
static size_t tmpfs_size_get(void *nodep)
86
static size_t tmpfs_size_get(void *nodep)
87
{
87
{
88
    return ((tmpfs_dentry_t *) nodep)->size;
88
    return ((tmpfs_dentry_t *) nodep)->size;
89
}
89
}
90
 
90
 
91
static unsigned tmpfs_lnkcnt_get(void *nodep)
91
static unsigned tmpfs_lnkcnt_get(void *nodep)
92
{
92
{
93
    return ((tmpfs_dentry_t *) nodep)->lnkcnt;
93
    return ((tmpfs_dentry_t *) nodep)->lnkcnt;
94
}
94
}
95
 
95
 
96
static bool tmpfs_has_children(void *nodep)
96
static bool tmpfs_has_children(void *nodep)
97
{
97
{
98
    return ((tmpfs_dentry_t *) nodep)->child != NULL;
98
    return ((tmpfs_dentry_t *) nodep)->child != NULL;
99
}
99
}
100
 
100
 
101
static void *tmpfs_root_get(dev_handle_t dev_handle)
101
static void *tmpfs_root_get(dev_handle_t dev_handle)
102
{
102
{
103
    return root;
103
    return root;
104
}
104
}
105
 
105
 
106
static char tmpfs_plb_get_char(unsigned pos)
106
static char tmpfs_plb_get_char(unsigned pos)
107
{
107
{
108
    return tmpfs_reg.plb_ro[pos % PLB_SIZE];
108
    return tmpfs_reg.plb_ro[pos % PLB_SIZE];
109
}
109
}
110
 
110
 
111
static bool tmpfs_is_directory(void *nodep)
111
static bool tmpfs_is_directory(void *nodep)
112
{
112
{
113
    return ((tmpfs_dentry_t *) nodep)->type == TMPFS_DIRECTORY;
113
    return ((tmpfs_dentry_t *) nodep)->type == TMPFS_DIRECTORY;
114
}
114
}
115
 
115
 
116
static bool tmpfs_is_file(void *nodep)
116
static bool tmpfs_is_file(void *nodep)
117
{
117
{
118
    return ((tmpfs_dentry_t *) nodep)->type == TMPFS_FILE;
118
    return ((tmpfs_dentry_t *) nodep)->type == TMPFS_FILE;
119
}
119
}
120
 
120
 
121
/** libfs operations */
121
/** libfs operations */
122
libfs_ops_t tmpfs_libfs_ops = {
122
libfs_ops_t tmpfs_libfs_ops = {
123
    .match = tmpfs_match,
123
    .match = tmpfs_match,
124
    .node_get = tmpfs_node_get,
124
    .node_get = tmpfs_node_get,
125
    .node_put = tmpfs_node_put,
125
    .node_put = tmpfs_node_put,
126
    .create = tmpfs_create_node,
126
    .create = tmpfs_create_node,
127
    .destroy = tmpfs_destroy_node,
127
    .destroy = tmpfs_destroy_node,
128
    .link = tmpfs_link_node,
128
    .link = tmpfs_link_node,
129
    .unlink = tmpfs_unlink_node,
129
    .unlink = tmpfs_unlink_node,
130
    .index_get = tmpfs_index_get,
130
    .index_get = tmpfs_index_get,
131
    .size_get = tmpfs_size_get,
131
    .size_get = tmpfs_size_get,
132
    .lnkcnt_get = tmpfs_lnkcnt_get,
132
    .lnkcnt_get = tmpfs_lnkcnt_get,
133
    .has_children = tmpfs_has_children,
133
    .has_children = tmpfs_has_children,
134
    .root_get = tmpfs_root_get,
134
    .root_get = tmpfs_root_get,
135
    .plb_get_char = tmpfs_plb_get_char,
135
    .plb_get_char = tmpfs_plb_get_char,
136
    .is_directory = tmpfs_is_directory,
136
    .is_directory = tmpfs_is_directory,
137
    .is_file = tmpfs_is_file
137
    .is_file = tmpfs_is_file
138
};
138
};
139
 
139
 
140
/** Hash table of all directory entries. */
140
/** Hash table of all directory entries. */
141
hash_table_t dentries;
141
hash_table_t dentries;
142
 
142
 
143
/* Implementation of hash table interface for the dentries hash table. */
143
/* Implementation of hash table interface for the dentries hash table. */
144
static hash_index_t dentries_hash(unsigned long *key)
144
static hash_index_t dentries_hash(unsigned long *key)
145
{
145
{
146
    return *key % DENTRIES_BUCKETS;
146
    return *key % DENTRIES_BUCKETS;
147
}
147
}
148
 
148
 
149
static int dentries_compare(unsigned long *key, hash_count_t keys,
149
static int dentries_compare(unsigned long *key, hash_count_t keys,
150
    link_t *item)
150
    link_t *item)
151
{
151
{
152
    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,
153
        dh_link);
153
        dh_link);
154
    return dentry->index == *key;
154
    return dentry->index == *key;
155
}
155
}
156
 
156
 
157
static void dentries_remove_callback(link_t *item)
157
static void dentries_remove_callback(link_t *item)
158
{
158
{
159
}
159
}
160
 
160
 
161
/** TMPFS dentries hash table operations. */
161
/** TMPFS dentries hash table operations. */
162
hash_table_operations_t dentries_ops = {
162
hash_table_operations_t dentries_ops = {
163
    .hash = dentries_hash,
163
    .hash = dentries_hash,
164
    .compare = dentries_compare,
164
    .compare = dentries_compare,
165
    .remove_callback = dentries_remove_callback
165
    .remove_callback = dentries_remove_callback
166
};
166
};
167
 
167
 
168
fs_index_t tmpfs_next_index = 1;
168
fs_index_t tmpfs_next_index = 1;
169
 
169
 
170
typedef struct {
170
typedef struct {
171
    char *name;
171
    char *name;
172
    tmpfs_dentry_t *parent;
172
    tmpfs_dentry_t *parent;
173
    link_t link;
173
    link_t link;
174
} tmpfs_name_t;
174
} tmpfs_name_t;
175
 
175
 
176
/* Implementation of hash table interface for the names hash table. */
176
/* Implementation of hash table interface for the names hash table. */
177
static hash_index_t names_hash(unsigned long *key)
177
static hash_index_t names_hash(unsigned long *key)
178
{
178
{
179
    tmpfs_dentry_t *dentry = (tmpfs_dentry_t *) *key;
179
    tmpfs_dentry_t *dentry = (tmpfs_dentry_t *) *key;
180
    return dentry->index % NAMES_BUCKETS;
180
    return dentry->index % NAMES_BUCKETS;
181
}
181
}
182
 
182
 
183
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)
184
{
184
{
185
    tmpfs_dentry_t *dentry = (tmpfs_dentry_t *) *key;
185
    tmpfs_dentry_t *dentry = (tmpfs_dentry_t *) *key;
186
    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,
187
        link);
187
        link);
188
    return dentry == namep->parent;
188
    return dentry == namep->parent;
189
}
189
}
190
 
190
 
191
static void names_remove_callback(link_t *item)
191
static void names_remove_callback(link_t *item)
192
{
192
{
193
    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,
194
        link);
194
        link);
195
    free(namep->name);
195
    free(namep->name);
196
    free(namep);
196
    free(namep);
197
}
197
}
198
 
198
 
199
/** TMPFS node names hash table operations. */
199
/** TMPFS node names hash table operations. */
200
static hash_table_operations_t names_ops = {
200
static hash_table_operations_t names_ops = {
201
    .hash = names_hash,
201
    .hash = names_hash,
202
    .compare = names_compare,
202
    .compare = names_compare,
203
    .remove_callback = names_remove_callback
203
    .remove_callback = names_remove_callback
204
};
204
};
205
 
205
 
206
static void tmpfs_name_initialize(tmpfs_name_t *namep)
206
static void tmpfs_name_initialize(tmpfs_name_t *namep)
207
{
207
{
208
    namep->name = NULL;
208
    namep->name = NULL;
209
    namep->parent = NULL;
209
    namep->parent = NULL;
210
    link_initialize(&namep->link);
210
    link_initialize(&namep->link);
211
}
211
}
212
 
212
 
213
static bool tmpfs_dentry_initialize(tmpfs_dentry_t *dentry)
213
static bool tmpfs_dentry_initialize(tmpfs_dentry_t *dentry)
214
{
214
{
215
    dentry->index = 0;
215
    dentry->index = 0;
216
    dentry->sibling = NULL;
216
    dentry->sibling = NULL;
217
    dentry->child = NULL;
217
    dentry->child = NULL;
218
    dentry->type = TMPFS_NONE;
218
    dentry->type = TMPFS_NONE;
219
    dentry->lnkcnt = 0;
219
    dentry->lnkcnt = 0;
220
    dentry->size = 0;
220
    dentry->size = 0;
221
    dentry->data = NULL;
221
    dentry->data = NULL;
222
    link_initialize(&dentry->dh_link);
222
    link_initialize(&dentry->dh_link);
223
    return (bool)hash_table_create(&dentry->names, NAMES_BUCKETS, 1,
223
    return (bool)hash_table_create(&dentry->names, NAMES_BUCKETS, 1,
224
        &names_ops);
224
        &names_ops);
225
}
225
}
226
 
226
 
227
static bool tmpfs_init(void)
227
static bool tmpfs_init(void)
228
{
228
{
229
    if (!hash_table_create(&dentries, DENTRIES_BUCKETS, 1, &dentries_ops))
229
    if (!hash_table_create(&dentries, DENTRIES_BUCKETS, 1, &dentries_ops))
230
        return false;
230
        return false;
231
    root = (tmpfs_dentry_t *) tmpfs_create_node(L_DIRECTORY);
231
    root = (tmpfs_dentry_t *) tmpfs_create_node(L_DIRECTORY);
232
    if (!root) {
232
    if (!root) {
233
        hash_table_destroy(&dentries);
233
        hash_table_destroy(&dentries);
234
        return false;
234
        return false;
235
    }
235
    }
236
    root->lnkcnt = 1;
236
    root->lnkcnt = 1;
237
    return true;
237
    return true;
238
}
238
}
239
 
239
 
240
/** Compare one component of path to a directory entry.
240
/** Compare one component of path to a directory entry.
241
 *
241
 *
242
 * @param parentp   Pointer to node from which we descended.
242
 * @param parentp   Pointer to node from which we descended.
243
 * @param childp    Pointer to node to compare the path component with.
243
 * @param childp    Pointer to node to compare the path component with.
244
 * @param component Array of characters holding component name.
244
 * @param component Array of characters holding component name.
245
 *
245
 *
246
 * @return      True on match, false otherwise.
246
 * @return      True on match, false otherwise.
247
 */
247
 */
248
static bool
248
static bool
249
tmpfs_match_one(tmpfs_dentry_t *parentp, tmpfs_dentry_t *childp,
249
tmpfs_match_one(tmpfs_dentry_t *parentp, tmpfs_dentry_t *childp,
250
    const char *component)
250
    const char *component)
251
{
251
{
252
    unsigned long key = (unsigned long) parentp;
252
    unsigned long key = (unsigned long) parentp;
253
    link_t *hlp = hash_table_find(&childp->names, &key);
253
    link_t *hlp = hash_table_find(&childp->names, &key);
254
    assert(hlp);
254
    assert(hlp);
255
    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);
256
    return !strcmp(namep->name, component);
256
    return !strcmp(namep->name, component);
257
}
257
}
258
 
258
 
259
void *tmpfs_match(void *prnt, const char *component)
259
void *tmpfs_match(void *prnt, const char *component)
260
{
260
{
261
    tmpfs_dentry_t *parentp = (tmpfs_dentry_t *) prnt;
261
    tmpfs_dentry_t *parentp = (tmpfs_dentry_t *) prnt;
262
    tmpfs_dentry_t *childp = parentp->child;
262
    tmpfs_dentry_t *childp = parentp->child;
263
 
263
 
264
    while (childp && !tmpfs_match_one(parentp, childp, component))
264
    while (childp && !tmpfs_match_one(parentp, childp, component))
265
        childp = childp->sibling;
265
        childp = childp->sibling;
266
 
266
 
267
    return (void *) childp;
267
    return (void *) childp;
268
}
268
}
269
 
269
 
270
void *
270
void *
271
tmpfs_node_get(dev_handle_t dev_handle, fs_index_t index)
271
tmpfs_node_get(dev_handle_t dev_handle, fs_index_t index)
272
{
272
{
273
    unsigned long key = index;
273
    unsigned long key = index;
274
    link_t *lnk = hash_table_find(&dentries, &key);
274
    link_t *lnk = hash_table_find(&dentries, &key);
275
    if (!lnk)
275
    if (!lnk)
276
        return NULL;
276
        return NULL;
277
    return hash_table_get_instance(lnk, tmpfs_dentry_t, dh_link);
277
    return hash_table_get_instance(lnk, tmpfs_dentry_t, dh_link);
278
}
278
}
279
 
279
 
280
void tmpfs_node_put(void *node)
280
void tmpfs_node_put(void *node)
281
{
281
{
282
    /* nothing to do */
282
    /* nothing to do */
283
}
283
}
284
 
284
 
285
void *tmpfs_create_node(int lflag)
285
void *tmpfs_create_node(int lflag)
286
{
286
{
287
    assert((lflag & L_FILE) ^ (lflag & L_DIRECTORY));
287
    assert((lflag & L_FILE) ^ (lflag & L_DIRECTORY));
288
 
288
 
289
    tmpfs_dentry_t *node = malloc(sizeof(tmpfs_dentry_t));
289
    tmpfs_dentry_t *node = malloc(sizeof(tmpfs_dentry_t));
290
    if (!node)
290
    if (!node)
291
        return NULL;
291
        return NULL;
292
 
292
 
293
    if (!tmpfs_dentry_initialize(node)) {
293
    if (!tmpfs_dentry_initialize(node)) {
294
        free(node);
294
        free(node);
295
        return NULL;
295
        return NULL;
296
    }
296
    }
297
    node->index = tmpfs_next_index++;
297
    node->index = tmpfs_next_index++;
298
    if (lflag & L_DIRECTORY)
298
    if (lflag & L_DIRECTORY)
299
        node->type = TMPFS_DIRECTORY;
299
        node->type = TMPFS_DIRECTORY;
300
    else
300
    else
301
        node->type = TMPFS_FILE;
301
        node->type = TMPFS_FILE;
302
 
302
 
303
    /* Insert the new node into the dentry hash table. */
303
    /* Insert the new node into the dentry hash table. */
304
    unsigned long key = node->index;
304
    unsigned long key = node->index;
305
    hash_table_insert(&dentries, &key, &node->dh_link);
305
    hash_table_insert(&dentries, &key, &node->dh_link);
306
    return (void *) node;
306
    return (void *) node;
307
}
307
}
308
 
308
 
309
bool tmpfs_link_node(void *prnt, void *chld, const char *nm)
309
bool tmpfs_link_node(void *prnt, void *chld, const char *nm)
310
{
310
{
311
    tmpfs_dentry_t *parentp = (tmpfs_dentry_t *) prnt;
311
    tmpfs_dentry_t *parentp = (tmpfs_dentry_t *) prnt;
312
    tmpfs_dentry_t *childp = (tmpfs_dentry_t *) chld;
312
    tmpfs_dentry_t *childp = (tmpfs_dentry_t *) chld;
313
 
313
 
314
    assert(parentp->type == TMPFS_DIRECTORY);
314
    assert(parentp->type == TMPFS_DIRECTORY);
315
 
315
 
316
    tmpfs_name_t *namep = malloc(sizeof(tmpfs_name_t));
316
    tmpfs_name_t *namep = malloc(sizeof(tmpfs_name_t));
317
    if (!namep)
317
    if (!namep)
318
        return false;
318
        return false;
319
    tmpfs_name_initialize(namep);
319
    tmpfs_name_initialize(namep);
320
    size_t len = strlen(nm);
320
    size_t len = strlen(nm);
321
    namep->name = malloc(len + 1);
321
    namep->name = malloc(len + 1);
322
    if (!namep->name) {
322
    if (!namep->name) {
323
        free(namep);
323
        free(namep);
324
        return false;
324
        return false;
325
    }
325
    }
326
    strcpy(namep->name, nm);
326
    strcpy(namep->name, nm);
327
    namep->parent = parentp;
327
    namep->parent = parentp;
328
   
328
   
329
    childp->lnkcnt++;
329
    childp->lnkcnt++;
330
 
330
 
331
    unsigned long key = (unsigned long) parentp;
331
    unsigned long key = (unsigned long) parentp;
332
    hash_table_insert(&childp->names, &key, &namep->link);
332
    hash_table_insert(&childp->names, &key, &namep->link);
333
 
333
 
334
    /* Insert the new node into the namespace. */
334
    /* Insert the new node into the namespace. */
335
    if (parentp->child) {
335
    if (parentp->child) {
336
        tmpfs_dentry_t *tmp = parentp->child;
336
        tmpfs_dentry_t *tmp = parentp->child;
337
        while (tmp->sibling)
337
        while (tmp->sibling)
338
            tmp = tmp->sibling;
338
            tmp = tmp->sibling;
339
        tmp->sibling = childp;
339
        tmp->sibling = childp;
340
    } else {
340
    } else {
341
        parentp->child = childp;
341
        parentp->child = childp;
342
    }
342
    }
343
 
343
 
344
    return true;
344
    return true;
345
}
345
}
346
 
346
 
347
int tmpfs_unlink_node(void *prnt, void *chld)
347
int tmpfs_unlink_node(void *prnt, void *chld)
348
{
348
{
349
    tmpfs_dentry_t *parentp = (tmpfs_dentry_t *)prnt;
349
    tmpfs_dentry_t *parentp = (tmpfs_dentry_t *)prnt;
350
    tmpfs_dentry_t *childp = (tmpfs_dentry_t *)chld;
350
    tmpfs_dentry_t *childp = (tmpfs_dentry_t *)chld;
351
 
351
 
352
    if (!parentp)
352
    if (!parentp)
353
        return EBUSY;
353
        return EBUSY;
354
 
354
 
355
    if (childp->child)
355
    if (childp->child)
356
        return ENOTEMPTY;
356
        return ENOTEMPTY;
357
 
357
 
358
    if (parentp->child == childp) {
358
    if (parentp->child == childp) {
359
        parentp->child = childp->sibling;
359
        parentp->child = childp->sibling;
360
    } else {
360
    } else {
361
        /* TODO: consider doubly linked list for organizing siblings. */
361
        /* TODO: consider doubly linked list for organizing siblings. */
362
        tmpfs_dentry_t *tmp = parentp->child;
362
        tmpfs_dentry_t *tmp = parentp->child;
363
        while (tmp->sibling != childp)
363
        while (tmp->sibling != childp)
364
            tmp = tmp->sibling;
364
            tmp = tmp->sibling;
365
        tmp->sibling = childp->sibling;
365
        tmp->sibling = childp->sibling;
366
    }
366
    }
367
    childp->sibling = NULL;
367
    childp->sibling = NULL;
368
 
368
 
369
    unsigned long key = (unsigned long) parentp;
369
    unsigned long key = (unsigned long) parentp;
370
    hash_table_remove(&childp->names, &key, 1);
370
    hash_table_remove(&childp->names, &key, 1);
371
 
371
 
372
    childp->lnkcnt--;
372
    childp->lnkcnt--;
373
 
373
 
374
    return EOK;
374
    return EOK;
375
}
375
}
376
 
376
 
377
int tmpfs_destroy_node(void *nodep)
377
int tmpfs_destroy_node(void *nodep)
378
{
378
{
379
    tmpfs_dentry_t *dentry = (tmpfs_dentry_t *) nodep;
379
    tmpfs_dentry_t *dentry = (tmpfs_dentry_t *) nodep;
380
   
380
   
381
    assert(!dentry->lnkcnt);
381
    assert(!dentry->lnkcnt);
382
    assert(!dentry->child);
382
    assert(!dentry->child);
383
    assert(!dentry->sibling);
383
    assert(!dentry->sibling);
384
 
384
 
385
    unsigned long key = dentry->index;
385
    unsigned long key = dentry->index;
386
    hash_table_remove(&dentries, &key, 1);
386
    hash_table_remove(&dentries, &key, 1);
387
 
387
 
388
    hash_table_destroy(&dentry->names);
388
    hash_table_destroy(&dentry->names);
389
 
389
 
390
    if (dentry->type == TMPFS_FILE)
390
    if (dentry->type == TMPFS_FILE)
391
        free(dentry->data);
391
        free(dentry->data);
392
    free(dentry);
392
    free(dentry);
393
    return EOK;
393
    return EOK;
394
}
394
}
395
 
395
 
396
void tmpfs_mount(ipc_callid_t rid, ipc_call_t *request)
396
void tmpfs_mounted(ipc_callid_t rid, ipc_call_t *request)
397
{
397
{
398
    dev_handle_t mr_dev_handle = (dev_handle_t) IPC_GET_ARG1(*request);
398
    dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request);
399
    fs_index_t mr_index = (fs_index_t) IPC_GET_ARG2(*request);
-
 
400
    fs_handle_t mp_fs_handle = (fs_handle_t) IPC_GET_ARG3(*request);
-
 
401
    dev_handle_t mp_dev_handle = (dev_handle_t) IPC_GET_ARG4(*request);
-
 
402
    fs_index_t mp_index = (fs_index_t) IPC_GET_ARG5(*request);
-
 
403
   
-
 
404
    if ((mr_index == root->index) &&
-
 
405
        (mp_fs_handle == tmpfs_reg.fs_handle) &&
-
 
406
        (mp_index == mr_index)) {
-
 
407
       
-
 
408
        if (mr_dev_handle >= 0) {
-
 
409
            if (tmpfs_restore(mr_dev_handle))
-
 
410
                ipc_answer_0(rid, EOK);
-
 
411
            else
-
 
412
                ipc_answer_0(rid, ELIMIT);
-
 
413
        } else
-
 
414
            ipc_answer_0(rid, EOK);
-
 
415
    } else
-
 
416
        ipc_answer_0(rid, ENOTSUP);
-
 
417
}
-
 
418
 
399
 
419
void tmpfs_lookup(ipc_callid_t rid, ipc_call_t *request)
-
 
420
{
-
 
421
    /* Initialize TMPFS. */
400
    /* Initialize TMPFS. */
422
    if (!root && !tmpfs_init()) {
401
    if (!root && !tmpfs_init()) {
423
        ipc_answer_0(rid, ENOMEM);
402
        ipc_answer_0(rid, ENOMEM);
424
        return;
403
        return;
425
    }
404
    }
-
 
405
 
-
 
406
    if (dev_handle >= 0) {
-
 
407
        if (tmpfs_restore(dev_handle))
-
 
408
            ipc_answer_0(rid, EOK);
-
 
409
        else
-
 
410
            ipc_answer_0(rid, ELIMIT);
-
 
411
    } else {
-
 
412
            ipc_answer_0(rid, EOK);
-
 
413
    }
-
 
414
}
-
 
415
 
-
 
416
void tmpfs_mount(ipc_callid_t rid, ipc_call_t *request)
-
 
417
{
-
 
418
    dev_handle_t mp_dev_handle = (dev_handle_t) IPC_GET_ARG1(*request);
-
 
419
    fs_index_t mp_index = (fs_index_t) IPC_GET_ARG2(*request);
-
 
420
    fs_handle_t mr_fs_handle = (fs_handle_t) IPC_GET_ARG3(*request);
-
 
421
    dev_handle_t mr_dev_handle = (dev_handle_t) IPC_GET_ARG4(*request);
-
 
422
   
-
 
423
    ipc_answer_0(rid, ENOTSUP);
-
 
424
}
-
 
425
 
-
 
426
void tmpfs_lookup(ipc_callid_t rid, ipc_call_t *request)
-
 
427
{
426
    libfs_lookup(&tmpfs_libfs_ops, tmpfs_reg.fs_handle, rid, request);
428
    libfs_lookup(&tmpfs_libfs_ops, tmpfs_reg.fs_handle, rid, request);
427
}
429
}
428
 
430
 
429
void tmpfs_read(ipc_callid_t rid, ipc_call_t *request)
431
void tmpfs_read(ipc_callid_t rid, ipc_call_t *request)
430
{
432
{
431
    dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
433
    dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
432
    fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
434
    fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
433
    off_t pos = (off_t)IPC_GET_ARG3(*request);
435
    off_t pos = (off_t)IPC_GET_ARG3(*request);
434
 
436
 
435
    /*
437
    /*
436
     * Lookup the respective dentry.
438
     * Lookup the respective dentry.
437
     */
439
     */
438
    link_t *hlp;
440
    link_t *hlp;
439
    unsigned long key = index;
441
    unsigned long key = index;
440
    hlp = hash_table_find(&dentries, &key);
442
    hlp = hash_table_find(&dentries, &key);
441
    if (!hlp) {
443
    if (!hlp) {
442
        ipc_answer_0(rid, ENOENT);
444
        ipc_answer_0(rid, ENOENT);
443
        return;
445
        return;
444
    }
446
    }
445
    tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t,
447
    tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t,
446
        dh_link);
448
        dh_link);
447
 
449
 
448
    /*
450
    /*
449
     * Receive the read request.
451
     * Receive the read request.
450
     */
452
     */
451
    ipc_callid_t callid;
453
    ipc_callid_t callid;
452
    size_t len;
454
    size_t len;
453
    if (!ipc_data_read_receive(&callid, &len)) {
455
    if (!ipc_data_read_receive(&callid, &len)) {
454
        ipc_answer_0(callid, EINVAL);  
456
        ipc_answer_0(callid, EINVAL);  
455
        ipc_answer_0(rid, EINVAL);
457
        ipc_answer_0(rid, EINVAL);
456
        return;
458
        return;
457
    }
459
    }
458
 
460
 
459
    size_t bytes;
461
    size_t bytes;
460
    if (dentry->type == TMPFS_FILE) {
462
    if (dentry->type == TMPFS_FILE) {
461
        bytes = max(0, min(dentry->size - pos, len));
463
        bytes = max(0, min(dentry->size - pos, len));
462
        (void) ipc_data_read_finalize(callid, dentry->data + pos,
464
        (void) ipc_data_read_finalize(callid, dentry->data + pos,
463
            bytes);
465
            bytes);
464
    } else {
466
    } else {
465
        int i;
467
        int i;
466
        tmpfs_dentry_t *cur;
468
        tmpfs_dentry_t *cur;
467
       
469
       
468
        assert(dentry->type == TMPFS_DIRECTORY);
470
        assert(dentry->type == TMPFS_DIRECTORY);
469
       
471
       
470
        /*
472
        /*
471
         * Yes, we really use O(n) algorithm here.
473
         * Yes, we really use O(n) algorithm here.
472
         * If it bothers someone, it could be fixed by introducing a
474
         * If it bothers someone, it could be fixed by introducing a
473
         * hash table.
475
         * hash table.
474
         */
476
         */
475
        for (i = 0, cur = dentry->child; i < pos && cur; i++,
477
        for (i = 0, cur = dentry->child; i < pos && cur; i++,
476
            cur = cur->sibling)
478
            cur = cur->sibling)
477
            ;
479
            ;
478
 
480
 
479
        if (!cur) {
481
        if (!cur) {
480
            ipc_answer_0(callid, ENOENT);
482
            ipc_answer_0(callid, ENOENT);
481
            ipc_answer_1(rid, ENOENT, 0);
483
            ipc_answer_1(rid, ENOENT, 0);
482
            return;
484
            return;
483
        }
485
        }
484
 
486
 
485
        unsigned long key = (unsigned long) dentry;
487
        unsigned long key = (unsigned long) dentry;
486
        link_t *hlp = hash_table_find(&cur->names, &key);
488
        link_t *hlp = hash_table_find(&cur->names, &key);
487
        assert(hlp);
489
        assert(hlp);
488
        tmpfs_name_t *namep = hash_table_get_instance(hlp, tmpfs_name_t,
490
        tmpfs_name_t *namep = hash_table_get_instance(hlp, tmpfs_name_t,
489
            link);
491
            link);
490
 
492
 
491
        (void) ipc_data_read_finalize(callid, namep->name,
493
        (void) ipc_data_read_finalize(callid, namep->name,
492
            strlen(namep->name) + 1);
494
            strlen(namep->name) + 1);
493
        bytes = 1;
495
        bytes = 1;
494
    }
496
    }
495
 
497
 
496
    /*
498
    /*
497
     * Answer the VFS_READ call.
499
     * Answer the VFS_READ call.
498
     */
500
     */
499
    ipc_answer_1(rid, EOK, bytes);
501
    ipc_answer_1(rid, EOK, bytes);
500
}
502
}
501
 
503
 
502
void tmpfs_write(ipc_callid_t rid, ipc_call_t *request)
504
void tmpfs_write(ipc_callid_t rid, ipc_call_t *request)
503
{
505
{
504
    dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
506
    dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
505
    fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
507
    fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
506
    off_t pos = (off_t)IPC_GET_ARG3(*request);
508
    off_t pos = (off_t)IPC_GET_ARG3(*request);
507
 
509
 
508
    /*
510
    /*
509
     * Lookup the respective dentry.
511
     * Lookup the respective dentry.
510
     */
512
     */
511
    link_t *hlp;
513
    link_t *hlp;
512
    unsigned long key = index;
514
    unsigned long key = index;
513
    hlp = hash_table_find(&dentries, &key);
515
    hlp = hash_table_find(&dentries, &key);
514
    if (!hlp) {
516
    if (!hlp) {
515
        ipc_answer_0(rid, ENOENT);
517
        ipc_answer_0(rid, ENOENT);
516
        return;
518
        return;
517
    }
519
    }
518
    tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t,
520
    tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t,
519
        dh_link);
521
        dh_link);
520
 
522
 
521
    /*
523
    /*
522
     * Receive the write request.
524
     * Receive the write request.
523
     */
525
     */
524
    ipc_callid_t callid;
526
    ipc_callid_t callid;
525
    size_t len;
527
    size_t len;
526
    if (!ipc_data_write_receive(&callid, &len)) {
528
    if (!ipc_data_write_receive(&callid, &len)) {
527
        ipc_answer_0(callid, EINVAL);  
529
        ipc_answer_0(callid, EINVAL);  
528
        ipc_answer_0(rid, EINVAL);
530
        ipc_answer_0(rid, EINVAL);
529
        return;
531
        return;
530
    }
532
    }
531
 
533
 
532
    /*
534
    /*
533
     * Check whether the file needs to grow.
535
     * Check whether the file needs to grow.
534
     */
536
     */
535
    if (pos + len <= dentry->size) {
537
    if (pos + len <= dentry->size) {
536
        /* The file size is not changing. */
538
        /* The file size is not changing. */
537
        (void) ipc_data_write_finalize(callid, dentry->data + pos, len);
539
        (void) ipc_data_write_finalize(callid, dentry->data + pos, len);
538
        ipc_answer_2(rid, EOK, len, dentry->size);
540
        ipc_answer_2(rid, EOK, len, dentry->size);
539
        return;
541
        return;
540
    }
542
    }
541
    size_t delta = (pos + len) - dentry->size;
543
    size_t delta = (pos + len) - dentry->size;
542
    /*
544
    /*
543
     * At this point, we are deliberately extremely straightforward and
545
     * At this point, we are deliberately extremely straightforward and
544
     * simply realloc the contents of the file on every write that grows the
546
     * simply realloc the contents of the file on every write that grows the
545
     * file. In the end, the situation might not be as bad as it may look:
547
     * file. In the end, the situation might not be as bad as it may look:
546
     * our heap allocator can save us and just grow the block whenever
548
     * our heap allocator can save us and just grow the block whenever
547
     * possible.
549
     * possible.
548
     */
550
     */
549
    void *newdata = realloc(dentry->data, dentry->size + delta);
551
    void *newdata = realloc(dentry->data, dentry->size + delta);
550
    if (!newdata) {
552
    if (!newdata) {
551
        ipc_answer_0(callid, ENOMEM);
553
        ipc_answer_0(callid, ENOMEM);
552
        ipc_answer_2(rid, EOK, 0, dentry->size);
554
        ipc_answer_2(rid, EOK, 0, dentry->size);
553
        return;
555
        return;
554
    }
556
    }
555
    /* Clear any newly allocated memory in order to emulate gaps. */
557
    /* Clear any newly allocated memory in order to emulate gaps. */
556
    memset(newdata + dentry->size, 0, delta);
558
    memset(newdata + dentry->size, 0, delta);
557
    dentry->size += delta;
559
    dentry->size += delta;
558
    dentry->data = newdata;
560
    dentry->data = newdata;
559
    (void) ipc_data_write_finalize(callid, dentry->data + pos, len);
561
    (void) ipc_data_write_finalize(callid, dentry->data + pos, len);
560
    ipc_answer_2(rid, EOK, len, dentry->size);
562
    ipc_answer_2(rid, EOK, len, dentry->size);
561
}
563
}
562
 
564
 
563
void tmpfs_truncate(ipc_callid_t rid, ipc_call_t *request)
565
void tmpfs_truncate(ipc_callid_t rid, ipc_call_t *request)
564
{
566
{
565
    dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
567
    dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
566
    fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
568
    fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
567
    size_t size = (off_t)IPC_GET_ARG3(*request);
569
    size_t size = (off_t)IPC_GET_ARG3(*request);
568
 
570
 
569
    /*
571
    /*
570
     * Lookup the respective dentry.
572
     * Lookup the respective dentry.
571
     */
573
     */
572
    link_t *hlp;
574
    link_t *hlp;
573
    unsigned long key = index;
575
    unsigned long key = index;
574
    hlp = hash_table_find(&dentries, &key);
576
    hlp = hash_table_find(&dentries, &key);
575
    if (!hlp) {
577
    if (!hlp) {
576
        ipc_answer_0(rid, ENOENT);
578
        ipc_answer_0(rid, ENOENT);
577
        return;
579
        return;
578
    }
580
    }
579
    tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t,
581
    tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t,
580
        dh_link);
582
        dh_link);
581
 
583
 
582
    if (size == dentry->size) {
584
    if (size == dentry->size) {
583
        ipc_answer_0(rid, EOK);
585
        ipc_answer_0(rid, EOK);
584
        return;
586
        return;
585
    }
587
    }
586
 
588
 
587
    void *newdata = realloc(dentry->data, size);
589
    void *newdata = realloc(dentry->data, size);
588
    if (!newdata) {
590
    if (!newdata) {
589
        ipc_answer_0(rid, ENOMEM);
591
        ipc_answer_0(rid, ENOMEM);
590
        return;
592
        return;
591
    }
593
    }
592
    if (size > dentry->size) {
594
    if (size > dentry->size) {
593
        size_t delta = size - dentry->size;
595
        size_t delta = size - dentry->size;
594
        memset(newdata + dentry->size, 0, delta);
596
        memset(newdata + dentry->size, 0, delta);
595
    }
597
    }
596
    dentry->size = size;
598
    dentry->size = size;
597
    dentry->data = newdata;
599
    dentry->data = newdata;
598
    ipc_answer_0(rid, EOK);
600
    ipc_answer_0(rid, EOK);
599
}
601
}
600
 
602
 
601
void tmpfs_destroy(ipc_callid_t rid, ipc_call_t *request)
603
void tmpfs_destroy(ipc_callid_t rid, ipc_call_t *request)
602
{
604
{
603
    dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
605
    dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
604
    fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
606
    fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
605
    int rc;
607
    int rc;
606
 
608
 
607
    link_t *hlp;
609
    link_t *hlp;
608
    unsigned long key = index;
610
    unsigned long key = index;
609
    hlp = hash_table_find(&dentries, &key);
611
    hlp = hash_table_find(&dentries, &key);
610
    if (!hlp) {
612
    if (!hlp) {
611
        ipc_answer_0(rid, ENOENT);
613
        ipc_answer_0(rid, ENOENT);
612
        return;
614
        return;
613
    }
615
    }
614
    tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t,
616
    tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t,
615
        dh_link);
617
        dh_link);
616
    rc = tmpfs_destroy_node(dentry);
618
    rc = tmpfs_destroy_node(dentry);
617
    ipc_answer_0(rid, rc);
619
    ipc_answer_0(rid, rc);
618
}
620
}
619
 
621
 
620
/**
622
/**
621
 * @}
623
 * @}
622
 */
624
 */
623
 
625