summaryrefslogtreecommitdiffstats
path: root/xlators/cluster/map/src/map-helper.c
blob: 851397b68d3a43b47bc8d9263f4b2b63f068c897 (plain)
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/*
   Copyright (c) 2009-2012 Red Hat, Inc. <http://www.redhat.com>
   This file is part of GlusterFS.

   This file is licensed to you under your choice of the GNU Lesser
   General Public License, version 3 or any later version (LGPLv3 or
   later), or the GNU General Public License, version 2 (GPLv2), in all
   cases as published by the Free Software Foundation.
*/
#ifndef _CONFIG_H
#define _CONFIG_H
#include "config.h"
#endif

#include "xlator.h"
#include "map.h"


xlator_t *
map_subvol_next (xlator_t *this, xlator_t *prev)
{
	map_private_t *priv = NULL;
	xlator_t      *next = NULL;
	int            i = 0;

	priv = this->private;

	for (i = 0; i < priv->child_count; i++) {
		if (priv->xlarray[i].xl == prev) {
			if ((i + 1) < priv->child_count)
				next = priv->xlarray[i + 1].xl;
			break;
		}
	}

	return next;
}

int
map_subvol_cnt (xlator_t *this, xlator_t *subvol)
{
	int i = 0;
	int ret = -1;
	map_private_t *priv = NULL;

	priv = this->private;

	for (i = 0; i < priv->child_count; i++) {
		if (subvol == priv->xlarray[i].xl) {
			ret = i;
			break;
		}
	}

	return ret;
}

int
map_itransform (xlator_t *this, xlator_t *subvol, uint64_t x, uint64_t *y_p)
{
	map_private_t *priv = NULL;
	int         cnt = 0;
	int         max = 0;
	uint64_t    y = 0;

	if (x == ((uint64_t) -1)) {
		y = (uint64_t) -1;
		goto out;
	}

	priv = this->private;

	max = priv->child_count;
	cnt = map_subvol_cnt (this, subvol);

	y = ((x * max) + cnt);

out:
	if (y_p)
		*y_p = y;

	return 0;
}


int
map_deitransform (xlator_t *this, uint64_t y, xlator_t **subvol_p,
		  uint64_t *x_p)
{
	int         cnt = 0;
	int         max = 0;
	uint64_t    x = 0;
	xlator_t   *subvol = 0;
	map_private_t *priv = NULL;

	priv = this->private;
	max = priv->child_count;

	cnt = y % max;
	x   = y / max;

	subvol = priv->xlarray[cnt].xl;

	if (subvol_p)
		*subvol_p = subvol;

	if (x_p)
		*x_p = x;

	return 0;
}


xlator_t *
get_mapping_subvol_from_path (xlator_t *this, const char *path) 
{
	map_private_t      *priv = NULL;
	struct map_pattern *map = NULL;

	/* To make sure we handle '/' properly */
	if (!strcmp (path, "/"))
		return NULL;

	priv = this->private;

	map = priv->map;
	while (map) {
		if (!strncmp (map->directory, path, map->dir_len)) {
			if ((path[map->dir_len] == '/') ||
			    (path[map->dir_len] == '\0')) {
				return map->xl;
			}
		}
		
		map = map->next;
	}

	return priv->default_xl;
}

xlator_t *
get_mapping_subvol_from_ctx (xlator_t *this, inode_t *inode)
{
	uint64_t subvol = 0;
	int      ret    = -1;

	ret = inode_ctx_get (inode, this, &subvol);
	if (ret != 0) 
		return NULL;

	return (xlator_t *)(long)subvol;
}

int
check_multiple_volume_entry (xlator_t *this, 
			     xlator_t *subvol)
{
	int ret = -1;
	int idx = 0;
	map_private_t *priv = NULL;

	priv = this->private;
	
	for (idx = 0; idx < priv->child_count; idx++) {
		if (priv->xlarray[idx].xl == subvol) {
			if (priv->xlarray[idx].mapped) {
				gf_log (this->name, GF_LOG_ERROR,
					"subvolume '%s' is already mapped",
					subvol->name);
				goto out;
			}
			priv->xlarray[idx].mapped = 1;
			ret = 0;
			goto out;
		}
	}

	gf_log (this->name, GF_LOG_ERROR,
		"subvolume '%s' is not found",
		subvol->name);
	
 out:
	return ret;
}

int
verify_dir_and_assign_subvol (xlator_t *this, 
			      const char *directory, 
			      const char *subvol)
{
	int            default_flag = 0;
	int            ret  = -1;
	int            idx  = 0;
	map_private_t *priv = NULL;
	xlator_list_t *trav = NULL;
	struct map_pattern *tmp_map = NULL;

	priv = this->private;

	/* check if directory is valid, ie, its a top level dir, and 
	 * not includes a '*' in it.
	 */
	if (!strcmp ("*", directory)) {
		default_flag = 1;
	} else {
		if (directory[0] != '/') {
			gf_log (this->name, GF_LOG_ERROR,
				"map takes absolute path, starting with '/'. "
				"not '%s'", directory);
			goto out;
		}
		for (idx = 1; idx < (strlen (directory) - 1); idx++) {
			if (directory[idx] == '/') {
				gf_log (this->name, GF_LOG_ERROR,
					"map takes only top level directory, "
					"not '%s'", directory);
				goto out;
			}
		}
	}

	/* Assign proper subvolume */
	trav = this->children;
	while (trav) {
		if (!strcmp (trav->xlator->name, subvol)) {
			
			/* Check if there is another directory for 
			 * same volume, if yes, return error.
			 */
			ret = check_multiple_volume_entry (this, 
							   trav->xlator);
			if (ret != 0) {
				goto out;
			}

			ret = 0;
			if (default_flag) {
				if (priv->default_xl) {
					ret = -1;
					gf_log (this->name, GF_LOG_ERROR,
						"'*' specified more than "
						"once. don't confuse me!!!");
				}

				priv->default_xl = trav->xlator;
				goto out;
			}

			tmp_map = GF_CALLOC (1, sizeof (struct map_pattern),
                                             gf_map_mt_map_pattern);
			tmp_map->xl = trav->xlator;
			tmp_map->dir_len = strlen (directory);

			/* make sure that the top level directory starts 
			 * with '/' and ends without '/'
			 */
			tmp_map->directory = gf_strdup (directory);
			if (directory[tmp_map->dir_len - 1] == '/') {
				tmp_map->dir_len--;
			}

			if (!priv->map) 
				priv->map = tmp_map;
			else {
				struct map_pattern *trav_map = NULL;
				trav_map = priv->map;
				while (trav_map->next)
					trav_map = trav_map->next;
				trav_map->next = tmp_map;
			}
			
			goto out;
		}

		trav = trav->next;
	}

	gf_log (this->name, GF_LOG_ERROR, 
		"map volume '%s' is not proper subvolume", subvol);

 out:
	return ret;
}

int 
assign_default_subvol (xlator_t *this, const char *default_xl)
{
	int ret = -1;
	map_private_t *priv = NULL;
	xlator_list_t *trav = NULL;

	priv = this->private;
	trav = this->children;

	while (trav) {
		if (!strcmp (trav->xlator->name, default_xl)) {
			ret = check_multiple_volume_entry (this, 
							   trav->xlator);
			if (ret != 0) {
				goto out;
			}
			if (priv->default_xl)
				gf_log (this->name, GF_LOG_WARNING,
					"default-volume option provided, "
					"overriding earlier '*' option");
			priv->default_xl = trav->xlator;
			return 0;
		}
		trav = trav->next;
	}

	gf_log (this->name, GF_LOG_ERROR,
		"default-volume value is not an valid subvolume. check again");
 out:
	return -1;
}

void
verify_if_all_subvolumes_got_used (xlator_t *this)
{
	int idx = 0;
	map_private_t *priv = NULL;

	priv = this->private;
	
	for (idx = 0; idx < priv->child_count; idx++) {
		if (!priv->xlarray[idx].mapped) {
			if (!priv->default_xl) {
				priv->default_xl = priv->xlarray[idx].xl;
				priv->xlarray[idx].mapped = 1;
			} else {
				gf_log (this->name, GF_LOG_WARNING,
					"subvolume '%s' is not mapped to "
					"any directory",
					priv->xlarray[idx].xl->name);
			}
		}
	}

	if (!priv->default_xl) {
		gf_log (this->name, GF_LOG_WARNING,
			"default subvolume not specified, filesystem "
			"may not work properly. Check 'map' translator "
			"documentation for more info");
	}

	return ;
}