Subversion Repositories HelenOS

Rev

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

Rev 3674 Rev 4691
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 <fibril_sync.h>
48
#include <futex.h>
49
#include <mem.h>
49
 
50
 
50
/**
51
/**
51
 * 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
52
 * 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
53
 * deallocation of clusters.
54
 * deallocation of clusters.
54
 */  
55
 */  
55
static futex_t fat_alloc_lock = FUTEX_INITIALIZER;
56
static FIBRIL_MUTEX_INITIALIZE(fat_alloc_lock);
56
 
57
 
57
/** Walk the cluster chain.
58
/** Walk the cluster chain.
58
 *
59
 *
59
 * @param bs        Buffer holding the boot sector for the file.
60
 * @param bs        Buffer holding the boot sector for the file.
60
 * @param dev_handle    Device handle of the device with the file.
61
 * @param dev_handle    Device handle of the device with the file.
Line 323... Line 324...
323
    sf = uint16_t_le2host(bs->sec_per_fat);
324
    sf = uint16_t_le2host(bs->sec_per_fat);
324
   
325
   
325
    /*
326
    /*
326
     * Search FAT1 for unused clusters.
327
     * Search FAT1 for unused clusters.
327
     */
328
     */
328
    futex_down(&fat_alloc_lock);
329
    fibril_mutex_lock(&fat_alloc_lock);
329
    for (b = 0, cl = 0; b < sf; b++) {
330
    for (b = 0, cl = 0; b < sf; b++) {
330
        blk = block_get(dev_handle, rscnt + b, BLOCK_FLAGS_NONE);
331
        blk = block_get(dev_handle, rscnt + b, BLOCK_FLAGS_NONE);
331
        for (c = 0; c < bps / sizeof(fat_cluster_t); c++, cl++) {
332
        for (c = 0; c < bps / sizeof(fat_cluster_t); c++, cl++) {
332
            fat_cluster_t *clst = (fat_cluster_t *)blk->data + c;
333
            fat_cluster_t *clst = (fat_cluster_t *)blk->data + c;
333
            if (uint16_t_le2host(*clst) == FAT_CLST_RES0) {
334
            if (uint16_t_le2host(*clst) == FAT_CLST_RES0) {
Line 347... Line 348...
347
                    fat_alloc_shadow_clusters(bs,
348
                    fat_alloc_shadow_clusters(bs,
348
                        dev_handle, lifo, nclsts);
349
                        dev_handle, lifo, nclsts);
349
                    *mcl = lifo[found - 1];
350
                    *mcl = lifo[found - 1];
350
                    *lcl = lifo[0];
351
                    *lcl = lifo[0];
351
                    free(lifo);
352
                    free(lifo);
352
                    futex_up(&fat_alloc_lock);
353
                    fibril_mutex_unlock(&fat_alloc_lock);
353
                    return EOK;
354
                    return EOK;
354
                }
355
                }
355
            }
356
            }
356
        }
357
        }
357
        block_put(blk);
358
        block_put(blk);
358
    }
359
    }
359
    futex_up(&fat_alloc_lock);
360
    fibril_mutex_unlock(&fat_alloc_lock);
360
 
361
 
361
    /*
362
    /*
362
     * 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
363
     * we have allocated so far.
364
     * we have allocated so far.
364
     */
365
     */