Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8078299
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T15:44:56+00:00 2026-06-05T15:44:56+00:00

I want to make a basic read() from a SATA HDD /dev/sdd . A

  • 0

I want to make a basic read() from a SATA HDD /dev/sdd. A write() seems to work. Also read() and write() works without the O_DIRECT Flag. I’ve read, that it has to be aligned to the blocksize. So I used this to get the blocksize:

root$ blockdev --getsize /dev/sdd
488397168

root$ blockdev --getsize64 /dev/sdd
250059350016

root$ python -c "print 250059350016.0/488397168"
512.0

As you can see I have root. The HDD is connected via a PCIe SATA Card and lspci -vv shows me, that it uses the basic ahci (drivers/ata/ahci.c) driver.
I work with the 3.2.0 Linux Kernel on a 64 bit Power Architecture.

Here is my code:

#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;
}

The output is:

tmp_buffer is at: 1cc80010 % 512 = 16
buffer is at: 1cc80200 % 512 = 0
try to read and then dump buffer:
Error: Invalid argument

edit:
I have updated my source as suggested by Brett Hale’s answer. Unfortunately I still get the error. Is my way to find out the blocksize ok? Have I done the aligning right?

Thank you very much for reading,
Fabian

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T15:44:57+00:00Added an answer on June 5, 2026 at 3:44 pm

    Direct DMA transfer typically requires the buffer to be aligned. From man:

    The O_DIRECT flag may impose alignment restrictions on the length and address
    of userspace buffers and the file offset of I/Os. … Under Linux 2.6, alignment to 512-byte boundaries suffices.

    So char buffer[512]; might need to be aligned to a 512-byte address.

    It may not be possible to achieve this alignment on the stack, so something like:

    static char buffer[512] __attribute__ ((__aligned__ (512)));

    may work. Or maybe this alignment will work on the stack. Alternatively, if you’re using x86, you could use the <mm_malloc.h> intrinsic support functions: _mm_malloc and _mm_free.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to make basic gui-tests with the ms ui automation framework, therefore i'm
Edit For simplicity: I just want to make the most basic possible cursor that
I want make a bash script which returns the position of an element from
I want to read unsigned bytes from a binary file. So I wrote the
I want make datetimepicker in my project. Using jquery how it is possible? I
I want make interactive application where user launches it and can do various task
Let's say I want make some of my sources publicly available via my blog
I am new android app developer i want make app for tablets and phone
I have style sheet with a class name changebackgroundcolor i want make change in
Sorry google translate make mistake, to many mistake, i want make an alarm in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.