dm - fix typo in previous commit
[dragonfly.git] / sys / dev / disk / dm / dm_pdev.c
CommitLineData
ff56536e
AH
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>
5b279a20 37#include <sys/malloc.h>
ff56536e
AH
38#include <sys/namei.h>
39#include <sys/vnode.h>
5b279a20 40#include <sys/nlookup.h>
ff56536e 41
a84e173e 42#include <dev/disk/dm/dm.h>
ff56536e
AH
43
44SLIST_HEAD(dm_pdevs, dm_pdev) dm_pdev_list;
45
add6c27f 46struct lock dm_pdev_mutex;
ff56536e 47
add6c27f
AH
48static dm_pdev_t *dm_pdev_alloc(const char *);
49static int dm_pdev_rem(dm_pdev_t *);
50static dm_pdev_t *dm_pdev_lookup_name(const char *);
ff56536e
AH
51
52/*
53 * Find used pdev with name == dm_pdev_name.
add6c27f 54 * needs to be called with the dm_pdev_mutex held.
ff56536e 55 */
add6c27f
AH
56static dm_pdev_t *
57dm_pdev_lookup_name(const char *dm_pdev_name)
ff56536e
AH
58{
59 dm_pdev_t *dm_pdev;
ff56536e 60
5b279a20 61 KKASSERT(dm_pdev_name != NULL);
ff56536e 62
ff56536e 63 SLIST_FOREACH(dm_pdev, &dm_pdev_list, next_pdev) {
add6c27f 64 if (strcmp(dm_pdev_name, dm_pdev->name) == 0)
ff56536e
AH
65 return dm_pdev;
66 }
67
68 return NULL;
69}
5b279a20
AH
70
71static int
72dm_dk_lookup(const char *dev_name, struct vnode **vpp)
73{
74 struct nlookupdata nd;
75 int error;
76
5b279a20
AH
77 error = nlookup_init(&nd, dev_name, UIO_SYSSPACE, NLC_FOLLOW);
78 if (error)
776654af 79 return error;
5b279a20
AH
80
81 error = vn_open(&nd, NULL, FREAD|FWRITE, 0);
776654af 82 if (error) {
c9d49f70 83 nlookup_done(&nd);
776654af
AH
84 return error;
85 }
86
5b279a20
AH
87 *vpp = nd.nl_open_vp;
88 nd.nl_open_vp = NULL;
89 nlookup_done(&nd);
90
91 return 0;
92}
93
add6c27f
AH
94/*
95 * Since dm can have arbitrary stacking on any number of disks and any dm
96 * volume is at least stacked onto another disk, we need to adjust the
97 * dumping offset (which is a raw offset from the beginning of the lowest
98 * physical disk) taking into account the offset of the underlying device
99 * which in turn takes into account the offset below it, etc.
100 *
101 * This function adjusts the dumping offset that is passed to the next
102 * dev_ddump() so it is correct for that underlying device.
103 */
79b7159f
AH
104off_t
105dm_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
ff56536e
AH
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 */
122dm_pdev_t *
123dm_pdev_insert(const char *dev_name)
124{
125 dm_pdev_t *dmp;
126 int error;
127
5b279a20 128 KKASSERT(dev_name != NULL);
ff56536e 129
5b279a20 130 lockmgr(&dm_pdev_mutex, LK_EXCLUSIVE);
ff56536e
AH
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);
5b279a20 136 lockmgr(&dm_pdev_mutex, LK_RELEASE);
ff56536e
AH
137 return dmp;
138 }
5b279a20 139 lockmgr(&dm_pdev_mutex, LK_RELEASE);
ff56536e
AH
140
141 if ((dmp = dm_pdev_alloc(dev_name)) == NULL)
142 return NULL;
143
5b279a20 144 error = dm_dk_lookup(dev_name, &dmp->pdev_vnode);
ff56536e
AH
145 if (error) {
146 aprint_debug("dk_lookup on device: %s failed with error %d!\n",
147 dev_name, error);
5b279a20 148 kfree(dmp, M_DM);
ff56536e
AH
149 return NULL;
150 }
151 dmp->ref_cnt = 1;
152
79b7159f
AH
153 /*
154 * Get us the partinfo from the underlying device, it's needed for
155 * dumps.
156 */
157 bzero(&dmp->pdev_pinfo, sizeof(dmp->pdev_pinfo));
158 error = dev_dioctl(dmp->pdev_vnode->v_rdev, DIOCGPART,
159 (void *)&dmp->pdev_pinfo, 0, proc0.p_ucred, NULL);
160
5b279a20 161 lockmgr(&dm_pdev_mutex, LK_EXCLUSIVE);
ff56536e 162 SLIST_INSERT_HEAD(&dm_pdev_list, dmp, next_pdev);
5b279a20 163 lockmgr(&dm_pdev_mutex, LK_RELEASE);
ff56536e
AH
164
165 return dmp;
166}
ff56536e 167
ff56536e
AH
168/*
169 * Allocat new pdev structure if is not already present and
170 * set name.
171 */
172static dm_pdev_t *
173dm_pdev_alloc(const char *name)
174{
175 dm_pdev_t *dmp;
176
5b279a20 177 if ((dmp = kmalloc(sizeof(dm_pdev_t), M_DM, M_WAITOK | M_ZERO)) == NULL)
ff56536e
AH
178 return NULL;
179
180 strlcpy(dmp->name, name, MAX_DEV_NAME);
181
182 dmp->ref_cnt = 0;
183 dmp->pdev_vnode = NULL;
184
185 return dmp;
186}
187/*
188 * Destroy allocated dm_pdev.
189 */
190static int
191dm_pdev_rem(dm_pdev_t * dmp)
192{
193 int err;
194
5b279a20 195 KKASSERT(dmp != NULL);
ff56536e
AH
196
197 if (dmp->pdev_vnode != NULL) {
5b279a20 198 err = vn_close(dmp->pdev_vnode, FREAD | FWRITE);
ff56536e
AH
199 if (err != 0)
200 return err;
201 }
5b279a20 202 kfree(dmp, M_DM);
ff56536e
AH
203 dmp = NULL;
204
205 return 0;
206}
ff56536e 207
ff56536e
AH
208/*
209 * This funcion is called from dm_dev_remove_ioctl.
210 * When I'm removing device from list, I have to decrement
211 * reference counter. If reference counter is 0 I will remove
212 * dmp from global list and from device list to. And I will CLOSE
213 * dmp vnode too.
214 */
215
216/*
217 * Decrement pdev reference counter if 0 remove it.
218 */
219int
220dm_pdev_decr(dm_pdev_t * dmp)
221{
5b279a20 222 KKASSERT(dmp != NULL);
ff56536e
AH
223 /*
224 * If this was last reference remove dmp from
225 * global list also.
226 */
5b279a20 227 lockmgr(&dm_pdev_mutex, LK_EXCLUSIVE);
ff56536e
AH
228
229 if (--dmp->ref_cnt == 0) {
230 SLIST_REMOVE(&dm_pdev_list, dmp, dm_pdev, next_pdev);
5b279a20 231 lockmgr(&dm_pdev_mutex, LK_RELEASE);
ff56536e
AH
232 dm_pdev_rem(dmp);
233 return 0;
234 }
5b279a20 235 lockmgr(&dm_pdev_mutex, LK_RELEASE);
ff56536e
AH
236 return 0;
237}
9fada28a
AH
238
239/*
240 * Initialize pdev subsystem.
241 */
242int
243dm_pdev_init(void)
244{
245 SLIST_INIT(&dm_pdev_list); /* initialize global pdev list */
246 lockinit(&dm_pdev_mutex, "dmpdev", 0, LK_CANRECURSE);
247
248 return 0;
249}
250
251/*
252 * Destroy all existing pdev's in device-mapper.
253 */
254int
255dm_pdev_uninit(void)
256{
257 dm_pdev_t *dm_pdev;
258
259 lockmgr(&dm_pdev_mutex, LK_EXCLUSIVE);
260 while (!SLIST_EMPTY(&dm_pdev_list)) { /* List Deletion. */
261
262 dm_pdev = SLIST_FIRST(&dm_pdev_list);
263
264 SLIST_REMOVE_HEAD(&dm_pdev_list, next_pdev);
265
266 dm_pdev_rem(dm_pdev);
267 }
268 lockmgr(&dm_pdev_mutex, LK_RELEASE);
269
270 lockuninit(&dm_pdev_mutex);
271 return 0;
272}