/*
 * QEMU Block driver for virtual VFAT (shadows a local directory)
 * 
 * Copyright (c) 2004 Johannes E. Schindelin
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
#include <sys/stat.h>
#include <dirent.h>
#include <assert.h>
#include "vl.h"
#include "block_int.h"

// TODO: new file
// TODO: delete file
// TODO: make root directory larger
// TODO: make directory clusters connected, so they are reserved anyway... add a member which tells how many clusters are reserved after a directory
// TODO: introduce another member in mapping_t which says where the directory resides in s->directory (for mkdir and rmdir) 
// in _read and _write, before treating direntries or file contents, get_mapping to know what it is.
// TODO: mkdir
// TODO: rmdir 

// TODO: when commit_data'ing a direntry and is_consistent, commit_remove
// TODO: reset MODE_MODIFIED when commit_remove'ing

#define DEBUG

/* dynamic array functions */
typedef struct array_t {
    char* pointer;
    unsigned int size,next,item_size;
} array_t;

static inline void array_init(array_t* array,unsigned int item_size)
{
    array->pointer=0;
    array->size=0;
    array->next=0;
    array->item_size=item_size;
}

static inline void array_free(array_t* array)
{
    if(array->pointer)
        free(array->pointer);
    array->size=array->next=0;
}

/* make sure that memory is reserved at pointer[index*item_size] */
static inline void* array_get(array_t* array,unsigned int index) {
    if((index+1)*array->item_size>array->size) {
	int new_size=(index+32)*array->item_size;
	array->pointer=realloc(array->pointer,new_size);
	if(!array->pointer)
	    return 0;
	array->size=new_size;
	array->next=index+1;
    }
    return array->pointer+index*array->item_size;
}

static inline void* array_get_next(array_t* array) {
    unsigned int next=array->next;
    void* result=array_get(array,next);
    array->next=next+1;
    return result;
}

static inline void* array_insert(array_t* array,unsigned int index,unsigned int count) {
    if((array->next+count)*array->item_size>array->size) {
	int increment=count*array->item_size;
	array->pointer=realloc(array->pointer,array->size+increment);
	if(!array->pointer)
	    return 0;
	array->size+=increment;
    }
    memmove(array->pointer+(index+count)*array->item_size,
		array->pointer+index*array->item_size,
		(array->next-index)*array->item_size);
    array->next+=count;
    return array->pointer+index*array->item_size;
}

/* this performs a "roll", so that the element which was at index_from becomes
 * index_to, but the order of all other elements is preserved. */
static inline int array_roll(array_t* array,int index_to,int index_from,int count)
{
    char* buf;
    char* from;
    char* to;
    int is;

    if(!array ||
	    index_to<0 || index_to>=array->next ||
	    index_from<0 || index_from>=array->next)
	return -1;
    
    if(index_to==index_from)
	return 0;

    is=array->item_size;
    from=array->pointer+index_from*is;
    to=array->pointer+index_to*is;
    buf=malloc(is*count);
    memcpy(buf,from,is*count);

    if(index_to<index_from)
	memmove(to+is*count,to,from-to);
    else
	memmove(from,from+is*count,to-from);
    
    memcpy(to,buf,is*count);

    free(buf);

    return 0;
}

int array_remove(array_t* array,int index)
{
    if(array_roll(array,array->next-1,index,1))
	return -1;
    array->next--;
    return 0;
}

/* These structures are used to fake a disk and the VFAT filesystem.
 * For this reason we need to use __attribute__((packed)). */

typedef struct bootsector_t {
    uint8_t jump[3];
    uint8_t name[8];
    uint16_t sector_size;
    uint8_t sectors_per_cluster;
    uint16_t reserved_sectors;
    uint8_t number_of_fats;
    uint16_t root_entries;
    uint16_t zero;
    uint8_t media_type;
    uint16_t sectors_per_fat;
    uint16_t sectors_per_track;
    uint16_t number_of_heads;
    uint32_t hidden_sectors;
    uint32_t total_sectors;
    union {
        struct {
	    uint8_t drive_number;
	    uint8_t current_head;
	    uint8_t signature;
	    uint32_t id;
	    uint8_t volume_label[11];
	} __attribute__((packed)) fat16;
	struct {
	    uint32_t sectors_per_fat;
	    uint16_t flags;
	    uint8_t major,minor;
	    uint32_t first_cluster_of_root_directory;
	    uint16_t info_sector;
	    uint16_t backup_boot_sector;
	    uint16_t ignored;
	} __attribute__((packed)) fat32;
    } u;
    uint8_t fat_type[8];
    uint8_t ignored[0x1c0];
    uint8_t magic[2];
} __attribute__((packed)) bootsector_t;

typedef struct partition_t {
    uint8_t attributes; /* 0x80 = bootable */
    uint8_t start_head;
    uint8_t start_sector;
    uint8_t start_cylinder;
    uint8_t fs_type; /* 0x6 = FAT16, 0xb = FAT32 */
    uint8_t end_head;
    uint8_t end_sector;
    uint8_t end_cylinder;
    uint32_t start_sector_long;
    uint32_t end_sector_long;
} __attribute__((packed)) partition_t;

typedef struct mbr_t {
    uint8_t ignored[0x1be];
    partition_t partition[4];
    uint8_t magic[2];
} __attribute__((packed)) mbr_t;

typedef struct direntry_t {
    uint8_t name[8];
    uint8_t extension[3];
    uint8_t attributes;
    uint8_t reserved[2];
    uint16_t ctime;
    uint16_t cdate;
    uint16_t adate;
    uint16_t begin_hi;
    uint16_t mtime;
    uint16_t mdate;
    uint16_t begin;
    uint32_t size;
} __attribute__((packed)) direntry_t;

/* this structure are used to transparently access the files */

typedef struct mapping_t {
    /* begin is the first cluster, end is the last+1,
     * offset is the offset in the file in clusters of this slice */
    off_t begin,end,offset;
    char* filename;

    /* as s->directory is growable, no pointer may be used here */
    unsigned int dir_index;
    enum { MODE_NORMAL,MODE_UNDEFINED,MODE_MODIFIED,MODE_DELETED,MODE_DIRECTORY } mode;
} mapping_t;

/* this structure is used to hold sectors which need to be written, but it's
 * not known yet where to write them. */

typedef struct commit_t {
    uint32_t cluster_num;
    uint8_t* buf;
} commit_t;

/* write support exists for fat, direntry and file contents */
typedef enum {
    WRITE_UNDEFINED,WRITE_FAT,WRITE_DIRENTRY,WRITE_DATA
} write_action_t;

/* here begins the real VVFAT driver */

typedef struct BDRVVVFATState {
    unsigned int first_sectors_number; /* 1 for a single partition, 0x40 for a disk with partition table */
    unsigned char first_sectors[0x40*0x200];
    
    int fat_type; /* 16 or 32 */
    array_t fat,directory,mapping;
   
    unsigned int cluster_size;
    unsigned int sectors_per_cluster;
    unsigned int sectors_per_fat;
    unsigned int sectors_of_root_directory;
    unsigned int sectors_for_directory;
    unsigned int faked_sectors; /* how many sectors are faked before file data */
    uint32_t sector_count; /* total number of sectors of the partition */
    uint32_t cluster_count; /* total number of clusters of this partition */
    unsigned int first_file_mapping; /* index of the first mapping which is not a directory, but a file */
    uint32_t max_fat_value;
   
    int current_fd;
    char current_fd_is_writable; /* =0 if read only, !=0 if read/writable */
    mapping_t* current_mapping;
    unsigned char* cluster;
    unsigned int current_cluster;

    /* write support */
    array_t commit;
    /* for each file, the file contents, the direntry, and the fat entries are
     * written, but not necessarily in that order */
    write_action_t action[3];
} BDRVVVFATState;


static int vvfat_probe(const uint8_t *buf, int buf_size, const char *filename)
{
    if (strstart(filename, "fat:", NULL) ||
        strstart(filename, "fatrw:", NULL))
	return 100;
    return 0;
}

static void init_mbr(BDRVVVFATState* s)
{
    /* TODO: if the files mbr.img and bootsect.img exist, use them */
    mbr_t* real_mbr=(mbr_t*)s->first_sectors;
    partition_t* partition=&(real_mbr->partition[0]);

    memset(s->first_sectors,0,512);
   
    partition->attributes=0x80; /* bootable */
    partition->start_head=1;
    partition->start_sector=1;
    partition->start_cylinder=0;
    partition->fs_type=(s->fat_type==16?0x6:0xb); /* FAT16/FAT32 */
    partition->end_head=0xf;
    partition->end_sector=0xff; /* end sector & upper 2 bits of cylinder */;
    partition->end_cylinder=0xff; /* lower 8 bits of end cylinder */;
    partition->start_sector_long=cpu_to_le32(0x3f);
    partition->end_sector_long=cpu_to_le32(s->sector_count);

    real_mbr->magic[0]=0x55; real_mbr->magic[1]=0xaa;
}

/* dest is assumed to hold 258 bytes, and pads with 0xffff up to next multiple of 26 */
static inline int short2long_name(unsigned char* dest,const char* src)
{
    int i;
    for(i=0;i<129 && src[i];i++) {
        dest[2*i]=src[i];
	dest[2*i+1]=0;
    }
    dest[2*i]=dest[2*i+1]=0;
    for(i=2*i+2;(i%26);i++)
	dest[i]=0xff;
    return i;
}

static inline direntry_t* create_long_filename(BDRVVVFATState* s,const char* filename)
{
    char buffer[258];
    int length=short2long_name(buffer,filename),
        number_of_entries=(length+25)/26,i;
    direntry_t* entry;

    for(i=0;i<number_of_entries;i++) {
	entry=array_get_next(&(s->directory));
	entry->attributes=0xf;
	entry->reserved[0]=0;
	entry->begin=0;
	entry->name[0]=(number_of_entries-i)|(i==0?0x40:0);
    }
    for(i=0;i<length;i++) {
	int offset=(i%26);
	if(offset<10) offset=1+offset;
	else if(offset<22) offset=14+offset-10;
	else offset=28+offset-22;
	entry=array_get(&(s->directory),s->directory.next-1-(i/26));
	entry->name[offset]=buffer[i];
    }
    return array_get(&(s->directory),s->directory.next-number_of_entries);
}

/* fat functions */

static inline uint8_t fat_chksum(direntry_t* entry)
{
    uint8_t chksum=0;
    int i;

    for(i=0;i<11;i++)
	chksum=(((chksum&0xfe)>>1)|((chksum&0x01)?0x80:0))
	    +(unsigned char)entry->name[i];
    
    return chksum;
}

/* if return_time==0, this returns the fat_date, else the fat_time */
static uint16_t fat_datetime(time_t time,int return_time) {
    struct tm* t;
#ifdef _WIN32
    t=localtime(&time); /* this is not thread safe */
#else
    struct tm t1;
    t=&t1;
    localtime_r(&time,t);
#endif
    if(return_time)
	return cpu_to_le16((t->tm_sec/2)|(t->tm_min<<5)|(t->tm_hour<<11));
    return cpu_to_le16((t->tm_mday)|((t->tm_mon+1)<<5)|((t->tm_year-80)<<9));
}

static inline void fat_set(BDRVVVFATState* s,unsigned int cluster,uint32_t value)
{
    if(s->fat_type==12) {
	assert(0); /* TODO */
    } else if(s->fat_type==16) {
	uint16_t* entry=array_get(&(s->fat),cluster);
	*entry=cpu_to_le16(value&0xffff);
    } else {
	uint32_t* entry=array_get(&(s->fat),cluster);
	*entry=cpu_to_le32(value);
    }
}

static inline uint32_t fat_get(BDRVVVFATState* s,unsigned int cluster)
{
    //fprintf(stderr,"want to get fat for cluster %d\n",cluster);
    if(s->fat_type==12) {
	const uint8_t* x=s->fat.pointer+cluster*3/2;
	return ((x[0]|(x[1]<<8))>>(cluster&1?4:0))&0x0fff;
    } else if(s->fat_type==16) {
	uint16_t* entry=array_get(&(s->fat),cluster);
	return le16_to_cpu(*entry);
    } else {
	uint32_t* entry=array_get(&(s->fat),cluster);
	return le32_to_cpu(*entry);
    }
}

static inline int fat_eof(BDRVVVFATState* s,uint32_t fat_entry)
{
    if(fat_entry>s->max_fat_value-8)
	return -1;
    return 0;
}

static inline void init_fat(BDRVVVFATState* s)
{
    int i;
    
    array_init(&(s->fat),(s->fat_type==32?4:2));
    array_get(&(s->fat),s->sectors_per_fat*0x200/s->fat.item_size-1);
    memset(s->fat.pointer,0,s->fat.size);
    fat_set(s,0,0x7ffffff8);
    
    for(i=1;i<s->sectors_for_directory/s->sectors_per_cluster-1;i++)
	fat_set(s,i,i+1);
    fat_set(s,i,0x7fffffff);

    switch(s->fat_type) {
	case 12: s->max_fat_value=0xfff; break;
	case 16: s->max_fat_value=0xffff; break;
	case 32: s->max_fat_value=0xfffffff; break;
	default: s->max_fat_value=0; /* error... */
    }

}

static inline int long2unix_name(unsigned char* dest,int dest_size,direntry_t* direntry_short) {
    int i=-1,j;
    int chksum=fat_chksum(direntry_short);
    while(1) {
	char* buf=(char*)(direntry_short+i);
	if((buf[0]&0x3f)!=-i || direntry_short[i].reserved[1]!=chksum ||
		direntry_short[i].attributes!=0xf) {
	    if(i<-1)
		return -3;
	    /* take short name */
	    for(j=7;j>0 && direntry_short->name[j]==' ';j--);
	    if(j+1>dest_size)
		return -1;
	    strncpy(dest,direntry_short->name,j+1);
	    dest+=j+1; dest_size-=j+1;
	    for(j=2;j>=0 && direntry_short->extension[j]==' ';j--);
	    if(j>=0) {
		if(j+2>dest_size)
		    return -1;
		dest[0]='.';
	        strncpy(dest+1,direntry_short->extension,j+1);
	    }
	    return 0;
	}
	for(j=0;j<13;j++) {
	    dest_size--;
	    if(dest_size<0)
		return -2;
	    dest[0]=buf[2*j+((j<5)?1:(j<11)?4:6)];
	    if(dest[0]==0 && (buf[0]&0x40)!=0)
		return 0;
	    dest++;
	}
	/* last entry, but no trailing \0? */
	if(buf[0]&0x40)
	    return -3;
	i--;
    }
}

static inline direntry_t* create_short_filename(BDRVVVFATState* s,unsigned int directory_start,const char* filename,int is_dot)
{
    int i,long_index=s->directory.next;
    direntry_t* entry=0;
    direntry_t* entry_long=0;

    if(is_dot) {
	entry=array_get_next(&(s->directory));
	memset(entry->name,0x20,11);
	memcpy(entry->name,filename,strlen(filename));
	return entry;
    }
    
    for(i=1;i<8 && filename[i] && filename[i]!='.';i++);

    entry_long=create_long_filename(s,filename);
   
    entry=array_get_next(&(s->directory));
    memset(entry->name,0x20,11);
    strncpy(entry->name,filename,i);
    
    if(filename[i]) {
	int len=strlen(filename);
        for(i=len;i>0 && filename[i-1]!='.';i--);
        if(i>0)
            memcpy(entry->extension,filename+i,(len-i>3?3:len-i));
    }

    /* upcase & remove unwanted characters */
    for(i=10;i>=0;i--) {
	if(i==10 || i==7) for(;i>1 && entry->name[i]==' ';i--);
	if(entry->name[i]<=' ' || entry->name[i]>0x7f
		|| strchr("*?<>|\":/\\[];,+='",entry->name[i]))
	    entry->name[i]='_';
        else if(entry->name[i]>='a' && entry->name[i]<='z')
            entry->name[i]+='A'-'a';
    }

    /* mangle duplicates */
    while(1) {
	direntry_t* entry1=array_get(&(s->directory),directory_start);
	int j;

	for(;entry1<entry;entry1++)
	    if(!(entry1->attributes&0xf) && !memcmp(entry1->name,entry->name,11))
		break; /* found dupe */
	if(entry1==entry) /* no dupe found */
	    break;

	/* use all 8 characters of name */	
	if(entry->name[7]==' ') {
	    int j;
	    for(j=6;j>0 && entry->name[j]==' ';j--)
		entry->name[j]='~';
	}

	/* increment number */
	for(j=7;j>0 && entry->name[j]=='9';j--)
	    entry->name[j]='0';
	if(j>0) {
	    if(entry->name[j]<'0' || entry->name[j]>'9')
	        entry->name[j]='0';
	    else
	        entry->name[j]++;
	}
    }

    /* calculate checksum; propagate to long name */
    if(entry_long) {
        uint8_t chksum=fat_chksum(entry);

	/* calculate anew, because realloc could have taken place */
	entry_long=array_get(&(s->directory),long_index);
	while(entry_long<entry
		    && entry_long->attributes==0xf) {
	    entry_long->reserved[1]=chksum;
	    entry_long++;
	}
    }

    return entry;
}

static int read_directory(BDRVVVFATState* s,const char* dirname,
		int first_cluster_of_parent)
{

    DIR* dir=opendir(dirname);
    struct dirent* entry;
    struct stat st;
    unsigned int start_of_directory=s->directory.next;
    /* mappings before first_file_mapping are directories */
    unsigned int first_directory_mapping=s->first_file_mapping;
    unsigned int first_cluster=(start_of_directory/0x10/s->sectors_per_cluster);
    int i;

    if(!dir)
	return -1;
    
    while((entry=readdir(dir))) {
	unsigned int length=strlen(dirname)+2+strlen(entry->d_name);
        char* buffer;
	direntry_t* direntry;
	int is_dot=!strcmp(entry->d_name,".");
	int is_dotdot=!strcmp(entry->d_name,"..");

	if(start_of_directory==1 && (is_dotdot || is_dot))
	    continue;
	
	buffer=(char*)malloc(length);
	snprintf(buffer,length,"%s/%s",dirname,entry->d_name);

	if(stat(buffer,&st)<0) {
	    free(buffer);
            continue;
	}

	/* create directory entry for this file */
	//fprintf(stderr,"create direntry at %d (cluster %d) for %s\n",s->directory.next,s->directory.next/0x10/s->sectors_per_cluster,entry->d_name);
	direntry=create_short_filename(s,start_of_directory,entry->d_name,is_dot||is_dotdot);
	direntry->attributes=(S_ISDIR(st.st_mode)?0x10:0x20);
	direntry->reserved[0]=direntry->reserved[1]=0;
	direntry->ctime=fat_datetime(st.st_ctime,1);
	direntry->cdate=fat_datetime(st.st_ctime,0);
	direntry->adate=fat_datetime(st.st_atime,0);
	direntry->begin_hi=0;
	direntry->mtime=fat_datetime(st.st_mtime,1);
	direntry->mdate=fat_datetime(st.st_mtime,0);
	if(is_dotdot)
	    direntry->begin=cpu_to_le16(first_cluster_of_parent);
	else if(is_dot)
	    direntry->begin=cpu_to_le16(first_cluster);
	else
	    direntry->begin=cpu_to_le16(0); /* do that later */
	direntry->size=cpu_to_le32(st.st_size);

	/* create mapping for this file */
	if(!is_dot && !is_dotdot) {
	    if(S_ISDIR(st.st_mode))
		s->current_mapping=(mapping_t*)array_insert(&(s->mapping),s->first_file_mapping++,1);
	    else
		s->current_mapping=(mapping_t*)array_get_next(&(s->mapping));
	    s->current_mapping->begin=0;
	    s->current_mapping->end=st.st_size;
	    s->current_mapping->offset=0;
	    s->current_mapping->filename=buffer;
	    s->current_mapping->dir_index=s->directory.next-1;
	    s->current_mapping->mode=(S_ISDIR(st.st_mode)?MODE_DIRECTORY:MODE_UNDEFINED);
	}
    }
    closedir(dir);

    /* fill with zeroes up to the end of the cluster */
    while(s->directory.next%(0x10*s->sectors_per_cluster)) {
	direntry_t* direntry=array_get_next(&(s->directory));
	memset(direntry,0,sizeof(direntry_t));
    }

    /* reserve next cluster also (for new files) */
    for(i=0;i<0x10*s->sectors_per_cluster;i++) {
	direntry_t* direntry=array_get_next(&(s->directory));
	memset(direntry,0,sizeof(direntry_t));
    }

    /* was it the first directory? */
    if(start_of_directory==1) {
	mapping_t* mapping=array_insert(&(s->mapping),0,1);
	mapping->filename=strdup(dirname);
	mapping->mode=MODE_DIRECTORY;
	mapping->begin=0;
	mapping->end=1;
	mapping->offset=0;
	mapping->dir_index=0xffffffff;
	s->sectors_of_root_directory=s->directory.next/0x10;
    }

    /* recurse directories */
    {
	int i;

	//fprintf(stderr,"iterating subdirectories of %s (first cluster %d): %d to %d\n",dirname,first_cluster,first_directory_mapping,last_directory_mapping);
	for(i=first_directory_mapping;i<s->first_file_mapping;i++) {
	    mapping_t* mapping=array_get(&(s->mapping),i);
	    direntry_t* direntry=array_get(&(s->directory),mapping->dir_index);
	    /* the directory to be read can add more subdirectories */
	    int last_dir_mapping=s->first_file_mapping;
	    
	    assert(mapping->mode==MODE_DIRECTORY);
	    /* first, tell the mapping where the directory will start */
	    mapping->begin=s->directory.next/0x10/s->sectors_per_cluster;
	    if(i>0) {
		mapping[-1].end=mapping->begin;
		assert(mapping[-1].begin<mapping->begin);
	    }
	    /* then tell the direntry */
	    direntry->begin=cpu_to_le16(mapping->begin);
	    //fprintf(stderr,"read directory %s (begin %d)\n",mapping->filename,(int)mapping->begin);
	    /* then read it */
	    if(read_directory(s,mapping->filename,first_cluster))
		return -1;

	    if(last_dir_mapping!=s->first_file_mapping) {
		int diff=s->first_file_mapping-last_dir_mapping;
		assert(diff>0);

		if(last_dir_mapping!=i+1) {
		    int count=last_dir_mapping-i-1;
		    int to=s->first_file_mapping-count;

		    assert(count>0);
		    assert(to>i+1);
		    array_roll(&(s->mapping),to,i+1,count);
		    /* could have changed due to realloc */
		    mapping=array_get(&(s->mapping),i);
		    mapping->end=mapping[1].begin;
		}
		i+=diff;
	    }
	}
    }

    return 0;
}

static int init_directory(BDRVVVFATState* s,const char* dirname)
{
    bootsector_t* bootsector=(bootsector_t*)&(s->first_sectors[(s->first_sectors_number-1)*0x200]);
    unsigned int i;
    unsigned int cluster;

    memset(&(s->first_sectors[0]),0,0x40*0x200);

    /* TODO: if FAT32, this is probably wrong */
    s->sectors_per_fat=0xec;
    s->sectors_per_cluster=0x10;
    s->cluster_size=s->sectors_per_cluster*0x200;
    s->cluster=malloc(s->cluster_size);
    
    array_init(&(s->mapping),sizeof(mapping_t));
    array_init(&(s->directory),sizeof(direntry_t));
    array_init(&(s->commit),sizeof(commit_t));

    /* add volume label */
    {
	direntry_t* entry=array_get_next(&(s->directory));
	entry->attributes=0x28; /* archive | volume label */
	snprintf(entry->name,11,"QEMU VVFAT");
    }

    if(read_directory(s,dirname,0))
	return -1;

    /* make sure that the number of directory entries is multiple of 0x200/0x20 (to fit the last sector exactly) */
    s->sectors_for_directory=s->directory.next/0x10;

    s->faked_sectors=s->first_sectors_number+s->sectors_per_fat*2+s->sectors_for_directory;
    s->cluster_count=(s->sector_count-s->faked_sectors)/s->sectors_per_cluster;

    /* Now build FAT, and write back information into directory */
    init_fat(s);

    cluster=s->sectors_for_directory/s->sectors_per_cluster;
    assert(s->sectors_for_directory%s->sectors_per_cluster==0);

    /* set the end of the last read directory */
    if(s->first_file_mapping>0) {
	mapping_t* mapping=array_get(&(s->mapping),s->first_file_mapping-1);
	mapping->end=cluster;
    }

    for(i=1;i<s->mapping.next;i++) {
	mapping_t* mapping=array_get(&(s->mapping),i);
	direntry_t* direntry=array_get(&(s->directory),mapping->dir_index);
	if(mapping->mode==MODE_DIRECTORY) {
	    /* directory */
	    int i;
#ifdef DEBUG
	    fprintf(stderr,"assert: %s %d < %d\n",mapping->filename,(int)mapping->begin,(int)mapping->end);
#endif
	    assert(mapping->begin<mapping->end);
	    for(i=mapping->begin;i<mapping->end-1;i++)
		fat_set(s,i,i+1);
	    fat_set(s,i,0x7fffffff);
	} else {
	    /* as the space is virtual, we can be sloppy about it */
	    unsigned int end_cluster=cluster+mapping->end/s->cluster_size;

	    if(end_cluster>=s->cluster_count) {
		fprintf(stderr,"Directory does not fit in FAT%d\n",s->fat_type);
		return -1;
	    }
	    mapping->begin=cluster;
	    mapping->mode=MODE_NORMAL;
	    mapping->offset=0;
	    direntry->size=cpu_to_le32(mapping->end);
	    if(direntry->size==0) {
		direntry->begin=0;
		mapping->end=cluster;
		continue;
	    }

	    direntry->begin=cpu_to_le16(cluster);
	    mapping->end=end_cluster+1;
	    for(;cluster<end_cluster;cluster++)
	        fat_set(s,cluster,cluster+1);
	    fat_set(s,cluster,0x7fffffff);
	    cluster++;
	}
    }

    s->current_mapping=0;

    bootsector->jump[0]=0xeb;
    bootsector->jump[1]=0x3e;
    bootsector->jump[2]=0x90;
    memcpy(bootsector->name,"QEMU    ",8);
    bootsector->sector_size=cpu_to_le16(0x200);
    bootsector->sectors_per_cluster=s->sectors_per_cluster;
    bootsector->reserved_sectors=cpu_to_le16(1);
    bootsector->number_of_fats=0x2; /* number of FATs */
    bootsector->root_entries=cpu_to_le16(s->sectors_of_root_directory*0x10);
    bootsector->zero=0;
    bootsector->media_type=(s->first_sectors_number==1?0xf0:0xf8); /* media descriptor */
    bootsector->sectors_per_fat=cpu_to_le16(s->sectors_per_fat);
    bootsector->sectors_per_track=cpu_to_le16(0x3f);
    bootsector->number_of_heads=cpu_to_le16(0x10);
    bootsector->hidden_sectors=cpu_to_le32(s->first_sectors_number==1?0:0x3f);
    /* TODO: if FAT32, adjust */
    bootsector->total_sectors=cpu_to_le32(s->sector_count);

    /* TODO: if FAT32, this is wrong */
    bootsector->u.fat16.drive_number=0x80; /* assume this is hda (TODO) */
    bootsector->u.fat16.current_head=0;
    bootsector->u.fat16.signature=0x29;
    bootsector->u.fat16.id=cpu_to_le32(0xfabe1afd);

    memcpy(bootsector->u.fat16.volume_label,"QEMU VVFAT ",11);
    memcpy(bootsector->fat_type,(s->fat_type==12?"FAT12   ":s->fat_type==16?"FAT16   ":"FAT32   "),8);
    bootsector->magic[0]=0x55; bootsector->magic[1]=0xaa;

    return 0;
}

static int vvfat_open(BlockDriverState *bs, const char* dirname)
{
    BDRVVVFATState *s = bs->opaque;
    int i;

    /* TODO: automatically determine which FAT type */
    s->fat_type=16;
    s->sector_count=0xec04f;

    s->current_cluster=0xffffffff;
    s->first_file_mapping=0;

    /* TODO: if simulating a floppy, this is 1, because there is no partition table */
    s->first_sectors_number=0x40;
    
    if (strstart(dirname, "fat:", &dirname)) {
        /* read only is the default for safety */
        bs->read_only = 1;
    } else if (strstart(dirname, "fatrw:", &dirname)) {
        /* development only for now */
        bs->read_only = 0;
    } else {
        return -1;
    }
    if(init_directory(s,dirname))
	return -1;

    if(s->first_sectors_number==0x40)
	init_mbr(s);

    /* TODO: this could be wrong for FAT32 */
    bs->cyls=1023; bs->heads=15; bs->secs=63;
    bs->total_sectors=bs->cyls*bs->heads*bs->secs;

    /* write support */
    for(i=0;i<3;i++)
	s->action[i]=WRITE_UNDEFINED;
    return 0;
}

static inline void vvfat_close_current_file(BDRVVVFATState *s)
{
    if(s->current_mapping) {
	s->current_mapping = 0;
	close(s->current_fd);
    }
}

/* mappings between index1 and index2-1 are supposed to be ordered
 * return value is the index of the last mapping for which end>cluster_num
 */
static inline int find_mapping_for_cluster_aux(BDRVVVFATState* s,int cluster_num,int index1,int index2)
{
    int index3=index1+1;
    //fprintf(stderr,"find_aux: cluster_num=%d, index1=%d,index2=%d\n",cluster_num,index1,index2);
    while(1) {
	mapping_t* mapping;
	index3=(index1+index2)/2;
	mapping=array_get(&(s->mapping),index3);
	//fprintf(stderr,"index3: %d = (%d+%d)/2, end: %d\n",index3,index1,index2,(int)mapping->end);
	if(mapping->end>cluster_num) {
	    assert(index2!=index3 || index2==0);
	    if(index2==index3)
		return index2;
	    index2=index3;
	} else {
	    if(index1==index3)
		return index2;
	    index1=index3;
	}
	assert(index1<=index2);
    }
}

static inline mapping_t* find_mapping_for_cluster(BDRVVVFATState* s,int cluster_num)
{
    int index=find_mapping_for_cluster_aux(s,cluster_num,0,s->mapping.next);
    mapping_t* mapping;
    if(index>=s->mapping.next)
	return 0;
    mapping=array_get(&(s->mapping),index);
    if(mapping->begin>cluster_num)
	return 0;
    return mapping;
}

static int open_file(BDRVVVFATState* s,mapping_t* mapping,int flags)
{
    if(!mapping)
	return -1;
    assert(flags==O_RDONLY || flags==O_RDWR);
    if(!s->current_mapping ||
	    strcmp(s->current_mapping->filename,mapping->filename) ||
	    (flags==O_RDWR && !s->current_fd_is_writable)) {
	/* open file */
	int fd = open(mapping->filename, flags | O_BINARY | O_LARGEFILE);
	if(fd<0)
	    return -1;
	vvfat_close_current_file(s);
	s->current_fd = fd;
	s->current_fd_is_writable = (flags==O_RDWR?-1:0);
	s->current_mapping = mapping;
    }
    return 0;
}

static inline int read_cluster(BDRVVVFATState *s,int cluster_num)
{
    if(s->current_cluster != cluster_num) {
	int result=0;
	off_t offset;
	if(!s->current_mapping
		|| s->current_mapping->begin>cluster_num
		|| s->current_mapping->end<=cluster_num) {
	    /* binary search of mappings for file */
	    mapping_t* mapping=find_mapping_for_cluster(s,cluster_num);
	    if(open_file(s,mapping,O_RDONLY))
		return -2;
	}

	offset=s->cluster_size*(cluster_num-s->current_mapping->begin+s->current_mapping->offset);
	if(lseek(s->current_fd, offset, SEEK_SET)!=offset)
	    return -3;
	result=read(s->current_fd,s->cluster,s->cluster_size);
	if(result<0) {
	    s->current_cluster = -1;
	    return -1;
	}
	s->current_cluster = cluster_num;
    }
    return 0;
}

static int vvfat_read(BlockDriverState *bs, int64_t sector_num, 
                    uint8_t *buf, int nb_sectors)
{
    BDRVVVFATState *s = bs->opaque;
    int i;

    //    fprintf(stderr,"vvfat_read: sector %d+%d\n",(int)sector_num,nb_sectors);

    for(i=0;i<nb_sectors;i++,sector_num++) {
	if(sector_num<s->faked_sectors) {
		if(sector_num<s->first_sectors_number)
		    memcpy(buf+i*0x200,&(s->first_sectors[sector_num*0x200]),0x200);
		else if(sector_num-s->first_sectors_number<s->sectors_per_fat)
			memcpy(buf+i*0x200,&(s->fat.pointer[(sector_num-s->first_sectors_number)*0x200]),0x200);
		else if(sector_num-s->first_sectors_number-s->sectors_per_fat<s->sectors_per_fat)
			memcpy(buf+i*0x200,&(s->fat.pointer[(sector_num-s->first_sectors_number-s->sectors_per_fat)*0x200]),0x200);
		else if(sector_num-s->first_sectors_number-s->sectors_per_fat*2<s->sectors_for_directory)
			memcpy(buf+i*0x200,&(s->directory.pointer[(sector_num-s->first_sectors_number-s->sectors_per_fat*2)*0x200]),0x200);
	} else {
            uint32_t sector=sector_num-s->first_sectors_number-s->sectors_per_fat*2,
	        sector_offset_in_cluster=(sector%s->sectors_per_cluster),
                cluster_num=sector/s->sectors_per_cluster;
		if(read_cluster(s, cluster_num) != 0) {
			//fprintf(stderr,"failed to read cluster %d\n",(int)cluster_num);
			// TODO: strict: return -1;
			memset(buf+i*0x200,0,0x200);
			continue;
		}
		memcpy(buf+i*0x200,s->cluster+sector_offset_in_cluster*0x200,0x200);
	}
    }
    return 0;
}

static void print_direntry(direntry_t* direntry)
{
    if(!direntry)
	return;
    if(direntry->attributes==0xf) {
	unsigned char* c=(unsigned char*)direntry;
	int i;
	for(i=1;i<11 && c[i] && c[i]!=0xff;i+=2)
	    fputc(c[i],stderr);
	for(i=14;i<26 && c[i] && c[i]!=0xff;i+=2)
	    fputc(c[i],stderr);
	for(i=28;i<32 && c[i] && c[i]!=0xff;i+=2)
	    fputc(c[i],stderr);
	fputc('\n',stderr);
    } else {
	int i;
	for(i=0;i<11;i++)
	    fputc(direntry->name[i],stderr);
	fprintf(stderr,"attributes=0x%02x begin=%d size=%d\n",
		direntry->attributes,
		direntry->begin,direntry->size);
    }
}

static void print_changed_sector(BlockDriverState *bs,int64_t sector_num,const uint8_t *buf)
{
    BDRVVVFATState *s = bs->opaque;

    if(sector_num<s->first_sectors_number)
	return;
    if(sector_num<s->first_sectors_number+s->sectors_per_fat*2) {
	int first=((sector_num-s->first_sectors_number)%s->sectors_per_fat);
	int first_fat_entry=first*0x200/2;
	int i;

	fprintf(stderr, "fat:\n");
	for(i=0;i<0x200;i+=2) {
	    uint16_t* f=array_get(&(s->fat),first_fat_entry+i/2);
	    if(memcmp(buf+i,f,2))
		fprintf(stderr,"%d(%d->%d) ",first_fat_entry+i/2,*f,*(uint16_t*)(buf+i));
	}
	fprintf(stderr, "\n");
    } else if(sector_num<s->faked_sectors) {
	direntry_t* d=(direntry_t*)buf;
	int i;
	fprintf(stderr, "directory:\n");
	for(i=0;i<0x200/sizeof(direntry_t);i++) {
	    direntry_t* d_old=(direntry_t*)(s->directory.pointer+0x200*(sector_num-s->first_sectors_number-s->sectors_per_fat*2)+i*sizeof(direntry_t));
	    if(memcmp(d+i,d_old,sizeof(direntry_t))) {
		fprintf(stderr, "old: "); print_direntry(d_old);
		fprintf(stderr, "new: "); print_direntry(d+i);
		fprintf(stderr, "\n");
	    }
	}
    } else {
	int sec=(sector_num-s->first_sectors_number-2*s->sectors_per_fat);
	fprintf(stderr, "\tcluster: %d(+%d sectors)\n",sec/s->sectors_per_cluster,sec%s->sectors_per_cluster);
    }
}

char direntry_is_free(const direntry_t* direntry)
{
    return direntry->name[0]==0 || direntry->name[0]==0xe5;
}

/* TODO: use this everywhere */
static inline uint32_t begin_of_direntry(direntry_t* direntry)
{
    return le16_to_cpu(direntry->begin)|(le16_to_cpu(direntry->begin_hi)<<16);
}

int consistency_check1(BDRVVVFATState *s) {
    /* check all mappings */
    int i;
    for(i=0;i<s->mapping.next;i++) {
	mapping_t* mapping=array_get(&(s->mapping),i);
	int j;
	for(j=mapping->begin;j<mapping->end-1;j++)
	    assert(fat_get(s,j)==j+1);
	assert(fat_get(s,j)==(0x7fffffff&s->max_fat_value));
    }
    return 0;
}

int consistency_check2(BDRVVVFATState *s) {
    /* check fat entries: consecutive fat entries should be mapped in one mapping */
    int i;
    /* TODO: i=0 (mappings for direntries have to be sorted) */
    for(i=s->sectors_for_directory/s->sectors_per_cluster;i<s->fat.next-1;i++) {
	uint32_t j=fat_get(s,i);
	if(j!=i+1 && j!=0 && !fat_eof(s,j)) {
	    mapping_t* mapping=find_mapping_for_cluster(s,i+1);
	    assert(mapping->begin==i+1);
	}
    }
    return 0;
}

int consistency_check3(BDRVVVFATState *s) {
    /* check that for each file there is exactly one mapping per cluster */
    int i,count_non_next=0;
    for(i=0;i<s->mapping.next;i++) {
	mapping_t* mapping=array_get(&(s->mapping),i);
	/* TODO: when directories are correctly adapted, add them here */
	assert(mapping->begin<mapping->end);
	if(mapping->mode==MODE_NORMAL) {
	    int j,count=0,count_next=0;
	    for(j=0;j<s->mapping.next;j++) {
		mapping_t* other=array_get(&(s->mapping),j);
		if(mapping->begin<other->end&&mapping->end>other->begin)
		    count++;
		if(mapping->end==other->begin)
		    count_next++;
	    }
	    assert(count==1); /* no overlapping mappings */
	    assert(count_next==1 || count_next==0); /* every mapping except the last one has a successor */
	    if(!count_next)
		count_non_next++;
	}
    }
    assert(count_non_next==1); /* only one last mapping */
    return 0;
}

static inline commit_t* commit_get_next(BDRVVVFATState* s)
{
    commit_t* commit=array_get_next(&(s->commit));
    if((commit->buf=malloc(s->cluster_size))==0) {
	/* out of memory */
	s->commit.next--;
	return 0;
    }
    return commit;
}

int commit_remove(BDRVVVFATState* s,commit_t* commit)
{
    int index=commit-(commit_t*)s->commit.pointer;
    free(commit->buf);
    if(array_roll(&(s->commit),s->commit.next-1,index,1))
	return -1;
    s->commit.next--;
    return 0;
}

/* TODO: the plan for write support:
 *
 * it seems that the direntries are written first, then the data is committed
 * to the free sectors, then fat 1 is updated, then fat2.
 *
 * Plan: when sectors are written, do the following:
 *
 * - if they are in a directory, check if the entry has changed. if yes,
 *   look what has changed (different strategies for name, begin & size).
 *
 *   if it is new (old entry is only 0's or has E5 at the start), create it,
 *   and also create mapping, but in a special mode "undefined" (TODO),
 *   because we cannot know which clusters belong to it yet.
 *
 *   if it is zeroed, or has E5 at the start, look if has just moved. If yes,
 *   copy the entry to the new position. If no, delete the file.
 *
 * - if they are in data, and the cluster is undefined, add it to the commit
 *   list. if the cluster is defined (find_mapping), then write it into the
 *   corresponding file.
 *
 *   If it is the last cluster (TODO: add a function
 *   fat_get(s,cluster); ), make sure not to write a complete cluster_size.
 *
 *   If the data is in current_cluster, update s->cluster.
 *
 * - if they are in fat 1, update mappings, look in the commit list
 *   (assertions!) and if the cluster is now known (or changed from undefined
 *   state to defined state, like when begin or size changed in a direntry),
 *   write it.
 *
 * - if they are in fat 2, make sure they match with current fat.
 *
 */

void mapping_modify_from_direntry(BDRVVVFATState* s,mapping_t* mapping,direntry_t* direntry)
{
    int begin=le16_to_cpu(direntry->begin),
        end=begin+le32_to_cpu(direntry->size)/s->cluster_size+1,
	i;
    mapping->mode = MODE_MODIFIED;
    /* TODO: what if begin==0 (size==0)? */
    mapping->begin = begin;
    /* TODO: why not just mapping->end = begin+1 ? */
    for(i=begin+1;i<end && (fat_get(s,i)==0 || fat_get(s,i)==i+1);i++);
    mapping->end = i;
}

mapping_t* find_mapping_for_direntry(BDRVVVFATState* s,direntry_t* direntry)
{
    int i;
    int dir_index=direntry-((direntry_t*)s->directory.pointer);
    
    /* TODO: support allocation for new clusters for directories (new/larger directory */
    assert(dir_index<0x200/0x20*s->sectors_for_directory);
    
    for(i=0;i<s->mapping.next;i++) {
	mapping_t* mapping=array_get(&(s->mapping),i);
	if(mapping->dir_index==dir_index && mapping->offset==0 &&
		mapping->mode!=MODE_UNDEFINED)
	    return mapping;
    }
    return 0;
}

static inline uint32_t sector2cluster(BDRVVVFATState* s,off_t sector_num)
{
    return (sector_num-s->first_sectors_number-2*s->sectors_per_fat)/s->sectors_per_cluster;
}

static inline uint32_t sector_offset_in_cluster(BDRVVVFATState* s,off_t sector_num)
{
    return (sector_num-s->first_sectors_number-2*s->sectors_per_fat)%s->sectors_per_cluster;
}

static commit_t* get_commit_for_cluster(BDRVVVFATState* s,uint32_t cluster_num)
{
    int i;
    for(i=0;i<s->commit.next;i++) {
	commit_t* commit=array_get(&(s->commit),i);
	if(commit->cluster_num==cluster_num)
	    return commit;
    }
    return 0;
}

static inline commit_t* create_or_get_commit_for_sector(BDRVVVFATState* s,off_t sector_num)
{
    int i;
    commit_t* commit;
    uint32_t cluster_num=sector2cluster(s,sector_num);

    for(i=0;i<s->commit.next;i++) {
	commit=array_get(&(s->commit),i);
	if(commit->cluster_num==cluster_num)
	    return commit;
    }

    commit=commit_get_next(s);
    commit->cluster_num=cluster_num;
    /* we can ignore read errors here */
    read_cluster(s,cluster_num);
    memcpy(commit->buf,s->cluster,s->cluster_size);
    return commit;
}

static direntry_t* get_direntry_for_mapping(BDRVVVFATState* s,mapping_t* mapping)
{
    if(mapping->mode==MODE_UNDEFINED)
	return 0;
    if(mapping->dir_index>=0x200/0x20*s->sectors_for_directory)
	return 0;
    return (direntry_t*)(s->directory.pointer+sizeof(direntry_t)*mapping->dir_index);
}

static void print_mappings(BDRVVVFATState* s)
{
    int i;
    fprintf(stderr,"mapping:\n");
    for(i=0;i<s->mapping.next;i++) {
	mapping_t* m=array_get(&(s->mapping),i);
	direntry_t* d=get_direntry_for_mapping(s,m);
	fprintf(stderr,"%02d %d-%d (%d) %s (dir: %d)",i,(int)m->begin,(int)m->end,(int)m->offset,m->filename,m->dir_index);
	print_direntry(d);
	fprintf(stderr,"\n");
    }
    fprintf(stderr,"mappings end.\n");
}

/* TODO: statify all functions */

/* This function is only meant for file contents.
 * It will return an error if used for other sectors. */
static int write_cluster(BDRVVVFATState* s,uint32_t cluster_num,const uint8_t* buf)
{
    /* sector_offset is the sector_num relative to the first cluster */
    mapping_t* mapping=find_mapping_for_cluster(s,cluster_num);
    direntry_t* direntry;
    int next_cluster,write_size,last_cluster;
    off_t offset;

    /* if this cluster is free, return error */
    next_cluster=fat_get(s,cluster_num);
    if(next_cluster<2)
	return -1;
    
    /* TODO: MODE_DIRECTORY */
    if(!mapping || mapping->mode==MODE_UNDEFINED || mapping->mode==MODE_DIRECTORY)
	return -1;
    direntry=get_direntry_for_mapping(s,mapping);
    if(!direntry)
	return -2;

    /* get size to write */
    last_cluster=fat_eof(s,next_cluster);
    write_size=!last_cluster?s->cluster_size:
	(le32_to_cpu(direntry->size)%s->cluster_size);
    if(write_size<=0)
	return 0;
    //fprintf(stderr,"next_cluster: %d (%d), write_size: %d, %d, %d\n",next_cluster,s->max_fat_value-8,write_size,direntry->size,s->cluster_size);

    if(open_file(s,mapping,O_RDWR))
	return -4;
   
    offset=(cluster_num-mapping->begin+mapping->offset)*s->cluster_size;
    if(lseek(s->current_fd,offset,SEEK_SET)!=offset)
	return -3;
    if(write(s->current_fd,buf,write_size)!=write_size) {
	lseek(s->current_fd,0,SEEK_END);
	vvfat_close_current_file(s);
	return -2;
    }

    /* seek to end of file, so it doesn't get truncated */
    if(!last_cluster)
	lseek(s->current_fd,0,SEEK_END);
    else {
	ftruncate(s->current_fd,le32_to_cpu(direntry->size));
	vvfat_close_current_file(s);
    }

    /* update s->cluster if necessary */
    if(cluster_num==s->current_cluster && s->cluster!=buf)
	memcpy(s->cluster,buf,s->cluster_size);

    return 0;
}

/* this function returns !=0 on error */
int mapping_is_consistent(BDRVVVFATState* s,mapping_t* mapping)
{
    direntry_t* direntry=get_direntry_for_mapping(s,mapping);
    uint32_t cluster_count=0;
    int commit_count=0; /* number of commits for this file (we also write incomplete files; think "append") */
    //fprintf(stderr,"check direntry for %s\n",mapping->filename);
    while(mapping) {
	int i;
	assert(mapping->begin<mapping->end);
	for(i=mapping->begin;i<mapping->end-1;i++) {
	    if(i<=0 || fat_get(s,i)!=i+1) {
		/*fprintf(stderr,"the fat mapping of %d is not %d, but %d\n",
			i,i+1,fat_get(s,i));*/
		return -1;
	    }
	    if(get_commit_for_cluster(s,i))
		commit_count++;
	}
	if(get_commit_for_cluster(s,i))
	    commit_count++;

	cluster_count+=mapping->end-mapping->begin;
	
	i=fat_get(s,mapping->end-1);
	if(fat_eof(s,i))
	    break;

	mapping=find_mapping_for_cluster(s,i);
	if(!mapping) {
	    //fprintf(stderr,"No mapping found for %d\n",i);
	    print_mappings(s);
	    return -2;
	}
    }

    if(cluster_count!=(le32_to_cpu(direntry->size)+s->cluster_size-1)/s->cluster_size) {
	//fprintf(stderr,"cluster_count is %d, but size is %d\n",cluster_count,le32_to_cpu(direntry->size));
	return -3;
    }

    if(commit_count==0)
	return -4;

    //fprintf(stderr,"okay\n");
    return 0;
}

/* TODO: remember what comes third, and what's first in this OS:
 * FAT, direntry or data.
 * If the last written sector is either last in cluster or sector_num+nb_sectors-1,
 * 	- commit every cluster for this file if mapping_is_consistent()==0
 * 	- if the last written sector is first_action, and last_action=third_action, clear commit
 */

static int commit_cluster_aux(BDRVVVFATState* s,commit_t* commit)
{
    int result=write_cluster(s,commit->cluster_num,commit->buf);
    return result;
}


static int commit_cluster(BDRVVVFATState* s,uint32_t cluster_num)
{
    commit_t* commit;

    /* commit the sectors of this cluster */
    commit=get_commit_for_cluster(s,cluster_num);
    if(commit)
	return commit_cluster_aux(s,commit);
    return 0;
}

/* this function checks the consistency for the direntry which belongs to
 * the mapping. if everything is found consistent, the data is committed.
 * this returns 0 if no error occurred (even if inconsistencies were found) */
static inline int commit_data_if_consistent(BDRVVVFATState* s,mapping_t* mapping,write_action_t action)
{
    direntry_t* direntry;
    
    if(!mapping)
	return 0;

    //fprintf(stderr,"7\n");
#define d(x) fprintf(stderr,#x "\n")
    direntry=get_direntry_for_mapping(s,mapping);

    //d(8);

    assert(action==WRITE_FAT || action==WRITE_DIRENTRY || action==WRITE_DATA);

    //d(9);
    //fprintf(stderr,"mapping: 0x%x s=0x%x\n",(uint32_t)mapping,(uint32_t)s);
    /*fprintf(stderr,"commit? file=%s, action=%s\n",
	    mapping->filename,action==WRITE_FAT?"fat":action==WRITE_DIRENTRY?"direntry":"data");*/

    //d(10);
    if(s->action[2]==WRITE_UNDEFINED) {
	int i;
	for(i=2;i>0 && s->action[i-1]==WRITE_UNDEFINED;i--);
	if(i>0 && action!=s->action[i-1])
	    s->action[i]=action;
	assert(i<2 || s->action[0]!=s->action[2]);
    }
    //d(11);
    
    if(mapping_is_consistent(s,mapping)==0) {
	uint32_t cluster_num=begin_of_direntry(direntry);
	off_t remaining_bytes=le32_to_cpu(direntry->size);
	//fprintf(stderr,"the data for %s was found consistent\n",mapping->filename);
	while(remaining_bytes>0) {
	    commit_t* commit=get_commit_for_cluster(s,cluster_num);
	    if(!commit)
		continue;
		
	    //fprintf(stderr,"commit_cluster %d (%d), remaining: %d\n",cluster_num,s->max_fat_value-15,(int)remaining_bytes);
	    assert(cluster_num>1);
	    assert(cluster_num<s->max_fat_value-15);
	    if(commit_cluster(s,cluster_num)) {
		fprintf(stderr,"error committing cluster %d\n",cluster_num);
		return -1;
	    }
	    cluster_num=fat_get(s,cluster_num);
	    remaining_bytes-=s->cluster_size;
	    /* TODO: if(action==s->action[2]) {
		commit_t* commit=get_commit_for_cluster(s,cluster_num);
		commit_remove(s,commit);
	    } */
	}
    }
    //print_mappings(s);
    //fprintf(stderr,"finish vvfat_write\n");
    return 0;
}

static int vvfat_write(BlockDriverState *bs, int64_t sector_num, 
                    const uint8_t *buf, int nb_sectors)
{
    BDRVVVFATState *s = bs->opaque;
    int i;

    /* fprintf(stderr,"vvfat_write %d+%d (%s)\n",(int)sector_num,nb_sectors,
		    (sector_num>=s->faked_sectors?"data":
		     (sector_num>=s->first_sectors_number+2*s->sectors_per_fat?"directory":
		      (sector_num>=s->first_sectors_number+s->sectors_per_fat?"fat 2":
		       (sector_num>=s->first_sectors_number?"fat 1":"boot sector"))))); */

    for(i=0;i<nb_sectors;i++,sector_num++,buf+=0x200) {
	print_changed_sector(bs,sector_num,buf);

	if(sector_num<s->first_sectors_number) {
	    /* change the bootsector or partition table? no! */
	    return -1;
	} else if(sector_num<s->first_sectors_number+s->sectors_per_fat) {
	    /* FAT 1 */
	    int fat_entries_per_cluster=s->cluster_size*8/s->fat_type;
	    int first_cluster=(sector_num-s->first_sectors_number)*fat_entries_per_cluster,i;
	    mapping_t* mapping=0;

	    /* write back */
	    memcpy(s->fat.pointer+0x200*(sector_num-s->first_sectors_number),
		    buf,0x200);

	    /* for each changed FAT entry, */
	    for(i=0;i<fat_entries_per_cluster;i++) {
		int new_value;
		
		/* TODO: MODE_DIRENTRY */
		if(first_cluster+i<s->sectors_for_directory/s->sectors_per_cluster)
		    continue;

		new_value=fat_get(s,first_cluster+i);

		/* check the current fat entry */
		if(new_value<2 || (new_value>=s->max_fat_value-0xf && !fat_eof(s,new_value))) {
		    /* free, reserved or bad cluster */
		    mapping=find_mapping_for_cluster(s,first_cluster+i);
		    //assert(!mapping || mapping->mode==MODE_DELETED);
		    if(mapping && mapping->mode==MODE_DELETED &&
			    first_cluster+i+1==mapping->end)
			array_remove(&(s->mapping),mapping-(mapping_t*)s->mapping.pointer);
		    mapping=0;
		    continue;
		}

		/* get the mapping for the current entry */
		if(!mapping || mapping->begin>new_value || mapping->end<=new_value) {
		    mapping=find_mapping_for_cluster(s,first_cluster+i);
		}

		print_mappings(s);
		fprintf(stderr,"fat_get(%d)=%d\n",first_cluster+i,new_value);
		/* TODO: what if there's no mapping? this is valid. */
		/* TODO: refactor the rest of this clause so it can be called when the direntry changes, too */
		assert(mapping);

		if(new_value>1 && new_value<s->max_fat_value-0xf) {
		    /* the cluster new_value points to is valid */

		    if(first_cluster+i+1==new_value) {
			/* consecutive cluster */
			if(mapping->end<=new_value)
			    mapping->end=new_value+1;
		    } else {
			mapping_t* next_mapping;
			
			/* the current mapping ends here */
			mapping->end=first_cluster+i+1;
			
			/* the next mapping */
			next_mapping=find_mapping_for_cluster(s,new_value);
			if(next_mapping) {
			    assert(mapping!=next_mapping);
			    /* assert next mapping's filename is the same */
			    assert(next_mapping->filename==mapping->filename);
			    assert(next_mapping->dir_index==mapping->dir_index);
			    /* assert next mapping is MODIFIED or UNDEFINED */
			    assert(next_mapping->mode==MODE_MODIFIED || next_mapping->mode==MODE_UNDEFINED);
			} else {
			    int index=find_mapping_for_cluster_aux(s,new_value,0,s->mapping.next);
			    next_mapping=array_insert(&(s->mapping),index,1);
			    next_mapping->filename=mapping->filename;
			    next_mapping->dir_index=mapping->dir_index;
			    next_mapping->mode=MODE_MODIFIED;
			    next_mapping->begin=0;
			}
			/* adjust offset of next mapping */
			next_mapping->offset=mapping->offset+mapping->end-mapping->begin;
			/* set begin and possible end */
			if(next_mapping->begin!=new_value) {
			    next_mapping->begin=new_value;
			    next_mapping->end=new_value+1;
			}
			if(commit_data_if_consistent(s,mapping,WRITE_FAT))
			    return -4;
			mapping=0;
		    }
		} else if(fat_eof(s,new_value)) {
		    /* the last cluster of the file */
		    mapping->end=first_cluster+i+1;
		    if(commit_data_if_consistent(s,mapping,WRITE_FAT))
			return -4;
		    mapping=0;
		}
	    }
	} else if(sector_num<s->first_sectors_number+2*s->sectors_per_fat) {
	    /* FAT 2: check if it is the same as FAT 1 */
	    if(memcmp(array_get(&(s->fat),sector_num-s->first_sectors_number),buf,0x200))
		return -1; /* mismatch */
	} else if(sector_num<s->faked_sectors) {
	    /* direntry */
	    /* - if they are in a directory, check if the entry has changed.
	     *   if yes, look what has changed (different strategies for name,
	     *   begin & size).
	     *
	     *   if it is new (old entry is only 0's or has E5 at the start),
	     *   create it, and also create mapping, but in a special mode
	     *   "undefined", because we cannot know which clusters belong
	     *   to it yet.
	     *
	     *   if it is zeroed, or has E5 at the start, look if has just
	     *   moved. If yes, copy the entry to the new position. If no,
	     *   delete the file.
	     */
	    mapping_t* dir_mapping=find_mapping_for_cluster(s,sector2cluster(s,sector_num));
	    direntry_t *original=array_get(&(s->directory),sector_num-s->first_sectors_number-2*s->sectors_per_fat);
	    direntry_t *new_=(direntry_t*)buf;
	    int first_dir_index=(sector_num-s->first_sectors_number-2*s->sectors_per_fat)*0x200/0x20;
	    int j;

#if 0
	    fprintf(stderr,"direntry: consistency check\n");

	    if(s->commit.next==0) {
		consistency_check1(s);
		consistency_check2(s);
		consistency_check3(s);
	    }
#endif

	    assert(sizeof(direntry_t)==0x20);

	    for(j=0;j<0x200/0x20;j++) {
		//fprintf(stderr,"compare direntry %d: 0x%x,0x%x\n",j,(uint32_t)original+j,(uint32_t)new_+j);
		if(memcmp(original+j,new_+j,sizeof(direntry_t))) {
		    //fprintf(stderr,"different\n");
		    /* TODO: in create_short_filename, 0xe5->0x05 is not yet handled! */
		    if(direntry_is_free(original+j)) {
			mapping_t* mapping;
			char buffer[4096];
			int fd,i;

			if(new_[j].attributes==0xf)
			    continue; /* long entry */

			print_mappings(s);
			//fprintf(stderr,"sector: %d cluster: %d\n",(int)sector_num,(int)sector2cluster(s,sector_num));

			/* construct absolute path */
			strncpy(buffer,dir_mapping->filename,4096);
			i=strlen(buffer);
			if(i+2>=4096)
				return -1;
			buffer[i]='/';
			if(long2unix_name(buffer+i+1,4096-i-1,new_+j))
				return -2;

			/* new file/directory */
			if(new_[j].attributes&0x10) {
#ifdef _WIN32
#define SEVENFIVEFIVE
#else
#define SEVENFIVEFIVE ,0755
#endif
			    if(mkdir(buffer SEVENFIVEFIVE))
				return -3;
			    /* TODO: map direntry.begin as directory, together with new array_t direntries */
			    assert(0);
			} else {
			    fd=open(buffer,O_CREAT|O_EXCL,0644);
			    if(!fd)
				return -3;
			    close(fd);
			}

			/* create mapping */
			i=find_mapping_for_cluster_aux(s,begin_of_direntry(new_+j),0,s->mapping.next);
			mapping=array_insert(&(s->mapping),i,1);
			mapping->filename=strdup(buffer);
			mapping->offset=0;
			/* back pointer to direntry */
			mapping->dir_index=first_dir_index+j;
			/* set mode to modified */
			mapping->mode=MODE_MODIFIED;
			/* set begin to direntry.begin */
			mapping->begin=begin_of_direntry(new_+j);
			/* set end to begin+1 */
			mapping->end=mapping->begin+1;
			/* commit file contents */
			if(commit_data_if_consistent(s,mapping,WRITE_DIRENTRY)) {
			    fprintf(stderr,"error committing file contents for new file %s!\n",buffer);
			    return -4;
			}
		    } else if(direntry_is_free(new_+j)) {
			assert(0);
			/* TODO: delete file */
			/* TODO: write direntry */
			/* TODO: modify mapping: set mode=deleted */
		    } else {
			/* modified file */
			mapping_t* mapping=0;
			/* if direntry.begin has changed,
			 * set mode to modified,
			 * adapt begin,
			 * adapt end */
			/* TODO: handle rename */
			assert(!memcmp(new_[j].name,original[j].name,11));
			//fprintf(stderr,"1\n");
			if(new_[j].begin!=original[j].begin || new_[j].size/s->cluster_size!=original[j].size/s->cluster_size) {
			//fprintf(stderr,"2\n");
			    mapping = find_mapping_for_direntry(s,original+j);
			//fprintf(stderr,"3\n");
			    if(!mapping) /* this should never happen! */
				return -2;
			    mapping_modify_from_direntry(s,mapping,new_+j);
			    //fprintf(stderr,"4\n");
			    if(commit_data_if_consistent(s,mapping,WRITE_DIRENTRY)) {
				fprintf(stderr,"big error\n");
				return -4;
			    }
			}
			/* TODO: handle modified times and other attributes */

			//fprintf(stderr,"5: mapping=0x%x, s=0x%x, s->mapping.pointer=0x%x\n",(uint32_t)mapping,(uint32_t)s,(uint32_t)s->mapping.pointer);
			//fprintf(stderr,"6\n");
		    }
		}
	    }
	    /* write back direntries */
	    memcpy(original,new_,0x200);
	} else {
	    /* data */
	    off_t sector=sector_num-s->first_sectors_number-2*s->sectors_per_fat;
	    off_t cluster=sector/s->sectors_per_cluster;
	    mapping_t* mapping=find_mapping_for_cluster(s,cluster);
	    if(mapping && mapping->mode==MODE_DELETED)
		return -3; /* this is an error: no writes to these clusters before committed */
	    {
		/* as of yet, undefined: put into commits */
		commit_t* commit=create_or_get_commit_for_sector(s,sector_num);

		if(!commit)
		    return -1; /* out of memory */
		memcpy(commit->buf+0x200*sector_offset_in_cluster(s,sector_num),buf,0x200);

		//fprintf(stderr,"mapping: 0x%x\n",(uint32_t)mapping);
		if(commit_data_if_consistent(s,mapping,WRITE_DATA))
		    return -4;
	    }
	}
    }
    return 0;
}

static void vvfat_close(BlockDriverState *bs)
{
    BDRVVVFATState *s = bs->opaque;

    vvfat_close_current_file(s);
    array_free(&(s->fat));
    array_free(&(s->directory));
    array_free(&(s->mapping));
    if(s->cluster)
        free(s->cluster);
}

BlockDriver bdrv_vvfat = {
    "vvfat",
    sizeof(BDRVVVFATState),
    vvfat_probe,
    vvfat_open,
    vvfat_read,
    vvfat_write,
    vvfat_close,
};


