Subversion Repositories HelenOS

Rev

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

Rev 2831 Rev 2843
Line 45... Line 45...
45
#include <byteorder.h>
45
#include <byteorder.h>
46
#include <libadt/hash_table.h>
46
#include <libadt/hash_table.h>
47
#include <libadt/list.h>
47
#include <libadt/list.h>
48
#include <assert.h>
48
#include <assert.h>
49
 
49
 
-
 
50
#define BS_BLOCK        0
-
 
51
 
-
 
52
#define FIN_KEY_DEV_HANDLE  0
-
 
53
#define FIN_KEY_INDEX       1
-
 
54
 
50
/** Hash table of FAT in-core nodes. */
55
/** Hash table of FAT in-core nodes. */
51
hash_table_t fin_hash;
56
hash_table_t fin_hash;
52
 
57
 
53
/** List of free FAT in-core nodes. */
58
/** List of free FAT in-core nodes. */
54
link_t ffn_head;
59
link_t ffn_head;
Line 123... Line 128...
123
    node->lnkcnt = 0;
128
    node->lnkcnt = 0;
124
    node->refcnt = 0;
129
    node->refcnt = 0;
125
    node->dirty = false;
130
    node->dirty = false;
126
}
131
}
127
 
132
 
-
 
133
static uint16_t fat_bps_get(dev_handle_t dev_handle)
-
 
134
{
-
 
135
    block_t *bb;
-
 
136
    uint16_t bps;
-
 
137
   
-
 
138
    bb = block_get(dev_handle, BS_BLOCK);
-
 
139
    assert(bb != NULL);
-
 
140
    bps = uint16_t_le2host(((fat_bs_t *)bb->data)->bps);
-
 
141
    block_put(bb);
-
 
142
 
-
 
143
    return bps;
-
 
144
}
-
 
145
 
128
static void fat_sync_node(fat_node_t *node)
146
static void fat_sync_node(fat_node_t *node)
129
{
147
{
130
    /* TODO */
148
    /* TODO */
131
}
149
}
132
 
150
 
-
 
151
/** Instantiate a FAT in-core node.
-
 
152
 *
-
 
153
 * FAT stores the info necessary for instantiation of a node in the parent of
-
 
154
 * that node.  This design necessitated the addition of the parent node index
-
 
155
 * parameter to this otherwise generic libfs API.
-
 
156
 */
133
static void *
157
static void *
134
fat_node_get(dev_handle_t dev_handle, fs_index_t index, fs_index_t pindex)
158
fat_node_get(dev_handle_t dev_handle, fs_index_t index, fs_index_t pindex)
135
{
159
{
136
    link_t *lnk;
160
    link_t *lnk;
137
    fat_node_t *node = NULL;
161
    fat_node_t *node = NULL;
138
    block_t *bb;
-
 
139
    block_t *b;
162
    block_t *b;
-
 
163
    unsigned bps;
-
 
164
    unsigned dps;
140
    fat_dentry_t *d;
165
    fat_dentry_t *d;
141
    unsigned bps;       /* bytes per sector */
166
    unsigned i, j;
142
    unsigned dps;       /* dentries per sector */
-
 
143
 
167
 
144
    unsigned long key[] = {
168
    unsigned long key[] = {
145
        dev_handle,
169
        [FIN_KEY_DEV_HANDLE] = dev_handle,
146
        index
170
        [FIN_KEY_INDEX] = index
147
    };
171
    };
148
 
172
 
149
    lnk = hash_table_find(&fin_hash, key);
173
    lnk = hash_table_find(&fin_hash, key);
150
    if (lnk) {
174
    if (lnk) {
151
        /*
175
        /*
Line 155... Line 179...
155
        if (!node->refcnt++)
179
        if (!node->refcnt++)
156
            list_remove(&node->ffn_link);
180
            list_remove(&node->ffn_link);
157
        return (void *) node;  
181
        return (void *) node;  
158
    }
182
    }
159
 
183
 
-
 
184
    bps = fat_bps_get(dev_handle);
-
 
185
    dps = bps / sizeof(fat_dentry_t);
-
 
186
   
160
    if (!list_empty(&ffn_head)) {
187
    if (!list_empty(&ffn_head)) {
161
        /*
188
        /*
162
         * We are going to reuse a node from the free list.
189
         * We are going to reuse a node from the free list.
163
         */
190
         */
164
        lnk = ffn_head.next;
191
        lnk = ffn_head.next;
165
        list_remove(lnk);
192
        list_remove(lnk);
166
        node = list_get_instance(lnk, fat_node_t, ffn_link);
193
        node = list_get_instance(lnk, fat_node_t, ffn_link);
167
        assert(!node->refcnt);
194
        assert(!node->refcnt);
168
        if (node->dirty)
195
        if (node->dirty)
169
            fat_sync_node(node);
196
            fat_sync_node(node);
-
 
197
        key[FIN_KEY_DEV_HANDLE] = node->dev_handle;
-
 
198
        key[FIN_KEY_INDEX] = node->index;
-
 
199
        hash_table_remove(&fin_hash, key, sizeof(key)/sizeof(*key));
170
    } else {
200
    } else {
171
        /*
201
        /*
172
         * We need to allocate a new node.
202
         * We need to allocate a new node.
173
         */
203
         */
174
        node = malloc(sizeof(fat_node_t));
204
        node = malloc(sizeof(fat_node_t));
175
        if (!node)
205
        if (!node)
176
            return NULL;
206
            return NULL;
177
    }
207
    }
178
    fat_node_initialize(node);
208
    fat_node_initialize(node);
-
 
209
    node->refcnt++;
-
 
210
    node->lnkcnt++;
-
 
211
    node->dev_handle = dev_handle;
-
 
212
    node->index = index;
-
 
213
    node->pindex = pindex;
179
 
214
 
-
 
215
    /*
-
 
216
     * Because of the design of the FAT file system, we have no clue about
-
 
217
     * how big (i.e. how many directory entries it contains) is the parent
-
 
218
     * of the node we are trying to instantiate.  However, we know that it
-
 
219
     * must contain a directory entry for our node of interest.  We simply
-
 
220
     * scan the parent until we find it.
-
 
221
     */
-
 
222
    for (i = 0; ; i++) {
-
 
223
        b = fat_block_get(node->dev_handle, node->pindex, i);
180
    if (!pindex) {
224
        if (!b) {
-
 
225
            node->refcnt--;
-
 
226
            list_append(&node->ffn_link, &ffn_head);
-
 
227
            return NULL;
181
       
228
        }
-
 
229
        for (j = 0; j < dps; j++) {
-
 
230
            d = ((fat_dentry_t *)b->data) + j;
-
 
231
            if (d->firstc == node->index)
182
    } else {
232
                goto found;
-
 
233
        }
-
 
234
        block_put(b);
183
    }
235
    }
-
 
236
   
-
 
237
found:
-
 
238
    if (!(d->attr & (FAT_ATTR_SUBDIR | FAT_ATTR_VOLLABEL)))
-
 
239
        node->type = FAT_FILE;
-
 
240
    if ((d->attr & FAT_ATTR_SUBDIR) || !pindex)
-
 
241
        node->type = FAT_DIRECTORY;
-
 
242
    assert((node->type == FAT_FILE) || (node->type == FAT_DIRECTORY));
-
 
243
   
-
 
244
    node->size = uint32_t_le2host(d->size);
-
 
245
    block_put(b);
-
 
246
   
-
 
247
    key[FIN_KEY_DEV_HANDLE] = node->dev_handle;
-
 
248
    key[FIN_KEY_INDEX] = node->index;
-
 
249
    hash_table_insert(&fin_hash, key, &node->fin_link);
184
 
250
 
-
 
251
    return node;
185
}
252
}
186
 
253
 
187
#define BS_BLOCK    0
-
 
188
 
-
 
189
static void *fat_match(void *prnt, const char *component)
254
static void *fat_match(void *prnt, const char *component)
190
{
255
{
191
    fat_node_t *parentp = (fat_node_t *)prnt;
256
    fat_node_t *parentp = (fat_node_t *)prnt;
192
    char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1];
257
    char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1];
193
    unsigned i, j;
258
    unsigned i, j;
194
    unsigned bps;       /* bytes per sector */
259
    unsigned bps;       /* bytes per sector */
195
    unsigned dps;       /* dentries per sector */
260
    unsigned dps;       /* dentries per sector */
196
    unsigned blocks;
261
    unsigned blocks;
197
    fat_dentry_t *d;
262
    fat_dentry_t *d;
198
    block_t *bb;
-
 
199
    block_t *b;
263
    block_t *b;
200
 
264
 
201
    bb = block_get(parentp->dev_handle, BS_BLOCK);
265
    bps = fat_bps_get(parentp->dev_handle);
202
    if (!bb)
-
 
203
        return NULL;
-
 
204
    bps = uint16_t_le2host(((fat_bs_t *)bb->data)->bps);
-
 
205
    block_put(bb);
-
 
206
    dps = bps / sizeof(fat_dentry_t);
266
    dps = bps / sizeof(fat_dentry_t);
207
    blocks = parentp->size / bps + (parentp->size % bps != 0);
267
    blocks = parentp->size / bps + (parentp->size % bps != 0);
208
    for (i = 0; i < blocks; i++) {
268
    for (i = 0; i < blocks; i++) {
209
        unsigned dentries;
269
        unsigned dentries;
210
       
270