Subversion Repositories HelenOS

Rev

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

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