mmap returns MAP_FAILED trying to write structs in C

I’m trying to develop a FUSE filesystem driver to create a mount point. To make it persistent I figured to save inodes into a binary file. So this is my inode struct:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>struct s_fuseInode {`
int id; /* Identificador del inodo */
char name[MAX_NAME]; /* Nombre del archivo */
char path[MAX_PATH]; /* Ruta del archivo */
int size; /* Tamaño del archivo */
int block_count; /* Número de bloques ocupados por el archivo */
int block[MAX_BLOCKS]; /* Bloques ocupados por el archivo */
struct s_fuseInode parent; / Inodo padre */
int parent_id; /* Identificador del inodo padre */
struct s_fuseInode *children; / Inodos hijos */
int children_count; /* Número de inodos hijos */
int is_dir; /* Indica si el inodo es un directorio */
char data; / Datos del archivo */
time_t creation_time; /* Fecha de creación del archivo */
time_t modification_time; /* Fecha de modificación del archivo */
time_t access_time; /* Fecha de acceso al archivo */
int links; /* Número de enlaces al archivo */
mode_t mode; /* Permisos del archivo */
uid_t uid; /* Identificador de usuario */
gid_t gid; /* Identificador de grupo */
};
</code>
<code>struct s_fuseInode {` int id; /* Identificador del inodo */ char name[MAX_NAME]; /* Nombre del archivo */ char path[MAX_PATH]; /* Ruta del archivo */ int size; /* Tamaño del archivo */ int block_count; /* Número de bloques ocupados por el archivo */ int block[MAX_BLOCKS]; /* Bloques ocupados por el archivo */ struct s_fuseInode parent; / Inodo padre */ int parent_id; /* Identificador del inodo padre */ struct s_fuseInode *children; / Inodos hijos */ int children_count; /* Número de inodos hijos */ int is_dir; /* Indica si el inodo es un directorio */ char data; / Datos del archivo */ time_t creation_time; /* Fecha de creación del archivo */ time_t modification_time; /* Fecha de modificación del archivo */ time_t access_time; /* Fecha de acceso al archivo */ int links; /* Número de enlaces al archivo */ mode_t mode; /* Permisos del archivo */ uid_t uid; /* Identificador de usuario */ gid_t gid; /* Identificador de grupo */ }; </code>
struct s_fuseInode {`
        int                     id; /* Identificador del inodo */
        char                    name[MAX_NAME]; /* Nombre del archivo */
        char                    path[MAX_PATH]; /* Ruta del archivo */
        int                     size; /* Tamaño del archivo */
        int                     block_count; /* Número de bloques ocupados por el archivo */
        int                     block[MAX_BLOCKS]; /* Bloques ocupados por el archivo */
        struct s_fuseInode      parent; / Inodo padre */
        int                     parent_id; /* Identificador del inodo padre */
        struct s_fuseInode      *children; / Inodos hijos */
        int                     children_count; /* Número de inodos hijos */
        int                     is_dir; /* Indica si el inodo es un directorio */
        char                    data; / Datos del archivo */
        time_t                  creation_time; /* Fecha de creación del archivo */
        time_t                  modification_time; /* Fecha de modificación del archivo */
        time_t                  access_time; /* Fecha de acceso al archivo */
        int                     links; /* Número de enlaces al archivo */
        mode_t                  mode; /* Permisos del archivo */
        uid_t    uid; /* Identificador de usuario */
        gid_t                   gid; /* Identificador de grupo */
};

And this is the function that saves the inode into the file

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>
static int save_inode(struct s_fuseInode *inode)
{
int fd = open("virtual_disk.bin", O_RDWR, S_IRUSR | S_IWUSR); // Abrimos el archivo de disco virtual
if (fd == -1) // Si no se ha podido abrir
{
fprintf(stderr, "save_inode: Error al abrir el archivo de disco virtualn");
return (1);
}
off_t offset = lseek(fd, 0, SEEK_END); // Nos situamos al final del archivo
fprintf(stderr, "offset %ldn", offset);
if (offset == -1) // Si no se ha podido situar
{
fprintf(stderr, "save_inode: Error al situarse al final del archivon");
return (1);
}
ftruncate(fd, offset + sizeof(struct s_fuseInode)); // Truncamos el archivo
fprintf(stderr, "size %ldn", offset + sizeof(struct s_fuseInode));
struct s_fuseInode *map = mmap(0, sizeof(struct s_fuseInode), PROT_READ | PROT_WRITE, MAP_SHARED, fd, offset); // Mapeamos el archivo
if (map == MAP_FAILED) // Si no se ha podido mapear
{
close(fd);
fprintf(stderr, "save_inode: Error al mapear el archivon");
return (1);
}
memcpy(map, inode, sizeof(struct s_fuseInode)); // Copiamos el inodo en el archivo
if (munmap(map, sizeof(struct s_fuseInode)) == -1) // Si no se ha podido desmapear
{
close(fd);
fprintf(stderr, "save_inode: Error al desmapear el archivon");
return (1);
}
close(fd); // Cerramos el archivo
return (0);
}
</code>
<code> static int save_inode(struct s_fuseInode *inode) { int fd = open("virtual_disk.bin", O_RDWR, S_IRUSR | S_IWUSR); // Abrimos el archivo de disco virtual if (fd == -1) // Si no se ha podido abrir { fprintf(stderr, "save_inode: Error al abrir el archivo de disco virtualn"); return (1); } off_t offset = lseek(fd, 0, SEEK_END); // Nos situamos al final del archivo fprintf(stderr, "offset %ldn", offset); if (offset == -1) // Si no se ha podido situar { fprintf(stderr, "save_inode: Error al situarse al final del archivon"); return (1); } ftruncate(fd, offset + sizeof(struct s_fuseInode)); // Truncamos el archivo fprintf(stderr, "size %ldn", offset + sizeof(struct s_fuseInode)); struct s_fuseInode *map = mmap(0, sizeof(struct s_fuseInode), PROT_READ | PROT_WRITE, MAP_SHARED, fd, offset); // Mapeamos el archivo if (map == MAP_FAILED) // Si no se ha podido mapear { close(fd); fprintf(stderr, "save_inode: Error al mapear el archivon"); return (1); } memcpy(map, inode, sizeof(struct s_fuseInode)); // Copiamos el inodo en el archivo if (munmap(map, sizeof(struct s_fuseInode)) == -1) // Si no se ha podido desmapear { close(fd); fprintf(stderr, "save_inode: Error al desmapear el archivon"); return (1); } close(fd); // Cerramos el archivo return (0); } </code>


static int      save_inode(struct s_fuseInode *inode)
{
        int fd = open("virtual_disk.bin", O_RDWR, S_IRUSR | S_IWUSR); // Abrimos el archivo de disco virtual
        if (fd == -1) // Si no se ha podido abrir
        {
                fprintf(stderr, "save_inode: Error al abrir el archivo de disco virtualn");
                return (1);
        }
        off_t offset = lseek(fd, 0, SEEK_END); // Nos situamos al final del archivo
        fprintf(stderr, "offset %ldn", offset);
        if (offset == -1) // Si no se ha podido situar
        {
                fprintf(stderr, "save_inode: Error al situarse al final del archivon");
                return (1);
        }
        ftruncate(fd, offset + sizeof(struct s_fuseInode)); // Truncamos el archivo
        fprintf(stderr, "size %ldn", offset + sizeof(struct s_fuseInode));

        struct s_fuseInode *map = mmap(0, sizeof(struct s_fuseInode), PROT_READ | PROT_WRITE, MAP_SHARED, fd, offset); // Mapeamos el archivo
        if (map == MAP_FAILED) // Si no se ha podido mapear
        {
                close(fd);
                fprintf(stderr, "save_inode: Error al mapear el archivon");
                return (1);
        }
        memcpy(map, inode, sizeof(struct s_fuseInode)); // Copiamos el inodo en el archivo
        if (munmap(map, sizeof(struct s_fuseInode)) == -1) // Si no se ha podido desmapear
        {
                close(fd);
                fprintf(stderr, "save_inode: Error al desmapear el archivon");
                return (1);
        }
        close(fd); // Cerramos el archivo
        return (0);
}

It returns the error of MAP_FAILED. The inode struct is 256 bits and PAGE_SIZE is 4096. I thought the problem was page allignment but I cannot see how to solve the problem, first time using mmap.

I tried including a char padding[3840] to reach 4096 size and it works, but the problem is the size that the file occupies 4KB for each inode, I want it to be optimized.

New contributor

d3str3k is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật