Subversion Repositories HelenOS

Rev

Rev 4555 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 4555 Rev 4584
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    vfs_node.c
34
 * @file    vfs_node.c
35
 * @brief   Various operations on VFS nodes have their home in this file.
35
 * @brief   Various operations on VFS nodes have their home in this file.
36
 */
36
 */
37
 
37
 
38
#include "vfs.h"
38
#include "vfs.h"
39
#include <stdlib.h>
39
#include <stdlib.h>
40
#include <string.h>
40
#include <string.h>
41
#include <fibril_sync.h>
41
#include <fibril_sync.h>
42
#include <adt/hash_table.h>
42
#include <adt/hash_table.h>
43
#include <assert.h>
43
#include <assert.h>
44
#include <async.h>
44
#include <async.h>
45
#include <errno.h>
45
#include <errno.h>
46
 
46
 
47
/** Mutex protecting the VFS node hash table. */
47
/** Mutex protecting the VFS node hash table. */
48
FIBRIL_MUTEX_INITIALIZE(nodes_mutex);
48
FIBRIL_MUTEX_INITIALIZE(nodes_mutex);
49
 
49
 
50
#define NODES_BUCKETS_LOG   8
50
#define NODES_BUCKETS_LOG   8
51
#define NODES_BUCKETS       (1 << NODES_BUCKETS_LOG)
51
#define NODES_BUCKETS       (1 << NODES_BUCKETS_LOG)
52
 
52
 
53
/** VFS node hash table containing all active, in-memory VFS nodes. */
53
/** VFS node hash table containing all active, in-memory VFS nodes. */
54
hash_table_t nodes;
54
hash_table_t nodes;
55
 
55
 
56
#define KEY_FS_HANDLE   0
56
#define KEY_FS_HANDLE   0
57
#define KEY_DEV_HANDLE  1
57
#define KEY_DEV_HANDLE  1
58
#define KEY_INDEX   2
58
#define KEY_INDEX   2
59
 
59
 
60
static hash_index_t nodes_hash(unsigned long []);
60
static hash_index_t nodes_hash(unsigned long []);
61
static int nodes_compare(unsigned long [], hash_count_t, link_t *);
61
static int nodes_compare(unsigned long [], hash_count_t, link_t *);
62
static void nodes_remove_callback(link_t *);
62
static void nodes_remove_callback(link_t *);
63
 
63
 
64
/** VFS node hash table operations. */
64
/** VFS node hash table operations. */
65
hash_table_operations_t nodes_ops = {
65
hash_table_operations_t nodes_ops = {
66
    .hash = nodes_hash,
66
    .hash = nodes_hash,
67
    .compare = nodes_compare,
67
    .compare = nodes_compare,
68
    .remove_callback = nodes_remove_callback
68
    .remove_callback = nodes_remove_callback
69
};
69
};
70
 
70
 
71
/** Initialize the VFS node hash table.
71
/** Initialize the VFS node hash table.
72
 *
72
 *
73
 * @return      Return true on success, false on failure.
73
 * @return      Return true on success, false on failure.
74
 */
74
 */
75
bool vfs_nodes_init(void)
75
bool vfs_nodes_init(void)
76
{
76
{
77
    return hash_table_create(&nodes, NODES_BUCKETS, 3, &nodes_ops);
77
    return hash_table_create(&nodes, NODES_BUCKETS, 3, &nodes_ops);
78
}
78
}
79
 
79
 
80
static inline void _vfs_node_addref(vfs_node_t *node)
80
static inline void _vfs_node_addref(vfs_node_t *node)
81
{
81
{
82
    node->refcnt++;
82
    node->refcnt++;
83
}
83
}
84
 
84
 
85
/** Increment reference count of a VFS node.
85
/** Increment reference count of a VFS node.
86
 *
86
 *
87
 * @param node      VFS node that will have its refcnt incremented.
87
 * @param node      VFS node that will have its refcnt incremented.
88
 */
88
 */
89
void vfs_node_addref(vfs_node_t *node)
89
void vfs_node_addref(vfs_node_t *node)
90
{
90
{
91
    fibril_mutex_lock(&nodes_mutex);
91
    fibril_mutex_lock(&nodes_mutex);
92
    _vfs_node_addref(node);
92
    _vfs_node_addref(node);
93
    fibril_mutex_unlock(&nodes_mutex);
93
    fibril_mutex_unlock(&nodes_mutex);
94
}
94
}
95
 
95
 
96
/** Decrement reference count of a VFS node.
96
/** Decrement reference count of a VFS node.
97
 *
97
 *
98
 * This function handles the case when the reference count drops to zero.
98
 * This function handles the case when the reference count drops to zero.
99
 *
99
 *
100
 * @param node      VFS node that will have its refcnt decremented.
100
 * @param node      VFS node that will have its refcnt decremented.
101
 */
101
 */
102
void vfs_node_delref(vfs_node_t *node)
102
void vfs_node_delref(vfs_node_t *node)
103
{
103
{
104
    bool free_vfs_node = false;
104
    bool free_vfs_node = false;
105
    bool free_fs_node = false;
105
    bool free_fs_node = false;
106
 
106
 
107
    fibril_mutex_lock(&nodes_mutex);
107
    fibril_mutex_lock(&nodes_mutex);
108
    if (node->refcnt-- == 1) {
108
    if (node->refcnt-- == 1) {
109
        /*
109
        /*
110
         * We are dropping the last reference to this node.
110
         * We are dropping the last reference to this node.
111
         * Remove it from the VFS node hash table.
111
         * Remove it from the VFS node hash table.
112
         */
112
         */
113
        unsigned long key[] = {
113
        unsigned long key[] = {
114
            [KEY_FS_HANDLE] = node->fs_handle,
114
            [KEY_FS_HANDLE] = node->fs_handle,
115
            [KEY_DEV_HANDLE] = node->dev_handle,
115
            [KEY_DEV_HANDLE] = node->dev_handle,
116
            [KEY_INDEX] = node->index
116
            [KEY_INDEX] = node->index
117
        };
117
        };
118
        hash_table_remove(&nodes, key, 3);
118
        hash_table_remove(&nodes, key, 3);
119
        free_vfs_node = true;
119
        free_vfs_node = true;
120
        if (!node->lnkcnt)
120
        if (!node->lnkcnt)
121
            free_fs_node = true;
121
            free_fs_node = true;
122
    }
122
    }
123
    fibril_mutex_unlock(&nodes_mutex);
123
    fibril_mutex_unlock(&nodes_mutex);
124
 
124
 
125
    if (free_fs_node) {
125
    if (free_fs_node) {
126
        /*
126
        /*
127
         * The node is not visible in the file system namespace.
127
         * The node is not visible in the file system namespace.
128
         * Free up its resources.
128
         * Free up its resources.
129
         */
129
         */
130
        int phone = vfs_grab_phone(node->fs_handle);
130
        int phone = vfs_grab_phone(node->fs_handle);
131
        ipcarg_t rc;
131
        ipcarg_t rc;
132
        rc = async_req_2_0(phone, VFS_DESTROY,
132
        rc = async_req_2_0(phone, VFS_OUT_DESTROY,
133
            (ipcarg_t)node->dev_handle, (ipcarg_t)node->index);
133
            (ipcarg_t)node->dev_handle, (ipcarg_t)node->index);
134
        assert(rc == EOK);
134
        assert(rc == EOK);
135
        vfs_release_phone(phone);
135
        vfs_release_phone(phone);
136
    }
136
    }
137
    if (free_vfs_node)
137
    if (free_vfs_node)
138
        free(node);
138
        free(node);
139
}
139
}
140
 
140
 
141
/** Find VFS node.
141
/** Find VFS node.
142
 *
142
 *
143
 * This function will try to lookup the given triplet in the VFS node hash
143
 * This function will try to lookup the given triplet in the VFS node hash
144
 * table. In case the triplet is not found there, a new VFS node is created.
144
 * table. In case the triplet is not found there, a new VFS node is created.
145
 * In any case, the VFS node will have its reference count incremented. Every
145
 * In any case, the VFS node will have its reference count incremented. Every
146
 * node returned by this call should be eventually put back by calling
146
 * node returned by this call should be eventually put back by calling
147
 * vfs_node_put() on it.
147
 * vfs_node_put() on it.
148
 *
148
 *
149
 * @param result    Populated lookup result structure.
149
 * @param result    Populated lookup result structure.
150
 *
150
 *
151
 * @return      VFS node corresponding to the given triplet.
151
 * @return      VFS node corresponding to the given triplet.
152
 */
152
 */
153
vfs_node_t *vfs_node_get(vfs_lookup_res_t *result)
153
vfs_node_t *vfs_node_get(vfs_lookup_res_t *result)
154
{
154
{
155
    unsigned long key[] = {
155
    unsigned long key[] = {
156
        [KEY_FS_HANDLE] = result->triplet.fs_handle,
156
        [KEY_FS_HANDLE] = result->triplet.fs_handle,
157
        [KEY_DEV_HANDLE] = result->triplet.dev_handle,
157
        [KEY_DEV_HANDLE] = result->triplet.dev_handle,
158
        [KEY_INDEX] = result->triplet.index
158
        [KEY_INDEX] = result->triplet.index
159
    };
159
    };
160
    link_t *tmp;
160
    link_t *tmp;
161
    vfs_node_t *node;
161
    vfs_node_t *node;
162
 
162
 
163
    fibril_mutex_lock(&nodes_mutex);
163
    fibril_mutex_lock(&nodes_mutex);
164
    tmp = hash_table_find(&nodes, key);
164
    tmp = hash_table_find(&nodes, key);
165
    if (!tmp) {
165
    if (!tmp) {
166
        node = (vfs_node_t *) malloc(sizeof(vfs_node_t));
166
        node = (vfs_node_t *) malloc(sizeof(vfs_node_t));
167
        if (!node) {
167
        if (!node) {
168
            fibril_mutex_unlock(&nodes_mutex);
168
            fibril_mutex_unlock(&nodes_mutex);
169
            return NULL;
169
            return NULL;
170
        }
170
        }
171
        memset(node, 0, sizeof(vfs_node_t));
171
        memset(node, 0, sizeof(vfs_node_t));
172
        node->fs_handle = result->triplet.fs_handle;
172
        node->fs_handle = result->triplet.fs_handle;
173
        node->dev_handle = result->triplet.dev_handle;
173
        node->dev_handle = result->triplet.dev_handle;
174
        node->index = result->triplet.index;
174
        node->index = result->triplet.index;
175
        node->size = result->size;
175
        node->size = result->size;
176
        node->lnkcnt = result->lnkcnt;
176
        node->lnkcnt = result->lnkcnt;
177
        node->type = result->type;
177
        node->type = result->type;
178
        link_initialize(&node->nh_link);
178
        link_initialize(&node->nh_link);
179
        fibril_rwlock_initialize(&node->contents_rwlock);
179
        fibril_rwlock_initialize(&node->contents_rwlock);
180
        hash_table_insert(&nodes, key, &node->nh_link);
180
        hash_table_insert(&nodes, key, &node->nh_link);
181
    } else {
181
    } else {
182
        node = hash_table_get_instance(tmp, vfs_node_t, nh_link);
182
        node = hash_table_get_instance(tmp, vfs_node_t, nh_link);
183
        if (node->type == VFS_NODE_UNKNOWN &&
183
        if (node->type == VFS_NODE_UNKNOWN &&
184
            result->type != VFS_NODE_UNKNOWN) {
184
            result->type != VFS_NODE_UNKNOWN) {
185
            /* Upgrade the node type. */
185
            /* Upgrade the node type. */
186
            node->type = result->type;
186
            node->type = result->type;
187
        }
187
        }
188
    }
188
    }
189
 
189
 
190
    assert(node->size == result->size);
190
    assert(node->size == result->size);
191
    assert(node->lnkcnt == result->lnkcnt);
191
    assert(node->lnkcnt == result->lnkcnt);
192
    assert(node->type == result->type || result->type == VFS_NODE_UNKNOWN);
192
    assert(node->type == result->type || result->type == VFS_NODE_UNKNOWN);
193
 
193
 
194
    _vfs_node_addref(node);
194
    _vfs_node_addref(node);
195
    fibril_mutex_unlock(&nodes_mutex);
195
    fibril_mutex_unlock(&nodes_mutex);
196
 
196
 
197
    return node;
197
    return node;
198
}
198
}
199
 
199
 
200
/** Return VFS node when no longer needed by the caller.
200
/** Return VFS node when no longer needed by the caller.
201
 *
201
 *
202
 * This function will remove the reference on the VFS node created by
202
 * This function will remove the reference on the VFS node created by
203
 * vfs_node_get(). This function can only be called as a closing bracket to the
203
 * vfs_node_get(). This function can only be called as a closing bracket to the
204
 * preceding vfs_node_get() call.
204
 * preceding vfs_node_get() call.
205
 *
205
 *
206
 * @param node      VFS node being released.
206
 * @param node      VFS node being released.
207
 */
207
 */
208
void vfs_node_put(vfs_node_t *node)
208
void vfs_node_put(vfs_node_t *node)
209
{
209
{
210
    vfs_node_delref(node);
210
    vfs_node_delref(node);
211
}
211
}
212
 
212
 
213
hash_index_t nodes_hash(unsigned long key[])
213
hash_index_t nodes_hash(unsigned long key[])
214
{
214
{
215
    hash_index_t a = key[KEY_FS_HANDLE] << (NODES_BUCKETS_LOG / 4);
215
    hash_index_t a = key[KEY_FS_HANDLE] << (NODES_BUCKETS_LOG / 4);
216
    hash_index_t b = (a | key[KEY_DEV_HANDLE]) << (NODES_BUCKETS_LOG / 2);
216
    hash_index_t b = (a | key[KEY_DEV_HANDLE]) << (NODES_BUCKETS_LOG / 2);
217
   
217
   
218
    return (b | key[KEY_INDEX]) & (NODES_BUCKETS - 1);
218
    return (b | key[KEY_INDEX]) & (NODES_BUCKETS - 1);
219
}
219
}
220
 
220
 
221
int nodes_compare(unsigned long key[], hash_count_t keys, link_t *item)
221
int nodes_compare(unsigned long key[], hash_count_t keys, link_t *item)
222
{
222
{
223
    vfs_node_t *node = hash_table_get_instance(item, vfs_node_t, nh_link);
223
    vfs_node_t *node = hash_table_get_instance(item, vfs_node_t, nh_link);
224
    return (node->fs_handle == key[KEY_FS_HANDLE]) &&
224
    return (node->fs_handle == key[KEY_FS_HANDLE]) &&
225
        (node->dev_handle == key[KEY_DEV_HANDLE]) &&
225
        (node->dev_handle == key[KEY_DEV_HANDLE]) &&
226
        (node->index == key[KEY_INDEX]);
226
        (node->index == key[KEY_INDEX]);
227
}
227
}
228
 
228
 
229
void nodes_remove_callback(link_t *item)
229
void nodes_remove_callback(link_t *item)
230
{
230
{
231
}
231
}
232
 
232
 
233
/**
233
/**
234
 * @}
234
 * @}
235
 */
235
 */
236
 
236