Subversion Repositories HelenOS

Rev

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

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