summaryrefslogtreecommitdiff
path: root/utils/open-isns/relation.c
blob: caac38bca92d098fc0b8d2d720f6762b3c955271 (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
/*
 * iSNS object relationships
 *
 * Relations are used to express a connection between two
 * objects. Currently, two relationship types are implemented:
 *
 *  - portal group: this relates a storage node and a portal
 *  - visibility: this relates a nodes nodes that share a
 *	common discovery domain.
 *
 * Relation objects are nice for portals groups, but kind of
 * awkward for DDs. A better way of expressing DD membership
 * (which also allows for a fast visibility check) could be
 * to store a [bit] vector of DD IDs in each storage node.
 * A visibility check would amount to just doing the bitwise
 * AND of two vectors, and checking for NULL. The only thing
 * to take care of would be to make sure a DD object takes a
 * reference on its members (this is necessary so that objects
 * maintain their ID/name associations even when removed from
 * the database).
 *
 * Aug 22 2007 - changed DD code to use bit vectors. A lot
 *	of code in this file is now obsolete.
 *
 * Copyright (C) 2007 Olaf Kirch <olaf.kirch@oracle.com>
 */

#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <stdarg.h>

#include "isns.h"
#include "objects.h"
#include "util.h"
#include "db.h"

struct isns_relation_soup {
	/* For now, use one plain list. For better
	 * scalability, we'll need a hash table or
	 * something similar later. */
	isns_relation_list_t	irs_data;
};

static void	isns_relation_list_append(isns_relation_list_t *,
			isns_relation_t *);
static int	isns_relation_list_remove(isns_relation_list_t *,
			isns_relation_t *);

isns_relation_soup_t *
isns_relation_soup_alloc(void)
{
	return isns_calloc(1, sizeof(isns_relation_soup_t));
}

void
isns_relation_add(isns_relation_soup_t *soup,
		isns_relation_t *rp)
{
	isns_relation_list_append(&soup->irs_data, rp);
}

isns_relation_t *
isns_relation_find_edge(isns_relation_soup_t *soup,
		const isns_object_t *left,
		const isns_object_t *right,
		unsigned int relation_type)
{
	isns_relation_list_t *list = &soup->irs_data;
	unsigned int	i;

	for (i = 0; i < list->irl_count; ++i) {
		isns_relation_t *rp = list->irl_data[i];

		if (rp->ir_type != relation_type)
			continue;
		if (rp->ir_subordinate[0].obj == left
		 && rp->ir_subordinate[1].obj == right)
			return rp;
		if (rp->ir_subordinate[0].obj == right
		 && rp->ir_subordinate[1].obj == left)
			return rp;
	}
	return NULL;
}

void
isns_relation_get_edge_objects(isns_relation_soup_t *soup,
		const isns_object_t *left,
		unsigned int relation_type,
		isns_object_list_t *result)
{
	isns_relation_list_t *list = &soup->irs_data;
	unsigned int	i;

	for (i = 0; i < list->irl_count; ++i) {
		isns_relation_t *rp = list->irl_data[i];

		if (rp->ir_type != relation_type)
			continue;
		if (rp->ir_object == NULL)
			continue;
		if (rp->ir_subordinate[0].obj == left
		 || rp->ir_subordinate[1].obj == left) {
			isns_object_list_append(result,
				rp->ir_object);
		}
	}
}



void
isns_relation_halfspace(isns_relation_soup_t *soup,
		const isns_object_t *left,
		unsigned int relation_type,
		isns_object_list_t *result)
{
	isns_relation_list_t *list = &soup->irs_data;
	unsigned int	i;

	for (i = 0; i < list->irl_count; ++i) {
		isns_relation_t *rp = list->irl_data[i];

		if (rp->ir_type != relation_type)
			continue;
		if (rp->ir_subordinate[0].obj == left) {
			isns_object_list_append(result,
				rp->ir_subordinate[1].obj);
		} else
		if (rp->ir_subordinate[1].obj == left) {
			isns_object_list_append(result,
				rp->ir_subordinate[0].obj);
		}
	}
}

int
isns_relation_exists(isns_relation_soup_t *soup,
		const isns_object_t *relating_object,
		const isns_object_t *left,
		const isns_object_t *right,
		unsigned int relation_type)
{
	isns_relation_list_t *list = &soup->irs_data;
	unsigned int	i;

	for (i = 0; i < list->irl_count; ++i) {
		isns_relation_t *rp = list->irl_data[i];

		if (rp->ir_type != relation_type)
			continue;
		if (rp->ir_object != relating_object)
			continue;
		if (rp->ir_subordinate[0].obj == left
		 && rp->ir_subordinate[1].obj == right)
			return 1;
		if (rp->ir_subordinate[0].obj == right
		 && rp->ir_subordinate[1].obj == left)
			return 1;
	}
	return 0;
}

isns_object_t *
isns_relation_get_other(const isns_relation_t *rp,
			const isns_object_t *this)
{
	if (rp->ir_subordinate[0].obj == this)
		return rp->ir_subordinate[1].obj;
	if (rp->ir_subordinate[1].obj == this)
		return rp->ir_subordinate[0].obj;
	return NULL;
}

void
isns_relation_remove(isns_relation_soup_t *soup,
		isns_relation_t *rp)
{
	isns_object_release(rp->ir_object);
	rp->ir_object = NULL;

	isns_relation_list_remove(&soup->irs_data, rp);
}

isns_relation_t *
isns_create_relation(isns_object_t *relating_object,
		unsigned int relation_type,
		isns_object_t *subordinate_object1,
		isns_object_t *subordinate_object2)
{
	isns_relation_t *rp;
	
	rp = isns_calloc(1, sizeof(*rp));
	rp->ir_type = relation_type;
	rp->ir_users = 1;
	rp->ir_object = isns_object_get(relating_object);
	isns_object_reference_set(&rp->ir_subordinate[0], subordinate_object1);
	isns_object_reference_set(&rp->ir_subordinate[1], subordinate_object2);

#if 0
	if (relating_object) {
		relating_object->ie_relation = rp;
		rp->ir_users++;
	}
#endif

	return rp;
}

void
isns_relation_sever(isns_relation_t *rp)
{
	isns_object_release(rp->ir_object);
	rp->ir_object = NULL;

	isns_object_reference_drop(&rp->ir_subordinate[0]);
	isns_object_reference_drop(&rp->ir_subordinate[1]);
}

void
isns_relation_release(isns_relation_t *rp)
{
	if (--(rp->ir_users))
		return;

	isns_relation_sever(rp);
	isns_free(rp);
}

/*
 * Check whether the relation references two dead/limbo objects.
 * This is used for dead PG removal.
 */
int
isns_relation_is_dead(const isns_relation_t *rel)
{
	isns_object_t	*left, *right;

	left = rel->ir_subordinate[0].obj;
	right = rel->ir_subordinate[1].obj;
	if ((left->ie_flags & ISNS_OBJECT_DEAD)
	 && (right->ie_flags & ISNS_OBJECT_DEAD))
		return 1;

	return 0;
}

void
isns_relation_list_append(isns_relation_list_t *list,
			isns_relation_t *rp)
{
	if ((list->irl_count % 128) == 0) {
		list->irl_data = isns_realloc(list->irl_data,
				(list->irl_count + 128) * sizeof(void *));
		if (list->irl_data == NULL)
			isns_fatal("out of memory!\n");
	}

	list->irl_data[list->irl_count++] = rp;
	rp->ir_users++;
}

int
isns_relation_list_remove(isns_relation_list_t *list,
			isns_relation_t *rp)
{
	unsigned int	i, count = list->irl_count;

	for (i = 0; i < count; ++i) {
		if (list->irl_data[i] != rp)
			continue;
		if (i < count - 1)
			list->irl_data[i] = list->irl_data[count-1];
		isns_relation_release(rp);
		list->irl_count -= 1;
		return 1;
	}

	return 0;
}