Skip to contentSkip to conversation

O_DIRECT for block device

DISCUSSION Karthik Sarangan 3 participants 4 messages Sep 22, 2005 – Sep 23, 2005 2 messages not in the archive

lore
Share & tools
lore
#0 lore raw

I have a scsi hdd configured at /dev/sdb.

open("/dev/sdb", O_DIRECT) succeeds.
A 'read' or 'write' returns -1 and errno is EINVAL.

How to read and write data to it? They do not seem to work. I have a 262144 byte buffer for data transfer.

My kernel is 2.6.9-11 and I use RedHat Enterprise Linux.

#1 lore raw
Quoted 6 lines · Karthik Sarangan

I have a scsi hdd configured at /dev/sdb.

open("/dev/sdb", O_DIRECT) succeeds.
A 'read' or 'write' returns -1 and errno is EINVAL.

How to read and write data to it? They do not seem to work. I have a 262144 byte buffer for data transfer.

make sure your buffer is aligned on a 512 byte boundary.

#2 lore raw
In reply to Karthik Sarangan
Quoted 2 lines · Karthik Sarangan

open("/dev/sdb", O_DIRECT) succeeds.
A 'read' or 'write' returns -1 and errno is EINVAL.

You must align your buffer to at least sector size. See
posix_memalign(3).

Signature · 9 lines
Life's Little Instruction Book #30

	"Never buy a house without a fireplace."

Joel Becker
Principal Software Developer
Oracle
E-mail: [email protected]
Phone: (650) 506-8127
#5 lore raw
In reply to a message not in the archive
Quoted 9 lines · Joel Becker
Quoted 1 line · Karthik Sarangan

I tried your tip and the read works beautifully but the write returns -1 and errno = EBADF!

Did you open with O_RDWR?

Joel

Alright I didn't, though using O_DIRECT | O_RDWR the second time solved it :-) I finally got 69MBps read and 49 MBps writes on my SCSI disk.

Thanks for helping me out.