xref: /dflybsd-src/sys/dev/disk/dm/dm_target.c (revision a6b470c8dd8f2636821a0158478eb060535770f1)
1ff56536eSAlex Hornung /*        $NetBSD: dm_target.c,v 1.12 2010/01/04 00:14:41 haad Exp $      */
2ff56536eSAlex Hornung 
3ff56536eSAlex Hornung /*
4ba2ce341SAlex Hornung  * Copyright (c) 2010-2011 Alex Hornung <alex@alexhornung.com>
5ff56536eSAlex Hornung  * Copyright (c) 2008 The NetBSD Foundation, Inc.
6ff56536eSAlex Hornung  * All rights reserved.
7ff56536eSAlex Hornung  *
8ff56536eSAlex Hornung  * This code is derived from software contributed to The NetBSD Foundation
9ff56536eSAlex Hornung  * by Adam Hamsik.
10ff56536eSAlex Hornung  *
11ff56536eSAlex Hornung  * Redistribution and use in source and binary forms, with or without
12ff56536eSAlex Hornung  * modification, are permitted provided that the following conditions
13ff56536eSAlex Hornung  * are met:
14ff56536eSAlex Hornung  * 1. Redistributions of source code Must retain the above copyright
15ff56536eSAlex Hornung  *    notice, this list of conditions and the following disclaimer.
16ff56536eSAlex Hornung  * 2. Redistributions in binary form must reproduce the above copyright
17ff56536eSAlex Hornung  *    notice, this list of conditions and the following disclaimer in the
18ff56536eSAlex Hornung  *    documentation and/or other materials provided with the distribution.
19ff56536eSAlex Hornung  *
20ff56536eSAlex Hornung  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21ff56536eSAlex Hornung  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22ff56536eSAlex Hornung  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23ff56536eSAlex Hornung  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24ff56536eSAlex Hornung  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25ff56536eSAlex Hornung  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26ff56536eSAlex Hornung  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27ff56536eSAlex Hornung  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28ff56536eSAlex Hornung  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29ff56536eSAlex Hornung  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30ff56536eSAlex Hornung  * POSSIBILITY OF SUCH DAMAGE.
31ff56536eSAlex Hornung  */
32ff56536eSAlex Hornung 
33ff56536eSAlex Hornung #include <sys/types.h>
34ff56536eSAlex Hornung 
355b279a20SAlex Hornung #include <sys/malloc.h>
36ff56536eSAlex Hornung #include <sys/module.h>
377115a22bSAlex Hornung #include <sys/linker.h>
38a84e173eSAlex Hornung #include <dev/disk/dm/dm.h>
39ff56536eSAlex Hornung 
40ff56536eSAlex Hornung #include "netbsd-dm.h"
41ff56536eSAlex Hornung 
42ff56536eSAlex Hornung static dm_target_t *dm_target_lookup_name(const char *);
43ff56536eSAlex Hornung 
444e359672STomohiro Kusumi static TAILQ_HEAD(, dm_target) dm_target_list;
45ff56536eSAlex Hornung 
46637b454aSTomohiro Kusumi static struct lock dm_target_mutex;
47ff56536eSAlex Hornung 
48ff56536eSAlex Hornung /*
49ff56536eSAlex Hornung  * Called indirectly from dm_table_load_ioctl to mark target as used.
50ff56536eSAlex Hornung  */
51ff56536eSAlex Hornung void
52ff56536eSAlex Hornung dm_target_busy(dm_target_t *target)
53ff56536eSAlex Hornung {
545b279a20SAlex Hornung 	atomic_add_int(&target->ref_cnt, 1);
55ff56536eSAlex Hornung }
56ff56536eSAlex Hornung /*
57ff56536eSAlex Hornung  * Release reference counter on target.
58ff56536eSAlex Hornung  */
59ff56536eSAlex Hornung void
60ff56536eSAlex Hornung dm_target_unbusy(dm_target_t *target)
61ff56536eSAlex Hornung {
625b279a20SAlex Hornung 	KKASSERT(target->ref_cnt > 0);
635b279a20SAlex Hornung 	atomic_subtract_int(&target->ref_cnt, 1);
64ff56536eSAlex Hornung }
65ff56536eSAlex Hornung 
66ff56536eSAlex Hornung /*
677115a22bSAlex Hornung  * Try to autoload the module for the requested target.
687115a22bSAlex Hornung  */
697115a22bSAlex Hornung dm_target_t *
707115a22bSAlex Hornung dm_target_autoload(const char *dm_target_name)
717115a22bSAlex Hornung {
727115a22bSAlex Hornung 	char mod_name[128];
737115a22bSAlex Hornung 	dm_target_t *dmt;
747115a22bSAlex Hornung 	linker_file_t linker_file;
757115a22bSAlex Hornung 	int error;
767115a22bSAlex Hornung 
777115a22bSAlex Hornung 	ksnprintf(mod_name, sizeof(mod_name), "dm_target_%s", dm_target_name);
787115a22bSAlex Hornung 	error = linker_reference_module(mod_name, NULL, &linker_file);
797115a22bSAlex Hornung 	if (error != 0) {
807115a22bSAlex Hornung 		kprintf("dm: could not autoload module for target %s\n",
817115a22bSAlex Hornung 		    dm_target_name);
827115a22bSAlex Hornung 		return NULL;
837115a22bSAlex Hornung 	}
847115a22bSAlex Hornung 
857115a22bSAlex Hornung 	dmt = dm_target_lookup(dm_target_name);
867115a22bSAlex Hornung 	if (dmt == NULL) {
877115a22bSAlex Hornung 		linker_release_module(NULL, NULL, linker_file);
887115a22bSAlex Hornung 		return NULL;
897115a22bSAlex Hornung 	}
907115a22bSAlex Hornung 
917115a22bSAlex Hornung 	/* XXX: extra-big hack to allow users to kldunload the module */
927115a22bSAlex Hornung 	linker_file->userrefs = 1;
937115a22bSAlex Hornung 
947115a22bSAlex Hornung 	return dmt;
957115a22bSAlex Hornung }
967115a22bSAlex Hornung 
977115a22bSAlex Hornung /*
98ff56536eSAlex Hornung  * Lookup for target in global target list.
99ff56536eSAlex Hornung  */
100ff56536eSAlex Hornung dm_target_t *
101ff56536eSAlex Hornung dm_target_lookup(const char *dm_target_name)
102ff56536eSAlex Hornung {
103ff56536eSAlex Hornung 	dm_target_t *dmt;
104ff56536eSAlex Hornung 
105ff56536eSAlex Hornung 	dmt = NULL;
106ff56536eSAlex Hornung 
107ff56536eSAlex Hornung 	if (dm_target_name == NULL)
108ff56536eSAlex Hornung 		return NULL;
109ff56536eSAlex Hornung 
1105b279a20SAlex Hornung 	lockmgr(&dm_target_mutex, LK_EXCLUSIVE);
111ff56536eSAlex Hornung 
112ff56536eSAlex Hornung 	dmt = dm_target_lookup_name(dm_target_name);
113ff56536eSAlex Hornung 	if (dmt != NULL)
114ff56536eSAlex Hornung 		dm_target_busy(dmt);
115ff56536eSAlex Hornung 
1165b279a20SAlex Hornung 	lockmgr(&dm_target_mutex, LK_RELEASE);
117ff56536eSAlex Hornung 
118ff56536eSAlex Hornung 	return dmt;
119ff56536eSAlex Hornung }
120ff56536eSAlex Hornung /*
121ff56536eSAlex Hornung  * Search for name in TAIL and return apropriate pointer.
122ff56536eSAlex Hornung  */
123ff56536eSAlex Hornung static dm_target_t *
124ff56536eSAlex Hornung dm_target_lookup_name(const char *dm_target_name)
125ff56536eSAlex Hornung {
126ff56536eSAlex Hornung 	dm_target_t *dm_target;
127ff56536eSAlex Hornung 
128ff56536eSAlex Hornung 	TAILQ_FOREACH(dm_target, &dm_target_list, dm_target_next) {
1290d42d35bSAlex Hornung 		if (strcmp(dm_target_name, dm_target->name) == 0)
130ff56536eSAlex Hornung 			return dm_target;
131ff56536eSAlex Hornung 	}
132ff56536eSAlex Hornung 
133ff56536eSAlex Hornung 	return NULL;
134ff56536eSAlex Hornung }
135ff56536eSAlex Hornung /*
136ff56536eSAlex Hornung  * Insert new target struct into the TAIL.
137ff56536eSAlex Hornung  * dm_target
138ff56536eSAlex Hornung  *   contains name, version, function pointer to specifif target functions.
139ff56536eSAlex Hornung  */
140ff56536eSAlex Hornung int
141ff56536eSAlex Hornung dm_target_insert(dm_target_t *dm_target)
142ff56536eSAlex Hornung {
143ff56536eSAlex Hornung 	dm_target_t *dmt;
144ff56536eSAlex Hornung 
145b4e97860STomohiro Kusumi 	if (dm_target->init == NULL) {
146b4e97860STomohiro Kusumi 		kprintf("dm: %s missing init\n", dm_target->name);
147b4e97860STomohiro Kusumi 		return EINVAL;
148b4e97860STomohiro Kusumi 	}
149b4e97860STomohiro Kusumi 	if (dm_target->destroy == NULL) {
150b4e97860STomohiro Kusumi 		kprintf("dm: %s missing destroy\n", dm_target->name);
151b4e97860STomohiro Kusumi 		return EINVAL;
152b4e97860STomohiro Kusumi 	}
153b4e97860STomohiro Kusumi 	if (dm_target->strategy == NULL) {
154b4e97860STomohiro Kusumi 		kprintf("dm: %s missing strategy\n", dm_target->name);
155b4e97860STomohiro Kusumi 		return EINVAL;
156b4e97860STomohiro Kusumi 	}
157b4e97860STomohiro Kusumi 
1585b279a20SAlex Hornung 	lockmgr(&dm_target_mutex, LK_EXCLUSIVE);
159ff56536eSAlex Hornung 
160ff56536eSAlex Hornung 	dmt = dm_target_lookup_name(dm_target->name);
161ff56536eSAlex Hornung 	if (dmt != NULL) {
1625b279a20SAlex Hornung 		kprintf("uhoh, target_insert EEXIST\n");
1635b279a20SAlex Hornung 		lockmgr(&dm_target_mutex, LK_RELEASE);
164ff56536eSAlex Hornung 		return EEXIST;
165ff56536eSAlex Hornung 	}
166ff56536eSAlex Hornung 	TAILQ_INSERT_TAIL(&dm_target_list, dm_target, dm_target_next);
167ff56536eSAlex Hornung 
1685b279a20SAlex Hornung 	lockmgr(&dm_target_mutex, LK_RELEASE);
169ff56536eSAlex Hornung 
170ff56536eSAlex Hornung 	return 0;
171ff56536eSAlex Hornung }
172ff56536eSAlex Hornung 
173ff56536eSAlex Hornung 
174ff56536eSAlex Hornung /*
175ff56536eSAlex Hornung  * Remove target from TAIL, target is selected with it's name.
176ff56536eSAlex Hornung  */
177ff56536eSAlex Hornung int
178*a6b470c8STomohiro Kusumi dm_target_remove(char *dm_target_name)
179ff56536eSAlex Hornung {
180ff56536eSAlex Hornung 	dm_target_t *dmt;
181ff56536eSAlex Hornung 
1825b279a20SAlex Hornung 	KKASSERT(dm_target_name != NULL);
183ff56536eSAlex Hornung 
1845b279a20SAlex Hornung 	lockmgr(&dm_target_mutex, LK_EXCLUSIVE);
185ff56536eSAlex Hornung 
186ff56536eSAlex Hornung 	dmt = dm_target_lookup_name(dm_target_name);
187ff56536eSAlex Hornung 	if (dmt == NULL) {
1885b279a20SAlex Hornung 		lockmgr(&dm_target_mutex, LK_RELEASE);
189ff56536eSAlex Hornung 		return ENOENT;
190ff56536eSAlex Hornung 	}
191ff56536eSAlex Hornung 	if (dmt->ref_cnt > 0) {
1925b279a20SAlex Hornung 		lockmgr(&dm_target_mutex, LK_RELEASE);
193ff56536eSAlex Hornung 		return EBUSY;
194ff56536eSAlex Hornung 	}
195ff56536eSAlex Hornung 	TAILQ_REMOVE(&dm_target_list,
196ff56536eSAlex Hornung 	    dmt, dm_target_next);
197ff56536eSAlex Hornung 
1985b279a20SAlex Hornung 	lockmgr(&dm_target_mutex, LK_RELEASE);
199ff56536eSAlex Hornung 
200d880b9dfSTomohiro Kusumi 	dm_target_free(dmt);
201ff56536eSAlex Hornung 
202ff56536eSAlex Hornung 	return 0;
203ff56536eSAlex Hornung }
204ff56536eSAlex Hornung 
205ff56536eSAlex Hornung /*
206ff56536eSAlex Hornung  * Allocate new target entry.
207ff56536eSAlex Hornung  */
208ff56536eSAlex Hornung dm_target_t *
209ff56536eSAlex Hornung dm_target_alloc(const char *name)
210ff56536eSAlex Hornung {
211efaf91a7STomohiro Kusumi 	dm_target_t *dmt;
212efaf91a7STomohiro Kusumi 
213efaf91a7STomohiro Kusumi 	dmt = kmalloc(sizeof(*dmt), M_DM, M_WAITOK | M_ZERO);
214efaf91a7STomohiro Kusumi 	if (dmt == NULL)
215efaf91a7STomohiro Kusumi 		return NULL;
216efaf91a7STomohiro Kusumi 
217efaf91a7STomohiro Kusumi 	if (name)
218efaf91a7STomohiro Kusumi 		strlcpy(dmt->name, name, sizeof(dmt->name));
219efaf91a7STomohiro Kusumi 
220efaf91a7STomohiro Kusumi 	return dmt;
221ff56536eSAlex Hornung }
222efaf91a7STomohiro Kusumi 
223d880b9dfSTomohiro Kusumi int
224d880b9dfSTomohiro Kusumi dm_target_free(dm_target_t *dmt)
225d880b9dfSTomohiro Kusumi {
226d880b9dfSTomohiro Kusumi 	KKASSERT(dmt != NULL);
227d880b9dfSTomohiro Kusumi 
228d880b9dfSTomohiro Kusumi 	kfree(dmt, M_DM);
229d880b9dfSTomohiro Kusumi 
230d880b9dfSTomohiro Kusumi 	return 0;
231d880b9dfSTomohiro Kusumi }
232d880b9dfSTomohiro Kusumi 
233ff56536eSAlex Hornung /*
234ff56536eSAlex Hornung  * Return prop_array of dm_target dictionaries.
235ff56536eSAlex Hornung  */
236ff56536eSAlex Hornung prop_array_t
237ff56536eSAlex Hornung dm_target_prop_list(void)
238ff56536eSAlex Hornung {
239ff56536eSAlex Hornung 	prop_array_t target_array, ver;
240ff56536eSAlex Hornung 	prop_dictionary_t target_dict;
241ff56536eSAlex Hornung 	dm_target_t *dm_target;
242ff56536eSAlex Hornung 
243ff56536eSAlex Hornung 	size_t i;
244ff56536eSAlex Hornung 
245ff56536eSAlex Hornung 	target_array = prop_array_create();
246ff56536eSAlex Hornung 
2475b279a20SAlex Hornung 	lockmgr(&dm_target_mutex, LK_EXCLUSIVE);
248ff56536eSAlex Hornung 
249ff56536eSAlex Hornung 	TAILQ_FOREACH(dm_target, &dm_target_list, dm_target_next) {
250ff56536eSAlex Hornung 
251ff56536eSAlex Hornung 		target_dict = prop_dictionary_create();
252ff56536eSAlex Hornung 		ver = prop_array_create();
253ff56536eSAlex Hornung 		prop_dictionary_set_cstring(target_dict, DM_TARGETS_NAME,
254ff56536eSAlex Hornung 		    dm_target->name);
255ff56536eSAlex Hornung 
256ff56536eSAlex Hornung 		for (i = 0; i < 3; i++)
257ff56536eSAlex Hornung 			prop_array_add_uint32(ver, dm_target->version[i]);
258ff56536eSAlex Hornung 
259ff56536eSAlex Hornung 		prop_dictionary_set(target_dict, DM_TARGETS_VERSION, ver);
260ff56536eSAlex Hornung 		prop_array_add(target_array, target_dict);
261ff56536eSAlex Hornung 
262ff56536eSAlex Hornung 		prop_object_release(ver);
263ff56536eSAlex Hornung 		prop_object_release(target_dict);
264ff56536eSAlex Hornung 	}
265ff56536eSAlex Hornung 
2665b279a20SAlex Hornung 	lockmgr(&dm_target_mutex, LK_RELEASE);
267ff56536eSAlex Hornung 
268ff56536eSAlex Hornung 	return target_array;
269ff56536eSAlex Hornung }
2705c411e8eSAlex Hornung 
271ff56536eSAlex Hornung /* Initialize dm_target subsystem. */
272ff56536eSAlex Hornung int
273ff56536eSAlex Hornung dm_target_init(void)
274ff56536eSAlex Hornung {
2754e359672STomohiro Kusumi 	TAILQ_INIT(&dm_target_list);	/* initialize global target list */
2765b279a20SAlex Hornung 	lockinit(&dm_target_mutex, "dmtrgt", 0, LK_CANRECURSE);
277ff56536eSAlex Hornung 
2785c411e8eSAlex Hornung 	return 0;
279ff56536eSAlex Hornung }
2809fada28aSAlex Hornung 
2819fada28aSAlex Hornung /*
2829fada28aSAlex Hornung  * Destroy all targets and remove them from queue.
2837350b8b6STomohiro Kusumi  * This routine is called from dmdestroy, before module
2849fada28aSAlex Hornung  * is unloaded.
2859fada28aSAlex Hornung  */
2869fada28aSAlex Hornung int
2879fada28aSAlex Hornung dm_target_uninit(void)
2889fada28aSAlex Hornung {
2899fada28aSAlex Hornung 	dm_target_t *dm_target;
2909fada28aSAlex Hornung 
2919fada28aSAlex Hornung 	lockmgr(&dm_target_mutex, LK_EXCLUSIVE);
2929fada28aSAlex Hornung 
29333b3a12eSTomohiro Kusumi 	while ((dm_target = TAILQ_FIRST(&dm_target_list)) != NULL) {
29433b3a12eSTomohiro Kusumi 		TAILQ_REMOVE(&dm_target_list, dm_target, dm_target_next);
295d880b9dfSTomohiro Kusumi 		dm_target_free(dm_target);
2969fada28aSAlex Hornung 	}
29733b3a12eSTomohiro Kusumi 	KKASSERT(TAILQ_EMPTY(&dm_target_list));
29833b3a12eSTomohiro Kusumi 
2999fada28aSAlex Hornung 	lockmgr(&dm_target_mutex, LK_RELEASE);
3009fada28aSAlex Hornung 
3019fada28aSAlex Hornung 	lockuninit(&dm_target_mutex);
3029fada28aSAlex Hornung 
3039fada28aSAlex Hornung 	return 0;
3049fada28aSAlex Hornung }
305