/dev/pmem0 open file error

256 views
Skip to first unread message

hiten chauhan

unread,
Sep 21, 2021, 7:02:59 AM9/21/21
to pmem
In my linux system pmem is connected and i can see this memory node using ls /dev command
also, i mount successfully pmem0 to /mnt 

 df -h
Filesystem           Size  Used Avail Use% Mounted on
devtmpfs              79G     0   79G   0% /dev
tmpfs                 79G     0   79G   0% /dev/shm
tmpfs                 79G   11M   79G   1% /run
tmpfs                 79G     0   79G   0% /sys/fs/cgroup
/dev/mapper/cl-root   70G  5.6G   65G   8% /
/dev/mapper/cl-home  815G  6.2G  809G   1% /home
/dev/nvme0n1p2      1014M  238M  777M  24% /boot
/dev/nvme0n1p1       599M  7.3M  592M   2% /boot/efi
tmpfs                 16G   20K   16G   1% /run/user/42
tmpfs                 16G  4.0K   16G   1% /run/user/1000
/dev/pmem0            16G  148M   16G   1% /mnt/dax

Now I want to do  write and read operations on this memory using mmap 
I write code like

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h> /* mmap() is defined in this header */
#include <fcntl.h>


int main(void)
{
         int mode = 0x0777;

        int fdout;
         if ((fdout = open ("/dev/pmem0", O_RDWR | O_CREAT | O_TRUNC, mode )) <       0)//edited here
           {printf ("can't create file");
            return 0;
           }
}

but I get the error "can't create file"
so what do I miss?


Jan K

unread,
Sep 21, 2021, 7:20:29 AM9/21/21
to hiten chauhan, pmem
I have no idea what you are tying to achieve with this.
I think you really need to read about pmem and try to understand it first.

Things that will make the code work:

1a) Use a sane path in open (for instance: "/mnt/dax/someFileName").
Make sure that you have permissions to /mnt/dax directory.
1b) Use devdax instead of fsdax provided you want to open whole device
directly. Make sure you have permissions for it.
1c) Run as root and remove at least flag O_TRUNC if you are really
sure that you want to open /dev/pmem0 rather than a file on filesystem
mounted there, but I strongly doubt that you want this option.

2) Fix mode from '0x0777' to '0777' or else you end up with some
bizarre permissions

3) Be more verbose about the reason of errors - errno, perror,
strerror, glibc error function etc. are there to tell you why open
failed.

I really recommend you to go through resources on pmem first.

Regards,
Jan
> --
> You received this message because you are subscribed to the Google Groups
> "pmem" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to pmem+uns...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/pmem/f3c06bb7-f359-42a6-88e2-54239840c543n%40googlegroups.com.
>

hiten chauhan

unread,
Sep 21, 2021, 7:28:58 AM9/21/21
to pmem
I have Questions
1 = So I can't use /dev/pmem0 in open function? I can only use my mount part am I right?
2 = If I use devdax instead of fsdax , can I use /dev/pmem0 in the open function?

Jan K

unread,
Sep 21, 2021, 8:05:50 AM9/21/21
to hiten chauhan, pmem
> 1 = So I can't use /dev/pmem0 in open function? I can only use my mount
> part am I right?

You can open a block device such as /dev/pmem0. It makes sense if you
want to tamper with partition table or filesystem structure manually,
for instance to recover lost data or test how well a filesystem
repairs itself when you inject faults. This obviously requires root
privileges. I doubt you need this.

> 2 = If I use devdax instead of fsdax , can I use /dev/pmem0 in the open
> function?

If you use devdax, you'll have a device such as '/dev/dax0.0' instead
of '/dev/pmem0', and the intended use of devdax is to open the device
directly. To open such device you obviously need sufficient
permissions.

You still did not tell us what you want to do, and I guess you stuck
with some wrong concept. You would really benefit from looking at the
docs to revise your thinking.

Regards,
Jan
> https://groups.google.com/d/msgid/pmem/466a8b2c-925a-4cfd-a42e-e73da59f4b6en%40googlegroups.com.
>

hiten chauhan

unread,
Sep 21, 2021, 8:34:23 AM9/21/21
to pmem
I have pmem based server and it running centos so i just want to write and read data on pmem device
I refer PMDK library for this write and read operation but the issue is I can't found any method to write directly on a particular pmem address.
So after some research, I try mmap method 
So that is all that I want to 

Jan K

unread,
Sep 21, 2021, 8:52:08 AM9/21/21
to hiten chauhan, pmem
> I have pmem based server and it running centos so i just want to write and
> read data on pmem device
> I refer PMDK library for this write and read operation but the issue is I
> can't found any method to write directly on a particular pmem address.

Then you probably want 'pmem_map_file' function:
https://pmem.io/pmdk/manpages/linux/master/libpmem/pmem_is_pmem.3

There's a complete example of that what you want in the docs, right here:
https://pmem.io/pmdk/manpages/linux/master/libpmem/libpmem.7.html#example

Again, read the docs first.

> write directly on a particular pmem address.

Keep in mind that virtual memory addresses of the same physical memory
address will change across runs of the same application. It's safe to
use a particular offset from the base address of the memory mapped
file, but it's not feasible to use a particular address.
> https://groups.google.com/d/msgid/pmem/90d923f4-7935-4202-9799-839539e3b6bdn%40googlegroups.com.
>
Reply all
Reply to author
Forward
Message has been deleted
0 new messages