Example #1
0
/* 
 * Allocate a free block, mark it in the freemap and flush the freemap to disk
 */
static blkid sfs_alloc_block()
{
	u32 size = sb.nfreemap_blocks * BLOCK_SIZE / sizeof(u32);	
	//  size = 1 * 512 / 4 = 128 four-byte sections = 4096 bits

	//Renaming variables i and j to be more specific
	u32 fm_blk, fm_byte;
	blkid free_block_id;				
	
	//Search each freemap block
	for(fm_blk = 0; fm_blk < sb.nfreemap_blocks; fm_blk++){
		//Read the freemap block located at the block ID
		int count;
		int freemap_id = fm_blk + 1;

		sfs_read_block(freemap, freemap_id);
		
		//Search each index of the freemap block
		for(fm_byte = 0; fm_byte < BLOCK_SIZE; fm_byte++){

			u32 bitmap = freemap[fm_byte];	

			if((bitmap ^ 0xffffffff) != 0){
				//There is a free bit at this index in the freemap					
				u32 mask = 0x1;
				u32 result;
				count = 0;
				
				//Search each bit of the freemap block
				while(count < SFS_NBITS_IN_FREEMAP_ENTRY){
					
					result = bitmap & mask;

					if(result == mask){
						mask = mask << 1;
						count++;
						
					} else{
						free_block_id = fm_byte * SFS_NBITS_IN_FREEMAP_ENTRY + count;

						//Set the bit and flush to disk
						u32 newBitmap = bitmap | mask;
						freemap[fm_byte] = newBitmap;

						sfs_flush_freemap();

						//Clear the data stored in the block
						char empty_blk[BLOCK_SIZE];
						memset(empty_blk, 0, BLOCK_SIZE);

						sfs_write_block(empty_blk, free_block_id);

						return free_block_id;
					}
				}
			}
		}
	}
	return 0;
}
Example #2
0
File: fs.c Project: lhsieh814/OS
/* 
 * Allocate a free block, mark it in the freemap and flush the freemap to disk
 */
static blkid sfs_alloc_block()
{
	u32 size = sb.nfreemap_blocks * BLOCK_SIZE / sizeof(u32);	
	u32 i, j, k;
	/* TODO: find a freemap entry that has a free block */
	/* TODO: find out which bit in the entry is zero,
	   set the bit, flush and return the bid
	*/
	for (i=0; i<size; i++)
	{
		for (k = 0; k < (sizeof(u32) * 8); k++) 
		{
			j = (freemap[i] & (1 << k));
			j = j >> k;
			if (j == 0)
			{
				// Found a free block
				freemap[i] |= (1 << k);
				sfs_flush_freemap();

				return (i * sizeof(u32) + k);
			}
		}
	}
	
	return 0;
}
Example #3
0
File: fs.c Project: lhsieh814/OS
/*
 * Free a block, unmark it in the freemap and flush
 */
static void sfs_free_block(blkid bid)
{
	/* TODO find the entry and bit that correspond to the block */
	int entry_loc;
	int bit_loc;
	entry_loc = bid / SFS_NBITS_IN_FREEMAP_ENTRY;
	bit_loc = bid % SFS_NBITS_IN_FREEMAP_ENTRY;

	/* TODO unset the bit and flush the freemap */
	freemap[entry_loc] = (freemap[entry_loc] & ~(1 << bit_loc));
	sfs_flush_freemap();
}
Example #4
0
/*
 * Free a block, unmark it in the freemap and flush
 */
static void sfs_free_block(blkid bid)
{
	u32 fm_block;
	u32 bitmap;
	u32 newBitmap;
	u32 mask; 

	//Find the freemap block and index where the block is
	fm_block = bid / (BLOCK_SIZE * 8);
	fm_block = fm_block + 1;	//Freemap blocks start at BID 1

	sfs_read_block(freemap, fm_block);

	bitmap = freemap[bid/32];
	bitmap = bitmap & ~(1<<(bid%32));

	freemap[bid/32] = bitmap;

	sfs_flush_freemap();
}