Subversion Repositories HelenOS

Rev

Rev 4465 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 4465 Rev 4552
Line 43... Line 43...
43
#include <libblock.h>
43
#include <libblock.h>
44
#include <errno.h>
44
#include <errno.h>
45
#include <byteorder.h>
45
#include <byteorder.h>
46
#include <align.h>
46
#include <align.h>
47
#include <assert.h>
47
#include <assert.h>
48
#include <futex.h>
48
#include <fibril_sync.h>
49
#include <mem.h>
49
#include <mem.h>
50
 
50
 
51
/**
51
/**
52
 * The fat_alloc_lock futex protects all copies of the File Allocation Table
52
 * The fat_alloc_lock mutex protects all copies of the File Allocation Table
53
 * during allocation of clusters. The lock does not have to be held durring
53
 * during allocation of clusters. The lock does not have to be held durring
54
 * deallocation of clusters.
54
 * deallocation of clusters.
55
 */  
55
 */  
56
static futex_t fat_alloc_lock = FUTEX_INITIALIZER;
56
static FIBRIL_MUTEX_INITIALIZE(fat_alloc_lock);
57
 
57
 
58
/** Walk the cluster chain.
58
/** Walk the cluster chain.
59
 *
59
 *
60
 * @param bs        Buffer holding the boot sector for the file.
60
 * @param bs        Buffer holding the boot sector for the file.
61
 * @param dev_handle    Device handle of the device with the file.
61
 * @param dev_handle    Device handle of the device with the file.
Line 324... Line 324...
324
    sf = uint16_t_le2host(bs->sec_per_fat);
324
    sf = uint16_t_le2host(bs->sec_per_fat);
325
   
325
   
326
    /*
326
    /*
327
     * Search FAT1 for unused clusters.
327
     * Search FAT1 for unused clusters.
328
     */
328
     */
329
    futex_down(&fat_alloc_lock);
329
    fibril_mutex_lock(&fat_alloc_lock);
330
    for (b = 0, cl = 0; b < sf; b++) {
330
    for (b = 0, cl = 0; b < sf; b++) {
331
        blk = block_get(dev_handle, rscnt + b, BLOCK_FLAGS_NONE);
331
        blk = block_get(dev_handle, rscnt + b, BLOCK_FLAGS_NONE);
332
        for (c = 0; c < bps / sizeof(fat_cluster_t); c++, cl++) {
332
        for (c = 0; c < bps / sizeof(fat_cluster_t); c++, cl++) {
333
            fat_cluster_t *clst = (fat_cluster_t *)blk->data + c;
333
            fat_cluster_t *clst = (fat_cluster_t *)blk->data + c;
334
            if (uint16_t_le2host(*clst) == FAT_CLST_RES0) {
334
            if (uint16_t_le2host(*clst) == FAT_CLST_RES0) {
Line 348... Line 348...
348
                    fat_alloc_shadow_clusters(bs,
348
                    fat_alloc_shadow_clusters(bs,
349
                        dev_handle, lifo, nclsts);
349
                        dev_handle, lifo, nclsts);
350
                    *mcl = lifo[found - 1];
350
                    *mcl = lifo[found - 1];
351
                    *lcl = lifo[0];
351
                    *lcl = lifo[0];
352
                    free(lifo);
352
                    free(lifo);
353
                    futex_up(&fat_alloc_lock);
353
                    fibril_mutex_unlock(&fat_alloc_lock);
354
                    return EOK;
354
                    return EOK;
355
                }
355
                }
356
            }
356
            }
357
        }
357
        }
358
        block_put(blk);
358
        block_put(blk);
359
    }
359
    }
360
    futex_up(&fat_alloc_lock);
360
    fibril_mutex_unlock(&fat_alloc_lock);
361
 
361
 
362
    /*
362
    /*
363
     * We could not find enough clusters. Now we need to free the clusters
363
     * We could not find enough clusters. Now we need to free the clusters
364
     * we have allocated so far.
364
     * we have allocated so far.
365
     */
365
     */