@@ -39,24 +39,57 @@ utils_translate_mem_visibility_flag(umf_memory_visibility_t in_flag,
3939}
4040
4141/*
42- * MMap a /dev/dax device.
43- * First try to mmap with (MAP_SHARED_VALIDATE | MAP_SYNC) flags
44- * which allows flushing from the user-space. If MAP_SYNC fails
45- * try to mmap with MAP_SHARED flag (without MAP_SYNC).
42+ * Map given file into memory.
43+ * If (flags & MAP_PRIVATE) it uses just mmap. Otherwise, if (flags & MAP_SYNC)
44+ * it tries to mmap with (flags | MAP_SHARED_VALIDATE | MAP_SYNC)
45+ * which allows flushing from the user-space. If MAP_SYNC fails and the user
46+ * did not specify it by himself it tries to mmap with (flags | MAP_SHARED).
4647 */
47- void * utils_devdax_mmap (void * hint_addr , size_t length , int prot , int fd ) {
48- void * ptr = utils_mmap (hint_addr , length , prot ,
49- MAP_SHARED_VALIDATE | MAP_SYNC , fd , 0 );
50- if (ptr ) {
51- LOG_DEBUG (
52- "devdax mapped with the (MAP_SHARED_VALIDATE | MAP_SYNC) flags" );
53- return ptr ;
54- }
55-
56- ptr = utils_mmap (hint_addr , length , prot , MAP_SHARED , fd , 0 );
57- if (ptr ) {
58- LOG_DEBUG ("devdax mapped with the MAP_SHARED flag" );
59- return ptr ;
48+ void * utils_mmap_file (void * hint_addr , size_t length , int prot , int flags ,
49+ int fd , size_t fd_offset ) {
50+ void * addr ;
51+
52+ /*
53+ * MAP_PRIVATE and MAP_SHARED are mutually exclusive,
54+ * therefore mmap with MAP_PRIVATE is executed separately.
55+ */
56+ if (flags & MAP_PRIVATE ) {
57+ addr = utils_mmap (hint_addr , length , prot , flags , fd , fd_offset );
58+ if (addr == MAP_FAILED ) {
59+ LOG_PERR ("mapping file with the MAP_PRIVATE flag failed" );
60+ return NULL ;
61+ }
62+
63+ LOG_DEBUG ("file mapped with the MAP_PRIVATE flag" );
64+ return addr ;
65+ }
66+
67+ errno = 0 ;
68+
69+ if (flags & MAP_SYNC ) {
70+ /* try to mmap with MAP_SYNC flag */
71+ const int sync_flags = MAP_SHARED_VALIDATE | MAP_SYNC ;
72+ addr = utils_mmap (hint_addr , length , prot , flags | sync_flags , fd ,
73+ fd_offset );
74+ if (addr ) {
75+ LOG_DEBUG ("file mapped with the MAP_SYNC flag" );
76+ return addr ;
77+ }
78+
79+ LOG_PERR ("mapping file with the MAP_SYNC flag failed" );
80+ }
81+
82+ if ((!(flags & MAP_SYNC )) || errno == EINVAL || errno == ENOTSUP ||
83+ errno == EOPNOTSUPP ) {
84+ /* try to mmap with MAP_SHARED flag (without MAP_SYNC) */
85+ const int shared_flags = (flags & (~MAP_SYNC )) | MAP_SHARED ;
86+ addr = utils_mmap (hint_addr , length , prot , shared_flags , fd , fd_offset );
87+ if (addr ) {
88+ LOG_DEBUG ("file mapped with the MAP_SHARED flag" );
89+ return addr ;
90+ }
91+
92+ LOG_PERR ("mapping file with the MAP_SHARED flag failed" );
6093 }
6194
6295 return NULL ;
0 commit comments