示例#1
0
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;
}
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;
	//printf("__________ Enters write data with block_nr=%ld \n",block_nr);
	long block_ix = start % BLOCK_SIZE;
	int ret;
	// int req_b= ((start+size)/BLOCK_SIZE);
	long long part=start;

	if (block_ix + size > BLOCK_SIZE) {
		int req_b= DIVROUNDUP(block_ix+size,BLOCK_SIZE);
	
		int i;
		size_t write_block_size, remaining_bytes;
		remaining_bytes = size;

		write_block_size = BLOCK_SIZE - block_ix;
		remaining_bytes = remaining_bytes-write_block_size;
		ret = testfs_allocate_block(in, block_nr, block);
		if (ret < 0)
			return ret;
		memcpy(block+block_ix, buf, write_block_size);
		write_blocks(in->sb,block,ret,1);

		part += write_block_size;

		block_nr++;

		for (i=1; i<req_b; i++)
		{
			 
			memset(&block[0],0,sizeof(block));
			ret = testfs_allocate_block(in, block_nr, block);

			if (ret < 0){
				break;
			}
			if (remaining_bytes >= BLOCK_SIZE)
			{
				write_block_size=BLOCK_SIZE;
				remaining_bytes=remaining_bytes - BLOCK_SIZE;
			}

			else if (remaining_bytes < BLOCK_SIZE)
			{
				write_block_size=remaining_bytes;
				remaining_bytes=0;
			}

			else if (remaining_bytes == 0)
			{
				break;
			}

			part += write_block_size;

			memcpy(block, buf+size - (remaining_bytes + write_block_size), write_block_size);
			write_blocks(in->sb, block, ret, 1);
			block_nr++;
		}

		

		if (size > 0)
			in->in.i_size = MAX(in->in.i_size, part);
		in->i_flags |= I_FLAGS_DIRTY;
		if (ret<0)
			return ret;
		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;
}