xref: /dragonfly/sys/dev/disk/dm/dm_pdev.c (revision bcb3e04d)
1 /*        $NetBSD: dm_pdev.c,v 1.6 2010/01/04 00:19:08 haad 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 
32 #include <sys/types.h>
33 #include <sys/param.h>
34 
35 #include <sys/disk.h>
36 #include <sys/fcntl.h>
37 #include <sys/malloc.h>
38 #include <sys/namei.h>
39 #include <sys/vnode.h>
40 #include <sys/nlookup.h>
41 
42 #include "dm.h"
43 
44 SLIST_HEAD(dm_pdevs, dm_pdev) dm_pdev_list;
45 
46 	struct lock dm_pdev_mutex;
47 
48 	static dm_pdev_t *dm_pdev_alloc(const char *);
49 	static int dm_pdev_rem(dm_pdev_t *);
50 	static dm_pdev_t *dm_pdev_lookup_name(const char *);
51 
52 /*
53  * Find used pdev with name == dm_pdev_name.
54  */
55 	dm_pdev_t *
56 	          dm_pdev_lookup_name(const char *dm_pdev_name)
57 {
58 	dm_pdev_t *dm_pdev;
59 	int dlen;
60 	int slen;
61 
62 	KKASSERT(dm_pdev_name != NULL);
63 
64 	slen = strlen(dm_pdev_name);
65 
66 	SLIST_FOREACH(dm_pdev, &dm_pdev_list, next_pdev) {
67 		dlen = strlen(dm_pdev->name);
68 
69 		if (slen != dlen)
70 			continue;
71 
72 		if (strncmp(dm_pdev_name, dm_pdev->name, slen) == 0)
73 			return dm_pdev;
74 	}
75 
76 	return NULL;
77 }
78 
79 static int
80 dm_dk_lookup(const char *dev_name, struct vnode **vpp)
81 {
82 	struct nlookupdata nd;
83 	int error;
84 
85 #ifdef notyet
86 	if (this is a root mount) {
87 		error = vn_opendisk(dev_name, FREAD|FWRITE, vpp);
88 		return error;
89 	}
90 #endif
91 
92 	error = nlookup_init(&nd, dev_name, UIO_SYSSPACE, NLC_FOLLOW);
93 	if (error)
94 	    return error;
95 
96 	error = vn_open(&nd, NULL, FREAD|FWRITE, 0);
97 	*vpp = nd.nl_open_vp;
98 	nd.nl_open_vp = NULL;
99 	nlookup_done(&nd);
100 
101 	return 0;
102 }
103 
104 off_t
105 dm_pdev_correct_dump_offset(dm_pdev_t *pdev, off_t offset)
106 {
107 	off_t noffset;
108 
109 	noffset = pdev->pdev_pinfo.reserved_blocks +
110 	    pdev->pdev_pinfo.media_offset / pdev->pdev_pinfo.media_blksize;
111 	noffset *= DEV_BSIZE;
112 	noffset += offset;
113 
114 	return noffset;
115 }
116 
117 /*
118  * Create entry for device with name dev_name and open vnode for it.
119  * If entry already exists in global SLIST I will only increment
120  * reference counter.
121  */
122 dm_pdev_t *
123 dm_pdev_insert(const char *dev_name)
124 {
125 	dm_pdev_t *dmp;
126 	int error;
127 
128 	KKASSERT(dev_name != NULL);
129 
130 	lockmgr(&dm_pdev_mutex, LK_EXCLUSIVE);
131 	dmp = dm_pdev_lookup_name(dev_name);
132 
133 	if (dmp != NULL) {
134 		dmp->ref_cnt++;
135 		aprint_debug("dmp_pdev_insert pdev %s already in tree\n", dev_name);
136 		lockmgr(&dm_pdev_mutex, LK_RELEASE);
137 		return dmp;
138 	}
139 	lockmgr(&dm_pdev_mutex, LK_RELEASE);
140 
141 	if ((dmp = dm_pdev_alloc(dev_name)) == NULL)
142 		return NULL;
143 
144 	/* XXX: nlookup and/or vn_opendisk */
145 	error = dm_dk_lookup(dev_name, &dmp->pdev_vnode);
146 	if (error) {
147 		aprint_debug("dk_lookup on device: %s failed with error %d!\n",
148 		    dev_name, error);
149 		kfree(dmp, M_DM);
150 		return NULL;
151 	}
152 	dmp->ref_cnt = 1;
153 
154 	/*
155 	 * Get us the partinfo from the underlying device, it's needed for
156 	 * dumps.
157 	 */
158 	bzero(&dmp->pdev_pinfo, sizeof(dmp->pdev_pinfo));
159 	error = dev_dioctl(dmp->pdev_vnode->v_rdev, DIOCGPART,
160 	    (void *)&dmp->pdev_pinfo, 0, proc0.p_ucred, NULL);
161 
162 	lockmgr(&dm_pdev_mutex, LK_EXCLUSIVE);
163 	SLIST_INSERT_HEAD(&dm_pdev_list, dmp, next_pdev);
164 	lockmgr(&dm_pdev_mutex, LK_RELEASE);
165 
166 	return dmp;
167 }
168 /*
169  * Initialize pdev subsystem.
170  */
171 int
172 dm_pdev_init(void)
173 {
174 	SLIST_INIT(&dm_pdev_list);	/* initialize global pdev list */
175 	lockinit(&dm_pdev_mutex, "dmpdev", 0, LK_CANRECURSE);
176 
177 	return 0;
178 }
179 /*
180  * Allocat new pdev structure if is not already present and
181  * set name.
182  */
183 static dm_pdev_t *
184 dm_pdev_alloc(const char *name)
185 {
186 	dm_pdev_t *dmp;
187 
188 	if ((dmp = kmalloc(sizeof(dm_pdev_t), M_DM, M_WAITOK | M_ZERO)) == NULL)
189 		return NULL;
190 
191 	strlcpy(dmp->name, name, MAX_DEV_NAME);
192 
193 	dmp->ref_cnt = 0;
194 	dmp->pdev_vnode = NULL;
195 
196 	return dmp;
197 }
198 /*
199  * Destroy allocated dm_pdev.
200  */
201 static int
202 dm_pdev_rem(dm_pdev_t * dmp)
203 {
204 	int err;
205 
206 	KKASSERT(dmp != NULL);
207 
208 	if (dmp->pdev_vnode != NULL) {
209 		err = vn_close(dmp->pdev_vnode, FREAD | FWRITE);
210 		if (err != 0)
211 			return err;
212 	}
213 	kfree(dmp, M_DM);
214 	dmp = NULL;
215 
216 	return 0;
217 }
218 /*
219  * Destroy all existing pdev's in device-mapper.
220  */
221 int
222 dm_pdev_destroy(void)
223 {
224 	dm_pdev_t *dm_pdev;
225 
226 	lockmgr(&dm_pdev_mutex, LK_EXCLUSIVE);
227 	while (!SLIST_EMPTY(&dm_pdev_list)) {	/* List Deletion. */
228 
229 		dm_pdev = SLIST_FIRST(&dm_pdev_list);
230 
231 		SLIST_REMOVE_HEAD(&dm_pdev_list, next_pdev);
232 
233 		dm_pdev_rem(dm_pdev);
234 	}
235 	lockmgr(&dm_pdev_mutex, LK_RELEASE);
236 
237 	lockuninit(&dm_pdev_mutex);
238 	return 0;
239 }
240 /*
241  * This funcion is called from dm_dev_remove_ioctl.
242  * When I'm removing device from list, I have to decrement
243  * reference counter. If reference counter is 0 I will remove
244  * dmp from global list and from device list to. And I will CLOSE
245  * dmp vnode too.
246  */
247 
248 /*
249  * Decrement pdev reference counter if 0 remove it.
250  */
251 int
252 dm_pdev_decr(dm_pdev_t * dmp)
253 {
254 	KKASSERT(dmp != NULL);
255 	/*
256 	 * If this was last reference remove dmp from
257 	 * global list also.
258 	 */
259 	lockmgr(&dm_pdev_mutex, LK_EXCLUSIVE);
260 
261 	if (--dmp->ref_cnt == 0) {
262 		SLIST_REMOVE(&dm_pdev_list, dmp, dm_pdev, next_pdev);
263 		lockmgr(&dm_pdev_mutex, LK_RELEASE);
264 		dm_pdev_rem(dmp);
265 		return 0;
266 	}
267 	lockmgr(&dm_pdev_mutex, LK_RELEASE);
268 	return 0;
269 }
270 /*static int
271   dm_pdev_dump_list(void)
272   {
273   dm_pdev_t *dmp;
274 
275   aprint_verbose("Dumping dm_pdev_list \n");
276 
277   SLIST_FOREACH(dmp, &dm_pdev_list, next_pdev) {
278   aprint_verbose("dm_pdev_name %s ref_cnt %d list_rf_cnt %d\n",
279   dmp->name, dmp->ref_cnt, dmp->list_ref_cnt);
280   }
281 
282   return 0;
283 
284   }*/
285