File: factory.c

package info (click to toggle)
reiser4progs 1.0.6-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 5,348 kB
  • ctags: 3,714
  • sloc: ansic: 33,468; sh: 8,489; makefile: 1,012
file content (352 lines) | stat: -rw-r--r-- 7,977 bytes parent folder | download | duplicates (4)
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
349
350
351
352
/* Copyright (C) 2001-2005 by Hans Reiser, licensing governed by
   reiser4progs/COPYING.
   
   factory.c -- reiser4 plugin factory.It contains code for loading, unloading
   and finding plugins. */

#include <reiser4/libreiser4.h>

/* Hash table contains all known libreiser4 plugins. */
reiser4_plug_t **plugins;

static uint8_t plugs_max[LAST_PLUG_TYPE + 1] = {
	[OBJECT_PLUG_TYPE]	= OBJECT_LAST_ID,
	[ITEM_PLUG_TYPE]	= ITEM_LAST_ID,
	[NODE_PLUG_TYPE]	= NODE_LAST_ID,
	[HASH_PLUG_TYPE]	= HASH_LAST_ID,
	[FIBRE_PLUG_TYPE]	= FIBRE_LAST_ID,
	[POLICY_PLUG_TYPE]	= TAIL_LAST_ID,
	[SDEXT_PLUG_TYPE]	= SDEXT_LAST_ID,
	[FORMAT_PLUG_TYPE]	= FORMAT_LAST_ID,
	[OID_PLUG_TYPE]		= OID_LAST_ID,
	[ALLOC_PLUG_TYPE]	= ALLOC_LAST_ID,
	[JOURNAL_PLUG_TYPE]	= JOURNAL_LAST_ID,
	[KEY_PLUG_TYPE]		= KEY_LAST_ID,
	[COMPRESS_PLUG_TYPE]	= COMPRESS_LAST_ID,
	[CMODE_PLUG_TYPE]	= CMODE_LAST_ID,
	[CLUSTER_PLUG_TYPE]	= CLUSTER_LAST_ID,
	[LAST_PLUG_TYPE]	= 0,
};

/* Structure that contains libreiser4 functions available for all plugins to be
   used. */
extern reiser4_core_t core;

#ifndef ENABLE_MINIMAL
/* Helper callback for checking plugin validness. It if called for each plugin
   in order to compare its characteristics with characteristics of new
   registered one. */
static errno_t cb_check_plug(reiser4_plug_t *plug, void *data) {
	reiser4_plug_t *examined = (reiser4_plug_t *)data;

	if (!plug || examined == plug)
		return 0;

	/* Check plugin labels. They should not be the same. */
	if (!aal_strncmp(examined->label, plug->label,
			 PLUG_MAX_LABEL))
	{
		aal_error("Can't load another plugin with "
			  "the same label %s.", plug->label);
		return -EINVAL;
	}
	
	/* Check plugin group. It should not be more or equal LAST_ITEM. */
	if (examined->id.group >= LAST_ITEM) {
		aal_error("Plugin %s has invalid group id 0x%x.", 
			  examined->label, examined->id.group);
		return -EINVAL;
	}

	/* Check plugin id, type and group. There should be only one plugin with
	   particular id. */
	if (examined->id.group == plug->id.group &&
	    examined->id.id == plug->id.id &&
	    examined->id.type == plug->id.type)
	{
		aal_error("Plugin %s has the same id as %s.", 
			  examined->label, plug->label);
		return -EINVAL;
	}

	return 0;
}
#endif

/* Helper functions used for calculating hash and for comparing two entries from
   plugin hash table during its modifying. */
#define plug_hash_func(type, id) (plugs_max[type] + (id))
#define plug_type_count(type) ((uint8_t)(plugs_max[type + 1] - plugs_max[type]))

#ifndef ENABLE_MINIMAL
/* Unloads plugin and removes it from plugin hash table. */
static errno_t reiser4_factory_unload(reiser4_plug_t *plug) {
	aal_assert("umka-1496", plug != NULL);

	plugins[plug_hash_func(plug->id.type, plug->id.id)] = NULL;
	
	return 0;
}
#endif

/* Loads and initializes plugin by its entry. Also this function makes register
   the plugin in plugins list. */
void reiser4_factory_load(reiser4_plug_t *plug) {
#ifndef ENABLE_MINIMAL
	if (reiser4_factory_foreach(cb_check_plug, (void *)plug)) {
		aal_error("Plugin %s will not be attached to "
			  "plugin factory.", plug->label);
		reiser4_factory_unload(plug);
		return;
	}
#endif
	
	plugins[plug_hash_func(plug->id.type, plug->id.id)] = plug;
}

/* Macro for loading plugin by its name. */
#define __load_plug(name) {			\
	extern reiser4_plug_t name##_plug;	\
	reiser4_factory_load(&name##_plug);	\
}

#define __init_plug(name) {			\
	extern reiser4_core_t *name##_core;	\
	name##_core = &core;			\
}

/* Initializes all built-in plugins. Other kinds of plugins are not supported
   for now.  */
errno_t reiser4_factory_init(void) {
	uint8_t prev, max;
	int i;
	
	prev = 0;
	/* Init the plugin array. 
	   Note: reiser4_factory_init is called at every access to libreiser4
	   from grub, however plug_max is static and its re-initialization
	   should be avoided -- check plugs_max[LAST_PLUG_TYPE] == 0. */
	if (plugs_max[LAST_PLUG_TYPE] == 0) {
		for (i = 0; i <= LAST_PLUG_TYPE; i++) {
			max = plugs_max[i];
			if (i == 0)
				plugs_max[i] = 0;
			else
				plugs_max[i] = plugs_max[i - 1] + prev;

			prev = max;
		}
	}
	
	plugins = aal_calloc((plugs_max[LAST_PLUG_TYPE]) * sizeof(*plugins), 0);
	
	/* Registering all known plugins. */
	__load_plug(format40);
	__init_plug(format40);

#ifndef ENABLE_MINIMAL
	__load_plug(oid40);
	
	__load_plug(alloc40);
	
	__load_plug(journal40);
#endif
	
#ifdef ENABLE_R5_HASH
	__load_plug(r5_hash);
#endif

#ifdef ENABLE_TEA_HASH
	__load_plug(tea_hash);
#endif

#ifdef ENABLE_DEG_HASH
	__load_plug(deg_hash);
#endif
	
#ifdef ENABLE_FNV1_HASH
	__load_plug(fnv1_hash);
#endif
	
#ifdef ENABLE_RUPASOV_HASH
	__load_plug(rupasov_hash);
#endif

#ifdef ENABLE_LEXIC_FIBRE
	__load_plug(fibre_lexic);
#endif
	
#ifdef ENABLE_DOT_O_FIBRE
	__load_plug(fibre_dot_o);
#endif
	
#ifdef ENABLE_EXT_1_FIBRE
	__load_plug(fibre_ext_1);
#endif
	
#ifdef ENABLE_EXT_3_FIBRE
	__load_plug(fibre_ext_3);
#endif
	
	__load_plug(sdext_lw);
	__init_plug(sdext_lw);
	
	__load_plug(sdext_lt);
	__init_plug(sdext_lt);
	
	__load_plug(sdext_unix);
	__init_plug(sdext_unix);
	
	__load_plug(sdext_pset);
	__init_plug(sdext_pset);
	
	__load_plug(sdext_hset);
#ifndef ENABLE_MINIMAL
	__load_plug(sdext_crypto);
	__init_plug(sdext_crypto);
#endif
	__load_plug(sdext_flags);
	__init_plug(sdext_flags);

	__load_plug(cde40);
	__init_plug(cde40);
	
	__load_plug(stat40);
	__init_plug(stat40);
	
	__load_plug(plain40);
	__init_plug(plain40);
#ifndef ENABLE_MINIMAL
	__load_plug(ctail40);
	__init_plug(ctail40);
#endif
	__load_plug(extent40);
	__init_plug(extent40);
	
	__load_plug(nodeptr40);
	__init_plug(nodeptr40);
	
	__load_plug(bbox40);
	__init_plug(bbox40);

#ifdef ENABLE_LARGE_KEYS
	__load_plug(key_large);
#endif
	
#ifdef ENABLE_SHORT_KEYS
	__load_plug(key_short);
#endif
	
	__load_plug(node40);
	__init_plug(node40);
	
	__load_plug(dir40);
	__load_plug(reg40);
	
#ifdef ENABLE_SPECIAL
	__load_plug(spl40);
#endif

#ifndef ENABLE_MINIMAL
	__load_plug(ccreg40);
#endif
	
#ifdef ENABLE_SYMLINKS
	__load_plug(sdext_symlink);
	__init_plug(sdext_symlink);
	
	__load_plug(sym40);
#endif
	
	__init_plug(obj40);

#ifndef ENABLE_MINIMAL
	__load_plug(extents);
	__load_plug(smart);
	__load_plug(tails);

	__load_plug(lzo1);
	__load_plug(gzip1);

	__load_plug(nocompress);
	__load_plug(lattd);
	__load_plug(ultim);
	__load_plug(force);
	__load_plug(convx);

	__load_plug(clust64);
	__load_plug(clust32);
	__load_plug(clust16);
	__load_plug(clust8);
	__load_plug(clust4);
#endif

        return 0;
}

/* Finalizes plugin factory, by means of unloading the all plugins. */
void reiser4_factory_fini(void) {
	aal_free(plugins);
}

/* Calls specified function for every plugin from plugin list. This functions is
   used for getting any plugins information. */
errno_t reiser4_factory_foreach(plug_func_t plug_func, void *data) {
	errno_t res;
	uint8_t i;
	
	aal_assert("umka-3006", plug_func != NULL);

	for (i = 0; i < plugs_max[LAST_PLUG_TYPE]; i++) {
		if (plugins[i] && (res = plug_func(plugins[i], data)))
			return res;
	}
	
	return 0;
}

/* Finds plugin by its type and id. */
reiser4_plug_t *reiser4_factory_ifind(rid_t type, rid_t id) {
	if (type >= LAST_PLUG_TYPE)
		return 0;

	if (id >= plug_type_count(type))
		return 0;
	
	return plugins[plug_hash_func(type, id)];
}

/* Finds plugin by variable criterios implemented by passed @plug_func. */
reiser4_plug_t *reiser4_factory_cfind(plug_func_t plug_func, void *data) {
	errno_t res;
	uint8_t i;
	
	aal_assert("vpf-1886", plug_func != NULL);

	for (i = 0; i < plugs_max[LAST_PLUG_TYPE]; i++) {
		if (plugins[i] && (res = plug_func(plugins[i], data)))
			return plugins[i];
	}
	
	return NULL;
}

#ifndef ENABLE_MINIMAL
/* Makes search for plugin by @name. */
reiser4_plug_t *reiser4_factory_nfind(char *name) {
	uint8_t i;
	
	for (i = 0; i < plugs_max[LAST_PLUG_TYPE]; i++) {
		if (!plugins[i])
			continue;
		
		if (!aal_strncmp(plugins[i]->label, name,
				 sizeof(plugins[i]->label)))
		{
			return plugins[i];
		}
	}
	
	return NULL;
}
#endif