1
/******************************************************************************
3
* Copyright (c) 2009 Citrix Systems, Inc. (Grzegorz Milos)
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 2 of the License, or
8
* (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26
#include <sys/types.h>
28
#include "memshr-priv.h"
29
#include "bidir-hash.h"
32
#define MEMSHR_INFO_SHM_FILE "/memshr-info"
33
#define MEMSHR_INFO_MAGIC 0x15263748
35
#define FGPRT_HASH_SHM_FILE "/blktap-fgprts"
36
#define FGPRT_HASH_PAGES 10000
38
#define BLOCK_HASH_SHM_FILE "/blktap-blks"
39
#define BLOCK_HASH_PAGES 10000
41
typedef struct shm_area {
48
struct shm_area shared_info_area;
49
struct shm_area fgprts_area;
50
struct shm_area blocks_area;
53
private_shm_info_t shm_info;
57
static int shm_area_open(const char *file, size_t size, int unlink, shm_area_t *shma)
59
/* TODO: If blktapctrl can be restarted while system is running, this needs
61
if(unlink) shm_unlink(file);
64
shma->fd = shm_open(file,
66
(S_IREAD | S_IWRITE));
68
if(shma->fd < 0) return -1;
70
if(ftruncate(shma->fd, size) < 0) return -2;
72
shma->base_addr = mmap(NULL,
74
PROT_READ | PROT_WRITE,
79
if(shma->base_addr == MAP_FAILED) return -2;
84
static void shm_area_close(shm_area_t *shma)
86
munmap(shma->base_addr, shma->size);
91
shared_memshr_info_t * shm_shared_info_open(int unlink)
93
shared_memshr_info_t *shared_info;
94
pthread_mutexattr_t lock_attr;
97
nr_pages = (sizeof(shared_memshr_info_t) >> XC_PAGE_SHIFT) + 1;
98
if(shm_area_open(MEMSHR_INFO_SHM_FILE,
99
nr_pages * XC_PAGE_SIZE,
101
&(shm_info.shared_info_area)) < 0)
103
DPRINTF("Failed to open shma for shared info.\n");
106
shared_info = (shared_memshr_info_t *)
107
shm_info.shared_info_area.base_addr;
110
memset(shared_info, 0, sizeof(shared_memshr_info_t));
111
if(pthread_mutexattr_init(&lock_attr) ||
112
pthread_mutexattr_setpshared(&lock_attr, PTHREAD_PROCESS_SHARED) ||
113
pthread_mutex_init(&shared_info->lock, &lock_attr) ||
114
pthread_mutexattr_destroy(&lock_attr))
116
DPRINTF("Failed to init shared info lock.\n");
119
strcpy(shared_info->vbd_images[0].file, "list-head");
120
for(i=1; i<MAX_NR_VBD_IMAGES; i++)
122
shared_info->vbd_images[i].next = i;
123
shared_info->vbd_images[i].prev = i;
125
shared_info->magic = MEMSHR_INFO_MAGIC;
128
if(shared_info->magic != MEMSHR_INFO_MAGIC)
130
DPRINTF("Incorrect magic in shared info.\n");
138
struct fgprtshr_hash * shm_fgprtshr_hash_open(int unlink)
140
struct fgprtshr_hash *h;
141
if(shm_area_open(FGPRT_HASH_SHM_FILE,
142
FGPRT_HASH_PAGES * XC_PAGE_SIZE,
144
&(shm_info.fgprts_area)) < 0)
146
DPRINTF("Failed to init shma for fgprtshr_hash.\n");
152
h = fgprtshr_shm_hash_init(
153
(unsigned long) shm_info.fgprts_area.base_addr,
154
FGPRT_HASH_PAGES * XC_PAGE_SIZE);
157
h = fgprtshr_shm_hash_get(
158
(unsigned long) shm_info.fgprts_area.base_addr);
164
struct blockshr_hash * shm_blockshr_hash_open(int unlink)
166
struct blockshr_hash *h;
167
if(shm_area_open(BLOCK_HASH_SHM_FILE,
168
BLOCK_HASH_PAGES * XC_PAGE_SIZE,
170
&(shm_info.blocks_area)) < 0)
172
DPRINTF("Failed to init shma for blockshr_hash.\n");
178
h = blockshr_shm_hash_init(
179
(unsigned long) shm_info.blocks_area.base_addr,
180
BLOCK_HASH_PAGES * XC_PAGE_SIZE);
183
h = blockshr_shm_hash_get(
184
(unsigned long) shm_info.blocks_area.base_addr);
190
uint16_t shm_vbd_image_get(char* file, vbd_image_info_t *vbd_imgs)
192
vbd_image_info_t *img, *next_img;
195
/* Try to find the file in the existing list first */
197
while(img->next != 0)
199
img = vbd_imgs + img->next;
200
if(strncmp(img->file, file, MAX_NAME_LEN) == 0)
203
return (uint16_t)(img - vbd_imgs);
207
/* Couldn't find an existing entry. We need to add one. Find empty slot */
208
for(i=1; i<MAX_NR_VBD_IMAGES; i++)
211
if((img->next == i) && (img->prev == i))
214
/* No entries left! */
215
if(i == MAX_NR_VBD_IMAGES)
217
DPRINTF("No space in vbds table.\n");
220
if(strlen(file) > MAX_NAME_LEN)
222
DPRINTF("Filename: %s too long (>%d).\n", file, MAX_NAME_LEN);
226
img_id = (img - vbd_imgs);
227
next_img = vbd_imgs + vbd_imgs[0].next;
228
strcpy(img->file, file);
230
img->next = vbd_imgs[0].next;
232
next_img->prev = img_id;
233
vbd_imgs[0].next = img_id;
239
void shm_vbd_image_put(uint16_t memshr_id, vbd_image_info_t *vbd_imgs)
241
vbd_image_info_t *img, *next_img, *prev_img;
243
img = vbd_imgs + memshr_id;
244
if(img->ref_cnt == 0)
246
DPRINTF("Incorrect image put.\n");
252
/* Remove from list if ref_cnt is zero */
253
if(img->ref_cnt == 0)
255
next_img = vbd_imgs + img->next;
256
prev_img = vbd_imgs + img->prev;
257
prev_img->next = img->next;
258
next_img->prev = img->prev;
259
img->next = img->prev = (img - vbd_imgs);
260
memset(img->file, 0, MAX_NAME_LEN);