Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 26 Oct 2005 16:25:01 +0800
From:      Xin LI <delphij@frontfree.net>
To:        freebsd-hackers@FreeBSD.org
Cc:        zhuyan@staff.sina.com.cn, LI Xin <delphij@delphij.net>
Subject:   Is it possible to mmap() raw disk device?
Message-ID:  <1130315101.810.5.camel@spirit>

next in thread | raw e-mail | index | archive | help

--=-d+sFaITjGw0SPWFkVk8H
Content-Type: text/plain
Content-Transfer-Encoding: quoted-printable

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:


=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D
#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 =3D strdup("abcdefghijklmn");
        if (dst =3D=3D NULL) {
                printf("Can not allocate memory\n");
                exit(-1);
        }

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

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

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

        exit(0);
}
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D

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,
--=20
Xin LI <delphij delphij net>  http://www.delphij.net/

--=-d+sFaITjGw0SPWFkVk8H
Content-Type: application/pgp-signature; name=signature.asc
Content-Description: This is a digitally signed message part

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (FreeBSD)

iD8DBQBDXz1d/cVsHxFZiIoRAuweAJ4lkRXRjkXNUEP3Zk8Fv5EW89ODAwCghEjf
s5YomQWnRju+otn6BzuCBFY=
=yNeR
-----END PGP SIGNATURE-----

--=-d+sFaITjGw0SPWFkVk8H--




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?1130315101.810.5.camel>