6984775497c7335f9bfa28ab594595d7d1e058a6
[dragonfly.git] / sys / dev / disk / dm / dm_pdev.c
1 /*        $NetBSD: dm_pdev.c,v 1.6 2010/01/04 00:19:08 haad Exp $      */
2
3 /*
4  * Copyright (c) 2010-2011 Alex Hornung <alex@alexhornung.com>
5  * Copyright (c) 2008 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Adam Hamsik.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #include <sys/types.h>
34 #include <sys/disk.h>
35 #include <sys/fcntl.h>
36 #include <sys/malloc.h>
37 #include <sys/namei.h>
38 #include <sys/vnode.h>
39 #include <sys/nlookup.h>
40
41 #include <dev/disk/dm/dm.h>
42
43 static TAILQ_HEAD(, dm_pdev) dm_pdev_list;
44
45 static struct lock dm_pdev_mutex;
46
47 static dm_pdev_t *dm_pdev_alloc(const char *);
48 static int dm_pdev_free(dm_pdev_t *);
49 static dm_pdev_t *dm_pdev_lookup_name(const char *);
50
51 /*
52  * Find used pdev with name == dm_pdev_name.
53  * needs to be called with the dm_pdev_mutex held.
54  */
55 static dm_pdev_t *
56 dm_pdev_lookup_name(const char *dm_pdev_name)
57 {
58         dm_pdev_t *dmp;
59
60         KKASSERT(dm_pdev_name != NULL);
61
62         TAILQ_FOREACH(dmp, &dm_pdev_list, next_pdev) {
63                 if (strcmp(dm_pdev_name, dmp->name) == 0)
64                         return dmp;
65         }
66
67         return NULL;
68 }
69
70 static int
71 dm_dk_lookup(const char *dev_name, struct vnode **vpp)
72 {
73         struct nlookupdata nd;
74         int error;
75
76         error = nlookup_init(&nd, dev_name, UIO_SYSSPACE, NLC_FOLLOW);
77         if (error)
78                 return error;
79
80         error = vn_open(&nd, NULL, FREAD|FWRITE, 0);
81         if (error) {
82                 nlookup_done(&nd);
83                 return error;
84         }
85
86         *vpp = nd.nl_open_vp;
87         nd.nl_open_vp = NULL;
88         nlookup_done(&nd);
89
90         return 0;
91 }
92
93 /*
94  * Since dm can have arbitrary stacking on any number of disks and any dm
95  * volume is at least stacked onto another disk, we need to adjust the
96  * dumping offset (which is a raw offset from the beginning of the lowest
97  * physical disk) taking into account the offset of the underlying device
98  * which in turn takes into account the offset below it, etc.
99  *
100  * This function adjusts the dumping offset that is passed to the next
101  * dev_ddump() so it is correct for that underlying device.
102  */
103 off_t
104 dm_pdev_correct_dump_offset(dm_pdev_t *pdev, off_t offset)
105 {
106         off_t noffset;
107
108         noffset = pdev->pdev_pinfo.reserved_blocks +
109             pdev->pdev_pinfo.media_offset / pdev->pdev_pinfo.media_blksize;
110         noffset *= DEV_BSIZE;
111         noffset += offset;
112
113         return noffset;
114 }
115
116 /*
117  * Create entry for device with name dev_name and open vnode for it.
118  * If entry already exists in global TAILQ I will only increment
119  * reference counter.
120  */
121 dm_pdev_t *
122 dm_pdev_insert(const char *dev_name)
123 {
124         dm_pdev_t *dmp;
125         struct vattr va;
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                 dmdebug("dmp_pdev_insert pdev %s already in tree\n", dev_name);
136                 lockmgr(&dm_pdev_mutex, LK_RELEASE);
137                 return dmp;
138         }
139
140         if ((dmp = dm_pdev_alloc(dev_name)) == NULL) {
141                 lockmgr(&dm_pdev_mutex, LK_RELEASE);
142                 return NULL;
143         }
144
145         error = dm_dk_lookup(dev_name, &dmp->pdev_vnode);
146         if (error) {
147                 dmdebug("dk_lookup on device: %s failed with error %d!\n",
148                     dev_name, error);
149                 dm_pdev_free(dmp);
150                 lockmgr(&dm_pdev_mutex, LK_RELEASE);
151                 return NULL;
152         }
153         dmp->ref_cnt = 1;
154
155         if (dm_pdev_get_vattr(dmp, &va) == -1) {
156                 dmdebug("makeudev %s failed\n", dev_name);
157                 dm_pdev_free(dmp);
158                 lockmgr(&dm_pdev_mutex, LK_RELEASE);
159                 return NULL;
160         }
161         ksnprintf(dmp->udev_name, sizeof(dmp->udev_name),
162                 "%d:%d", va.va_rmajor, va.va_rminor);
163         dmp->udev = dm_pdev_get_udev(dmp);
164
165         /*
166          * Get us the partinfo from the underlying device, it's needed for
167          * dumps.
168          */
169         bzero(&dmp->pdev_pinfo, sizeof(dmp->pdev_pinfo));
170         error = dev_dioctl(dmp->pdev_vnode->v_rdev, DIOCGPART,
171             (void *)&dmp->pdev_pinfo, 0, proc0.p_ucred, NULL, NULL);
172         if (!error) {
173                 struct partinfo *dpart = &dmp->pdev_pinfo;
174                 dmdebug("dmp_pdev_insert DIOCGPART "
175                         "offset=%ju size=%ju blocks=%ju blksize=%d\n",
176                         dpart->media_offset,
177                         dpart->media_size,
178                         dpart->media_blocks,
179                         dpart->media_blksize);
180         } else {
181                 kprintf("dmp_pdev_insert DIOCGPART failed %d\n", error);
182         }
183
184         TAILQ_INSERT_TAIL(&dm_pdev_list, dmp, next_pdev);
185         lockmgr(&dm_pdev_mutex, LK_RELEASE);
186
187         dmdebug("dmp_pdev_insert pdev %s %s 0x%016jx\n",
188                 dmp->name, dmp->udev_name, (uintmax_t)dmp->udev);
189
190         return dmp;
191 }
192
193 /*
194  * Allocat new pdev structure if is not already present and
195  * set name.
196  */
197 static dm_pdev_t *
198 dm_pdev_alloc(const char *name)
199 {
200         dm_pdev_t *dmp;
201
202         dmp = kmalloc(sizeof(*dmp), M_DM, M_WAITOK | M_ZERO);
203         if (dmp == NULL)
204                 return NULL;
205
206         if (name)
207                 strlcpy(dmp->name, name, DM_MAX_DEV_NAME);
208
209         return dmp;
210 }
211
212 /*
213  * Destroy allocated dm_pdev.
214  */
215 static int
216 dm_pdev_free(dm_pdev_t *dmp)
217 {
218         int err;
219
220         KKASSERT(dmp != NULL);
221
222         if (dmp->pdev_vnode != NULL) {
223                 err = vn_close(dmp->pdev_vnode, FREAD | FWRITE, NULL);
224                 if (err != 0) {
225                         kfree(dmp, M_DM);
226                         return err;
227                 }
228         }
229         kfree(dmp, M_DM);
230
231         return 0;
232 }
233
234 /*
235  * This funcion is called from targets' destroy() handler.
236  * When I'm removing device from list, I have to decrement
237  * reference counter. If reference counter is 0 I will remove
238  * dmp from global list and from device list to. And I will CLOSE
239  * dmp vnode too.
240  */
241 /*
242  * Decrement pdev reference counter if 0 remove it.
243  */
244 int
245 dm_pdev_decr(dm_pdev_t *dmp)
246 {
247         KKASSERT(dmp != NULL);
248         /*
249          * If this was last reference remove dmp from
250          * global list also.
251          */
252         lockmgr(&dm_pdev_mutex, LK_EXCLUSIVE);
253
254         if (--dmp->ref_cnt == 0) {
255                 TAILQ_REMOVE(&dm_pdev_list, dmp, next_pdev);
256                 lockmgr(&dm_pdev_mutex, LK_RELEASE);
257                 dm_pdev_free(dmp);
258                 return 0;
259         }
260         lockmgr(&dm_pdev_mutex, LK_RELEASE);
261         return 0;
262 }
263
264 uint64_t
265 dm_pdev_get_udev(dm_pdev_t *dmp)
266 {
267         struct vattr va;
268         int ret;
269
270         if (dmp->pdev_vnode == NULL)
271                 return (uint64_t)-1;
272
273         ret = dm_pdev_get_vattr(dmp, &va);
274         if (ret)
275                 return (uint64_t)-1;
276
277         ret = makeudev(va.va_rmajor, va.va_rminor);
278
279         return ret;
280 }
281
282 int
283 dm_pdev_get_vattr(dm_pdev_t *dmp, struct vattr *vap)
284 {
285         int ret;
286
287         if (dmp->pdev_vnode == NULL)
288                 return -1;
289
290         KKASSERT(vap);
291         ret = VOP_GETATTR(dmp->pdev_vnode, vap);
292         if (ret)
293                 return -1;
294
295         return 0;
296 }
297
298 /*
299  * Initialize pdev subsystem.
300  */
301 int
302 dm_pdev_init(void)
303 {
304         TAILQ_INIT(&dm_pdev_list);      /* initialize global pdev list */
305         lockinit(&dm_pdev_mutex, "dmpdev", 0, LK_CANRECURSE);
306
307         return 0;
308 }
309
310 /*
311  * Destroy all existing pdev's in device-mapper.
312  */
313 int
314 dm_pdev_uninit(void)
315 {
316         dm_pdev_t *dmp;
317
318         lockmgr(&dm_pdev_mutex, LK_EXCLUSIVE);
319
320         while ((dmp = TAILQ_FIRST(&dm_pdev_list)) != NULL) {
321                 TAILQ_REMOVE(&dm_pdev_list, dmp, next_pdev);
322                 dm_pdev_free(dmp);
323         }
324         KKASSERT(TAILQ_EMPTY(&dm_pdev_list));
325
326         lockmgr(&dm_pdev_mutex, LK_RELEASE);
327
328         lockuninit(&dm_pdev_mutex);
329         return 0;
330 }