read()
SATAHDDからベーシックを作りたい/dev/sdd
。Awrite()
は機能しているようです。また、フラグなしで動作しread()
ます。ブロックサイズに合わせる必要があることを読みました。だから私はこれを使ってブロックサイズを取得しました:write()
O_DIRECT
root$ blockdev --getsize /dev/sdd
488397168
root$ blockdev --getsize64 /dev/sdd
250059350016
root$ python -c "print 250059350016.0/488397168"
512.0
ご覧のとおり、私にはルートがあります。HDDはPCIeSATAカードを介して接続されてlspci -vv
おり、基本的なahci(drivers/ata/ahci.c
)ドライバーを使用していることがわかります。私は64ビットのPowerArchitectureで3.2.0Linuxカーネルを使用しています。
これが私のコードです:
#define _GNU_SOURCE
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
int main() {
int r;
char *buffer, *tmp_buffer;
// alloc more than necesarry to align the real buffer
tmp_buffer = malloc(2*512*sizeof(char));
long align = (unsigned long)tmp_buffer%512;
printf("tmp_buffer is at: %x \% 512 = %d\n",tmp_buffer,align);
buffer = tmp_buffer+(512-align);
printf("buffer is at: %x \% 512 = %d\n",buffer,(unsigned long)buffer%512);
memset(buffer,0,sizeof(512));
// OPEN
int fd = open("/dev/sdd",O_DIRECT | O_RDWR | O_SYNC);
if(fd!=3) printf("fd = %d\n",fd);
// READ
printf("try to read and then dump buffer:\n");
r = read(fd,buffer,sizeof(512));
if(r == -1) printf("Error: %s\n",strerror(errno));
else {
// DUMP BUFFER
int i;
for(i=0; i<sizeof(512); i++)
printf("%c",buffer[i]);
}
printf("\n");
return 0;
}
出力は次のとおりです。
tmp_buffer is at: 1cc80010 % 512 = 16
buffer is at: 1cc80200 % 512 = 0
try to read and then dump buffer:
Error: Invalid argument
編集:Brett Haleの回答で提案されているように、ソースを更新しました。残念ながら、まだエラーが発生します。ブロックサイズを見つける私の方法は大丈夫ですか?私は正しい位置合わせをしましたか?
読んでくれてありがとう、
ファビアン