Subversion Repositories HelenOS

Rev

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

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