linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* rawio usage
@ 2001-02-06  6:36 Mayank Vasa
  2001-02-06 14:17 ` Stephen C. Tweedie
  0 siblings, 1 reply; 4+ messages in thread
From: Mayank Vasa @ 2001-02-06  6:36 UTC (permalink / raw)
  To: Linux-Kernel

Hi,

I am quite new to rawio and am experimenting with with its usage. My test
environment is Redhat 7.0, kernel version 2.2.16-22 having an external fibre
channel drive having 2 disks (/dev/sda1 and /dev/sdb1)

All I am trying to do is to write and read to & from the disk using a raw
device. Externally I did a "raw /dev/raw/raw1 /dev/sdb1" and then I wrote a
small program to do the read/write. The program is:

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

int main(int argc, char *argv[])
{
    int fd;
    char writeBuf[512];
    char readBuf[100];

    memset(readBuf, '\0', 100);
    memset(writeBuf, '\0', 100);

    memcpy(writeBuf, "This is a test", 14);
    printf("writeBuf = %s\n", writeBuf);

    fd = open(argv[1], O_RDWR);
    if (fd < 0) {
        perror("open");
        exit (1);
    }

    if ((lseek(fd, 0L, 0)) < 0){
        perror("lseek");
        exit (1);
    }

    if ((write(fd, writeBuf, 512)) < 0) {
        printf ("errno = %d\n", errno);
        perror("write");
        exit(1);
    }

    lseek(fd, 0L, 0);
    if ((read(fd, readBuf, 512)) < 0) {
        perror("read");
        exit(1);
    }

    printf("The readbuf is %s\n", readBuf);
    return 0;
}

When I run this program as root, I get the error "write: Invalid argument".
It is basically returning errno = 22 which is EINVAL and as per the write
manpage means that fd is attached to an object which is unsuitable for
writing.

Could someone guide me on where I am going wrong & how to use raw devices?

--
Mayank Vasa
Confluence Networks.


-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
Please read the FAQ at http://www.tux.org/lkml/

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: rawio usage
  2001-02-06  6:36 rawio usage Mayank Vasa
@ 2001-02-06 14:17 ` Stephen C. Tweedie
  0 siblings, 0 replies; 4+ messages in thread
From: Stephen C. Tweedie @ 2001-02-06 14:17 UTC (permalink / raw)
  To: Mayank Vasa; +Cc: Linux-Kernel, Stephen Tweedie

Hi,

On Mon, Feb 05, 2001 at 10:36:32PM -0800, Mayank Vasa wrote:
> 
> When I run this program as root, I get the error "write: Invalid argument".

Raw IO requires that the buffers are aligned on a 512-byte boundary in
memory.

--Stephen
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
Please read the FAQ at http://www.tux.org/lkml/

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: rawio usage
@ 2001-02-06 19:09 Douglas Gilbert
  0 siblings, 0 replies; 4+ messages in thread
From: Douglas Gilbert @ 2001-02-06 19:09 UTC (permalink / raw)
  To: linux-kernel

 
Content-Type: multipart/mixed;
 boundary="------------D4869FCB9AEAF2CC69FB9DEF"

This is a multi-part message in MIME format.
--------------D4869FCB9AEAF2CC69FB9DEF
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

"Mayank Vasa" <mvasa@confluencenetworks.com> wrote:
> I am quite new to rawio and am experimenting with with its usage. My test
> environment is Redhat 7.0, kernel version 2.2.16-22 having an external fibre
> channel drive having 2 disks (/dev/sda1 and /dev/sdb1)
> 
> All I am trying to do is to write and read to & from the disk using a raw
> device. Externally I did a "raw /dev/raw/raw1 /dev/sdb1" and then I wrote a
> small program to do the read/write.

[snip]

Raw devices need to meet the alignment requirements of the
device they are bound to; in the case of most disk this
will be 512 bytes. You need to take this into account for:
  - the buffer you give to the read() and write() calls
  - the 'size' given to read() and write() should be a
    multiple of 512
  - the SEEK_SET 'offset' given to lseek() should be a
    multiple of 512. Note you have a 2 G limit here.
    You can use _llseek() to get around this.

A small program that just reads from a raw device (or the
corresponding block device which should give the same
result) attached.

If you were binding a raw device to a cdrom device then
the BLKSIZE would need to be 2048 bytes (in most cases).

Doug Gilbert
--------------D4869FCB9AEAF2CC69FB9DEF
Content-Type: text/plain; charset=us-ascii;
 name="my_rawio_ex.c"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="my_rawio_ex.c"

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

#define BLKSIZE 512
#define BLKS2READ 1

int main(int argc, char *argv[])
{
    int fd, k;
    unsigned char buff[BLKSIZE * (BLKS2READ + 1)]; // allow extra for alignment
    unsigned char * arbp;			   // aligned read buffer ptr
    long block_addr = 0;

    arbp = (char *)(((unsigned long)buff + (BLKSIZE - 1)) & (~(BLKSIZE - 1)));

    fd = open(argv[1], O_RDONLY);
    if (fd < 0) {
        perror("open");
        exit (1);
    }
    if ((lseek(fd, block_addr * BLKSIZE, SEEK_SET)) < 0){
        perror("lseek");	// problem if 2nd arg > 2G
        exit (1);
    }
    if ((read(fd, arbp, BLKSIZE * BLKS2READ)) < 0) {
        perror("read");
        exit(1);
    }

    printf("First 16 bytes of the readbuf (in hex) are:\n   ");
    for (k = 0; k < 16; ++k)
	printf("%x ", (int)arbp[k]);
    printf("\n");
    close(fd);
    return 0;
}

--------------D4869FCB9AEAF2CC69FB9DEF--

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
Please read the FAQ at http://www.tux.org/lkml/

^ permalink raw reply	[flat|nested] 4+ messages in thread

* RE: rawio usage
@ 2001-02-06 14:48 Nathan Black
  0 siblings, 0 replies; 4+ messages in thread
From: Nathan Black @ 2001-02-06 14:48 UTC (permalink / raw)
  To: 'Mayank Vasa'; +Cc: 'linux-kernel@vger.kernel.org'

need 512 byte alignment

i.e. 

change 
    char writeBuf[512];
to:
    char writeBuf[1023];
	writeBuf = (char *)(((int )&writeBuf[0] +  511) &~511);

This will typecast the writeBuffer address to an int  and add 511 to the
address. When you and that with ~511( invert 511). That will result int
something in a multiple of 512 for the address.
Then just typecast it back.
That is how to align it. Jens Was kind enough to tell me how to do this.
Nathan


-----Original Message-----
From: Mayank Vasa [mailto:mvasa@confluencenetworks.com]
Sent: Tuesday, February 06, 2001 1:37 AM
To: Linux-Kernel
Subject: rawio usage


Hi,

I am quite new to rawio and am experimenting with with its usage. My test
environment is Redhat 7.0, kernel version 2.2.16-22 having an external fibre
channel drive having 2 disks (/dev/sda1 and /dev/sdb1)

All I am trying to do is to write and read to & from the disk using a raw
device. Externally I did a "raw /dev/raw/raw1 /dev/sdb1" and then I wrote a
small program to do the read/write. The program is:

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

int main(int argc, char *argv[])
{
    int fd;
    char writeBuf[512];
    char readBuf[100];

    memset(readBuf, '\0', 100);
    memset(writeBuf, '\0', 100);

    memcpy(writeBuf, "This is a test", 14);
    printf("writeBuf = %s\n", writeBuf);

    fd = open(argv[1], O_RDWR);
    if (fd < 0) {
        perror("open");
        exit (1);
    }

    if ((lseek(fd, 0L, 0)) < 0){
        perror("lseek");
        exit (1);
    }

    if ((write(fd, writeBuf, 512)) < 0) {
        printf ("errno = %d\n", errno);
        perror("write");
        exit(1);
    }

    lseek(fd, 0L, 0);
    if ((read(fd, readBuf, 512)) < 0) {
        perror("read");
        exit(1);
    }

    printf("The readbuf is %s\n", readBuf);
    return 0;
}

When I run this program as root, I get the error "write: Invalid argument".
It is basically returning errno = 22 which is EINVAL and as per the write
manpage means that fd is attached to an object which is unsuitable for
writing.

Could someone guide me on where I am going wrong & how to use raw devices?

--
Mayank Vasa
Confluence Networks.


-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
Please read the FAQ at http://www.tux.org/lkml/
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
Please read the FAQ at http://www.tux.org/lkml/

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2001-02-06 19:15 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-02-06  6:36 rawio usage Mayank Vasa
2001-02-06 14:17 ` Stephen C. Tweedie
2001-02-06 14:48 Nathan Black
2001-02-06 19:09 Douglas Gilbert

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).