Subversion Repositories HelenOS

Rev

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

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