Subversion Repositories HelenOS

Rev

Rev 2884 | Rev 2890 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2884 Rev 2889
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    fat_idx.c
34
 * @file    fat_idx.c
35
 * @brief   Layer for translating FAT entities to VFS node indices.
35
 * @brief   Layer for translating FAT entities to VFS node indices.
36
 */
36
 */
37
 
37
 
38
#include "fat.h"
38
#include "fat.h"
39
#include "../../vfs/vfs.h"
39
#include "../../vfs/vfs.h"
40
#include <errno.h>
40
#include <errno.h>
41
#include <string.h>
41
#include <string.h>
42
#include <libadt/hash_table.h>
42
#include <libadt/hash_table.h>
43
#include <libadt/list.h>
43
#include <libadt/list.h>
44
#include <assert.h>
44
#include <assert.h>
45
#include <futex.h>
45
#include <futex.h>
46
 
46
 
47
/** Each instance of this type describes one interval of freed VFS indices. */
47
/** Each instance of this type describes one interval of freed VFS indices. */
48
typedef struct {
48
typedef struct {
49
    link_t      link;
49
    link_t      link;
50
    fs_index_t  first;
50
    fs_index_t  first;
51
    fs_index_t  last;
51
    fs_index_t  last;
52
} freed_t;
52
} freed_t;
53
 
53
 
54
/**
54
/**
55
 * Each instance of this type describes state of all VFS indices that
55
 * Each instance of this type describes state of all VFS indices that
56
 * are currently unused.
56
 * are currently unused.
57
 */
57
 */
58
typedef struct {
58
typedef struct {
59
    link_t      link;
59
    link_t      link;
60
    dev_handle_t    dev_handle;
60
    dev_handle_t    dev_handle;
61
 
61
 
62
    /** Next unassigned index. */
62
    /** Next unassigned index. */
63
    fs_index_t  next;
63
    fs_index_t  next;
64
    /** Number of remaining unassigned indices. */
64
    /** Number of remaining unassigned indices. */
65
    uint64_t    remaining;
65
    uint64_t    remaining;
66
 
66
 
67
    /** Sorted list of intervals of freed indices. */
67
    /** Sorted list of intervals of freed indices. */
68
    link_t      freed_head;
68
    link_t      freed_head;
69
} unused_t;
69
} unused_t;
70
 
70
 
71
static LIST_INITIALIZE(unused_head);
71
static LIST_INITIALIZE(unused_head);
72
 
72
 
-
 
73
/** Global hash table of all used fat_idx_t structures. */
-
 
74
static hash_table_t used_hash;
-
 
75
 
-
 
76
#define USED_HASH_BUCKETS_LOG   12
-
 
77
#define USED_HASH_BUCKETS   (1 << USED_HASH_BUCKETS_LOG)
-
 
78
 
-
 
79
#define USED_HASH_DH_KEY    0
-
 
80
#define USED_HASH_PFC_KEY   1
-
 
81
#define USED_HASH_PDI_KEY   2
-
 
82
 
-
 
83
static hash_index_t idx_hash(unsigned long key[])
-
 
84
{
-
 
85
    dev_handle_t dev_handle = (dev_handle_t)key[USED_HASH_DH_KEY];
-
 
86
    fat_cluster_t pfc = (fat_cluster_t)key[USED_HASH_PFC_KEY];
-
 
87
    unsigned pdi = (unsigned)key[USED_HASH_PDI_KEY];
-
 
88
 
-
 
89
    hash_index_t h;
-
 
90
 
-
 
91
    /*
-
 
92
     * The least significant half of all bits are the least significant bits
-
 
93
     * of the parent node's first cluster.
-
 
94
     *
-
 
95
     * The least significant half of the most significant half of all bits
-
 
96
     * are the least significant bits of the node's dentry index within the
-
 
97
     * parent directory node.
-
 
98
     *
-
 
99
     * The most significant half of the most significant half of all bits
-
 
100
     * are the least significant bits of the device handle.
-
 
101
     */
-
 
102
    h = pfc & ((USED_HASH_BUCKETS_LOG / 2) - 1);
-
 
103
    h |= (pdi & ((USED_HASH_BUCKETS_LOG / 4) - 1)) <<
-
 
104
        (USED_HASH_BUCKETS_LOG / 2);
-
 
105
    h |= (dev_handle & ((USED_HASH_BUCKETS_LOG / 4) - 1)) <<
-
 
106
        (3 * (USED_HASH_BUCKETS_LOG / 4));
-
 
107
 
-
 
108
    return h;
-
 
109
}
-
 
110
 
-
 
111
static int idx_compare(unsigned long key[], hash_count_t keys, link_t *item)
-
 
112
{
-
 
113
    dev_handle_t dev_handle = (dev_handle_t)key[USED_HASH_DH_KEY];
-
 
114
    fat_cluster_t pfc = (fat_cluster_t)key[USED_HASH_PFC_KEY];
-
 
115
    unsigned pdi = (unsigned)key[USED_HASH_PDI_KEY];
-
 
116
    fat_idx_t *fidx = list_get_instance(item, fat_idx_t, uh_link);
-
 
117
 
-
 
118
    return (dev_handle == fidx->dev_handle) && (pfc == fidx->pfc) &&
-
 
119
        (pdi == fidx->pdi);
-
 
120
}
-
 
121
 
-
 
122
static void idx_remove_callback(link_t *item)
-
 
123
{
-
 
124
    /* nothing to do */
-
 
125
}
-
 
126
 
-
 
127
static hash_table_operations_t used_idx_ops = {
-
 
128
    .hash = idx_hash,
-
 
129
    .compare = idx_compare,
-
 
130
    .remove_callback = idx_remove_callback,
-
 
131
};
-
 
132
 
73
/** Allocate a VFS index which is not currently in use. */
133
/** Allocate a VFS index which is not currently in use. */
74
static bool fat_idx_alloc(dev_handle_t dev_handle, fs_index_t *index)
134
static bool fat_idx_alloc(dev_handle_t dev_handle, fs_index_t *index)
75
{
135
{
76
    link_t *l;
136
    link_t *l;
77
    unused_t *u;
137
    unused_t *u;
78
   
138
   
79
    assert(index);
139
    assert(index);
80
    for (l = unused_head.next; l != &unused_head; l = l->next) {
140
    for (l = unused_head.next; l != &unused_head; l = l->next) {
81
        u = list_get_instance(l, unused_t, link);
141
        u = list_get_instance(l, unused_t, link);
82
        if (u->dev_handle == dev_handle)
142
        if (u->dev_handle == dev_handle)
83
            goto hit;
143
            goto hit;
84
    }
144
    }
85
 
145
 
86
    /* dev_handle not found */
146
    /* dev_handle not found */
87
    return false;  
147
    return false;  
88
 
148
 
89
hit:
149
hit:
90
    if (list_empty(&u->freed_head)) {
150
    if (list_empty(&u->freed_head)) {
91
        if (u->remaining) {
151
        if (u->remaining) {
92
            /*
152
            /*
93
             * There are no freed indices, allocate one directly
153
             * There are no freed indices, allocate one directly
94
             * from the counter.
154
             * from the counter.
95
             */
155
             */
96
            *index = u->next++;
156
            *index = u->next++;
97
            --u->remaining;
157
            --u->remaining;
98
            return true;
158
            return true;
99
        }
159
        }
100
    } else {
160
    } else {
101
        /* There are some freed indices which we can reuse. */
161
        /* There are some freed indices which we can reuse. */
102
        freed_t *f = list_get_instance(u->freed_head.next, freed_t,
162
        freed_t *f = list_get_instance(u->freed_head.next, freed_t,
103
            link);
163
            link);
104
        *index = f->first;
164
        *index = f->first;
105
        if (f->first++ == f->last) {
165
        if (f->first++ == f->last) {
106
            /* Destroy the interval. */
166
            /* Destroy the interval. */
107
            list_remove(&f->link);
167
            list_remove(&f->link);
108
            free(f);
168
            free(f);
109
        }
169
        }
110
        return true;
170
        return true;
111
    }
171
    }
112
    /*
172
    /*
113
     * We ran out of indices, which is extremely unlikely with FAT16, but
173
     * We ran out of indices, which is extremely unlikely with FAT16, but
114
     * theoretically still possible (e.g. too many open unlinked nodes or
174
     * theoretically still possible (e.g. too many open unlinked nodes or
115
     * too many zero-sized nodes).
175
     * too many zero-sized nodes).
116
     */
176
     */
117
    return false;
177
    return false;
118
}
178
}
119
 
179
 
120
/** If possible, coalesce two intervals of freed indices. */
180
/** If possible, coalesce two intervals of freed indices. */
121
static void try_coalesce_intervals(link_t *l, link_t *r, link_t *cur)
181
static void try_coalesce_intervals(link_t *l, link_t *r, link_t *cur)
122
{
182
{
123
    freed_t *fl = list_get_instance(l, freed_t, link);
183
    freed_t *fl = list_get_instance(l, freed_t, link);
124
    freed_t *fr = list_get_instance(r, freed_t, link);
184
    freed_t *fr = list_get_instance(r, freed_t, link);
125
 
185
 
126
    if (fl->last + 1 == fr->first) {
186
    if (fl->last + 1 == fr->first) {
127
        if (cur == l) {
187
        if (cur == l) {
128
            fl->last = fr->last;
188
            fl->last = fr->last;
129
            list_remove(r);
189
            list_remove(r);
130
            free(r);
190
            free(r);
131
        } else {
191
        } else {
132
            fr->first = fl->first;
192
            fr->first = fl->first;
133
            list_remove(l);
193
            list_remove(l);
134
            free(l);
194
            free(l);
135
        }
195
        }
136
    }
196
    }
137
}
197
}
138
 
198
 
139
/** Free a VFS index, which is no longer in use. */
199
/** Free a VFS index, which is no longer in use. */
140
static void fat_idx_free(dev_handle_t dev_handle, fs_index_t index)
200
static void fat_idx_free(dev_handle_t dev_handle, fs_index_t index)
141
{
201
{
142
    link_t *l;
202
    link_t *l;
143
    unused_t *u;
203
    unused_t *u;
144
 
204
 
145
    for (l = unused_head.next; l != &unused_head; l = l->next) {
205
    for (l = unused_head.next; l != &unused_head; l = l->next) {
146
        u = list_get_instance(l, unused_t, link);
206
        u = list_get_instance(l, unused_t, link);
147
        if (u->dev_handle == dev_handle)
207
        if (u->dev_handle == dev_handle)
148
            goto hit;
208
            goto hit;
149
    }
209
    }
150
 
210
 
151
    /* should not happen */
211
    /* should not happen */
152
    assert(0);
212
    assert(0);
153
 
213
 
154
hit:
214
hit:
155
    if (u->next == index + 1) {
215
    if (u->next == index + 1) {
156
        /* The index can be returned directly to the counter. */
216
        /* The index can be returned directly to the counter. */
157
        u->next--;
217
        u->next--;
158
        u->remaining++;
218
        u->remaining++;
159
        return;
219
        return;
160
    } else {
220
    } else {
161
        /*
221
        /*
162
         * The index must be returned either to an existing freed
222
         * The index must be returned either to an existing freed
163
         * interval or a new interval must be created.
223
         * interval or a new interval must be created.
164
         */
224
         */
165
        link_t *lnk;
225
        link_t *lnk;
166
        freed_t *n;
226
        freed_t *n;
167
        for (lnk = u->freed_head.next; lnk != &u->freed_head;
227
        for (lnk = u->freed_head.next; lnk != &u->freed_head;
168
            lnk = lnk->next) {
228
            lnk = lnk->next) {
169
            freed_t *f = list_get_instance(lnk, freed_t, link);
229
            freed_t *f = list_get_instance(lnk, freed_t, link);
170
            if (f->first == index + 1) {
230
            if (f->first == index + 1) {
171
                f->first--;
231
                f->first--;
172
                if (lnk->prev != &u->freed_head)
232
                if (lnk->prev != &u->freed_head)
173
                    try_coalesce_intervals(lnk->prev, lnk,
233
                    try_coalesce_intervals(lnk->prev, lnk,
174
                        lnk);
234
                        lnk);
175
                return;
235
                return;
176
            }
236
            }
177
            if (f->last == index - 1) {
237
            if (f->last == index - 1) {
178
                f->last++;
238
                f->last++;
179
                if (lnk->next != &u->freed_head)
239
                if (lnk->next != &u->freed_head)
180
                    try_coalesce_intervals(lnk, lnk->next,
240
                    try_coalesce_intervals(lnk, lnk->next,
181
                        lnk);
241
                        lnk);
182
                return;
242
                return;
183
            }
243
            }
184
            if (index > f->first) {
244
            if (index > f->first) {
185
                n = malloc(sizeof(freed_t));
245
                n = malloc(sizeof(freed_t));
186
                /* TODO: sleep until allocation succeeds */
246
                /* TODO: sleep until allocation succeeds */
187
                assert(n);
247
                assert(n);
188
                link_initialize(&n->link);
248
                link_initialize(&n->link);
189
                n->first = index;
249
                n->first = index;
190
                n->last = index;
250
                n->last = index;
191
                list_insert_before(&n->link, lnk);
251
                list_insert_before(&n->link, lnk);
192
                return;
252
                return;
193
            }
253
            }
194
 
254
 
195
        }
255
        }
196
        /* The index will form the last interval. */
256
        /* The index will form the last interval. */
197
        n = malloc(sizeof(freed_t));
257
        n = malloc(sizeof(freed_t));
198
        /* TODO: sleep until allocation succeeds */
258
        /* TODO: sleep until allocation succeeds */
199
        assert(n);
259
        assert(n);
200
        link_initialize(&n->link);
260
        link_initialize(&n->link);
201
        n->first = index;
261
        n->first = index;
202
        n->last = index;
262
        n->last = index;
203
        list_append(&n->link, &u->freed_head);
263
        list_append(&n->link, &u->freed_head);
204
    }
264
    }
205
}
265
}
206
 
266
 
207
fat_idx_t *fat_idx_map(dev_handle_t dev_handle, fat_cluster_t pfc, unsigned pdi)
267
fat_idx_t *fat_idx_map(dev_handle_t dev_handle, fat_cluster_t pfc, unsigned pdi)
208
{
268
{
-
 
269
    fat_idx_t *fidx;
-
 
270
    link_t *l;
-
 
271
    unsigned long key[] = {
-
 
272
        [USED_HASH_DH_KEY] = dev_handle,
-
 
273
        [USED_HASH_PFC_KEY] = pfc,
-
 
274
        [USED_HASH_PDI_KEY] = pdi,
-
 
275
    };
-
 
276
 
-
 
277
    l = hash_table_find(&used_hash, key);
-
 
278
    if (l) {
-
 
279
        fidx = hash_table_get_instance(l, fat_idx_t, uh_link);
-
 
280
    } else {
-
 
281
        fidx = (fat_idx_t *) malloc(sizeof(fat_idx_t));
-
 
282
        if (!fidx) {
209
    return NULL;    /* TODO */
283
            return NULL;
-
 
284
        }
-
 
285
        if (!fat_idx_alloc(dev_handle, &fidx->index)) {
-
 
286
            free(fidx);
-
 
287
            return NULL;
-
 
288
        }
-
 
289
        link_initialize(&fidx->uh_link);
-
 
290
        fidx->dev_handle = dev_handle;
-
 
291
        fidx->pfc = pfc;
-
 
292
        fidx->pdi = pdi;
-
 
293
        fidx->nodep = NULL;
-
 
294
        hash_table_insert(&used_hash, key, &fidx->uh_link);
210
}
295
    }
-
 
296
 
-
 
297
    return fidx;
-
 
298
}
-
 
299
 
211
 
300