Subversion Repositories HelenOS

Rev

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

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