summaryrefslogtreecommitdiffstats
path: root/src/server/utils.c
blob: 0be15699a56787a9b4167ea9afcf4d7e7a5676a0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*
 * This file is part of the Distributed Network Block Device 3
 *
 * Copyright(c) 2011-2012 Johann Latocha <johann@latocha.de>
 *
 * This file may be licensed under the terms of of the
 * GNU General Public License Version 2 (the ``GPL'').
 *
 * Software distributed under the License is distributed
 * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
 * express or implied. See the GPL for the specific language
 * governing rights and limitations.
 *
 * You should have received a copy of the GPL along with this
 * program. If not, go to http://www.gnu.org/licenses/gpl.html
 * or write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

#include <stdlib.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <pthread.h>
#include <string.h>

#include "server.h"
#include "utils.h"

void dnbd3_load_config(char *file)
{
    int fd;
    gint i;
    GKeyFile* gkf;

    gkf = g_key_file_new();
    if (!g_key_file_load_from_file(gkf, file, G_KEY_FILE_NONE, NULL))
    {
        printf("ERROR: Config file not found: %s\n", file);
        exit(EXIT_FAILURE);
    }

    gchar **groups = NULL;
    groups = g_key_file_get_groups(gkf, &_num_images);
    _images = calloc(_num_images, sizeof(dnbd3_image_t));

    for (i = 0; i < _num_images; i++)
    {
    	_images[i].group = malloc(strlen(groups[i]));
    	strcpy(_images[i].group, groups[i]);
        _images[i].file = g_key_file_get_string(gkf, groups[i], "file", NULL);
        _images[i].servers = g_key_file_get_string_list(gkf, groups[i], "servers", &_images[i].num_servers, NULL);
        _images[i].serverss = g_key_file_get_string(gkf, groups[i], "servers", NULL);
        _images[i].vid = g_key_file_get_integer(gkf, groups[i], "vid", NULL);
        _images[i].rid = g_key_file_get_integer(gkf, groups[i], "rid", NULL);
        _images[i].cache_file = g_key_file_get_string(gkf, groups[i], "cache", NULL);
        _images[i].atime = 0;

        if (_images[i].num_servers > NUMBER_SERVERS)
            printf("WARN: Max allowed servers %i\n", NUMBER_SERVERS);

        fd = open(_images[i].file, O_RDONLY);
        if (fd > 0)
            _images[i].filesize = lseek(fd, 0, SEEK_END);
        else
            printf("ERROR: Image file not found: %s\n", _images[i].file);

        close(fd);

        if (_images[i].cache_file)
        {
            // read cache map from file
            _images[i].cache_map  = calloc(_images[i].filesize >> 15, sizeof(char));
            memset(_images[i].cache_map, 0, (_images[i].filesize >> 15) * sizeof(char));
            char tmp[strlen(_images[i].cache_file)+4];
            strcpy(tmp, _images[i].cache_file);
            strcat(tmp, ".map");
            fd = open(tmp, O_RDONLY);
            if (fd > 0)
                read(fd, _images[i].cache_map, (_images[i].filesize >> 15) * sizeof(char));
            close(fd);

            // open cache file
            fd = open(_images[i].cache_file, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
            if (fd < 1)
                printf("ERROR: Could't create cache file\n");

            if (_images[i].filesize != lseek(fd, 0, SEEK_END))
                fallocate(fd, 0, 0, _images[i].filesize);

            close(fd);
        }
    }

    g_strfreev(groups);
    g_key_file_free(gkf);
}

void dnbd3_reload_config(char* config_file_name)
{
    int i, fd;
    pthread_spin_lock(&_spinlock);
    GSList *iterator = NULL;
    for (iterator = _dnbd3_clients; iterator; iterator = iterator->next)
    {
        dnbd3_client_t *client = iterator->data;
        pthread_spin_lock(&client->spinlock);
        client->image = NULL;
    }

    for (i = 0; i < _num_images; i++)
    {
    	// save cache maps
        if (_images[i].cache_file)
        {
            char tmp[strlen(_images[i].cache_file)+4];
            strcpy(tmp, _images[i].cache_file);
            strcat(tmp, ".map");
            fd = open(tmp, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);

            if (fd > 0)
                write(fd, _images[i].cache_map,  (_images[i].filesize >> 15) * sizeof(char));

            close(fd);
        }

        free(_images[i].group);
        free(_images[i].file);
        free(_images[i].servers);
        free(_images[i].serverss);
        free(_images[i].cache_file);
        free(_images[i].cache_map);
    }

    _num_images = 0;
    free(_images);
    dnbd3_load_config(config_file_name);
    for (iterator = _dnbd3_clients; iterator; iterator = iterator->next)
    {
        dnbd3_client_t *client = iterator->data;
        pthread_spin_unlock(&client->spinlock);
    }
    pthread_spin_unlock(&_spinlock);
}

int dnbd3_add_image(dnbd3_image_t *image, char *file)
{
	FILE* f = fopen(image->file,"r");
	if (f == NULL)
	{
		printf("ERROR: Image file not found: %s\n", image->file);
		return ERROR_FILE_NOT_FOUND;
	}
	fclose (f);

	dnbd3_image_t* tmp = dnbd3_get_image(image->vid, image->rid);
	if (image->rid == 0)
	{
		if(tmp)
			image->rid = tmp->rid +1;
		else
			image->rid = 1;
	} else if (tmp)
	{
		printf("ERROR: Image already exists (%d,%d)\n", image->vid, image->rid);
		return ERROR_IMAGE_ALREADY_EXISTS;
	}

    GKeyFile* gkf;
    gkf = g_key_file_new();
    if (!g_key_file_load_from_file(gkf, file, G_KEY_FILE_NONE, NULL))
    {
        printf("ERROR: Config file not found: %s\n", file);
        exit(EXIT_FAILURE);
    }

    g_key_file_set_integer(gkf, image->group, "vid", image->vid);
    g_key_file_set_integer(gkf, image->group, "rid", image->rid);
    g_key_file_set_string(gkf, image->group, "file", image->file);
    g_key_file_set_string(gkf, image->group, "servers", image->serverss);
    g_key_file_set_string(gkf, image->group, "cache", image->cache_file);

    gchar* data = g_key_file_to_data(gkf, NULL, NULL);

    f = fopen(file,"w");
    if (f)
    {
    	fputs((char*) data, f);
    	fclose(f);
        g_free(data);
        g_key_file_free(gkf);
        return 0;
    }
    else
    {
    	g_free(data);
    	g_key_file_free(gkf);
    	printf("ERROR: Config file is not writable: %s\n", file);
    	return ERROR_CONFIG_FILE_PERMISSIONS;
    }
}

int dnbd3_del_image(dnbd3_image_t *image, char *file)
{
	if (image->rid == 0)
	{
        printf("ERROR: Delete with rid=0 is not allowed\n");
        return ERROR_RID;
	}

	dnbd3_image_t* tmp = dnbd3_get_image(image->vid, image->rid);
	if (!tmp)
	{
        printf("ERROR: Image not found: (%d,%d)\n", image->vid, image->rid);
        return ERROR_IMAGE_NOT_FOUND;
	}

	GSList *iterator = NULL;
    for (iterator = _dnbd3_clients; iterator; iterator = iterator->next)
    {
        dnbd3_client_t *client = iterator->data;
        if (tmp == client->image)
        {
        	printf("ERROR: Delete is not allowed, image is in use (%d,%d)\n", tmp->vid, tmp->rid);
        	return ERROR_IMAGE_IN_USE;
        }
    }

    GKeyFile* gkf;
    gkf = g_key_file_new();
    if (!g_key_file_load_from_file(gkf, file, G_KEY_FILE_NONE, NULL))
    {
        printf("ERROR: Config file not found: %s\n", file);
        exit(EXIT_FAILURE);
    }

    g_key_file_remove_group(gkf, tmp->group, NULL);
    gchar* data = g_key_file_to_data(gkf, NULL, NULL);

    FILE* f = fopen(file,"w");
    if (f)
    {
    	fputs((char*) data, f);
    	fclose(f);
        g_free(data);
        g_key_file_free(gkf);
        // TODO: unlink image file
        return 0;
    }
    else
    {
    	g_free(data);
    	g_key_file_free(gkf);
    	printf("ERROR: Config file is not writable: %s\n", file);
    	return ERROR_CONFIG_FILE_PERMISSIONS;
    }
}

dnbd3_image_t* dnbd3_get_image(int vid, int rid)
{
    int i, max = 0;
    dnbd3_image_t *result = NULL;
    for (i = 0; i < _num_images; ++i)
    {
        if (rid != 0) // rid was specified
        {
            if (_images[i].vid == vid && _images[i].rid == rid)
                result = &_images[i];
        }
        else // search max. rid available
        {
            if (_images[i].vid == vid && _images[i].rid > max)
            {
                result = &_images[i];
                max = _images[i].rid;
            }
        }
    }
    return result;
}

void dnbd3_handle_sigpipe(int signum)
{
    printf("ERROR: SIGPIPE received!\n");
}

void dnbd3_handle_sigterm(int signum)
{
    printf("INFO: SIGTERM or SIGINT received!\n");
    dnbd3_cleanup();
}