Subversion Repositories HelenOS

Rev

Rev 2951 | Rev 3111 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2951 Rev 3110
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
/** Futex protecting the list of unused structures. */
71
/** Futex protecting the list of unused structures. */
72
static futex_t unused_futex = FUTEX_INITIALIZER;
72
static futex_t unused_futex = FUTEX_INITIALIZER;
73
 
73
 
74
/** List of unused structures. */
74
/** List of unused structures. */
75
static LIST_INITIALIZE(unused_head);
75
static LIST_INITIALIZE(unused_head);
76
 
76
 
-
 
77
static void unused_initialize(unused_t *u, dev_handle_t dev_handle)
-
 
78
{
-
 
79
    link_initialize(&u->link);
-
 
80
    u->dev_handle = dev_handle;
-
 
81
    u->next = 0;
-
 
82
    u->remaining = ((uint64_t)((fs_index_t)-1)) + 1;
-
 
83
    list_initialize(&u->freed_head);
-
 
84
}
-
 
85
 
77
/** Futex protecting the up_hash and ui_hash. */
86
/** Futex protecting the up_hash and ui_hash. */
78
static futex_t used_futex = FUTEX_INITIALIZER;
87
static futex_t used_futex = FUTEX_INITIALIZER;
79
 
88
 
80
/**
89
/**
81
 * Global hash table of all used fat_idx_t structures.
90
 * Global hash table of all used fat_idx_t structures.
82
 * The index structures are hashed by the dev_handle, parent node's first
91
 * The index structures are hashed by the dev_handle, parent node's first
83
 * cluster and index within the parent directory.
92
 * cluster and index within the parent directory.
84
 */
93
 */
85
static hash_table_t up_hash;
94
static hash_table_t up_hash;
86
 
95
 
87
#define UPH_BUCKETS_LOG 12
96
#define UPH_BUCKETS_LOG 12
88
#define UPH_BUCKETS (1 << UPH_BUCKETS_LOG)
97
#define UPH_BUCKETS (1 << UPH_BUCKETS_LOG)
89
 
98
 
90
#define UPH_DH_KEY  0
99
#define UPH_DH_KEY  0
91
#define UPH_PFC_KEY 1
100
#define UPH_PFC_KEY 1
92
#define UPH_PDI_KEY 2
101
#define UPH_PDI_KEY 2
93
 
102
 
94
static hash_index_t pos_hash(unsigned long key[])
103
static hash_index_t pos_hash(unsigned long key[])
95
{
104
{
96
    dev_handle_t dev_handle = (dev_handle_t)key[UPH_DH_KEY];
105
    dev_handle_t dev_handle = (dev_handle_t)key[UPH_DH_KEY];
97
    fat_cluster_t pfc = (fat_cluster_t)key[UPH_PFC_KEY];
106
    fat_cluster_t pfc = (fat_cluster_t)key[UPH_PFC_KEY];
98
    unsigned pdi = (unsigned)key[UPH_PDI_KEY];
107
    unsigned pdi = (unsigned)key[UPH_PDI_KEY];
99
 
108
 
100
    hash_index_t h;
109
    hash_index_t h;
101
 
110
 
102
    /*
111
    /*
103
     * The least significant half of all bits are the least significant bits
112
     * The least significant half of all bits are the least significant bits
104
     * of the parent node's first cluster.
113
     * of the parent node's first cluster.
105
     *
114
     *
106
     * The least significant half of the most significant half of all bits
115
     * The least significant half of the most significant half of all bits
107
     * are the least significant bits of the node's dentry index within the
116
     * are the least significant bits of the node's dentry index within the
108
     * parent directory node.
117
     * parent directory node.
109
     *
118
     *
110
     * The most significant half of the most significant half of all bits
119
     * The most significant half of the most significant half of all bits
111
     * are the least significant bits of the device handle.
120
     * are the least significant bits of the device handle.
112
     */
121
     */
113
    h = pfc & ((1 << (UPH_BUCKETS_LOG / 2)) - 1);
122
    h = pfc & ((1 << (UPH_BUCKETS_LOG / 2)) - 1);
114
    h |= (pdi & ((1 << (UPH_BUCKETS_LOG / 4)) - 1)) <<
123
    h |= (pdi & ((1 << (UPH_BUCKETS_LOG / 4)) - 1)) <<
115
        (UPH_BUCKETS_LOG / 2);
124
        (UPH_BUCKETS_LOG / 2);
116
    h |= (dev_handle & ((1 << (UPH_BUCKETS_LOG / 4)) - 1)) <<
125
    h |= (dev_handle & ((1 << (UPH_BUCKETS_LOG / 4)) - 1)) <<
117
        (3 * (UPH_BUCKETS_LOG / 4));
126
        (3 * (UPH_BUCKETS_LOG / 4));
118
 
127
 
119
    return h;
128
    return h;
120
}
129
}
121
 
130
 
122
static int pos_compare(unsigned long key[], hash_count_t keys, link_t *item)
131
static int pos_compare(unsigned long key[], hash_count_t keys, link_t *item)
123
{
132
{
124
    dev_handle_t dev_handle = (dev_handle_t)key[UPH_DH_KEY];
133
    dev_handle_t dev_handle = (dev_handle_t)key[UPH_DH_KEY];
125
    fat_cluster_t pfc = (fat_cluster_t)key[UPH_PFC_KEY];
134
    fat_cluster_t pfc = (fat_cluster_t)key[UPH_PFC_KEY];
126
    unsigned pdi = (unsigned)key[UPH_PDI_KEY];
135
    unsigned pdi = (unsigned)key[UPH_PDI_KEY];
127
    fat_idx_t *fidx = list_get_instance(item, fat_idx_t, uph_link);
136
    fat_idx_t *fidx = list_get_instance(item, fat_idx_t, uph_link);
128
 
137
 
129
    return (dev_handle == fidx->dev_handle) && (pfc == fidx->pfc) &&
138
    return (dev_handle == fidx->dev_handle) && (pfc == fidx->pfc) &&
130
        (pdi == fidx->pdi);
139
        (pdi == fidx->pdi);
131
}
140
}
132
 
141
 
133
static void pos_remove_callback(link_t *item)
142
static void pos_remove_callback(link_t *item)
134
{
143
{
135
    /* nothing to do */
144
    /* nothing to do */
136
}
145
}
137
 
146
 
138
static hash_table_operations_t uph_ops = {
147
static hash_table_operations_t uph_ops = {
139
    .hash = pos_hash,
148
    .hash = pos_hash,
140
    .compare = pos_compare,
149
    .compare = pos_compare,
141
    .remove_callback = pos_remove_callback,
150
    .remove_callback = pos_remove_callback,
142
};
151
};
143
 
152
 
144
/**
153
/**
145
 * Global hash table of all used fat_idx_t structures.
154
 * Global hash table of all used fat_idx_t structures.
146
 * The index structures are hashed by the dev_handle and index.
155
 * The index structures are hashed by the dev_handle and index.
147
 */
156
 */
148
static hash_table_t ui_hash;
157
static hash_table_t ui_hash;
149
 
158
 
150
#define UIH_BUCKETS_LOG 12
159
#define UIH_BUCKETS_LOG 12
151
#define UIH_BUCKETS (1 << UIH_BUCKETS_LOG)
160
#define UIH_BUCKETS (1 << UIH_BUCKETS_LOG)
152
 
161
 
153
#define UIH_DH_KEY  0
162
#define UIH_DH_KEY  0
154
#define UIH_INDEX_KEY   1
163
#define UIH_INDEX_KEY   1
155
 
164
 
156
static hash_index_t idx_hash(unsigned long key[])
165
static hash_index_t idx_hash(unsigned long key[])
157
{
166
{
158
    dev_handle_t dev_handle = (dev_handle_t)key[UIH_DH_KEY];
167
    dev_handle_t dev_handle = (dev_handle_t)key[UIH_DH_KEY];
159
    fs_index_t index = (fs_index_t)key[UIH_INDEX_KEY];
168
    fs_index_t index = (fs_index_t)key[UIH_INDEX_KEY];
160
 
169
 
161
    hash_index_t h;
170
    hash_index_t h;
162
 
171
 
163
    h = dev_handle & ((1 << (UIH_BUCKETS_LOG / 2)) - 1);
172
    h = dev_handle & ((1 << (UIH_BUCKETS_LOG / 2)) - 1);
164
    h |= (index & ((1 << (UIH_BUCKETS_LOG / 2)) - 1)) <<
173
    h |= (index & ((1 << (UIH_BUCKETS_LOG / 2)) - 1)) <<
165
        (UIH_BUCKETS_LOG / 2);
174
        (UIH_BUCKETS_LOG / 2);
166
 
175
 
167
    return h;
176
    return h;
168
}
177
}
169
 
178
 
170
static int idx_compare(unsigned long key[], hash_count_t keys, link_t *item)
179
static int idx_compare(unsigned long key[], hash_count_t keys, link_t *item)
171
{
180
{
172
    dev_handle_t dev_handle = (dev_handle_t)key[UIH_DH_KEY];
181
    dev_handle_t dev_handle = (dev_handle_t)key[UIH_DH_KEY];
173
    fs_index_t index = (fs_index_t)key[UIH_INDEX_KEY];
182
    fs_index_t index = (fs_index_t)key[UIH_INDEX_KEY];
174
    fat_idx_t *fidx = list_get_instance(item, fat_idx_t, uih_link);
183
    fat_idx_t *fidx = list_get_instance(item, fat_idx_t, uih_link);
175
 
184
 
176
    return (dev_handle == fidx->dev_handle) && (index == fidx->index);
185
    return (dev_handle == fidx->dev_handle) && (index == fidx->index);
177
}
186
}
178
 
187
 
179
static void idx_remove_callback(link_t *item)
188
static void idx_remove_callback(link_t *item)
180
{
189
{
181
    /* nothing to do */
190
    /* nothing to do */
182
}
191
}
183
 
192
 
184
static hash_table_operations_t uih_ops = {
193
static hash_table_operations_t uih_ops = {
185
    .hash = idx_hash,
194
    .hash = idx_hash,
186
    .compare = idx_compare,
195
    .compare = idx_compare,
187
    .remove_callback = idx_remove_callback,
196
    .remove_callback = idx_remove_callback,
188
};
197
};
189
 
198
 
190
/** Allocate a VFS index which is not currently in use. */
199
/** Allocate a VFS index which is not currently in use. */
191
static bool fat_idx_alloc(dev_handle_t dev_handle, fs_index_t *index)
200
static bool fat_idx_alloc(dev_handle_t dev_handle, fs_index_t *index)
192
{
201
{
193
    link_t *l;
202
    link_t *l;
194
    unused_t *u;
203
    unused_t *u;
195
   
204
   
196
    assert(index);
205
    assert(index);
197
    futex_down(&unused_futex);
206
    futex_down(&unused_futex);
198
    for (l = unused_head.next; l != &unused_head; l = l->next) {
207
    for (l = unused_head.next; l != &unused_head; l = l->next) {
199
        u = list_get_instance(l, unused_t, link);
208
        u = list_get_instance(l, unused_t, link);
200
        if (u->dev_handle == dev_handle)
209
        if (u->dev_handle == dev_handle)
201
            goto hit;
210
            goto hit;
202
    }
211
    }
203
    futex_up(&unused_futex);
212
    futex_up(&unused_futex);
204
   
213
   
205
    /* dev_handle not found */
214
    /* dev_handle not found */
206
    return false;  
215
    return false;  
207
 
216
 
208
hit:
217
hit:
209
    if (list_empty(&u->freed_head)) {
218
    if (list_empty(&u->freed_head)) {
210
        if (u->remaining) {
219
        if (u->remaining) {
211
            /*
220
            /*
212
             * There are no freed indices, allocate one directly
221
             * There are no freed indices, allocate one directly
213
             * from the counter.
222
             * from the counter.
214
             */
223
             */
215
            *index = u->next++;
224
            *index = u->next++;
216
            --u->remaining;
225
            --u->remaining;
217
            futex_up(&unused_futex);
226
            futex_up(&unused_futex);
218
            return true;
227
            return true;
219
        }
228
        }
220
    } else {
229
    } else {
221
        /* There are some freed indices which we can reuse. */
230
        /* There are some freed indices which we can reuse. */
222
        freed_t *f = list_get_instance(u->freed_head.next, freed_t,
231
        freed_t *f = list_get_instance(u->freed_head.next, freed_t,
223
            link);
232
            link);
224
        *index = f->first;
233
        *index = f->first;
225
        if (f->first++ == f->last) {
234
        if (f->first++ == f->last) {
226
            /* Destroy the interval. */
235
            /* Destroy the interval. */
227
            list_remove(&f->link);
236
            list_remove(&f->link);
228
            free(f);
237
            free(f);
229
        }
238
        }
230
        futex_up(&unused_futex);
239
        futex_up(&unused_futex);
231
        return true;
240
        return true;
232
    }
241
    }
233
    /*
242
    /*
234
     * We ran out of indices, which is extremely unlikely with FAT16, but
243
     * We ran out of indices, which is extremely unlikely with FAT16, but
235
     * theoretically still possible (e.g. too many open unlinked nodes or
244
     * theoretically still possible (e.g. too many open unlinked nodes or
236
     * too many zero-sized nodes).
245
     * too many zero-sized nodes).
237
     */
246
     */
238
    futex_up(&unused_futex);
247
    futex_up(&unused_futex);
239
    return false;
248
    return false;
240
}
249
}
241
 
250
 
242
/** If possible, coalesce two intervals of freed indices. */
251
/** If possible, coalesce two intervals of freed indices. */
243
static void try_coalesce_intervals(link_t *l, link_t *r, link_t *cur)
252
static void try_coalesce_intervals(link_t *l, link_t *r, link_t *cur)
244
{
253
{
245
    freed_t *fl = list_get_instance(l, freed_t, link);
254
    freed_t *fl = list_get_instance(l, freed_t, link);
246
    freed_t *fr = list_get_instance(r, freed_t, link);
255
    freed_t *fr = list_get_instance(r, freed_t, link);
247
 
256
 
248
    if (fl->last + 1 == fr->first) {
257
    if (fl->last + 1 == fr->first) {
249
        if (cur == l) {
258
        if (cur == l) {
250
            fl->last = fr->last;
259
            fl->last = fr->last;
251
            list_remove(r);
260
            list_remove(r);
252
            free(r);
261
            free(r);
253
        } else {
262
        } else {
254
            fr->first = fl->first;
263
            fr->first = fl->first;
255
            list_remove(l);
264
            list_remove(l);
256
            free(l);
265
            free(l);
257
        }
266
        }
258
    }
267
    }
259
}
268
}
260
 
269
 
261
/** Free a VFS index, which is no longer in use. */
270
/** Free a VFS index, which is no longer in use. */
262
static void fat_idx_free(dev_handle_t dev_handle, fs_index_t index)
271
static void fat_idx_free(dev_handle_t dev_handle, fs_index_t index)
263
{
272
{
264
    link_t *l;
273
    link_t *l;
265
    unused_t *u;
274
    unused_t *u;
266
 
275
 
267
    futex_down(&unused_futex);
276
    futex_down(&unused_futex);
268
    for (l = unused_head.next; l != &unused_head; l = l->next) {
277
    for (l = unused_head.next; l != &unused_head; l = l->next) {
269
        u = list_get_instance(l, unused_t, link);
278
        u = list_get_instance(l, unused_t, link);
270
        if (u->dev_handle == dev_handle)
279
        if (u->dev_handle == dev_handle)
271
            goto hit;
280
            goto hit;
272
    }
281
    }
273
    futex_up(&unused_futex);
282
    futex_up(&unused_futex);
274
 
283
 
275
    /* should not happen */
284
    /* should not happen */
276
    assert(0);
285
    assert(0);
277
 
286
 
278
hit:
287
hit:
279
    if (u->next == index + 1) {
288
    if (u->next == index + 1) {
280
        /* The index can be returned directly to the counter. */
289
        /* The index can be returned directly to the counter. */
281
        u->next--;
290
        u->next--;
282
        u->remaining++;
291
        u->remaining++;
283
    } else {
292
    } else {
284
        /*
293
        /*
285
         * The index must be returned either to an existing freed
294
         * The index must be returned either to an existing freed
286
         * interval or a new interval must be created.
295
         * interval or a new interval must be created.
287
         */
296
         */
288
        link_t *lnk;
297
        link_t *lnk;
289
        freed_t *n;
298
        freed_t *n;
290
        for (lnk = u->freed_head.next; lnk != &u->freed_head;
299
        for (lnk = u->freed_head.next; lnk != &u->freed_head;
291
            lnk = lnk->next) {
300
            lnk = lnk->next) {
292
            freed_t *f = list_get_instance(lnk, freed_t, link);
301
            freed_t *f = list_get_instance(lnk, freed_t, link);
293
            if (f->first == index + 1) {
302
            if (f->first == index + 1) {
294
                f->first--;
303
                f->first--;
295
                if (lnk->prev != &u->freed_head)
304
                if (lnk->prev != &u->freed_head)
296
                    try_coalesce_intervals(lnk->prev, lnk,
305
                    try_coalesce_intervals(lnk->prev, lnk,
297
                        lnk);
306
                        lnk);
298
                futex_up(&unused_futex);
307
                futex_up(&unused_futex);
299
                return;
308
                return;
300
            }
309
            }
301
            if (f->last == index - 1) {
310
            if (f->last == index - 1) {
302
                f->last++;
311
                f->last++;
303
                if (lnk->next != &u->freed_head)
312
                if (lnk->next != &u->freed_head)
304
                    try_coalesce_intervals(lnk, lnk->next,
313
                    try_coalesce_intervals(lnk, lnk->next,
305
                        lnk);
314
                        lnk);
306
                futex_up(&unused_futex);
315
                futex_up(&unused_futex);
307
                return;
316
                return;
308
            }
317
            }
309
            if (index > f->first) {
318
            if (index > f->first) {
310
                n = malloc(sizeof(freed_t));
319
                n = malloc(sizeof(freed_t));
311
                /* TODO: sleep until allocation succeeds */
320
                /* TODO: sleep until allocation succeeds */
312
                assert(n);
321
                assert(n);
313
                link_initialize(&n->link);
322
                link_initialize(&n->link);
314
                n->first = index;
323
                n->first = index;
315
                n->last = index;
324
                n->last = index;
316
                list_insert_before(&n->link, lnk);
325
                list_insert_before(&n->link, lnk);
317
                futex_up(&unused_futex);
326
                futex_up(&unused_futex);
318
                return;
327
                return;
319
            }
328
            }
320
 
329
 
321
        }
330
        }
322
        /* The index will form the last interval. */
331
        /* The index will form the last interval. */
323
        n = malloc(sizeof(freed_t));
332
        n = malloc(sizeof(freed_t));
324
        /* TODO: sleep until allocation succeeds */
333
        /* TODO: sleep until allocation succeeds */
325
        assert(n);
334
        assert(n);
326
        link_initialize(&n->link);
335
        link_initialize(&n->link);
327
        n->first = index;
336
        n->first = index;
328
        n->last = index;
337
        n->last = index;
329
        list_append(&n->link, &u->freed_head);
338
        list_append(&n->link, &u->freed_head);
330
    }
339
    }
331
    futex_up(&unused_futex);
340
    futex_up(&unused_futex);
332
}
341
}
333
 
342
 
334
fat_idx_t *
343
fat_idx_t *
335
fat_idx_get_by_pos(dev_handle_t dev_handle, fat_cluster_t pfc, unsigned pdi)
344
fat_idx_get_by_pos(dev_handle_t dev_handle, fat_cluster_t pfc, unsigned pdi)
336
{
345
{
337
    fat_idx_t *fidx;
346
    fat_idx_t *fidx;
338
    link_t *l;
347
    link_t *l;
339
    unsigned long pkey[] = {
348
    unsigned long pkey[] = {
340
        [UPH_DH_KEY] = dev_handle,
349
        [UPH_DH_KEY] = dev_handle,
341
        [UPH_PFC_KEY] = pfc,
350
        [UPH_PFC_KEY] = pfc,
342
        [UPH_PDI_KEY] = pdi,
351
        [UPH_PDI_KEY] = pdi,
343
    };
352
    };
344
 
353
 
345
    futex_down(&used_futex);
354
    futex_down(&used_futex);
346
    l = hash_table_find(&up_hash, pkey);
355
    l = hash_table_find(&up_hash, pkey);
347
    if (l) {
356
    if (l) {
348
        fidx = hash_table_get_instance(l, fat_idx_t, uph_link);
357
        fidx = hash_table_get_instance(l, fat_idx_t, uph_link);
349
    } else {
358
    } else {
350
        fidx = (fat_idx_t *) malloc(sizeof(fat_idx_t));
359
        fidx = (fat_idx_t *) malloc(sizeof(fat_idx_t));
351
        if (!fidx) {
360
        if (!fidx) {
352
            futex_up(&used_futex);
361
            futex_up(&used_futex);
353
            return NULL;
362
            return NULL;
354
        }
363
        }
355
        if (!fat_idx_alloc(dev_handle, &fidx->index)) {
364
        if (!fat_idx_alloc(dev_handle, &fidx->index)) {
356
            free(fidx);
365
            free(fidx);
357
            futex_up(&used_futex);
366
            futex_up(&used_futex);
358
            return NULL;
367
            return NULL;
359
        }
368
        }
360
       
369
       
361
        unsigned long ikey[] = {
370
        unsigned long ikey[] = {
362
            [UIH_DH_KEY] = dev_handle,
371
            [UIH_DH_KEY] = dev_handle,
363
            [UIH_INDEX_KEY] = fidx->index,
372
            [UIH_INDEX_KEY] = fidx->index,
364
        };
373
        };
365
   
374
   
366
        link_initialize(&fidx->uph_link);
375
        link_initialize(&fidx->uph_link);
367
        link_initialize(&fidx->uih_link);
376
        link_initialize(&fidx->uih_link);
368
        futex_initialize(&fidx->lock, 1);
377
        futex_initialize(&fidx->lock, 1);
369
        fidx->dev_handle = dev_handle;
378
        fidx->dev_handle = dev_handle;
370
        fidx->pfc = pfc;
379
        fidx->pfc = pfc;
371
        fidx->pdi = pdi;
380
        fidx->pdi = pdi;
372
        fidx->nodep = NULL;
381
        fidx->nodep = NULL;
373
 
382
 
374
        hash_table_insert(&up_hash, pkey, &fidx->uph_link);
383
        hash_table_insert(&up_hash, pkey, &fidx->uph_link);
375
        hash_table_insert(&ui_hash, ikey, &fidx->uih_link);
384
        hash_table_insert(&ui_hash, ikey, &fidx->uih_link);
376
    }
385
    }
377
    futex_down(&fidx->lock);
386
    futex_down(&fidx->lock);
378
    futex_up(&used_futex);
387
    futex_up(&used_futex);
379
 
388
 
380
    return fidx;
389
    return fidx;
381
}
390
}
382
 
391
 
383
fat_idx_t *
392
fat_idx_t *
384
fat_idx_get_by_index(dev_handle_t dev_handle, fs_index_t index)
393
fat_idx_get_by_index(dev_handle_t dev_handle, fs_index_t index)
385
{
394
{
386
    fat_idx_t *fidx = NULL;
395
    fat_idx_t *fidx = NULL;
387
    link_t *l;
396
    link_t *l;
388
    unsigned long ikey[] = {
397
    unsigned long ikey[] = {
389
        [UIH_DH_KEY] = dev_handle,
398
        [UIH_DH_KEY] = dev_handle,
390
        [UIH_INDEX_KEY] = index,
399
        [UIH_INDEX_KEY] = index,
391
    };
400
    };
392
 
401
 
393
    futex_down(&used_futex);
402
    futex_down(&used_futex);
394
    l = hash_table_find(&ui_hash, ikey);
403
    l = hash_table_find(&ui_hash, ikey);
395
    if (l) {
404
    if (l) {
396
        fidx = hash_table_get_instance(l, fat_idx_t, uih_link);
405
        fidx = hash_table_get_instance(l, fat_idx_t, uih_link);
397
        futex_down(&fidx->lock);
406
        futex_down(&fidx->lock);
398
    }
407
    }
399
    futex_up(&used_futex);
408
    futex_up(&used_futex);
400
 
409
 
401
    return fidx;
410
    return fidx;
402
}
411
}
403
 
412
 
-
 
413
int fat_idx_init(void)
-
 
414
{
-
 
415
    if (!hash_table_create(&up_hash, UPH_BUCKETS, 3, &uph_ops))
-
 
416
        return ENOMEM;
-
 
417
    if (!hash_table_create(&ui_hash, UIH_BUCKETS, 2, &uih_ops)) {
-
 
418
        hash_table_destroy(&up_hash);
-
 
419
        return ENOMEM;
-
 
420
    }
-
 
421
    return EOK;
-
 
422
}
-
 
423
 
-
 
424
void fat_idx_fini(void)
-
 
425
{
-
 
426
    /* We assume the hash tables are empty. */
-
 
427
    hash_table_destroy(&up_hash);
-
 
428
    hash_table_destroy(&ui_hash);
-
 
429
}
-
 
430
 
-
 
431
int fat_idx_init_by_dev_handle(dev_handle_t dev_handle)
-
 
432
{
-
 
433
    unused_t *u = (unused_t *) malloc(sizeof(unused_t));
-
 
434
    if (!u)
-
 
435
        return ENOMEM;
-
 
436
    unused_initialize(u, dev_handle);
-
 
437
    futex_down(&unused_futex);
-
 
438
    list_append(&u->link, &unused_head);
-
 
439
    futex_up(&unused_futex);
-
 
440
    return EOK;
-
 
441
}
-
 
442
 
-
 
443
void fat_idx_fini_by_dev_handle(dev_handle_t dev_handle)
-
 
444
{
-
 
445
    unused_t *u;
-
 
446
    link_t *l;
-
 
447
 
-
 
448
    futex_down(&unused_futex);
-
 
449
    for (l = unused_head.next; l != &unused_head; l = l->next) {
-
 
450
        u = list_get_instance(l, unused_t, link);
-
 
451
        if (u->dev_handle == dev_handle)
-
 
452
            goto hit;
-
 
453
    }
-
 
454
    futex_up(&unused_futex);
-
 
455
 
-
 
456
    assert(false);  /* should not happen */
-
 
457
 
-
 
458
hit:
-
 
459
    list_remove(&u->link);
-
 
460
    futex_up(&unused_futex);
-
 
461
 
-
 
462
    while (!list_empty(&u->freed_head)) {
-
 
463
        freed_t *f;
-
 
464
        f = list_get_instance(u->freed_head.next, freed_t, link);
-
 
465
        list_remove(&f->link);
-
 
466
        free(f);
-
 
467
    }
-
 
468
    free(u);
-
 
469
}
-
 
470
 
-
 
471
/**
-
 
472
 * @}
-
 
473
 */
404
 
474