'Getting fsync error while writing in /dev/mtd

I have a NAND based device. I'm trying to write a character buffer into /mtd/dev5. I was able to erase but it was not able to write. It is giving fsync error. This is my program

int main()
{
    mtd_info_t mtd_info;          
    erase_info_t ei;              
    int i;

    unsigned char data[100];
    for(int i=0;i<100;i++)
    {
        data[i]='1';
    }
    for(int i=0;i<sizeof(data);i++)
    {
        printf("%c ",data[i]);
    }
    printf("\n");
    unsigned char read_buf[100] = {0};                
    printf("starting printing start \n");
    for(int i=0;i<sizeof(read_buf);i++)
    {
        printf("%c ",read_buf[i]);
    }
    printf("printing done\n");
    printf("\n");
    int fd = open("/dev/mtd5", O_RDWR | O_SYNC,0666); 
    printf("value of fd is %d\n",fd);
    if(fd<0) printf("error in opening partition\n");
    ioctl(fd, MEMGETINFO, &mtd_info);   

    printf("MTD Type: %x\nMTD total size: %x bytes\nMTD erase size: %x bytes\n",
         mtd_info.type, mtd_info.size, mtd_info.erasesize);

    ei.length = mtd_info.erasesize;   
    for(ei.start = 0; ei.start < mtd_info.size; ei.start += ei.length)
    {
        ioctl(fd, MEMUNLOCK, &ei);
        ioctl(fd, MEMERASE, &ei);
    }

    int lres=lseek(fd, 0, SEEK_SET);      
    int wres=write(fd, data, sizeof(data)); 
    printf("lres %d wres %d\n",lres,wres);
    if(fsync(fd)<0) printf("error in fsync\n");

    close(fd);
    return 0;
}

Output:

value of fd is 3
MTD Type: 4
MTD total size: 40000 bytes
MTD erase size: 20000 bytes
 MTD write size: b6f484fcbytes
lres 0 wres 2048
error in fsync Invalid argument
c++


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source