Subversion Repositories HelenOS

Rev

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

Rev 4348 Rev 4389
Line 52... Line 52...
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
 
139
 
142
#define DENTRIES_KEY_INDEX  0
140
#define NODES_KEY_INDEX 0
143
#define DENTRIES_KEY_DEV    1
141
#define NODES_KEY_DEV   1
144
 
142
 
145
/* Implementation of hash table interface for the dentries hash table. */
143
/* Implementation of hash table interface for the nodes hash table. */
146
static hash_index_t dentries_hash(unsigned long key[])
144
static hash_index_t nodes_hash(unsigned long key[])
147
{
145
{
148
    return key[DENTRIES_KEY_INDEX] % DENTRIES_BUCKETS;
146
    return key[NODES_KEY_INDEX] % NODES_BUCKETS;
149
}
147
}
150
 
148
 
151
static int dentries_compare(unsigned long key[], hash_count_t keys,
149
static int nodes_compare(unsigned long key[], hash_count_t keys, link_t *item)
152
    link_t *item)
-
 
153
{
150
{
154
    tmpfs_dentry_t *dentry = hash_table_get_instance(item, tmpfs_dentry_t,
151
    tmpfs_node_t *nodep = hash_table_get_instance(item, tmpfs_node_t,
155
        dh_link);
152
        nh_link);
156
    return (dentry->index == key[DENTRIES_KEY_INDEX] &&
153
    return (nodep->index == key[NODES_KEY_INDEX] &&
157
        dentry->dev_handle == key[DENTRIES_KEY_DEV]);
154
        nodep->dev_handle == key[NODES_KEY_DEV]);
158
}
155
}
159
 
156
 
160
static void dentries_remove_callback(link_t *item)
157
static void nodes_remove_callback(link_t *item)
161
{
158
{
162
}
159
}
163
 
160
 
164
/** TMPFS dentries hash table operations. */
161
/** TMPFS nodes hash table operations. */
165
hash_table_operations_t dentries_ops = {
162
hash_table_operations_t nodes_ops = {
166
    .hash = dentries_hash,
163
    .hash = nodes_hash,
167
    .compare = dentries_compare,
164
    .compare = nodes_compare,
168
    .remove_callback = dentries_remove_callback
165
    .remove_callback = nodes_remove_callback
169
};
166
};
170
 
167
 
171
typedef struct {
-
 
172
    char *name;
-
 
173
    tmpfs_dentry_t *parent;
-
 
174
    link_t link;
-
 
175
} tmpfs_name_t;
-
 
176
 
-
 
177
/* Implementation of hash table interface for the names hash table. */
-
 
178
static hash_index_t names_hash(unsigned long *key)
-
 
179
{
-
 
180
    tmpfs_dentry_t *dentry = (tmpfs_dentry_t *) *key;
168
static void tmpfs_node_initialize(tmpfs_node_t *nodep)
181
    return dentry->index % NAMES_BUCKETS;
-
 
182
}
-
 
183
 
-
 
184
static int names_compare(unsigned long *key, hash_count_t keys, link_t *item)
-
 
185
{
169
{
186
    tmpfs_dentry_t *dentry = (tmpfs_dentry_t *) *key;
-
 
187
    tmpfs_name_t *namep = hash_table_get_instance(item, tmpfs_name_t,
-
 
188
        link);
-
 
189
    return dentry == namep->parent;
-
 
190
}
-
 
191
 
-
 
192
static void names_remove_callback(link_t *item)
-
 
193
{
-
 
194
    tmpfs_name_t *namep = hash_table_get_instance(item, tmpfs_name_t,
-
 
195
        link);
-
 
196
    free(namep->name);
170
    nodep->bp = NULL;
197
    free(namep);
171
    nodep->index = 0;
198
}
-
 
199
 
-
 
200
/** TMPFS node names hash table operations. */
172
    nodep->dev_handle = 0;
201
static hash_table_operations_t names_ops = {
-
 
202
    .hash = names_hash,
173
    nodep->type = TMPFS_NONE;
203
    .compare = names_compare,
174
    nodep->lnkcnt = 0;
204
    .remove_callback = names_remove_callback
-
 
205
};
-
 
206
 
-
 
207
static void tmpfs_name_initialize(tmpfs_name_t *namep)
-
 
208
{
-
 
209
    namep->name = NULL;
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.
207
fs_node_t *tmpfs_match(fs_node_t *pfn, const char *component)
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
{
208
{
260
    unsigned long key = (unsigned long) parentp;
209
    tmpfs_node_t *parentp = TMPFS_NODE(pfn);
261
    link_t *hlp = hash_table_find(&childp->names, &key);
-
 
262
    assert(hlp);
210
    link_t *lnk;
263
    tmpfs_name_t *namep = hash_table_get_instance(hlp, tmpfs_name_t, link);
-
 
264
    return !str_cmp(namep->name, component);
-
 
265
}
-
 
266
 
211
 
267
void *tmpfs_match(void *prnt, const char *component)
212
    for (lnk = parentp->cs_head.next; lnk != &parentp->cs_head;
268
{
-
 
269
    tmpfs_dentry_t *parentp = (tmpfs_dentry_t *) prnt;
213
        lnk = lnk->next) {
270
    tmpfs_dentry_t *childp = parentp->child;
214
        tmpfs_dentry_t *dentryp = list_get_instance(lnk, tmpfs_dentry_t,
271
 
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
    nodep->bp->data = nodep;    /* link the FS and TMPFS nodes */
308
    if (!tmpfs_root_get(dev_handle))
254
    if (!tmpfs_root_get(dev_handle))
309
        node->index = TMPFS_SOME_ROOT;
255
        nodep->index = TMPFS_SOME_ROOT;
310
    else
256
    else
311
        node->index = tmpfs_next_index++;
257
        nodep->index = tmpfs_next_index++;
312
    node->dev_handle = dev_handle;
258
    nodep->dev_handle = dev_handle;
313
    if (lflag & L_DIRECTORY)
259
    if (lflag & L_DIRECTORY)
314
        node->type = TMPFS_DIRECTORY;
260
        nodep->type = TMPFS_DIRECTORY;
315
    else
261
    else
316
        node->type = TMPFS_FILE;
262
        nodep->type = TMPFS_FILE;
317
 
263
 
318
    /* Insert the new node into the dentry hash table. */
264
    /* Insert the new node into the nodes hash table. */
319
    unsigned long key[] = {
265
    unsigned long key[] = {
320
        [DENTRIES_KEY_INDEX] = node->index,
266
        [NODES_KEY_INDEX] = nodep->index,
321
        [DENTRIES_KEY_DEV] = node->dev_handle
267
        [NODES_KEY_DEV] = nodep->dev_handle
322
    };
268
    };
323
    hash_table_insert(&dentries, key, &node->dh_link);
269
    hash_table_insert(&nodes, key, &nodep->nh_link);
324
    return (void *) node;
270
    return FS_NODE(nodep);
325
}
271
}
326
 
272
 
327
int tmpfs_link_node(void *prnt, void *chld, const char *nm)
273
int tmpfs_link_node(fs_node_t *pfn, fs_node_t *cfn, const char *nm)
328
{
274
{
329
    tmpfs_dentry_t *parentp = (tmpfs_dentry_t *) prnt;
275
    tmpfs_node_t *parentp = TMPFS_NODE(pfn);
330
    tmpfs_dentry_t *childp = (tmpfs_dentry_t *) chld;
276
    tmpfs_node_t *childp = TMPFS_NODE(cfn);
-
 
277
    tmpfs_dentry_t *dentryp;
-
 
278
    link_t *lnk;
331
 
279
 
332
    assert(parentp->type == TMPFS_DIRECTORY);
280
    assert(parentp->type == TMPFS_DIRECTORY);
333
 
281
 
-
 
282
    /* Check for duplicit entries. */
-
 
283
    for (lnk = parentp->cs_head.next; lnk != &parentp->cs_head;
-
 
284
        lnk = lnk->next) {
-
 
285
        dentryp = list_get_instance(lnk, tmpfs_dentry_t, link);
-
 
286
        if (!str_cmp(dentryp->name, nm))
-
 
287
            return EEXIST;
-
 
288
    }
-
 
289
 
-
 
290
    /* Allocate and initialize the dentry. */
334
    tmpfs_name_t *namep = malloc(sizeof(tmpfs_name_t));
291
    dentryp = malloc(sizeof(tmpfs_dentry_t));
335
    if (!namep)
292
    if (!dentryp)
336
        return ENOMEM;
293
        return ENOMEM;
337
    tmpfs_name_initialize(namep);
294
    tmpfs_dentry_initialize(dentryp);
-
 
295
 
-
 
296
    /* Populate and link the new dentry. */
338
    size_t size = str_size(nm);
297
    size_t size = str_size(nm);
339
    namep->name = malloc(size + 1);
298
    dentryp->name = malloc(size + 1);
340
    if (!namep->name) {
299
    if (!dentryp->name) {
341
        free(namep);
300
        free(dentryp);
342
        return ENOMEM;
301
        return ENOMEM;
343
    }
302
    }
344
    str_cpy(namep->name, size + 1, nm);
303
    str_cpy(dentryp->name, size + 1, nm);
345
    namep->parent = parentp;
304
    dentryp->node = childp;
346
   
-
 
347
    childp->lnkcnt++;
305
    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;
306
    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
 
307
 
362
    return EOK;
308
    return EOK;
363
}
309
}
364
 
310
 
365
int tmpfs_unlink_node(void *prnt, void *chld)
311
int tmpfs_unlink_node(fs_node_t *pfn, fs_node_t *cfn, const char *nm)
366
{
312
{
367
    tmpfs_dentry_t *parentp = (tmpfs_dentry_t *)prnt;
313
    tmpfs_node_t *parentp = TMPFS_NODE(pfn);
-
 
314
    tmpfs_node_t *childp = NULL;
368
    tmpfs_dentry_t *childp = (tmpfs_dentry_t *)chld;
315
    tmpfs_dentry_t *dentryp;
-
 
316
    link_t *lnk;
369
 
317
 
370
    if (!parentp)
318
    if (!parentp)
371
        return EBUSY;
319
        return EBUSY;
372
 
-
 
373
    if (childp->child)
-
 
374
        return ENOTEMPTY;
-
 
375
 
320
   
376
    if (parentp->child == childp) {
321
    for (lnk = parentp->cs_head.next; lnk != &parentp->cs_head;
377
        parentp->child = childp->sibling;
322
        lnk = lnk->next) {
378
    } else {
-
 
379
        /* TODO: consider doubly linked list for organizing siblings. */
-
 
380
        tmpfs_dentry_t *tmp = parentp->child;
323
        dentryp = list_get_instance(lnk, tmpfs_dentry_t, link);
381
        while (tmp->sibling != childp)
324
        if (!str_cmp(dentryp->name, nm)) {
382
            tmp = tmp->sibling;
325
            childp = dentryp->node;
383
        tmp->sibling = childp->sibling;
326
            assert(FS_NODE(childp) == cfn);
-
 
327
            break;
-
 
328
        }  
384
    }
329
    }
385
    childp->sibling = NULL;
-
 
386
 
330
 
-
 
331
    if (!childp)
-
 
332
        return ENOENT;
-
 
333
       
387
    unsigned long key = (unsigned long) parentp;
334
    if ((childp->lnkcnt == 1) && !list_empty(&childp->cs_head))
388
    hash_table_remove(&childp->names, &key, 1);
335
        return ENOTEMPTY;
389
 
336
 
-
 
337
    list_remove(&dentryp->link);
-
 
338
    free(dentryp);
390
    childp->lnkcnt--;
339
    childp->lnkcnt--;
391
 
340
 
392
    return EOK;
341
    return EOK;
393
}
342
}
394
 
343
 
395
int tmpfs_destroy_node(void *nodep)
344
int tmpfs_destroy_node(fs_node_t *fn)
396
{
345
{
397
    tmpfs_dentry_t *dentry = (tmpfs_dentry_t *) nodep;
346
    tmpfs_node_t *nodep = TMPFS_NODE(fn);
398
   
347
   
399
    assert(!dentry->lnkcnt);
348
    assert(!nodep->lnkcnt);
400
    assert(!dentry->child);
349
    assert(list_empty(&nodep->cs_head));
401
    assert(!dentry->sibling);
-
 
402
 
350
 
403
    unsigned long key[] = {
351
    unsigned long key[] = {
404
        [DENTRIES_KEY_INDEX] = dentry->index,
352
        [NODES_KEY_INDEX] = nodep->index,
405
        [DENTRIES_KEY_DEV] = dentry->dev_handle
353
        [NODES_KEY_DEV] = nodep->dev_handle
406
    };
354
    };
407
    hash_table_remove(&dentries, key, 2);
355
    hash_table_remove(&nodes, key, 2);
408
 
356
 
409
    hash_table_destroy(&dentry->names);
-
 
410
 
-
 
411
    if (dentry->type == TMPFS_FILE)
357
    if (nodep->type == TMPFS_FILE)
412
        free(dentry->data);
358
        free(nodep->data);
-
 
359
    free(nodep->bp);
413
    free(dentry);
360
    free(nodep);
414
    return EOK;
361
    return EOK;
415
}
362
}
416
 
363
 
417
void tmpfs_mounted(ipc_callid_t rid, ipc_call_t *request)
364
void tmpfs_mounted(ipc_callid_t rid, ipc_call_t *request)
418
{
365
{
Line 444... Line 391...
444
    if (!tmpfs_instance_init(dev_handle)) {
391
    if (!tmpfs_instance_init(dev_handle)) {
445
        ipc_answer_0(rid, ENOMEM);
392
        ipc_answer_0(rid, ENOMEM);
446
        return;
393
        return;
447
    }
394
    }
448
 
395
 
449
    tmpfs_dentry_t *root = tmpfs_root_get(dev_handle);
396
    tmpfs_node_t *rootp = TMPFS_NODE(tmpfs_root_get(dev_handle));
450
    if (str_cmp(opts, "restore") == 0) {
397
    if (str_cmp(opts, "restore") == 0) {
451
        if (tmpfs_restore(dev_handle))
398
        if (tmpfs_restore(dev_handle))
452
            ipc_answer_3(rid, EOK, root->index, root->size,
399
            ipc_answer_3(rid, EOK, rootp->index, rootp->size,
453
                root->lnkcnt);
400
                rootp->lnkcnt);
454
        else
401
        else
455
            ipc_answer_0(rid, ELIMIT);
402
            ipc_answer_0(rid, ELIMIT);
456
    } else {
403
    } else {
457
        ipc_answer_3(rid, EOK, root->index, root->size, root->lnkcnt);
404
        ipc_answer_3(rid, EOK, rootp->index, rootp->size,
-
 
405
            rootp->lnkcnt);
458
    }
406
    }
459
}
407
}
460
 
408
 
461
void tmpfs_mount(ipc_callid_t rid, ipc_call_t *request)
409
void tmpfs_mount(ipc_callid_t rid, ipc_call_t *request)
462
{
410
{
Line 478... Line 426...
478
    dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
426
    dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
479
    fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
427
    fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
480
    off_t pos = (off_t)IPC_GET_ARG3(*request);
428
    off_t pos = (off_t)IPC_GET_ARG3(*request);
481
 
429
 
482
    /*
430
    /*
483
     * Lookup the respective dentry.
431
     * Lookup the respective TMPFS node.
484
     */
432
     */
485
    link_t *hlp;
433
    link_t *hlp;
486
    unsigned long key[] = {
434
    unsigned long key[] = {
487
        [DENTRIES_KEY_INDEX] = index,
435
        [NODES_KEY_INDEX] = index,
488
        [DENTRIES_KEY_DEV] = dev_handle,
436
        [NODES_KEY_DEV] = dev_handle,
489
    };
437
    };
490
    hlp = hash_table_find(&dentries, key);
438
    hlp = hash_table_find(&nodes, key);
491
    if (!hlp) {
439
    if (!hlp) {
492
        ipc_answer_0(rid, ENOENT);
440
        ipc_answer_0(rid, ENOENT);
493
        return;
441
        return;
494
    }
442
    }
495
    tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t,
443
    tmpfs_node_t *nodep = hash_table_get_instance(hlp, tmpfs_node_t,
496
        dh_link);
444
        nh_link);
497
 
445
 
498
    /*
446
    /*
499
     * Receive the read request.
447
     * Receive the read request.
500
     */
448
     */
501
    ipc_callid_t callid;
449
    ipc_callid_t callid;
Line 505... Line 453...
505
        ipc_answer_0(rid, EINVAL);
453
        ipc_answer_0(rid, EINVAL);
506
        return;
454
        return;
507
    }
455
    }
508
 
456
 
509
    size_t bytes;
457
    size_t bytes;
510
    if (dentry->type == TMPFS_FILE) {
458
    if (nodep->type == TMPFS_FILE) {
511
        bytes = max(0, min(dentry->size - pos, size));
459
        bytes = max(0, min(nodep->size - pos, size));
512
        (void) ipc_data_read_finalize(callid, dentry->data + pos,
460
        (void) ipc_data_read_finalize(callid, nodep->data + pos,
513
            bytes);
461
            bytes);
514
    } else {
462
    } else {
-
 
463
        tmpfs_dentry_t *dentryp;
-
 
464
        link_t *lnk;
515
        int i;
465
        int i;
516
        tmpfs_dentry_t *cur;
-
 
517
       
466
       
518
        assert(dentry->type == TMPFS_DIRECTORY);
467
        assert(nodep->type == TMPFS_DIRECTORY);
519
       
468
       
520
        /*
469
        /*
521
         * Yes, we really use O(n) algorithm here.
470
         * Yes, we really use O(n) algorithm here.
522
         * If it bothers someone, it could be fixed by introducing a
471
         * If it bothers someone, it could be fixed by introducing a
523
         * hash table.
472
         * hash table.
524
         */
473
         */
525
        for (i = 0, cur = dentry->child; i < pos && cur; i++,
474
        for (i = 0, lnk = nodep->cs_head.next;
-
 
475
            i < pos && lnk != &nodep->cs_head;
526
            cur = cur->sibling)
476
            i++, lnk = lnk->next)
527
            ;
477
            ;
528
 
478
 
529
        if (!cur) {
479
        if (lnk == &nodep->cs_head) {
530
            ipc_answer_0(callid, ENOENT);
480
            ipc_answer_0(callid, ENOENT);
531
            ipc_answer_1(rid, ENOENT, 0);
481
            ipc_answer_1(rid, ENOENT, 0);
532
            return;
482
            return;
533
        }
483
        }
534
 
484
 
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,
485
        dentryp = list_get_instance(lnk, tmpfs_dentry_t, link);
539
            link);
-
 
540
 
486
 
541
        (void) ipc_data_read_finalize(callid, namep->name,
487
        (void) ipc_data_read_finalize(callid, dentryp->name,
542
            str_size(namep->name) + 1);
488
            str_size(dentryp->name) + 1);
543
        bytes = 1;
489
        bytes = 1;
544
    }
490
    }
545
 
491
 
546
    /*
492
    /*
547
     * Answer the VFS_READ call.
493
     * Answer the VFS_READ call.
Line 554... Line 500...
554
    dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
500
    dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
555
    fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
501
    fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
556
    off_t pos = (off_t)IPC_GET_ARG3(*request);
502
    off_t pos = (off_t)IPC_GET_ARG3(*request);
557
 
503
 
558
    /*
504
    /*
559
     * Lookup the respective dentry.
505
     * Lookup the respective TMPFS node.
560
     */
506
     */
561
    link_t *hlp;
507
    link_t *hlp;
562
    unsigned long key[] = {
508
    unsigned long key[] = {
563
        [DENTRIES_KEY_INDEX] = index,
509
        [NODES_KEY_INDEX] = index,
564
        [DENTRIES_KEY_DEV] = dev_handle
510
        [NODES_KEY_DEV] = dev_handle
565
    };
511
    };
566
    hlp = hash_table_find(&dentries, key);
512
    hlp = hash_table_find(&nodes, key);
567
    if (!hlp) {
513
    if (!hlp) {
568
        ipc_answer_0(rid, ENOENT);
514
        ipc_answer_0(rid, ENOENT);
569
        return;
515
        return;
570
    }
516
    }
571
    tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t,
517
    tmpfs_node_t *nodep = hash_table_get_instance(hlp, tmpfs_node_t,
572
        dh_link);
518
        nh_link);
573
 
519
 
574
    /*
520
    /*
575
     * Receive the write request.
521
     * Receive the write request.
576
     */
522
     */
577
    ipc_callid_t callid;
523
    ipc_callid_t callid;
Line 583... Line 529...
583
    }
529
    }
584
 
530
 
585
    /*
531
    /*
586
     * Check whether the file needs to grow.
532
     * Check whether the file needs to grow.
587
     */
533
     */
588
    if (pos + size <= dentry->size) {
534
    if (pos + size <= nodep->size) {
589
        /* The file size is not changing. */
535
        /* The file size is not changing. */
590
        (void) ipc_data_write_finalize(callid, dentry->data + pos, size);
536
        (void) ipc_data_write_finalize(callid, nodep->data + pos, size);
591
        ipc_answer_2(rid, EOK, size, dentry->size);
537
        ipc_answer_2(rid, EOK, size, nodep->size);
592
        return;
538
        return;
593
    }
539
    }
594
    size_t delta = (pos + size) - dentry->size;
540
    size_t delta = (pos + size) - nodep->size;
595
    /*
541
    /*
596
     * At this point, we are deliberately extremely straightforward and
542
     * At this point, we are deliberately extremely straightforward and
597
     * simply realloc the contents of the file on every write that grows the
543
     * 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:
544
     * 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
545
     * our heap allocator can save us and just grow the block whenever
600
     * possible.
546
     * possible.
601
     */
547
     */
602
    void *newdata = realloc(dentry->data, dentry->size + delta);
548
    void *newdata = realloc(nodep->data, nodep->size + delta);
603
    if (!newdata) {
549
    if (!newdata) {
604
        ipc_answer_0(callid, ENOMEM);
550
        ipc_answer_0(callid, ENOMEM);
605
        ipc_answer_2(rid, EOK, 0, dentry->size);
551
        ipc_answer_2(rid, EOK, 0, nodep->size);
606
        return;
552
        return;
607
    }
553
    }
608
    /* Clear any newly allocated memory in order to emulate gaps. */
554
    /* Clear any newly allocated memory in order to emulate gaps. */
609
    memset(newdata + dentry->size, 0, delta);
555
    memset(newdata + nodep->size, 0, delta);
610
    dentry->size += delta;
556
    nodep->size += delta;
611
    dentry->data = newdata;
557
    nodep->data = newdata;
612
    (void) ipc_data_write_finalize(callid, dentry->data + pos, size);
558
    (void) ipc_data_write_finalize(callid, nodep->data + pos, size);
613
    ipc_answer_2(rid, EOK, size, dentry->size);
559
    ipc_answer_2(rid, EOK, size, nodep->size);
614
}
560
}
615
 
561
 
616
void tmpfs_truncate(ipc_callid_t rid, ipc_call_t *request)
562
void tmpfs_truncate(ipc_callid_t rid, ipc_call_t *request)
617
{
563
{
618
    dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
564
    dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
619
    fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
565
    fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
620
    size_t size = (off_t)IPC_GET_ARG3(*request);
566
    size_t size = (off_t)IPC_GET_ARG3(*request);
621
 
567
 
622
    /*
568
    /*
623
     * Lookup the respective dentry.
569
     * Lookup the respective TMPFS node.
624
     */
570
     */
625
    link_t *hlp;
571
    link_t *hlp;
626
    unsigned long key[] = {
572
    unsigned long key[] = {
627
        [DENTRIES_KEY_INDEX] = index,
573
        [NODES_KEY_INDEX] = index,
628
        [DENTRIES_KEY_DEV] = dev_handle
574
        [NODES_KEY_DEV] = dev_handle
629
    };
575
    };
630
    hlp = hash_table_find(&dentries, key);
576
    hlp = hash_table_find(&nodes, key);
631
    if (!hlp) {
577
    if (!hlp) {
632
        ipc_answer_0(rid, ENOENT);
578
        ipc_answer_0(rid, ENOENT);
633
        return;
579
        return;
634
    }
580
    }
635
    tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t,
581
    tmpfs_node_t *nodep = hash_table_get_instance(hlp, tmpfs_node_t,
636
        dh_link);
582
        nh_link);
637
 
583
 
638
    if (size == dentry->size) {
584
    if (size == nodep->size) {
639
        ipc_answer_0(rid, EOK);
585
        ipc_answer_0(rid, EOK);
640
        return;
586
        return;
641
    }
587
    }
642
 
588
 
643
    void *newdata = realloc(dentry->data, size);
589
    void *newdata = realloc(nodep->data, size);
644
    if (!newdata) {
590
    if (!newdata) {
645
        ipc_answer_0(rid, ENOMEM);
591
        ipc_answer_0(rid, ENOMEM);
646
        return;
592
        return;
647
    }
593
    }
648
    if (size > dentry->size) {
594
    if (size > nodep->size) {
649
        size_t delta = size - dentry->size;
595
        size_t delta = size - nodep->size;
650
        memset(newdata + dentry->size, 0, delta);
596
        memset(newdata + nodep->size, 0, delta);
651
    }
597
    }
652
    dentry->size = size;
598
    nodep->size = size;
653
    dentry->data = newdata;
599
    nodep->data = newdata;
654
    ipc_answer_0(rid, EOK);
600
    ipc_answer_0(rid, EOK);
655
}
601
}
656
 
602
 
657
void tmpfs_destroy(ipc_callid_t rid, ipc_call_t *request)
603
void tmpfs_destroy(ipc_callid_t rid, ipc_call_t *request)
658
{
604
{
Line 660... Line 606...
660
    fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
606
    fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
661
    int rc;
607
    int rc;
662
 
608
 
663
    link_t *hlp;
609
    link_t *hlp;
664
    unsigned long key[] = {
610
    unsigned long key[] = {
665
        [DENTRIES_KEY_INDEX] = index,
611
        [NODES_KEY_INDEX] = index,
666
        [DENTRIES_KEY_DEV] = dev_handle
612
        [NODES_KEY_DEV] = dev_handle
667
    };
613
    };
668
    hlp = hash_table_find(&dentries, key);
614
    hlp = hash_table_find(&nodes, key);
669
    if (!hlp) {
615
    if (!hlp) {
670
        ipc_answer_0(rid, ENOENT);
616
        ipc_answer_0(rid, ENOENT);
671
        return;
617
        return;
672
    }
618
    }
673
    tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t,
619
    tmpfs_node_t *nodep = hash_table_get_instance(hlp, tmpfs_node_t,
674
        dh_link);
620
        nh_link);
675
    rc = tmpfs_destroy_node(dentry);
621
    rc = tmpfs_destroy_node(FS_NODE(nodep));
676
    ipc_answer_0(rid, rc);
622
    ipc_answer_0(rid, rc);
677
}
623
}
678
 
624
 
679
/**
625
/**
680
 * @}
626
 * @}