Subversion Repositories HelenOS

Rev

Rev 4327 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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