我有一个基于 NAND 的设备。我正在尝试将字符缓冲区写入/mtd/dev5。我能够擦除但无法写入。它给出了 fsync 错误。 这是我的程序
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;
}
输出:
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
参见 https://man.archlinux.org/man/fsync.2
EINVAL
fd 绑定到不支持同步的特殊文件(例如管道、FIFO 或套接字)。
如果你查看源代码,你会发现
fsync
没有函数: https://github.com/torvalds/linux/blob/0ea923f443350c8c5cca6eef5b748d52b903f46c/drivers/mtd/mtdchar.c#L1401-L1417