1

I'm totally new to the Linux Kernel, so I probably mix things up. But any advice will help me ;)

I have a SATA HDD connected via a PCIe SATA Card and I try to use read and write like on a block device. I also want the data power blackout save on the HDD - not cached. And in the end I have to analyse how much time I loose in each linux stack layer. But one step at a time.

At the moment I try to open the device with *O_DIRECT*. But I don't really understand where I can find the device. It shows up as /dev/sdd and I created one partition /dev/sdd1.

  • open and read on the partition /dev/sdd1 works. write fails with *O_DIRECT* (But I'm sure I have the right blocksize)
  • open read and write called on /dev/sdd fails completely.
  • Is there maybe another file in /dev/ which represents my device on the block layer?
  • What are my mistakes and wrong assumptions?

This is my current test code

int main() {
    int w,r,s;
    char buffer[512] = "test string mit 512 byte";

    printf("test\n");

    // OPEN
    int fd = open("/dev/sdd", O_DIRECT | O_RDWR | O_SYNC);
    printf("fd = %d\n",fd);

    // WRITE
    printf("try to write %d byte : %s\n",sizeof(buffer),buffer);
    w = write(fd,buffer,sizeof(buffer));
    if(w == -1) printf("write failed\n");
    else printf("write ok\n");

    // RESET BUFFER
    memset(buffer,0,sizeof(buffer));

    // SEEK
    s = lseek(fd,0,SEEK_SET);   
    if(s == -1) printf("seek failed\n");
    else printf("seek ok\n");

    // READ
    r = read(fd,buffer,sizeof(buffer));
    if(r == -1) printf("read failed\n");
    else printf("read ok\n");

    // PRINT BUFFER
    printf("buffer = %s\n",buffer);

    return 0;
}

Edit: I work with the 3.2 Kernel on a power architecture - if this is important.

Thank you very much for your time, Fabian

samuirai
  • 762
  • 1
  • 9
  • 25

1 Answers1

1

Depending on your SDD's block size (could by 512bit or 4K), you can only read/write mulitple of that size.

Also: when using O_DIRECT flag, you need to make sure the buffer is rightly aligned to block boundaries. You cann't ensure that using an ordinary char array, use memalign to allocate aligned memory instead.

yangsuli
  • 1,252
  • 5
  • 16
  • 33