2005-11-29 23:30:50 +00:00
|
|
|
/*
|
|
|
|
*
|
2006-09-15 14:55:39 +00:00
|
|
|
* (c) 2005 Laurent Vivier <Laurent@lvivier.info>
|
2005-11-29 23:30:50 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
2005-12-02 01:17:30 +00:00
|
|
|
#include <sys/ioctl.h>
|
2005-11-29 23:30:50 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
2005-12-02 01:17:30 +00:00
|
|
|
#include <linux/fs.h>
|
2005-12-05 19:58:04 +00:00
|
|
|
#include <string.h>
|
2005-11-29 23:30:50 +00:00
|
|
|
|
|
|
|
#include "libemile.h"
|
|
|
|
|
2007-02-24 13:35:32 +00:00
|
|
|
struct emile_container *emile_second_create_mapfile(unsigned short *unit_id, char *mapfile, char* kernel)
|
2005-11-29 23:30:50 +00:00
|
|
|
{
|
|
|
|
struct emile_container *container;
|
|
|
|
int fd;
|
|
|
|
int ret;
|
2007-02-24 13:35:32 +00:00
|
|
|
unsigned short unit_id_map;
|
2005-12-02 01:17:30 +00:00
|
|
|
int block_size;
|
2005-11-29 23:30:50 +00:00
|
|
|
|
|
|
|
/* create container of the kernel */
|
|
|
|
|
|
|
|
fd = open(kernel, O_RDONLY);
|
|
|
|
if (fd == -1)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "ERROR: cannot open kernel\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2005-12-02 01:17:30 +00:00
|
|
|
/* get filesystem block size */
|
|
|
|
|
|
|
|
ret = ioctl(fd, FIGETBSZ, &block_size);
|
|
|
|
if (ret != 0) {
|
|
|
|
close(fd);
|
|
|
|
perror("ioctl(FIGETBSZ)");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
container = (struct emile_container *)malloc(block_size);
|
|
|
|
if (container == NULL)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "ERROR: cannot allocate memory for container\n");
|
|
|
|
close(fd);
|
|
|
|
return NULL;
|
|
|
|
}
|
2005-12-05 19:58:04 +00:00
|
|
|
memset(container, 0, block_size);
|
2005-12-02 01:17:30 +00:00
|
|
|
|
|
|
|
|
2005-12-01 22:16:06 +00:00
|
|
|
ret = emile_scsi_create_container(fd, &unit_id_map, container,
|
2005-12-02 01:17:30 +00:00
|
|
|
(block_size - sizeof(struct emile_container)) / sizeof(struct emile_block));
|
2005-11-29 23:30:50 +00:00
|
|
|
close(fd);
|
|
|
|
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "ERROR: cannot fill container\n");
|
|
|
|
free(container);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* write container to map file */
|
|
|
|
|
2005-12-02 00:37:36 +00:00
|
|
|
fd = open(mapfile, O_CREAT | O_WRONLY);
|
2005-11-29 23:30:50 +00:00
|
|
|
if (fd == -1)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "ERROR: cannot create map file (%s)\n", mapfile);
|
|
|
|
free(container);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2005-12-02 01:17:30 +00:00
|
|
|
ret = write(fd, container, block_size);
|
2005-11-29 23:30:50 +00:00
|
|
|
|
|
|
|
close(fd);
|
|
|
|
|
2005-12-02 01:17:30 +00:00
|
|
|
if (ret != block_size)
|
2005-11-29 23:30:50 +00:00
|
|
|
{
|
|
|
|
fprintf(stderr, "ERROR: cannot write map file (%s)\n", mapfile);
|
|
|
|
free(container);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* now, we must know where is the map file */
|
|
|
|
|
2005-12-05 19:58:04 +00:00
|
|
|
memset(container, 0, block_size);
|
2005-11-29 23:30:50 +00:00
|
|
|
fd = open(mapfile, O_RDONLY);
|
|
|
|
if (fd == -1)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "ERROR: cannot open map file to read (%s)\n", mapfile);
|
|
|
|
free(container);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2005-12-01 22:16:06 +00:00
|
|
|
ret = emile_scsi_create_container(fd, unit_id, container,
|
2005-12-02 01:17:30 +00:00
|
|
|
(block_size - sizeof(struct emile_container)) / sizeof(struct emile_block));
|
2005-11-29 23:30:50 +00:00
|
|
|
close(fd);
|
|
|
|
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "ERROR: cannot map map file...\n");
|
|
|
|
free(container);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2005-12-01 22:16:06 +00:00
|
|
|
if (unit_id_map != *unit_id)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "ERROR: map file must be on the same disk as the file to map\n");
|
|
|
|
free(container);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2005-12-02 01:17:30 +00:00
|
|
|
if (container->size != block_size)
|
2005-11-29 23:30:50 +00:00
|
|
|
{
|
|
|
|
fprintf(stderr, "ERROR: map file size is bad (%d)\n", container->size);
|
|
|
|
free(container);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (container->blocks[0].count == 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "ERROR: map file is empty !\n");
|
|
|
|
free(container);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (container->blocks[1].count != 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "ERROR: map file is not contiguous\n");
|
|
|
|
free(container);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return container;
|
|
|
|
}
|