Is it possible to mmap() raw disk device?

Xin LI delphij at frontfree.net
Wed Oct 26 01:25:28 PDT 2005


Dear folks,

When we were trying to make a userland FS application we have
encountered "Invalid Argument" when doing mmap() over an open descriptor
to a disk device.  The program goes here:


============================
#include        <sys/types.h>
#include        <sys/stat.h>
#include        <sys/mman.h>    /* mmap() */
#include        <fcntl.h>
#include        <errno.h>
#include        <assert.h>

int
main(int argc, char *argv[])
{
        int                     fd;
        char            *src, *dst;
        struct stat     statbuf;

        dst = strdup("abcdefghijklmn");
        if (dst == NULL) {
                printf("Can not allocate memory\n");
                exit(-1);
        }

        if ((fd = open("/dev/da2s1", O_RDWR | O_DIRECT)) < 0)
                printf("can't open for read/write");

        if ((src = mmap(0, 10, PROT_WRITE | PROT_READ,
                            MAP_SHARED, fd, 0)) == (caddr_t) -1) {
                perror("mmap error for input");
                return -1;
        }

        memcpy(dst, src, 10);   /* does the file copy */

        exit(0);
}
============================

Do we need some special prerequisite before doing mmap()?  According to
the vm_mmap.c it seems that we should map a DTYPE_VNODE object, which
looks somewhat confusing...

Thanks in advance!

Cheers,
-- 
Xin LI <delphij delphij net>  http://www.delphij.net/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 187 bytes
Desc: This is a digitally signed message part
Url : http://lists.freebsd.org/pipermail/freebsd-hackers/attachments/20051026/4e083e4f/attachment.bin


More information about the freebsd-hackers mailing list