Subversion Repositories HelenOS

Rev

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

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