Subversion Repositories HelenOS

Rev

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

Rev 2925 Rev 3011
Line 66... Line 66...
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. */
-
 
72
static futex_t unused_futex = FUTEX_INITIALIZER;
-
 
73
 
-
 
74
/** List of unused structures. */
71
static LIST_INITIALIZE(unused_head);
75
static LIST_INITIALIZE(unused_head);
72
 
76
 
-
 
77
/** Futex protecting the up_hash and ui_hash. */
-
 
78
static futex_t used_futex = FUTEX_INITIALIZER;
-
 
79
 
73
/**
80
/**
74
 * Global hash table of all used fat_idx_t structures.
81
 * Global hash table of all used fat_idx_t structures.
75
 * The index structures are hashed by the dev_handle, parent node's first
82
 * The index structures are hashed by the dev_handle, parent node's first
76
 * cluster and index within the parent directory.
83
 * cluster and index within the parent directory.
77
 */
84
 */
Line 185... Line 192...
185
{
192
{
186
    link_t *l;
193
    link_t *l;
187
    unused_t *u;
194
    unused_t *u;
188
   
195
   
189
    assert(index);
196
    assert(index);
-
 
197
    futex_down(&unused_futex);
190
    for (l = unused_head.next; l != &unused_head; l = l->next) {
198
    for (l = unused_head.next; l != &unused_head; l = l->next) {
191
        u = list_get_instance(l, unused_t, link);
199
        u = list_get_instance(l, unused_t, link);
192
        if (u->dev_handle == dev_handle)
200
        if (u->dev_handle == dev_handle)
193
            goto hit;
201
            goto hit;
194
    }
202
    }
-
 
203
    futex_up(&unused_futex);
195
 
204
   
196
    /* dev_handle not found */
205
    /* dev_handle not found */
197
    return false;  
206
    return false;  
198
 
207
 
199
hit:
208
hit:
200
    if (list_empty(&u->freed_head)) {
209
    if (list_empty(&u->freed_head)) {
Line 203... Line 212...
203
             * There are no freed indices, allocate one directly
212
             * There are no freed indices, allocate one directly
204
             * from the counter.
213
             * from the counter.
205
             */
214
             */
206
            *index = u->next++;
215
            *index = u->next++;
207
            --u->remaining;
216
            --u->remaining;
-
 
217
            futex_up(&unused_futex);
208
            return true;
218
            return true;
209
        }
219
        }
210
    } else {
220
    } else {
211
        /* There are some freed indices which we can reuse. */
221
        /* There are some freed indices which we can reuse. */
212
        freed_t *f = list_get_instance(u->freed_head.next, freed_t,
222
        freed_t *f = list_get_instance(u->freed_head.next, freed_t,
Line 215... Line 225...
215
        if (f->first++ == f->last) {
225
        if (f->first++ == f->last) {
216
            /* Destroy the interval. */
226
            /* Destroy the interval. */
217
            list_remove(&f->link);
227
            list_remove(&f->link);
218
            free(f);
228
            free(f);
219
        }
229
        }
-
 
230
        futex_up(&unused_futex);
220
        return true;
231
        return true;
221
    }
232
    }
222
    /*
233
    /*
223
     * We ran out of indices, which is extremely unlikely with FAT16, but
234
     * We ran out of indices, which is extremely unlikely with FAT16, but
224
     * theoretically still possible (e.g. too many open unlinked nodes or
235
     * theoretically still possible (e.g. too many open unlinked nodes or
225
     * too many zero-sized nodes).
236
     * too many zero-sized nodes).
226
     */
237
     */
-
 
238
    futex_up(&unused_futex);
227
    return false;
239
    return false;
228
}
240
}
229
 
241
 
230
/** If possible, coalesce two intervals of freed indices. */
242
/** If possible, coalesce two intervals of freed indices. */
231
static void try_coalesce_intervals(link_t *l, link_t *r, link_t *cur)
243
static void try_coalesce_intervals(link_t *l, link_t *r, link_t *cur)
Line 250... Line 262...
250
static void fat_idx_free(dev_handle_t dev_handle, fs_index_t index)
262
static void fat_idx_free(dev_handle_t dev_handle, fs_index_t index)
251
{
263
{
252
    link_t *l;
264
    link_t *l;
253
    unused_t *u;
265
    unused_t *u;
254
 
266
 
-
 
267
    futex_down(&unused_futex);
255
    for (l = unused_head.next; l != &unused_head; l = l->next) {
268
    for (l = unused_head.next; l != &unused_head; l = l->next) {
256
        u = list_get_instance(l, unused_t, link);
269
        u = list_get_instance(l, unused_t, link);
257
        if (u->dev_handle == dev_handle)
270
        if (u->dev_handle == dev_handle)
258
            goto hit;
271
            goto hit;
259
    }
272
    }
-
 
273
    futex_up(&unused_futex);
260
 
274
 
261
    /* should not happen */
275
    /* should not happen */
262
    assert(0);
276
    assert(0);
263
 
277
 
264
hit:
278
hit:
265
    if (u->next == index + 1) {
279
    if (u->next == index + 1) {
266
        /* The index can be returned directly to the counter. */
280
        /* The index can be returned directly to the counter. */
267
        u->next--;
281
        u->next--;
268
        u->remaining++;
282
        u->remaining++;
269
        return;
-
 
270
    } else {
283
    } else {
271
        /*
284
        /*
272
         * The index must be returned either to an existing freed
285
         * The index must be returned either to an existing freed
273
         * interval or a new interval must be created.
286
         * interval or a new interval must be created.
274
         */
287
         */
Line 280... Line 293...
280
            if (f->first == index + 1) {
293
            if (f->first == index + 1) {
281
                f->first--;
294
                f->first--;
282
                if (lnk->prev != &u->freed_head)
295
                if (lnk->prev != &u->freed_head)
283
                    try_coalesce_intervals(lnk->prev, lnk,
296
                    try_coalesce_intervals(lnk->prev, lnk,
284
                        lnk);
297
                        lnk);
-
 
298
                futex_up(&unused_futex);
285
                return;
299
                return;
286
            }
300
            }
287
            if (f->last == index - 1) {
301
            if (f->last == index - 1) {
288
                f->last++;
302
                f->last++;
289
                if (lnk->next != &u->freed_head)
303
                if (lnk->next != &u->freed_head)
290
                    try_coalesce_intervals(lnk, lnk->next,
304
                    try_coalesce_intervals(lnk, lnk->next,
291
                        lnk);
305
                        lnk);
-
 
306
                futex_up(&unused_futex);
292
                return;
307
                return;
293
            }
308
            }
294
            if (index > f->first) {
309
            if (index > f->first) {
295
                n = malloc(sizeof(freed_t));
310
                n = malloc(sizeof(freed_t));
296
                /* TODO: sleep until allocation succeeds */
311
                /* TODO: sleep until allocation succeeds */
297
                assert(n);
312
                assert(n);
298
                link_initialize(&n->link);
313
                link_initialize(&n->link);
299
                n->first = index;
314
                n->first = index;
300
                n->last = index;
315
                n->last = index;
301
                list_insert_before(&n->link, lnk);
316
                list_insert_before(&n->link, lnk);
-
 
317
                futex_up(&unused_futex);
302
                return;
318
                return;
303
            }
319
            }
304
 
320
 
305
        }
321
        }
306
        /* The index will form the last interval. */
322
        /* The index will form the last interval. */
Line 310... Line 326...
310
        link_initialize(&n->link);
326
        link_initialize(&n->link);
311
        n->first = index;
327
        n->first = index;
312
        n->last = index;
328
        n->last = index;
313
        list_append(&n->link, &u->freed_head);
329
        list_append(&n->link, &u->freed_head);
314
    }
330
    }
-
 
331
    futex_up(&unused_futex);
315
}
332
}
316
 
333
 
317
fat_idx_t *
334
fat_idx_t *
318
fat_idx_get_by_pos(dev_handle_t dev_handle, fat_cluster_t pfc, unsigned pdi)
335
fat_idx_get_by_pos(dev_handle_t dev_handle, fat_cluster_t pfc, unsigned pdi)
319
{
336
{
Line 323... Line 340...
323
        [UPH_DH_KEY] = dev_handle,
340
        [UPH_DH_KEY] = dev_handle,
324
        [UPH_PFC_KEY] = pfc,
341
        [UPH_PFC_KEY] = pfc,
325
        [UPH_PDI_KEY] = pdi,
342
        [UPH_PDI_KEY] = pdi,
326
    };
343
    };
327
 
344
 
-
 
345
    futex_down(&used_futex);
328
    l = hash_table_find(&up_hash, pkey);
346
    l = hash_table_find(&up_hash, pkey);
329
    if (l) {
347
    if (l) {
330
        fidx = hash_table_get_instance(l, fat_idx_t, uph_link);
348
        fidx = hash_table_get_instance(l, fat_idx_t, uph_link);
331
    } else {
349
    } else {
332
        fidx = (fat_idx_t *) malloc(sizeof(fat_idx_t));
350
        fidx = (fat_idx_t *) malloc(sizeof(fat_idx_t));
333
        if (!fidx) {
351
        if (!fidx) {
-
 
352
            futex_up(&used_futex);
334
            return NULL;
353
            return NULL;
335
        }
354
        }
336
        if (!fat_idx_alloc(dev_handle, &fidx->index)) {
355
        if (!fat_idx_alloc(dev_handle, &fidx->index)) {
337
            free(fidx);
356
            free(fidx);
-
 
357
            futex_up(&used_futex);
338
            return NULL;
358
            return NULL;
339
        }
359
        }
340
       
360
       
341
        unsigned long ikey[] = {
361
        unsigned long ikey[] = {
342
            [UIH_DH_KEY] = dev_handle,
362
            [UIH_DH_KEY] = dev_handle,
343
            [UIH_INDEX_KEY] = fidx->index,
363
            [UIH_INDEX_KEY] = fidx->index,
344
        };
364
        };
345
   
365
   
346
        link_initialize(&fidx->uph_link);
366
        link_initialize(&fidx->uph_link);
347
        link_initialize(&fidx->uih_link);
367
        link_initialize(&fidx->uih_link);
-
 
368
        futex_initialize(&fidx->lock, 1);
348
        fidx->dev_handle = dev_handle;
369
        fidx->dev_handle = dev_handle;
349
        fidx->pfc = pfc;
370
        fidx->pfc = pfc;
350
        fidx->pdi = pdi;
371
        fidx->pdi = pdi;
351
        fidx->nodep = NULL;
372
        fidx->nodep = NULL;
352
 
373
 
353
        hash_table_insert(&up_hash, pkey, &fidx->uph_link);
374
        hash_table_insert(&up_hash, pkey, &fidx->uph_link);
354
        hash_table_insert(&ui_hash, ikey, &fidx->uih_link);
375
        hash_table_insert(&ui_hash, ikey, &fidx->uih_link);
355
    }
376
    }
-
 
377
    futex_down(&fidx->lock);
-
 
378
    futex_up(&used_futex);
356
 
379
 
357
    return fidx;
380
    return fidx;
358
}
381
}
359
 
382
 
360
fat_idx_t *
383
fat_idx_t *
Line 365... Line 388...
365
    unsigned long ikey[] = {
388
    unsigned long ikey[] = {
366
        [UIH_DH_KEY] = dev_handle,
389
        [UIH_DH_KEY] = dev_handle,
367
        [UIH_INDEX_KEY] = index,
390
        [UIH_INDEX_KEY] = index,
368
    };
391
    };
369
 
392
 
-
 
393
    futex_down(&used_futex);
370
    l = hash_table_find(&ui_hash, ikey);
394
    l = hash_table_find(&ui_hash, ikey);
371
    if (l) {
395
    if (l) {
372
        fidx = hash_table_get_instance(l, fat_idx_t, uih_link);
396
        fidx = hash_table_get_instance(l, fat_idx_t, uih_link);
-
 
397
        futex_down(&fidx->lock);
373
    }
398
    }
-
 
399
    futex_up(&used_futex);
374
 
400
 
375
    return fidx;
401
    return fidx;
376
}
402
}
377
 
403