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:
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
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.
d3str3k is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.