~ubuntu-branches/ubuntu/utopic/xen/utopic

« back to all changes in this revision

Viewing changes to tools/memshr/shm.c

  • Committer: Bazaar Package Importer
  • Author(s): Bastian Blank
  • Date: 2010-05-06 15:47:38 UTC
  • mto: (1.3.1) (15.1.1 sid) (4.1.1 experimental)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20100506154738-agoz0rlafrh1fnq7
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/******************************************************************************
 
2
 *
 
3
 * Copyright (c) 2009 Citrix Systems, Inc. (Grzegorz Milos)
 
4
 *
 
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.
 
9
 *
 
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.
 
14
 *
 
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
 
18
 */
 
19
#include <assert.h>
 
20
#include <stdlib.h>
 
21
#include <string.h>
 
22
#include <fcntl.h>
 
23
#include <unistd.h>
 
24
#include <sys/mman.h>
 
25
#include <sys/stat.h>
 
26
#include <sys/types.h>
 
27
 
 
28
#include "memshr-priv.h"
 
29
#include "bidir-hash.h"
 
30
#include "shm.h"
 
31
 
 
32
#define MEMSHR_INFO_SHM_FILE  "/memshr-info"
 
33
#define MEMSHR_INFO_MAGIC     0x15263748 
 
34
 
 
35
#define FGPRT_HASH_SHM_FILE "/blktap-fgprts"
 
36
#define FGPRT_HASH_PAGES    10000
 
37
 
 
38
#define BLOCK_HASH_SHM_FILE "/blktap-blks"
 
39
#define BLOCK_HASH_PAGES    10000
 
40
 
 
41
typedef struct shm_area {
 
42
    void*  base_addr;
 
43
    size_t size;
 
44
    int    fd;
 
45
} shm_area_t;
 
46
 
 
47
typedef struct {
 
48
    struct shm_area shared_info_area;
 
49
    struct shm_area fgprts_area;
 
50
    struct shm_area blocks_area;
 
51
} private_shm_info_t;
 
52
 
 
53
private_shm_info_t shm_info;
 
54
 
 
55
 
 
56
 
 
57
static int shm_area_open(const char *file, size_t size, int unlink, shm_area_t *shma)
 
58
 
59
    /* TODO: If blktapctrl can be restarted while system is running, this needs
 
60
     * to be cleverer */
 
61
    if(unlink) shm_unlink(file);
 
62
 
 
63
    shma->size = size;
 
64
    shma->fd = shm_open(file,
 
65
                        (O_CREAT | O_RDWR),
 
66
                        (S_IREAD | S_IWRITE));
 
67
 
 
68
    if(shma->fd < 0) return -1;
 
69
 
 
70
    if(ftruncate(shma->fd, size) < 0) return -2;
 
71
 
 
72
    shma->base_addr = mmap(NULL, 
 
73
                      size,
 
74
                      PROT_READ | PROT_WRITE, 
 
75
                      MAP_SHARED,
 
76
                      shma->fd,
 
77
                      0);
 
78
    
 
79
    if(shma->base_addr == MAP_FAILED) return -2;
 
80
 
 
81
    return 0;
 
82
}
 
83
 
 
84
static void shm_area_close(shm_area_t *shma)
 
85
{
 
86
    munmap(shma->base_addr, shma->size);
 
87
    close(shma->fd);
 
88
}
 
89
 
 
90
 
 
91
shared_memshr_info_t * shm_shared_info_open(int unlink)
 
92
{
 
93
    shared_memshr_info_t *shared_info;
 
94
    pthread_mutexattr_t  lock_attr;
 
95
    int nr_pages, i;
 
96
 
 
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,
 
100
                     unlink, 
 
101
                     &(shm_info.shared_info_area)) < 0)
 
102
    {
 
103
        DPRINTF("Failed to open shma for shared info.\n");
 
104
        return NULL;
 
105
    }
 
106
    shared_info = (shared_memshr_info_t *)
 
107
                             shm_info.shared_info_area.base_addr;
 
108
    if(unlink)
 
109
    {
 
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))
 
115
        {
 
116
            DPRINTF("Failed to init shared info lock.\n");
 
117
            return NULL;
 
118
        }
 
119
        strcpy(shared_info->vbd_images[0].file, "list-head");
 
120
        for(i=1; i<MAX_NR_VBD_IMAGES; i++)
 
121
        {
 
122
            shared_info->vbd_images[i].next = i; 
 
123
            shared_info->vbd_images[i].prev = i; 
 
124
        }
 
125
        shared_info->magic = MEMSHR_INFO_MAGIC;
 
126
    } 
 
127
    else
 
128
    if(shared_info->magic != MEMSHR_INFO_MAGIC)
 
129
    {
 
130
        DPRINTF("Incorrect magic in shared info.\n");
 
131
        return NULL;
 
132
    }
 
133
    
 
134
    return shared_info;
 
135
}
 
136
 
 
137
 
 
138
struct fgprtshr_hash * shm_fgprtshr_hash_open(int unlink)
 
139
{
 
140
    struct fgprtshr_hash *h;
 
141
    if(shm_area_open(FGPRT_HASH_SHM_FILE, 
 
142
                     FGPRT_HASH_PAGES * XC_PAGE_SIZE,
 
143
                     unlink, 
 
144
                     &(shm_info.fgprts_area)) < 0)
 
145
    {
 
146
        DPRINTF("Failed to init shma for fgprtshr_hash.\n");
 
147
        return NULL;
 
148
    }
 
149
 
 
150
    if(unlink)
 
151
    {
 
152
        h = fgprtshr_shm_hash_init(
 
153
                     (unsigned long) shm_info.fgprts_area.base_addr, 
 
154
                     FGPRT_HASH_PAGES * XC_PAGE_SIZE);
 
155
    } else
 
156
    {
 
157
        h = fgprtshr_shm_hash_get(
 
158
                     (unsigned long) shm_info.fgprts_area.base_addr); 
 
159
    }
 
160
        
 
161
    return h;
 
162
 
163
 
 
164
struct blockshr_hash * shm_blockshr_hash_open(int unlink)
 
165
{
 
166
    struct blockshr_hash *h;
 
167
    if(shm_area_open(BLOCK_HASH_SHM_FILE, 
 
168
                     BLOCK_HASH_PAGES * XC_PAGE_SIZE,
 
169
                     unlink, 
 
170
                     &(shm_info.blocks_area)) < 0)
 
171
    {
 
172
        DPRINTF("Failed to init shma for blockshr_hash.\n");
 
173
        return NULL;
 
174
    }
 
175
 
 
176
    if(unlink)
 
177
    {
 
178
        h = blockshr_shm_hash_init(
 
179
                     (unsigned long) shm_info.blocks_area.base_addr, 
 
180
                     BLOCK_HASH_PAGES * XC_PAGE_SIZE);
 
181
    } else
 
182
    {
 
183
        h = blockshr_shm_hash_get(
 
184
                     (unsigned long) shm_info.blocks_area.base_addr); 
 
185
    }
 
186
        
 
187
    return h;
 
188
 
189
 
 
190
uint16_t shm_vbd_image_get(char* file, vbd_image_info_t *vbd_imgs)
 
191
{
 
192
    vbd_image_info_t *img, *next_img;
 
193
    int i, img_id;
 
194
 
 
195
    /* Try to find the file in the existing list first */ 
 
196
    img = vbd_imgs;
 
197
    while(img->next != 0)
 
198
    {
 
199
        img = vbd_imgs + img->next;
 
200
        if(strncmp(img->file, file, MAX_NAME_LEN) == 0)
 
201
        {
 
202
            img->ref_cnt++;
 
203
            return (uint16_t)(img - vbd_imgs); 
 
204
        }
 
205
    }
 
206
    
 
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++)
 
209
    {
 
210
        img = vbd_imgs + i;
 
211
        if((img->next == i) && (img->prev == i))
 
212
            break;
 
213
    }
 
214
    /* No entries left! */
 
215
    if(i == MAX_NR_VBD_IMAGES)
 
216
    {
 
217
        DPRINTF("No space in vbds table.\n");
 
218
        return 0;
 
219
    }
 
220
    if(strlen(file) > MAX_NAME_LEN)
 
221
    {
 
222
        DPRINTF("Filename: %s too long (>%d).\n", file, MAX_NAME_LEN);
 
223
        return 0; 
 
224
    }
 
225
    /* Init the entry */
 
226
    img_id = (img - vbd_imgs);
 
227
    next_img = vbd_imgs + vbd_imgs[0].next;
 
228
    strcpy(img->file, file);
 
229
    img->ref_cnt = 1;
 
230
    img->next = vbd_imgs[0].next;
 
231
    img->prev = 0;
 
232
    next_img->prev = img_id;
 
233
    vbd_imgs[0].next = img_id;
 
234
 
 
235
    return img_id;
 
236
}
 
237
 
 
238
 
 
239
void shm_vbd_image_put(uint16_t memshr_id, vbd_image_info_t *vbd_imgs)
 
240
{
 
241
    vbd_image_info_t *img, *next_img, *prev_img;
 
242
    
 
243
    img = vbd_imgs + memshr_id;
 
244
    if(img->ref_cnt == 0)
 
245
    {
 
246
        DPRINTF("Incorrect image put.\n");
 
247
        return;
 
248
    }
 
249
 
 
250
    img->ref_cnt--; 
 
251
 
 
252
    /* Remove from list if ref_cnt is zero */
 
253
    if(img->ref_cnt == 0)
 
254
    {
 
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);
 
261
    }
 
262
}
 
263