LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 06-16-2004, 10:22 AM   #1
rajar11
LQ Newbie
 
Registered: Jun 2004
Posts: 3

Rep: Reputation: 0
Raw Devices


Hi All,
Have the following code which tries to read a raw device on Linux 2.4.21-9.0.1.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main(int argc, char *argv[])
{
int fd=-1;
int readSize;
char buffer[2048];
int ioFlag = 0;
mode_t mode = 0;
if(argc < 2)
{
printf("usage:test <fileName>\n");
exit(1);
}

if((fd = open(argv[1], ioFlag, mode)) < 0)
{
perror("file open failure:");
exit(1);
}

printf("fd = %d \n" , fd);
if(read(fd, (void *)buffer, 1024 ) < 0)
{
printf("errno = %d \n", errno);
perror("file read failure:");
exit(1);
}

close(fd);

return 0;
}

I compiled the program using gcc 3.2.3 as follows
gcc -o test test.c

Ran the program as follows :-
./test /dev/raw/raw1

The output was as follows :-
fd = 3
errno = 22
file read failure:: Invalid argument


Anybody has any ideas why the program fails to read the raw device. The program can read other files successfully. It only has problems reading raw devices. Could not find anything on the net. The Oracle server seems to be able to read the raw device but my program cannot. Any help would be appreciated.

Thanks
rajar
 
Old 06-16-2004, 10:48 AM   #2
Vlad-A
Member
 
Registered: May 2004
Location: Vienna, Austria
Distribution: Open SuSE 11, Mac OS X 10.5
Posts: 299

Rep: Reputation: 32
Hello,

can you please try to read 512 Bytes (default disk sector size).

So,
read(fd, (void *)buffer, 512 )

Do you have a filesystem on that particular Block device ?

If yes, what is the Blocksize of that Filesystem ?

Can you read with reading byte count = FS block size ?
 
Old 06-16-2004, 12:43 PM   #3
rajar11
LQ Newbie
 
Registered: Jun 2004
Posts: 3

Original Poster
Rep: Reputation: 0
Raw Devices....

Tried with number of bytes to be read as 512. No change in result.
The block size for the device is 4096.
Tried with FS block size. No change in result.
 
Old 06-16-2004, 02:14 PM   #4
Vlad-A
Member
 
Registered: May 2004
Location: Vienna, Austria
Distribution: Open SuSE 11, Mac OS X 10.5
Posts: 299

Rep: Reputation: 32
Hello rajar11,

I think I know what is happening. Since you are using direct I/O the buffers you pass to
the read and write call *have* to be *boundary alligned*

If you just reserve an array, no one guarantees that the memory area for that array
is memory alligned.

So I have changed a little bit your program, in the way that now
memalign is used instead of an array. I have used 512 Byte as boundary
which is the default disk sector size. The reserved size is 1k (1024 Byte)


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main(int argc, char *argv[])
{
int fd=-1;
int readSize;
/* char buffer[2048]; We 'll allocate memory instead using Arrays */
char *buffer;
int ioFlag = 0;
mode_t mode = 0;
if(argc < 2)
{
printf("usage:test <fileName>\n");
exit(1);
}


/* Now allocate memory - boundary alligned */
if ((buffer=(char*) memalign(512, 1024))==NULL){
printf ("Error allocating memory\n");
exit (1);
}

if((fd = open(argv[1], ioFlag, mode)) < 0)
{
perror("file open failure:");
exit(1);
}

printf("fd = %d \n" , fd);
if(read(fd, (void *)buffer, 1024 ) < 0)
{
printf("errno = %d \n", errno);
perror("file read failure:");
exit(1);
}

close(fd);

return 0;
}
 
Old 06-16-2004, 02:36 PM   #5
rajar11
LQ Newbie
 
Registered: Jun 2004
Posts: 3

Original Poster
Rep: Reputation: 0
Raw Devices...

Thank you very much.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Require Raw devices on FC3 - New to Linux mikeh7519 Fedora 0 11-10-2005 07:36 PM
assigning raw devices: error when going to init3 GlennsPref Mandriva 1 11-25-2004 10:03 PM
Raw devices gauol Linux - General 2 09-23-2004 03:01 PM
UDEV and Raw devices swissworks Slackware 6 09-14-2004 02:08 PM
HD to HD raw copy with dd? mmx87 Linux - Hardware 1 09-10-2004 01:01 PM


All times are GMT -5. The time now is 04:56 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration