Example #1
0
File: fs.c Project: MG47/JOS-MG
// Set *blk to the address in memory where the filebno'th
// block of file 'f' would be mapped.
//
// Returns 0 on success, < 0 on error.  Errors are:
//	-E_NO_DISK if a block needed to be allocated but the disk is full.
//	-E_INVAL if filebno is out of range.
//
// Hint: Use file_block_walk and alloc_block.
int
file_get_block(struct File *f, uint32_t filebno, char **blk)
{
       // code for lab 5- M.G
       //      panic("file_get_block not implemented");

    uint32_t *ppdiskbno;
    uint32_t new_block_no;

    int return_value;    

    if ((return_value = file_block_walk(f, filebno, &ppdiskbno,true)) < 0)
    {
        return return_value;
    }    

    if (!*ppdiskbno)
    {
		if ((new_block_no = alloc_block()) < 0)
        {
			return -E_NO_DISK;
        }
	    *ppdiskbno = new_block_no;
    }   
    
    *blk = diskaddr(*ppdiskbno);

    return 0;
}
Example #2
0
// Set *blk to point at the filebno'th block in file 'f'.
// Allocate the block if it doesn't yet exist.
//
// Returns 0 on success, < 0 on error.  Errors are:
//	-E_NO_DISK if a block needed to be allocated but the disk is full.
//	-E_INVAL if filebno is out of range.
//
// Hint: Use file_block_walk and alloc_block.
int
file_get_block(struct File *f, uint32_t filebno, char **blk)
{
	// LAB 5: Your code here.
	int r;
	uint32_t *pblkno;

	if ((r = file_block_walk(f, filebno, &pblkno, 1)) < 0){
		return r;
	}

	// if not exist
	if (*pblkno == 0){
		if ((r = alloc_block()) < 0){
			return -E_NO_DISK;
		}
		*pblkno = r;
		// Clear this block
		 memset(diskaddr(r), 0, BLKSIZE);
		// flush this empty block into disk
		flush_block(diskaddr(r));
	}
	*blk = diskaddr(*pblkno);
	return 0;
}
Example #3
0
// Set *blk to point at the filebno'th block in file 'f'.
// Allocate the block if it doesn't yet exist.
//
// Returns 0 on success, < 0 on error.  Errors are:
//	-E_NO_DISK if a block needed to be allocated but the disk is full.
//	-E_INVAL if filebno is out of range.
//
// Hint: Use file_block_walk and alloc_block.
int
file_get_block(struct File *f, uint32_t filebno, char **blk)
{
	// LAB 5: Your code here.
	//panic("file_get_block not implemented");
	uint32_t *pdiskbno;
	int result;

	if((result=file_block_walk(f, filebno, &pdiskbno, 1)) < 0)
	{
		return result;
	}
	
	if(*pdiskbno == 0) 
	{
		if((result=alloc_block()) < 0)
		{
			return -E_NO_DISK;
		}
		else
		{	
			*pdiskbno=result;
			memset(diskaddr(result), 0, BLKSIZE);

			flush_block(diskaddr (result));
		}
	}
		
	*blk=diskaddr(*pdiskbno);
		
	return 0;
}
Example #4
0
// Remove a block from file f.  If it's not there, just silently succeed.
// Returns 0 on success, < 0 on error.
static int
file_free_block(struct File *f, uint32_t filebno)
{
	int r;
	uint32_t *ptr;

	if ((r = file_block_walk(f, filebno, &ptr, 0)) < 0)
		return r;
	if (*ptr) {
		free_block(*ptr);
		*ptr = 0;
	}
	return 0;
}
Example #5
0
// Flush the contents and metadata of file f out to disk.
// Loop over all the blocks in file.
// Translate the file block number into a disk block number
// and then check whether that disk block is dirty.  If so, write it out.
void
file_flush(struct File *f)
{
	int i;
	uint32_t *pdiskbno;

	for (i = 0; i < (f->f_size + BLKSIZE - 1) / BLKSIZE; i++) {
		if (file_block_walk(f, i, &pdiskbno, 0) < 0 ||
		    pdiskbno == NULL || *pdiskbno == 0)
			continue;
		flush_block(diskaddr(*pdiskbno));
	}
	flush_block(f);
	if (f->f_indirect)
		flush_block(diskaddr(f->f_indirect));
}
Example #6
0
File: fs.c Project: scau/JOS
// Set *blk to the address in memory where the filebno'th
// block of file 'f' would be mapped.
//
// Returns 0 on success, < 0 on error.  Errors are:
//	-E_NO_DISK if a block needed to be allocated but the disk is full.
//	-E_INVAL if filebno is out of range.
//
int
file_get_block(struct File *f, uint32_t filebno, char **blk)
{
	if (filebno >= NDIRECT + NINDIRECT) {
		return -E_INVAL;
	}
	uint32_t *ppdiskbno;
	int r = file_block_walk(f, filebno, &ppdiskbno, false);
	if (r < 0)
		return r;
    if (!*ppdiskbno)
        return -E_NO_DISK;
	*blk = (char*)diskaddr(*ppdiskbno);
	return 0;
	// LAB 5: Your code here.
	//panic("file_block_walk not implemented");
}
Example #7
0
// Set *blk to the address in memory where the filebno'th
// block of file 'f' would be mapped.
//
// Returns 0 on success, < 0 on error.  Errors are:
//	-E_NO_DISK if a block needed to be allocated but the disk is full.
//	-E_INVAL if filebno is out of range.
//
// Hint: Use file_block_walk and alloc_block.
int
file_get_block(struct File *f, uint32_t filebno, char **blk)
{
       // LAB 5: Your code here.
    int r;
    uint32_t *ppdiskbno;
    if ((r = file_block_walk(f, filebno, &ppdiskbno, 1)) < 0)
        return r;
    if (!*ppdiskbno)
    {
        if ((r = alloc_block()) < 0) 
            return r;
        *ppdiskbno = r;
    }
    *blk = (char *) diskaddr(*ppdiskbno);
    return 0;
}
Example #8
0
File: fs.c Project: ren85/jos2006
// Set '*diskbno' to the disk block number for the 'filebno'th block
// in file 'f'.
// If 'alloc' is set and the block does not exist, allocate it.
//
// Returns 0 on success, < 0 on error.  Errors are:
//	-E_NOT_FOUND if alloc was 0 but the block did not exist.
//	-E_NO_DISK if a block needed to be allocated but the disk is full.
//	-E_NO_MEM if we're out of memory.
//	-E_INVAL if filebno is out of range.
int
file_map_block(struct File *f, uint32_t filebno, uint32_t *diskbno, bool alloc)
{
	int r;
	uint32_t *ptr;

	if ((r = file_block_walk(f, filebno, &ptr, alloc)) < 0)
		return r;
	if (*ptr == 0) {
		if (alloc == 0)
			return -E_NOT_FOUND;
		if ((r = alloc_block()) < 0)
			return r;
		*ptr = r;
	}
	*diskbno = *ptr;
	return 0;
}
Example #9
0
// Set *blk to point at the filebno'th block in file 'f'.
// Allocate the block if it doesn't yet exist.
//
// Returns 0 on success, < 0 on error.  Errors are:
//	-E_NO_DISK if a block needed to be allocated but the disk is full.
//	-E_INVAL if filebno is out of range.
//
// Hint: Use file_block_walk and alloc_block.
int
file_get_block(struct File *f, uint32_t filebno, char **blk)
{
	// LAB 5: Your code here.
    int r;
    uint32_t *pdiskbno;
    if (0 != (r = file_block_walk(f, filebno, &pdiskbno, true)))
        return r;
    if (0 == *pdiskbno) {
        r = alloc_block();
        if (0 > r)
            return -E_NO_DISK;
        *pdiskbno = r;
    }
    if (NULL != blk)
        *blk = (char *)(DISKMAP + BLKSIZE * (*pdiskbno));

    return 0;
}
Example #10
0
// Set *blk to the address in memory where the filebno'th
// block of file 'f' would be mapped.
//
// Returns 0 on success, < 0 on error.  Errors are:
//	-E_NO_DISK if a block needed to be allocated but the disk is full.
//	-E_INVAL if filebno is out of range.
//
// Hint: Use file_block_walk and alloc_block.
int
file_get_block(struct File *f, uint32_t filebno, char **blk)
{
       // LAB 5: Your code here.
       // panic("file_get_block not implemented");
	int r;
	uint32_t *ptr;

	if ((r = file_block_walk(f, filebno, &ptr, 1)) < 0)
		return r;
	if (!*ptr) {
		*ptr = alloc_block();
	}
	if (!*ptr) {
		return -E_NO_DISK;
	}
	*blk = (char *)diskaddr(*ptr);
	return 0;

}
Example #11
0
// Set *blk to point at the filebno'th block in file 'f'.
// Allocate the block if it doesn't yet exist.
//
// Returns 0 on success, < 0 on error.  Errors are:
//	-E_NO_DISK if a block needed to be allocated but the disk is full.
//	-E_INVAL if filebno is out of range.
//
// Hint: Use file_block_walk and alloc_block.
int
file_get_block(struct File *f, uint32_t filebno, char **blk)
{
	// LAB 5: Your code here.
	if(filebno >= NDIRECT + NINDIRECT)
		return -E_INVAL;
	uint32_t* baddr;
	int status = 0;
	if((status = file_block_walk(f, filebno, &baddr, 1)) < 0)
		return status;
	if(*baddr == 0)
	{
		if((status = alloc_block()) < 0)
			return status;
		*baddr = status;
	}
	char* addr = (char*)diskaddr(*baddr);
	*blk = addr;
	return 0;
}
Example #12
0
File: fs.c Project: bosswissam/djos
// Set *blk to the address in memory where the filebno'th
// block of file 'f' would be mapped.
// Allocate the block if it doesn't yet exist.
//
// Returns 0 on success, < 0 on error.  Errors are:
//	-E_NO_DISK if a block needed to be allocated but the disk is full.
//	-E_INVAL if filebno is out of range.
//
// Hint: Use file_block_walk and alloc_block.
int
file_get_block(struct File *f, uint32_t filebno, char **blk)
{
	// LAB 5: Your code here.
	uint32_t *blkno;
	uint32_t err;
	
	if ((err = file_block_walk(f, filebno, &blkno, 1)) < 0) {
		return err;
	}

	if (!*blkno) { // no block allocated
		if ((err = alloc_block()) < 0)
			return -E_NO_DISK;
		*blkno = err;
	}

	*blk = (char *) diskaddr(*blkno);

	return 0;
}
Example #13
0
File: fs.c Project: Cai41/mit-6.828
// Set *blk to the address in memory where the filebno'th
// block of file 'f' would be mapped.
//
// Returns 0 on success, < 0 on error.  Errors are:
//	-E_NO_DISK if a block needed to be allocated but the disk is full.
//	-E_INVAL if filebno is out of range.
//
// Hint: Use file_block_walk and alloc_block.
int
file_get_block(struct File *f, uint32_t filebno, char **blk)
{
       // LAB 5: Your code here.
       // panic("file_get_block not implemented");
	if (!f) panic("file not exist!\n");
	if (filebno >= NDIRECT + NINDIRECT) return -E_INVAL;
	
	uint32_t *ppdiskbno = NULL;
	int r;
	if ((r = file_block_walk(f, filebno, &ppdiskbno, 1)) < 0)
		return r;
	if ((*ppdiskbno) == 0) {
		if ((r = alloc_block()) < 0)
			return r;
		*ppdiskbno = r;
	}
	*blk = diskaddr(*ppdiskbno);
	return 0;
	
}
Example #14
0
File: fs.c Project: yahu/JOS
// Set *blk to the address in memory where the filebno'th
// block of file 'f' would be mapped.
// Allocate the block if it doesn't yet exist.
//
// Returns 0 on success, < 0 on error.  Errors are:
//	-E_NO_DISK if a block needed to be allocated but the disk is full.
//	-E_INVAL if filebno is out of range.
//
// Hint: Use file_block_walk and alloc_block.
int
file_get_block(struct File *f, uint32_t filebno, char **blk)
{
	// LAB 5: Your code here.
	//panic("file_get_block not implemented");
	int r;
    uint32_t *pdiskbno;
    uint32_t blockno;

	if((r =file_block_walk(f,filebno, &pdiskbno, 1)) < 0)
        return r;

    if( *pdiskbno == 0){
        if((*pdiskbno = alloc_block()) < 0)
            return -E_NO_DISK;
        zero_block(*pdiskbno);
    }

    *blk = (char *)diskaddr(*pdiskbno);
    return 0;
}
Example #15
0
File: fs.c Project: sid9211/OSLab
// Set *blk to the address in memory where the filebno'th
// block of file 'f' would be mapped.
// Allocate the block if it doesn't yet exist.
//
// Returns 0 on success, < 0 on error.  Errors are:
//	-E_NO_DISK if a block needed to be allocated but the disk is full.
//	-E_INVAL if filebno is out of range.
//
// Hint: Use file_block_walk and alloc_block.
int
file_get_block(struct File *f, uint32_t filebno, char **blk)
{
	// LAB 5: Your code here.
	int new_block;
	uint32_t * blkno;
	if(!f || !blk)
		return -E_INVAL;
	int i;	
	i = file_block_walk(f, filebno, &blkno, 1);
	if(i<0)
		return i;
	if(*blkno == 0)
	{
		new_block = alloc_block();
                if(new_block == -E_NO_DISK)
                         return -E_NO_DISK;
                *blkno = (uint32_t)new_block;
	}
	*blk = (char *)diskaddr(*blkno);
	return 0;	
//	panic("file_get_block not implemented");
}
Example #16
0
File: fs.c Project: amisharma/OSLAB
// Set *blk to the address in memory where the filebno'th
// block of file 'f' would be mapped.
//
// Returns 0 on success, < 0 on error.  Errors are:
//	-E_NO_DISK if a block needed to be allocated but the disk is full.
//	-E_INVAL if filebno is out of range.
//
int
file_get_block(struct File *f, uint32_t filebno, char **blk)
{
	// LAB 5: Your code here.
//	cprintf("entering file_get_block\n");
	if(!f||!blk)
	{
		cprintf("invalid file or blk pointer\n");
		return -E_INVAL;
	}
	int i;
	uint32_t * blkno;
	i=file_block_walk(f,filebno,&blkno,0);
	if(i<0)
	{
		cprintf("error during file_block_walk in file_get_block\n");
		return i;
	}
	*blk=(char *)diskaddr(*blkno);
//	cprintf("exiting file_get_block\n");
	return 0;	
	panic("file_block_walk not implemented");
}
Example #17
0
// Set *blk to the address in memory where the filebno'th
// block of file 'f' would be mapped.
//
// Returns 0 on success, < 0 on error.  Errors are:
//	-E_NO_DISK if a block needed to be allocated but the disk is full.
//	-E_INVAL if filebno is out of range.
//
int
file_get_block(struct File *f, uint32_t filebno, char **blk)
{
	// LAB 5: Your code here.

    // Find block's disk block number.
    uint32_t *pdiskbno;
    int r = file_block_walk(f, filebno, &pdiskbno, 1);
    if (r)
        return r;

    // Not exist yet.
    if (!*pdiskbno)
        *pdiskbno = alloc_block();

    // Allocation failed.
    if (!*pdiskbno)
        return -E_NO_DISK;

    *blk = (char *)diskaddr(*pdiskbno);
    return 0;

	// panic("file_get_block not implemented");
}