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
23
#include "memshr-priv.h"
24
#include "bidir-hash.h"
26
#include "bidir-daemon.h"
34
memshr_vbd_info_t vbd_info = {0, DOMID_INVALID};
38
struct shared_memshr_info *shared_info;
39
struct fgprtshr_hash *fgprts;
40
struct blockshr_hash *blks;
41
} private_memshr_info_t;
43
private_memshr_info_t memshr;
45
#define SHARED_INFO (memshr.shared_info)
47
void memshr_set_domid(int domid)
49
vbd_info.domid = domid;
52
void memshr_daemon_initialize(void)
55
struct fgprtshr_hash *h;
57
memset(&memshr, 0, sizeof(private_memshr_info_t));
59
if((SHARED_INFO = shm_shared_info_open(1)) == NULL)
61
DPRINTF("Failed to init shared info.\n");
65
if((memshr.fgprts = shm_fgprtshr_hash_open(1)) == NULL)
67
DPRINTF("Failed to init fgprtshr hash.\n");
70
memshr.shared_info->fgprtshr_hash_inited = 1;
72
if((memshr.blks = shm_blockshr_hash_open(1)) == NULL)
74
DPRINTF("Failed to init blockshr hash.\n");
77
memshr.shared_info->blockshr_hash_inited = 1;
79
bidir_daemon_initialize(memshr.blks);
83
void memshr_vbd_initialize(void)
87
memset(&memshr, 0, sizeof(private_memshr_info_t));
89
if((SHARED_INFO = shm_shared_info_open(0)) == NULL)
91
DPRINTF("Failed to open shared info.\n");
95
if(!SHARED_INFO->fgprtshr_hash_inited)
97
DPRINTF("fgprtshr hash not inited.\n");
101
if((memshr.fgprts = shm_fgprtshr_hash_open(0)) == NULL)
103
DPRINTF("Failed to open fgprtshr_hash.\n");
107
if((memshr.blks = shm_blockshr_hash_open(0)) == NULL)
109
DPRINTF("Failed to open blockshr_hash.\n");
113
if(vbd_info.domid == DOMID_INVALID)
116
if((xc_handle = xc_interface_open()) < 0)
118
DPRINTF("Failed to open XC interface.\n");
122
vbd_info.xc_handle = xc_handle;
123
vbd_info.enabled = 1;
126
uint16_t memshr_vbd_image_get(char* file)
130
if(pthread_mutex_lock(&SHARED_INFO->lock)) goto error_out;
131
id = shm_vbd_image_get(file, SHARED_INFO->vbd_images);
132
if(pthread_mutex_unlock(&SHARED_INFO->lock)) goto error_out;
139
void memshr_vbd_image_put(uint16_t memshr_id)
141
if(pthread_mutex_lock(&SHARED_INFO->lock)) return;
142
shm_vbd_image_put(memshr_id, SHARED_INFO->vbd_images);
143
if(pthread_mutex_unlock(&SHARED_INFO->lock)) return;
146
int memshr_vbd_issue_ro_request(char *buf,
154
uint64_t s_hnd, c_hnd;
158
if(!vbd_info.enabled)
164
/* Nominate the granted page for sharing */
165
ret = xc_memshr_nominate_gref(vbd_info.xc_handle,
169
/* If page couldn't be made sharable, we cannot do anything about it */
174
/* Check if we've read matching disk block previously */
176
blk.disk_id = file_id;
177
if(blockshr_block_lookup(memshr.blks, blk, &s_hnd) > 0)
179
ret = xc_memshr_share(vbd_info.xc_handle, s_hnd, c_hnd);
181
/* Handles failed to be shared => at least one of them must be invalid,
182
remove the relevant ones from the map */
185
case XEN_DOMCTL_MEM_SHARING_S_HANDLE_INVALID:
186
ret = blockshr_shrhnd_remove(memshr.blks, s_hnd, NULL);
187
if(ret) DPRINTF("Could not rm invl s_hnd: %"PRId64"\n", s_hnd);
189
case XEN_DOMCTL_MEM_SHARING_C_HANDLE_INVALID:
190
ret = blockshr_shrhnd_remove(memshr.blks, c_hnd, NULL);
191
if(ret) DPRINTF("Could not rm invl c_hnd: %"PRId64"\n", c_hnd);
202
void memshr_vbd_complete_ro_request(uint64_t hnd,
209
if(!vbd_info.enabled)
216
blk.disk_id = file_id;
217
if(blockshr_insert(memshr.blks, blk, hnd) < 0)
218
DPRINTF("Could not insert block hint into hash.\n");