コード例 #1
0
ファイル: sfs_api.c プロジェクト: hahmed11/OS
//initializes the root directory
void init_root_dir(){
	dir_entry_t *buffer = malloc(ADJUST(MAX_FILES*sizeof(dir_entry_t)));

	if(buffer == NULL){
		return;
	}
	int i;
	for(i = 0; i < MAX_FILES; i++){
		//set up directory values
		buffer[i] = (dir_entry_t){.name = "\0",.inode_idx = LIMIT};
	}
	write_blocks(2, DIRECTORY_SIZE, buffer);
	free(buffer);
	return;
}
//method to create "free" list
void init_free_list(){
	unsigned int *buffer = malloc(BLOCK_SIZE);
	if (buffer == NULL){
		return;
	}
	int i;
	for(i = 0; i < (BLOCK_SIZE)/sizeof(unsigned int); i++){
		//sets all bits to 1
		buffer[i] = ~0;
	}
	write_blocks(1, FREELIST_SIZE, buffer);
	free(buffer);
	return;
}
コード例 #2
0
ファイル: sfs_api.c プロジェクト: ahmed-youssef/OS-Solutions
int get_blk_pointer(blk_pointers_t* pointers, int blknum)
{
    int indirect_pointers[BLKSIZ/BLKPOINTERSIZ];
    
    // If blk num is for a direct pointer
    if(blknum < NUMDIRECTPOINTERS) {
        if(pointers->direct[blknum] == 0) {
            if( (pointers->direct[blknum] = get_free_datablock()) == 0) return -1;     
        }
        return pointers->direct[blknum];
    }
    
    // If block num is the first indirect pointer entry
    if(pointers->indirect == 0) {
        if( (pointers->indirect = get_free_datablock()) == 0) return -1;
        memset(indirect_pointers, 0, BLKSIZ);
        write_blocks(pointers->indirect, 1, (void*)indirect_pointers);
    }
    
    // If block num is an indirect pointer entry that is not the first
    read_blocks(pointers->indirect, 1, (void*)indirect_pointers);  
    if(indirect_pointers[blknum - NUMDIRECTPOINTERS] == 0) {
        if( (indirect_pointers[blknum - NUMDIRECTPOINTERS] = get_free_datablock()) == 0) return -1;
        write_blocks(pointers->indirect, 1, (void*)indirect_pointers);
    }
    return indirect_pointers[blknum - NUMDIRECTPOINTERS];
}
コード例 #3
0
ファイル: sfs_api.c プロジェクト: ahmed-youssef/OS-Solutions
void free_blk_pointers(blk_pointers_t* pointers)
{
    int indirect_pointers[BLKSIZ/BLKPOINTERSIZ];
    char bitmap[BLKSIZ];
    int db_index, blk_num=0;
    
    read_blocks(DBBITMAPADD, 1, bitmap);
    
    for(blk_num = 0; blk_num <= NUMDIRECTPOINTERS; blk_num++) {
        db_index = pointers->direct[blk_num];
        if(db_index == 0) {
            write_blocks(DBBITMAPADD, 1, bitmap);
            return;
        }
        db_index -= DBSTARTADD;
        bitmap[db_index] = FREE;    
    }
    
    read_blocks(pointers->indirect, 1, (void*)indirect_pointers);  
    for(blk_num = 0; blk_num < BLKSIZ/BLKPOINTERSIZ; blk_num++) {
        db_index = indirect_pointers[blk_num];
        if(db_index == 0) {
            write_blocks(DBBITMAPADD, 1, bitmap);
            return;
        }
        db_index -= DBSTARTADD;
        bitmap[db_index] = FREE;
    }
}
コード例 #4
0
ファイル: super.c プロジェクト: shehbazj/fslice
void testfs_close_super_block(struct super_block *sb) {
	testfs_tx_start(sb, TX_UMOUNT);
	// write sb->sb of type dsuper_block to disk at offset 0.
	testfs_write_super_block(sb);
	// assume there are no entries in the inode hash table. 
	// delete the 256 hash size inode hash table 
	inode_hash_destroy();
	if (sb->inode_freemap) {
		// write inode map to disk.
		write_blocks(sb, bitmap_getdata(sb->inode_freemap),
				sb->sb.inode_freemap_start, INODE_FREEMAP_SIZE);
		// free in memory bitmap file.
		bitmap_destroy(sb->inode_freemap);
		sb->inode_freemap = NULL;
	}
	if (sb->block_freemap) {
		// write inode freemap to disk
		write_blocks(sb, bitmap_getdata(sb->block_freemap),
				sb->sb.block_freemap_start, BLOCK_FREEMAP_SIZE);
		// destroy inode freemap
		bitmap_destroy(sb->block_freemap);
		sb->block_freemap = NULL;
	}
	testfs_tx_commit(sb, TX_UMOUNT);
	fflush(sb->dev);
	fclose(sb->dev);
	sb->dev = NULL;
	// free in memory data structure sb superblock
	free(sb);
}
コード例 #5
0
ファイル: sfs_api.c プロジェクト: asawitt/Comp310
int sfs_remove(char *file) {
	int i, k, j, l;

	for (i=0;i<=MAX_INODES;i++){
                //File exists
                if(!strcmp(rootDirectory.entry[i].name, file)){
                        //Checks if file is open
                        for (k = 0;k<MAXOPENFILES;k++){
                                if (!strcmp((*fileDescriptorTable).entry[k].name, file)){
                                       printf("File is open, closing....\n"); 
					sfs_fclose(k);
					break;
                                }
                        }
			//Free and zero the blocks that the dataptrs attached to the file's INode point to
			for (j = 0;j<NUM_DATA_POINTERS;j++){
				if (rootDirectory.entry[i].inode.dataPointers[j] == 0) break;
				//Frees the block in the freeBlock Array and the dataptr in the rootDirectory inode for the file
				freeBlocks.freeBlocks[rootDirectory.entry[i].inode.dataPointers[j]] = 0;
				freeBlocks.numberFree++;
				rootDirectory.entry[i].inode.dataPointers[j] = 0;
			}		
			rootDirectory.entry[i].inode.size = 0;
			rootDirectory.entry[i].name[0] = '\0';
			write_blocks(1,1,&rootDirectory);
			write_blocks(MAX_BLOCKS-1, 1, &freeBlocks);
			
			//return 0 if successful
			return 0;
		}
	}
	printf("File doesn't exist, can't delete a file that doesn't exist\n");
	return -1; 
}
コード例 #6
0
int createINode( long mode, long linkCnt, long uid, long gid, long size){
	iNode newFileINode;
	newFileINode.mode = mode;
	newFileINode.linkCnt = linkCnt;
	newFileINode.uid = uid;
	newFileINode.gid = gid;
	newFileINode.size = size;
	//Calculate the number of blocks required from the size:
	int numBlocksToAllocate = size/BLOCKSIZE + 1;

	if(numBlocksToAllocate>(12+BLOCKSIZE/4)){
		//If the file is too large to be allocated
		printf("%s\n", "File too large to be created");
		return -1;
	}

	if(numBlocksToAllocate>=12){
		//In this case we need to use indirect pointers
		indirectINode indirect;
		newFileINode.indirectPointer = allocateFreeBlock();
		while(numBlocksToAllocate>=12){
			//allocate using indirect pointers
			indirect.pointers[numBlocksToAllocate] = allocateFreeBlock();
			numBlocksToAllocate--;
		}
		//Write the indirect INode to disk;
		unsigned char buffer[512];
		memcpy(buffer,&indirect,512);
		write_blocks(newFileINode.indirectPointer,1,buffer);
	}
	else {
		newFileINode.indirectPointer = FREE;
	}
	int i;
	for(i=0;i<12;i++){
		if(numBlocksToAllocate>0){
			newFileINode.directPointers[i] = allocateFreeBlock();
			numBlocksToAllocate--;
		}
		else{
			newFileINode.directPointers[i] = FREE;
		}
	}

	int numI = findEmptyINode();
	unsigned char blockBuffer[512];
	//Reading the corresponding block from disk
	read_blocks(findINodeBlockAddress(numI),1,&blockBuffer);
	//add the buffer containing the iNode to the block

	memcpy((void*)(blockBuffer+findINodeByteAddress(numI)),&newFileINode,sizeof(newFileINode));

	write_blocks(findINodeBlockAddress(numI),1,&blockBuffer);

	return numI;
}
コード例 #7
0
int updateDisk(){
	buffer = (char *) malloc(1024);
	memcpy(buffer, directory, 1024);
	write_blocks(0, 1, buffer);
	memcpy(buffer, FAT, 1024);
	write_blocks(1, 1, buffer);
	memcpy(buffer, free_list, 1024);
	write_blocks(1023, 1, buffer);
	return 0;
}
コード例 #8
0
ファイル: sfs_api.c プロジェクト: alexcarruthers/school
int sfs_write(int fileID, char *buf, int length){
	if(fdt[fileID][open] == false) return -1;
	if(fileID > MAX_NUM_FILES || fileID < 0) return -1;
	
	int fatEntry = files[fileID].fatIndex;
	//if this is the first time writing to this file, give it a fat index
	if(fat[fatEntry][block_index] == unused){
		fat[fatEntry][block_index] = list_shift_int(free_blocks);
		fat[fatEntry][next] = eof;
	}
	
	//we must find the block where the file ends
	int numWrittenBlocks = files[fileID].size / BLOCK_SIZE;
	
	int i;
	for(i=0; i < numWrittenBlocks; i++)
		fatEntry = fat[fatEntry][next];
	
	//need to append to the rest of this block
	char block[BLOCK_SIZE];
	read_blocks(fat[fatEntry][block_index], 1, block);
	int numBytes;
	if (length < (BLOCK_SIZE - fdt[fileID][write_ptr] % BLOCK_SIZE))
		numBytes = length;
	else
		numBytes = BLOCK_SIZE - fdt[fileID][write_ptr] % BLOCK_SIZE;
	memcpy(block + (fdt[fileID][write_ptr] % BLOCK_SIZE), buf, numBytes);
	write_blocks(fat[fatEntry][block_index], 1, block);
	fdt[fileID][write_ptr] += numBytes;
	int numBytesWritten = numBytes;
	
	//now we can write entire blocks until no more bytes need to be
	//written
	while(numBytesWritten < length){
		fat[fatEntry][next] = firstFreeFat();
		fatEntry = fat[fatEntry][next];
		fat[fatEntry][block_index] = list_shift_int(free_blocks);
		fat[fatEntry][next] = eof;
		write_blocks(fat[fatEntry][block_index], 1, buf+numBytesWritten);
		if((length - numBytesWritten) >= BLOCK_SIZE){
			numBytesWritten +=BLOCK_SIZE;
			fdt[fileID][write_ptr] +=BLOCK_SIZE;
		}
		else{
			numBytesWritten += length-numBytesWritten;
			fdt[fileID][write_ptr] += length-numBytesWritten;
		}
	}
	
	//update sizes and pointers and write file system blocks to disk
	files[fileID].size += numBytesWritten;
	write_fs_blocks();
	return numBytesWritten;
}
コード例 #9
0
//=======================================================================================
//==============================PRIMARY API FUNCTIONS====================================
//=======================================================================================
void mksfs(int fresh){
	buffer = (void*) malloc(BLOCK_SIZE);
	memset(buffer,0,BLOCK_SIZE);
	if (fresh){							//FORMAT VIRTUAL DISK, CREATE NEW FROM SCRATCH
		init_fresh_disk(FILESYSTEM, BLOCK_SIZE, FILESYSTEM_SIZE);
		//=========Storing sb in-memory===========
		sb = (superblock){0, BLOCK_SIZE, FILESYSTEM_SIZE, MAX_FD, 0};
		memcpy(buffer, &sb, sizeof(superblock));

		//========Storing sb on-disk==============
		write_blocks(0, 1, buffer);
		
		//========Initializing Bitmap block=======
		unsigned int bit_row =  4294967295;		//Row consisting of 32 '1' bits
		for (int i = 0; i<256; i++){
			bitmap[i] = bit_row;					//Filling table with 1's (1 means block is free)
		}
		/*We use the first 3 blocks to store the Root Directory mapping table*/
		bitmap[0] = bitmap[0] << 3;
		write_blocks(1, 1, bitmap);
		
		//========Storing inode table on-disk=====
		//inode inode_table[MAX_FD+1];
		for (int i = 0; i <= MAX_FD; i++){
			inode_table[i] = (inode){0, 1, 1, 0, {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}, -1};
			if (i == 0)	{	//First inode is root dir inode.
				//It spans 3 blocks of data -> first three blocks in the datablock section
				inode_table[i] = (inode){0,1,1,3,{FIRST_DB,FIRST_DB+1,FIRST_DB+2,-1,-1,-1,-1,-1,-1,-1,-1,-1},-1};
			}
			memcpy(buffer, &(inode_table[i]), sizeof(inode));
			write_blocks(i+2, 1, buffer);
		}
		//====Initializing rootdir in-memory======
		for (int i = 0; i < MAX_FD; i++){
			root[i] = (dir){"", -1};
		}
		//====Initializing rootdir on-disk========
		free(buffer);
		buffer = (void*) malloc(BLOCK_SIZE*3);
		memset(buffer,0,BLOCK_SIZE*3);
		memcpy(buffer, &root, 3*BLOCK_SIZE);
		write_blocks(MAX_FD+3, 3, buffer);

		//====Initializing fd table in-memory=====
		for (int i = 0; i < MAX_FD; i++)
			fd_table[i] = (fd){1,-1,-1,-1};			//mark all entries in fd_table as empty (flag = 1 means empty)
	} else {
		init_disk(FILESYSTEM, BLOCK_SIZE, FILESYSTEM_SIZE);
	}
	free(buffer);
}
コード例 #10
0
//Checks if pointer_index is initialized, and if it isn't it initializes it to the first free block
//returns -1 if failure, and block number in the sfs on success
int fetch_block(int inode, int pointer_index){
	int freeblock;
	//Procedure for direct pointers
	if (pointer_index < 12){
		if (inode_table[inode].pointer[pointer_index] == -1){	//if the pointer is NOT initialized
			freeblock = get_first_freeblock();
			if(freeblock){
				inode_table[inode].pointer[pointer_index] = freeblock;	//initialize to first free block
			} else {
				return -1;
			}
		}
		return inode_table[inode].pointer[pointer_index];
	//Procedure for indirect pointers
	} else if (pointer_index < 268){		//256 indirect pointers  + 12 direct = 268
		int indir_ptr[256];
		//If indirect pointer block is not initialized
		if (inode_table[inode].indirect_pntr == -1){
			freeblock = get_first_freeblock();
			if(freeblock){
				inode_table[inode].indirect_pntr = freeblock;	//initialize indirect pointer to first free block
				//initialize all indirect pointer to -1
				for (int i = 0; i < 256; i++){
					indir_ptr[i] = -1;
				}
				write_blocks(inode_table[inode].indirect_pntr, 1, indir_ptr);	//flush the indirect pointer block
			} else {
				return -1;
			}
		}
		//fetch the indirect pointer
		read_blocks(inode_table[inode].indirect_pntr,1,indir_ptr);
		int indir_ptr_index = pointer_index - 12;
		if (indir_ptr[indir_ptr_index] == -1){		//if the indirect pointer is not initialized
			freeblock = get_first_freeblock();
			if (freeblock){
				indir_ptr[indir_ptr_index] = freeblock;		//initialize to first free block
				write_blocks(inode_table[inode].indirect_pntr, 1, indir_ptr);	//flush the indirect pointer block
			} else {
				return -1;
			}
		}
		return indir_ptr[indir_ptr_index];
	//Pointer out of bound
	} else {
		printf("File too large, cannot initialize new pointer\n");
		return -1;
	}
}
コード例 #11
0
int sfs_fwrite(int fileID, const char *buf, int length){
	int numINode = fileDescTable[fileID].iNode;
	int writePointer = fileDescTable[fileID].RWPointer;

	unsigned char blockBuffer[512];
	read_blocks(findINodeBlockAddress(numINode),1,&blockBuffer);
	iNode fileINode;
	//memcpy((void*)(blockBuffer+findINodeByteAddress(numI)),buffer,sizeof(newFileINode));
	memcpy(&fileINode,(void*)(blockBuffer+findINodeByteAddress(numINode)),sizeof(iNode));

	if((writePointer+length)>fileINode.size){
		//grow file
		int sizeToAllocate = (writePointer+length)-fileINode.size;
		int blocksToAllocate = ceil(((double)(writePointer+length))/(double)BLOCKSIZE);
		int blocksAlreadyAllocated = ceil (((double)fileINode.size)/(double)BLOCKSIZE);
        if((blocksAlreadyAllocated+blocksAlreadyAllocated)>(12+BLOCKSIZE/4)){
            printf("File too large to be written");
            return -1;
        }
        int i;
        indirectINode indirect;
        for(i=blocksAlreadyAllocated;i<blocksToAllocate;i++){
            if(blocksAlreadyAllocated<12){
                fileINode.directPointers[blocksAlreadyAllocated]=allocateFreeBlock();
            }
            else if(blocksAlreadyAllocated==12 && fileINode.indirectPointer==0){
                fileINode.indirectPointer = allocateFreeBlock();
                indirect.pointers[blocksAlreadyAllocated]=allocateFreeBlock();
            }
            else{
                indirect.pointers[blocksAlreadyAllocated]=allocateFreeBlock();
            }
        }
        //write indirect inode
        if(blocksAlreadyAllocated>11){
            memcpy(blockBuffer,&indirect,sizeof(indirect));
            write_blocks(fileINode.indirectPointer,1,&blockBuffer);
        }
        //Write direct inode
        fileINode.size+=sizeToAllocate;;
        memcpy(blockBuffer,&fileINode,sizeof(fileINode));
        write_blocks(findINodeBlockAddress(numINode),1,&blockBuffer);

	}

	//Write

	return 0;
}
コード例 #12
0
ファイル: sfs_api.c プロジェクト: ahmed-youssef/OS-Solutions
int mksfs(int fresh)
{
    super_block spr_block;
    char buff[BLKSIZ];
    int i;
    
    // Initialize fd_table
    for(i = 0; i < MAXOPENFILES; i++)
        fd_table[i].status = FREE;
            
    if(fresh)
    {
        // Initialize and write super block to disk
        init_fresh_disk(DISKIMG, BLKSIZ, BLOCKNUM);
        spr_block.magic_number = MAGICNUM;
        spr_block.block_size = BLKSIZ;
        spr_block.fs_size = BLOCKNUM;
        spr_block.inode_tbl_length = INODETBLSIZ;
        spr_block.root_inode = ROOTINODENUM;
        write_blocks(SUPERBLOCKADD, 1,  (void*) &spr_block);
        
        // Setup datablock bitmap
        memset(buff, FREE, BLKSIZ);
        write_blocks(DBBITMAPADD, 1, buff);
        
        // Setup INODE Bitmap and allocate first i-node entry to root directory
        buff[ROOTINODENUM] = USED;
        write_blocks(INBITMAPADD, 1,  buff);
        
        // Allocate fd to root directory
        fd_table[ROOTFD].status = USED;
        fd_table[ROOTFD].inode_num = ROOTINODENUM;
        fd_table[ROOTFD].rw_pointer = 0;
        fd_table[ROOTFD].inode = (inode_struct*) calloc(1, sizeof(inode_struct));
        fd_table[ROOTFD].inode->size = 0;

        write_inode_by_index(fd_table[ROOTFD].inode_num, fd_table[ROOTFD].inode);
    } else 
    {
        init_disk(DISKIMG, BLKSIZ, BLOCKNUM);
        fd_table[ROOTFD].status = USED;
        fd_table[ROOTFD].inode_num = ROOTINODENUM;
        fd_table[ROOTFD].inode = get_inode_by_index(ROOTINODENUM);
        fd_table[ROOTFD].rw_pointer = fd_table[ROOTFD].inode->size;
    }   
    
    return 0;
}
コード例 #13
0
void initializeFreeBitMap(){
	//Compute the number of blocks required for the free block bit map:
	int blocksReq = (NUMBEROFBLOCKS+(BLOCKSIZE-1))/(BLOCKSIZE); //Adding blockSize to roundup.
	//We are going to write 1s in every bit
	unsigned char buffer[512];
	memset(buffer,FREE,512);
	//Compute start block:
	int currentBlock = NUMBEROFBLOCKS-blocksReq;

	for(currentBlock;currentBlock<NUMBEROFBLOCKS;currentBlock++){
		write_blocks(currentBlock,1,&buffer);
	}

	//Write the blocks for SB and I-Node table as occupied
	int numBlocksReqForINodes = (NUMBEROFINODES * sizeof(iNode) + (BLOCKSIZE-1))/BLOCKSIZE;
	int i;
	for(i=0;i<1+numBlocksReqForINodes;i++){
		changeBlockStatus(i,IN_USE);
	}

	//Write the blocks of the free bit map as occupied:
	for(i=NUMBEROFBLOCKS-1;i>=NUMBEROFBLOCKS-blocksReq;i--){
		changeBlockStatus(i,IN_USE);
	}
}
コード例 #14
0
ファイル: sfs_api.c プロジェクト: asawitt/Comp310
void mksfs(int fresh) {
	if (fresh == 1){
		//Makes a new disk if fresh flag is true
		printf("Initializing disk\n");
		init_fresh_disk(DISK,BLOCK_SIZE,MAX_BLOCKS);	
		fillWithZeroes();	
		//calloc for fdt
		fileDescriptorTable = (FileDescriptorTable*)calloc(1,sizeof(FileDescriptorTable));	
		//Write superblock 
		printf("Writing superblock\n");
		//setupSuperblock(superBlock);
		printf("Setting up superblock\n");
		write_blocks(0,1,&superBlock);
		//Write root directory
		bzero(&rootDirectory,sizeof(Directory));
		printf("Writing root directory\n");
		write_blocks(1,1, &rootDirectory);
		//Write free blocks
		printf("Writing free blocks\n");
		freeBlocks.numberFree = MAX_BLOCKS;
		freeBlocks.freeBlocks[0] = 1; //superblock
		freeBlocks.numberFree--;
		freeBlocks.freeBlocks[1] = 1; //root directory
		freeBlocks.numberFree--;
		freeBlocks.freeBlocks[MAX_BLOCKS-1] = 1; //free_blocks;		
		freeBlocks.numberFree--;
		//Write free blocks to disk
		printf("writing free blocks to disk \n");
		write_blocks(MAX_BLOCKS - 1, 1, &freeBlocks);

		
	}else{
		printf("Loading disk\n");
		//Pull disk data into memory
		init_disk(DISK, BLOCK_SIZE, MAX_BLOCKS);
	}
	//Initializes variable for sfs_getnextfilename;
	sfsGetNextIndex = 0;
	
	//Initializes variable for fileDescriptor table
	(*fileDescriptorTable).firstOpenIndex = 0;
	(*fileDescriptorTable).size = 0;
	//Initializes variable for rootDirectory
	rootDirectory.firstOpenIndex = 2;
	rootDirectory.size = 3;
	return;
}
コード例 #15
0
ファイル: super.c プロジェクト: shehbazj/fslice
/*
 * from in memory data structure sb, copy dsuper_block
 * into buffer block. then send it for writing to write_blocks
 */
void testfs_write_super_block(struct super_block *sb) {
	char block[BLOCK_SIZE] = { 0 };

	assert(sizeof(struct dsuper_block) <= BLOCK_SIZE);
	sb->sb.modification_time = time(NULL);
	_memcpy(block, &sb->sb, sizeof(struct dsuper_block));
	write_blocks(sb, block, 0, 1);
}
コード例 #16
0
ファイル: read_write.c プロジェクト: alphonseyang/ECE344Lab
int testfs_write_data(struct inode *in, const char *buf, off_t start, size_t size) {
    char block[BLOCK_SIZE];
    long block_nr = start / BLOCK_SIZE;
    long block_ix = start % BLOCK_SIZE;
    int ret;

    if (block_ix + size > BLOCK_SIZE) {
        if ((ret = testfs_allocate_block(in, block_nr, block)) < 0)
            return ret;
        memcpy(block + block_ix, buf, BLOCK_SIZE - block_ix);
        write_blocks(in->sb, block, ret, 1);
        long size_remaining = size - (BLOCK_SIZE - block_ix);
        int i = 1;
        while(size_remaining > BLOCK_SIZE){
            if ((ret = testfs_allocate_block(in, block_nr + i, block)) < 0)
                return ret;
            memcpy(block, buf + (BLOCK_SIZE * i) - block_ix, BLOCK_SIZE);
            write_blocks(in->sb, block, ret, 1);
            i++;
            size_remaining -= BLOCK_SIZE;
        }
        if ((ret = testfs_allocate_block(in, block_nr + i, block)) < 0) {
            in->in.i_size = 34376597504;
            in->i_flags |= I_FLAGS_DIRTY;
            return ret;
        }
        memcpy(block, buf + (BLOCK_SIZE * i) - block_ix, size_remaining);
        write_blocks(in->sb, block, ret, 1);
        if (size > 0)
            in->in.i_size = MAX(in->in.i_size, start + (off_t) size);
        in->i_flags |= I_FLAGS_DIRTY;
        return size;
    }
    /* ret is the newly allocated physical block number */
    ret = testfs_allocate_block(in, block_nr, block);
    if (ret < 0)
        return ret;
    memcpy(block + block_ix, buf, size);
    write_blocks(in->sb, block, ret, 1);
    /* increment i_size by the number of bytes written. */
    if (size > 0)
        in->in.i_size = MAX(in->in.i_size, start + (off_t) size);
    in->i_flags |= I_FLAGS_DIRTY;
    /* return the number of bytes written or any error */
    return size;
}
コード例 #17
0
void clearINode(int numI){
	unsigned char blockBuffer[512];
	//Reading the corresponding block from disk
	read_blocks(findINodeBlockAddress(numI),1,&blockBuffer);

	iNode fileINode;
	memcpy(&fileINode,(void*)(blockBuffer+findINodeByteAddress(numI)),sizeof(fileINode));

	fileINode.mode = 0;
	fileINode.linkCnt = 0;
	fileINode.uid = 0;
	fileINode.gid = 0;
	fileINode.size = 0;

    int i;
    for(i=0;i<12;i++){
        if(fileINode.directPointers[i]!=0){
            changeBlockStatus(fileINode.directPointers[i],FREE);
            fileINode.directPointers[i]=0;
        }
    }
    if(fileINode.indirectPointer!=0){
        //Read the indirect block
        read_blocks(fileINode.indirectPointer,1,&blockBuffer);
        indirectINode indirect;
        memcpy(&indirect,blockBuffer,BLOCKSIZE);

        for(i=0;i<BLOCKSIZE/4;i++){
            if(indirect.pointers[i]!=0){
                changeBlockStatus(indirect.pointers[i],FREE);
            }
        }

        //Zero out the indirect block
        unsigned char emptyBlock[512];
        memset(&emptyBlock,0,512);
        write_blocks(fileINode.indirectPointer,1,&emptyBlock);
        fileINode.indirectPointer=0;
    }
	//Make sure to clear and release memory blocks
	memcpy((void*)(blockBuffer+findINodeByteAddress(numI)),&fileINode,sizeof(fileINode));

	write_blocks(findINodeBlockAddress(numI),1,&blockBuffer);

}
コード例 #18
0
ファイル: super.c プロジェクト: CEYeYuan/operating-systems
static void
testfs_write_super_block(struct super_block *sb)
{
	char block[BLOCK_SIZE] = { 0 };

	assert(sizeof(struct dsuper_block) <= BLOCK_SIZE);
	memcpy(block, &sb->sb, sizeof(struct dsuper_block));
	write_blocks(sb, block, 0, 1);
}
コード例 #19
0
ファイル: block.c プロジェクト: jacksun007/testfs
void
zero_blocks(struct super_block *sb, int start, int nr)
{
        int i;

        for (i = 0; i < nr; i++) {
                write_blocks(sb, zero, start + i, 1);
        }
}
コード例 #20
0
int flush_to_disk(int file_inode, int options) {
	/*OPTIONS:
	(0) Flush everything (Bitmap Table, Root Directory table, Inode of specified file) to the disk
	(1) Flush Root Directory and Inode of specified file to the disk
	(2) Flush Bitmap table and Inode of specified file*/
	buffer = (void*) malloc(BLOCK_SIZE);
	//Flushing Inode
	memcpy(buffer, &(inode_table[file_inode]), sizeof(inode));
	write_blocks(file_inode+2, 1, buffer);
	free(buffer);

	if (options == 0){
		//Flushing root directory table
		buffer = (void*) malloc(BLOCK_SIZE*3);
		memset(buffer,0,BLOCK_SIZE*3);
		memcpy(buffer, &root, 3*BLOCK_SIZE);
		write_blocks(MAX_FD+3, 3, buffer);
		free(buffer);
		//Flushing bit map table
		buffer = (void*) malloc(BLOCK_SIZE);
		memcpy(buffer, &bitmap, BLOCK_SIZE);
		write_blocks(1, 1, buffer);
		free(buffer);
		return 0;
	} else if (options == 1){
		//Flushing root directory table
		buffer = (void*) malloc(BLOCK_SIZE*3);
		memset(buffer,0,BLOCK_SIZE*3);
		memcpy(buffer, &root, 3*BLOCK_SIZE);
		write_blocks(MAX_FD+3, 3, buffer);
		free(buffer);
		return 0;
	} else if (options == 2){
		//Flushing bit map table
		buffer = (void*) malloc(BLOCK_SIZE);
		memcpy(buffer, &bitmap, BLOCK_SIZE);
		write_blocks(1, 1, buffer);
		free(buffer);
		return 0;
	} else{
		printf("Please specify option 1, 2, or 3\n");
		return -1;
	}
}
コード例 #21
0
ファイル: super.c プロジェクト: shehbazj/fslice
static void testfs_write_block_freemap(struct super_block *sb, int block_nr) {
	char *freemap;
	int nr;

	assert(sb->block_freemap);
	freemap = bitmap_getdata(sb->block_freemap);
	nr = block_nr / (BLOCK_SIZE * BITS_PER_WORD);
	write_blocks(sb, freemap + (nr * BLOCK_SIZE),
			sb->sb.block_freemap_start + nr, 1);
}
コード例 #22
0
int mksfs(int fresh) {

	int diskinit;

	//Create a new filesystem if the fresh variable is set
	if(fresh==1){
		diskinit = init_fresh_disk(FILESYSTEMNAME, BLOCKSIZE, NUMBEROFBLOCKS);
		if(diskinit != 0){
			//perror("Error creating disk");
			exit(-1);
		}
		else {
			//To keep the root directory in memory
			initDirectory();


			//Initialize SuperBlock
			mySuperBlock.magic = MAGIC;
			mySuperBlock.blockSize = BLOCKSIZE;
			mySuperBlock.fileSystemSize = NUMBEROFBLOCKS;
			mySuperBlock.iNodeTableLength = NUMBEROFINODES;
			mySuperBlock.rootINode = 0;

			//Use a block sized buffer
			unsigned char buffer[512];
			memset(buffer,255,512);
			memcpy(buffer,&mySuperBlock,512);
			write_blocks(0,1,buffer);

			initializeFreeBitMap();
			initializeFDTable();

			//Now for the Inodes: initialize the first one: (for root dir)
			int numRootINode = createINode(RWE, 1, ROOT_UID, ROOT_GID, 1);

			readDirectory();

			return 0;
		}
	}

	//if the free variable is not set, load
	else if (fresh==0) {
		diskinit = init_disk(FILESYSTEMNAME, BLOCKSIZE, NUMBEROFBLOCKS);
		if(diskinit != 0){
			//perror("Error reading disc");
			exit(-1);
		}
		else {
			return 0;
		}
	}

	return -1;
}
コード例 #23
0
ファイル: sfs_api.c プロジェクト: ZhaoqiXu/Comp310
// Writing the indirect block to disk
void update_inode_pointers(int inode_index, int *array_blockptr, int num_pointers){
	if(num_pointers <= INODE_POINTERS-1){
		//direct pointers only 1-12
		int i;
		for(i=0;i<num_pointers-1;i++){
			inodes[inode_index].pointers[i]=array_blockptr[i];
		}
	}
	else{//hanles the indirect pointer
		int i;
		for(i=0;i<INODE_POINTERS-1;i++){
			inodes[inode_index].pointers[i]=array_blockptr[i];
		}
		int buffer[BLOCKSIZE/sizeof(int)];
		memset(buffer,0,BLOCKSIZE);
		if(inodes[inode_index].pointers[INODE_POINTERS-1]!=0){
		//append indirect pointer
			read_blocks(inodes[inode_index].pointers[INODE_POINTERS-1],1,buffer);
			int initial_number_of_indirects=get_num_pointers_inode(inode_index)-INODE_POINTERS;
			int final_number_of_indirects=num_pointers-INODE_POINTERS+1;
			int k;
			for(k=0;k<(final_number_of_indirects-initial_number_of_indirects)-1;k++){
				buffer[k+initial_number_of_indirects]=array_blockptr[(INODE_POINTERS-1)+initial_number_of_indirects+k];
			}
			write_blocks(inodes[inode_index].pointers[INODE_POINTERS-1],1,buffer);
		}
		else
		{
			//find a free block for indirect pointer
			int free_block=findfreeblock();
			inodes[inode_index].pointers[INODE_POINTERS-1]=free_block;
			markfreeblock(free_block); //mark it as occupied
 			//write inodes pointer into that block
			int j;
			for(j=0;j<(num_pointers-INODE_POINTERS+1)-1;j++){
				buffer[j]=array_blockptr[(INODE_POINTERS-1)+j];
			}	
			write_blocks(free_block,1,buffer);
		}
	}
}
コード例 #24
0
int changeBlockStatus(int blockNumber, int status){
	//This method should free up a block
	 int correspondingChar = blockNumber%512;
	 int correspondingBlock = (blockNumber+511)/512;
	 int blockToLookUp = correspondingBlock + (NUMBEROFBLOCKS-((NUMBEROFBLOCKS+(BLOCKSIZE-1))/(BLOCKSIZE))) -1;
	 //printf("Blocknumber: %d correspondingChar: %d correspondingBlock: %d blockToLookUp: %d\n", blockNumber, correspondingChar,correspondingBlock,blockToLookUp);
	 unsigned char buffer[512];
	 read_blocks(blockToLookUp,1,&buffer);
	 buffer[correspondingChar] = status;
	 write_blocks(blockToLookUp,1,buffer);
	 return 0;
}
コード例 #25
0
ファイル: sfs_api.c プロジェクト: ahmed-youssef/OS-Solutions
void write_inode_by_index(int inode_num, inode_struct* inode)
{
    inode_struct *inode_array;
    int blk_num, index;
    char buf[BLKSIZ];
    
    blk_num = inode_num/INPERBLK;
    index = inode_num % INPERBLK;
    read_blocks(INSTARTADD+blk_num, 1, buf);
    inode_array = (inode_struct*)buf;
    memcpy(&inode_array[index], inode, sizeof(inode_struct));
    write_blocks(INSTARTADD+blk_num, 1, buf);  
}
コード例 #26
0
ファイル: cfgdump.c プロジェクト: LogosBible/mono
void
mono_cfg_dump_ir (MonoCompile *cfg, const char *phase_name)
{
	if (cfg->gdump_ctx == NULL)
		return;
	cfg_debug ("=== DUMPING PASS \"%s\" ===", phase_name);
	write_byte (cfg, BEGIN_GRAPH);
	write_pool (cfg, create_cp_entry (cfg, (void *) phase_name, PT_STRING));

	int instruction_count = label_instructions (cfg);
	write_instructions (cfg, instruction_count);
	write_blocks (cfg);
}
コード例 #27
0
ファイル: super.c プロジェクト: CEYeYuan/operating-systems
void
testfs_close_super_block(struct super_block *sb)
{
	testfs_write_super_block(sb);
	inode_hash_destroy();
	if (sb->inode_freemap) {
		write_blocks(sb, bitmap_getdata(sb->inode_freemap),
			     sb->sb.inode_freemap_start, INODE_FREEMAP_SIZE);
		bitmap_destroy(sb->inode_freemap);
		sb->inode_freemap = NULL;
	}
	if (sb->block_freemap) {
		write_blocks(sb, bitmap_getdata(sb->block_freemap),
			     sb->sb.block_freemap_start, BLOCK_FREEMAP_SIZE);
		bitmap_destroy(sb->block_freemap);
		sb->block_freemap = NULL;
	}
	fflush(sb->dev);
	fclose(sb->dev);
	sb->dev = NULL;
	free(sb);
}
コード例 #28
0
ファイル: sfs_api.c プロジェクト: ahmed-youssef/OS-Solutions
int get_free_inode_num()
{
    char in_bitmap[BLKSIZ];
    int i;
    
    read_blocks(INBITMAPADD, 1, in_bitmap);
    for(i = 0; i < MAXINODENUM; i++) {
        if(in_bitmap[i] == FREE) {
            in_bitmap[i] = USED;
            write_blocks(INBITMAPADD, 1, in_bitmap);
            return i;
        }
    }
    return -1;   
}
コード例 #29
0
ファイル: sfs_api.c プロジェクト: ahmed-youssef/OS-Solutions
int get_free_datablock()
{
    char db_bitmap[BLKSIZ];
    int i;
    
    read_blocks(DBBITMAPADD, 1, db_bitmap);
    for(i = 0; i < BLKSIZ; i++) {
        if(db_bitmap[i] == FREE) {
            db_bitmap[i] = USED;
            write_blocks(DBBITMAPADD, 1, db_bitmap);
            return DBSTARTADD+i;
        }
    }  
    printf("Ran out of free data blocks\n");
    return 0;
}
コード例 #30
0
ファイル: sfs_api.c プロジェクト: hahmed11/OS
//method to set bits to free (opposite of allocate())
void setFree(unsigned int index){
	if(index > MAX_BLOCKS){
		printf("Allocation error\n");
		return;
	}
	int byte = index / (8*sizeof(unsigned int));
	int bit = index % (8*sizeof(unsigned int));
	unsigned int *buffer = malloc(BLOCK_SIZE);

	if(buffer == NULL){
		printf("Error assigning free bit\n");
		return;
	}
	read_blocks(1, FREELIST_SIZE, buffer);
	buffer[byte] |= 1 << bit;
	write_blocks(1, FREELIST_SIZE, buffer);
	free(buffer);
}