xref: /netbsd-src/sys/dev/dm/dm_pdev.c (revision 46f1475944f96f1efcd79b92337afebc7c7ba7d9)
1 /*        $NetBSD: dm_pdev.c,v 1.21 2019/12/15 14:39:42 tkusumi Exp $      */
2 
3 /*
4  * Copyright (c) 2008 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Adam Hamsik.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: dm_pdev.c,v 1.21 2019/12/15 14:39:42 tkusumi Exp $");
33 
34 #include <sys/types.h>
35 #include <sys/param.h>
36 #include <sys/disk.h>
37 #include <sys/fcntl.h>
38 #include <sys/kmem.h>
39 #include <sys/namei.h>
40 
41 #include <dev/dkvar.h>
42 
43 #include "dm.h"
44 
45 static SLIST_HEAD(dm_pdevs, dm_pdev) dm_pdev_list;
46 
47 static kmutex_t dm_pdev_mutex;
48 
49 static dm_pdev_t *dm_pdev_alloc(const char *);
50 static int dm_pdev_rem(dm_pdev_t *);
51 static dm_pdev_t *dm_pdev_lookup_name(const char *);
52 
53 /*
54 * Find used pdev with name == dm_pdev_name.
55 */
56 dm_pdev_t *
57 dm_pdev_lookup_name(const char *dm_pdev_name)
58 {
59 	dm_pdev_t *dm_pdev;
60 	size_t dlen;
61 	size_t slen;
62 
63 	KASSERT(dm_pdev_name != NULL);
64 
65 	slen = strlen(dm_pdev_name);
66 
67 	SLIST_FOREACH(dm_pdev, &dm_pdev_list, next_pdev) {
68 		dlen = strlen(dm_pdev->name);
69 
70 		if (slen != dlen)
71 			continue;
72 
73 		if (strncmp(dm_pdev_name, dm_pdev->name, slen) == 0)
74 			return dm_pdev;
75 	}
76 
77 	return NULL;
78 }
79 
80 /*
81  * Create entry for device with name dev_name and open vnode for it.
82  * If entry already exists in global SLIST I will only increment
83  * reference counter.
84  */
85 dm_pdev_t *
86 dm_pdev_insert(const char *dev_name)
87 {
88 	struct pathbuf *dev_pb;
89 	dm_pdev_t *dmp;
90 	int error;
91 
92 	KASSERT(dev_name != NULL);
93 
94 	mutex_enter(&dm_pdev_mutex);
95 	dmp = dm_pdev_lookup_name(dev_name);
96 
97 	if (dmp != NULL) {
98 		dmp->ref_cnt++;
99 		aprint_debug("%s: pdev %s already in tree\n",
100 		    __func__, dev_name);
101 		mutex_exit(&dm_pdev_mutex);
102 		return dmp;
103 	}
104 
105 	if ((dmp = dm_pdev_alloc(dev_name)) == NULL) {
106 		mutex_exit(&dm_pdev_mutex);
107 		return NULL;
108 	}
109 
110 	dev_pb = pathbuf_create(dev_name);
111 	if (dev_pb == NULL) {
112 		aprint_debug("%s: pathbuf_create on device: %s failed!\n",
113 		    __func__, dev_name);
114 		mutex_exit(&dm_pdev_mutex);
115 		kmem_free(dmp, sizeof(dm_pdev_t));
116 		return NULL;
117 	}
118 	error = vn_bdev_openpath(dev_pb, &dmp->pdev_vnode, curlwp);
119 	pathbuf_destroy(dev_pb);
120 	if (error) {
121 		aprint_debug("%s: lookup on device: %s (error %d)\n",
122 		    __func__, dev_name, error);
123 		mutex_exit(&dm_pdev_mutex);
124 		kmem_free(dmp, sizeof(dm_pdev_t));
125 		return NULL;
126 	}
127 	getdisksize(dmp->pdev_vnode, &dmp->pdev_numsec, &dmp->pdev_secsize);
128 	dmp->ref_cnt = 1;
129 
130 	SLIST_INSERT_HEAD(&dm_pdev_list, dmp, next_pdev);
131 	mutex_exit(&dm_pdev_mutex);
132 
133 	return dmp;
134 }
135 
136 /*
137  * Initialize pdev subsystem.
138  */
139 int
140 dm_pdev_init(void)
141 {
142 
143 	SLIST_INIT(&dm_pdev_list);	/* initialize global pdev list */
144 	mutex_init(&dm_pdev_mutex, MUTEX_DEFAULT, IPL_NONE);
145 
146 	return 0;
147 }
148 
149 /*
150  * Allocat new pdev structure if is not already present and
151  * set name.
152  */
153 static dm_pdev_t *
154 dm_pdev_alloc(const char *name)
155 {
156 	dm_pdev_t *dmp;
157 
158 	dmp = kmem_zalloc(sizeof(*dmp), KM_SLEEP);
159 	strlcpy(dmp->name, name, sizeof(dmp->name));
160 	dmp->ref_cnt = 0;
161 	dmp->pdev_vnode = NULL;
162 
163 	return dmp;
164 }
165 
166 /*
167  * Destroy allocated dm_pdev.
168  */
169 static int
170 dm_pdev_rem(dm_pdev_t *dmp)
171 {
172 
173 	KASSERT(dmp != NULL);
174 
175 	if (dmp->pdev_vnode != NULL) {
176 		int error = vn_close(dmp->pdev_vnode, FREAD | FWRITE, FSCRED);
177 		if (error != 0) {
178 			kmem_free(dmp, sizeof(*dmp));
179 			return error;
180 		}
181 	}
182 	kmem_free(dmp, sizeof(*dmp));
183 
184 	return 0;
185 }
186 
187 /*
188  * Destroy all existing pdev's in device-mapper.
189  */
190 int
191 dm_pdev_destroy(void)
192 {
193 	dm_pdev_t *dmp;
194 
195 	mutex_enter(&dm_pdev_mutex);
196 
197 	while ((dmp = SLIST_FIRST(&dm_pdev_list)) != NULL) {
198 		SLIST_REMOVE(&dm_pdev_list, dmp, dm_pdev, next_pdev);
199 		dm_pdev_rem(dmp);
200 	}
201 	KASSERT(SLIST_EMPTY(&dm_pdev_list));
202 
203 	mutex_exit(&dm_pdev_mutex);
204 
205 	mutex_destroy(&dm_pdev_mutex);
206 	return 0;
207 }
208 
209 /*
210  * This funcion is called from dm_dev_remove_ioctl.
211  * When I'm removing device from list, I have to decrement
212  * reference counter. If reference counter is 0 I will remove
213  * dmp from global list and from device list to. And I will CLOSE
214  * dmp vnode too.
215  */
216 
217 /*
218  * Decrement pdev reference counter if 0 remove it.
219  */
220 int
221 dm_pdev_decr(dm_pdev_t *dmp)
222 {
223 
224 	KASSERT(dmp != NULL);
225 	/*
226 	 * If this was last reference remove dmp from
227 	 * global list also.
228 	 */
229 	mutex_enter(&dm_pdev_mutex);
230 
231 	if (--dmp->ref_cnt == 0) {
232 		SLIST_REMOVE(&dm_pdev_list, dmp, dm_pdev, next_pdev);
233 		mutex_exit(&dm_pdev_mutex);
234 		dm_pdev_rem(dmp);
235 		return 0;
236 	}
237 
238 	mutex_exit(&dm_pdev_mutex);
239 	return 0;
240 }
241 
242 #if 0
243 static int
244 dm_pdev_dump_list(void)
245 {
246 	dm_pdev_t *dmp;
247 
248 	aprint_verbose("Dumping dm_pdev_list\n");
249 
250 	SLIST_FOREACH(dmp, &dm_pdev_list, next_pdev) {
251 		aprint_verbose("dm_pdev_name %s ref_cnt %d list_rf_cnt %d\n",
252 		dmp->name, dmp->ref_cnt, dmp->list_ref_cnt);
253 	}
254 
255 	return 0;
256 }
257 #endif
258