sys/dev/disk/dm: Fix device counter handling
[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
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 <dev/disk/dm/dm.h>
43
44 static TAILQ_HEAD(, dm_pdev) dm_pdev_list;
45
46 static struct lock dm_pdev_mutex;
47
48 static dm_pdev_t *dm_pdev_alloc(const char *);
49 static int dm_pdev_free(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  * needs to be called with the dm_pdev_mutex held.
55  */
56 static dm_pdev_t *
57 dm_pdev_lookup_name(const char *dm_pdev_name)
58 {
59         dm_pdev_t *dmp;
60
61         KKASSERT(dm_pdev_name != NULL);
62
63         TAILQ_FOREACH(dmp, &dm_pdev_list, next_pdev) {
64                 if (strcmp(dm_pdev_name, dmp->name) == 0)
65                         return dmp;
66         }
67
68         return NULL;
69 }
70
71 static int
72 dm_dk_lookup(const char *dev_name, struct vnode **vpp)
73 {
74         struct nlookupdata nd;
75         int error;
76
77         error = nlookup_init(&nd, dev_name, UIO_SYSSPACE, NLC_FOLLOW);
78         if (error)
79                 return error;
80
81         error = vn_open(&nd, NULL, FREAD|FWRITE, 0);
82         if (error) {
83                 nlookup_done(&nd);
84                 return error;
85         }
86
87         *vpp = nd.nl_open_vp;
88         nd.nl_open_vp = NULL;
89         nlookup_done(&nd);
90
91         return 0;
92 }
93
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  */
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 TAILQ 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         struct vattr va;
127         int error;
128
129         KKASSERT(dev_name != NULL);
130
131         lockmgr(&dm_pdev_mutex, LK_EXCLUSIVE);
132         dmp = dm_pdev_lookup_name(dev_name);
133
134         if (dmp != NULL) {
135                 dmp->ref_cnt++;
136                 dmdebug("dmp_pdev_insert pdev %s already in tree\n", dev_name);
137                 lockmgr(&dm_pdev_mutex, LK_RELEASE);
138                 return dmp;
139         }
140
141         if ((dmp = dm_pdev_alloc(dev_name)) == NULL) {
142                 lockmgr(&dm_pdev_mutex, LK_RELEASE);
143                 return NULL;
144         }
145
146         error = dm_dk_lookup(dev_name, &dmp->pdev_vnode);
147         if (error) {
148                 dmdebug("dk_lookup on device: %s failed with error %d!\n",
149                     dev_name, error);
150                 dm_pdev_free(dmp);
151                 lockmgr(&dm_pdev_mutex, LK_RELEASE);
152                 return NULL;
153         }
154         dmp->ref_cnt = 1;
155
156         if (dm_pdev_get_vattr(dmp, &va) == -1) {
157                 dmdebug("makeudev %s failed\n", dev_name);
158                 dm_pdev_free(dmp);
159                 lockmgr(&dm_pdev_mutex, LK_RELEASE);
160                 return NULL;
161         }
162         ksnprintf(dmp->udev_name, sizeof(dmp->udev_name),
163                 "%d:%d", va.va_rmajor, va.va_rminor);
164         dmp->udev = dm_pdev_get_udev(dmp);
165
166         /*
167          * Get us the partinfo from the underlying device, it's needed for
168          * dumps.
169          */
170         bzero(&dmp->pdev_pinfo, sizeof(dmp->pdev_pinfo));
171         error = dev_dioctl(dmp->pdev_vnode->v_rdev, DIOCGPART,
172             (void *)&dmp->pdev_pinfo, 0, proc0.p_ucred, NULL, NULL);
173         if (!error) {
174                 struct partinfo *dpart = &dmp->pdev_pinfo;
175                 dmdebug("dmp_pdev_insert DIOCGPART "
176                         "offset=%ju size=%ju blocks=%ju blksize=%d\n",
177                         dpart->media_offset,
178                         dpart->media_size,
179                         dpart->media_blocks,
180                         dpart->media_blksize);
181         } else {
182                 kprintf("dmp_pdev_insert DIOCGPART failed %d\n", error);
183         }
184
185         TAILQ_INSERT_TAIL(&dm_pdev_list, dmp, next_pdev);
186         lockmgr(&dm_pdev_mutex, LK_RELEASE);
187
188         dmdebug("dmp_pdev_insert pdev %s %s 0x%016jx\n",
189                 dmp->name, dmp->udev_name, (uintmax_t)dmp->udev);
190
191         return dmp;
192 }
193
194 /*
195  * Allocat new pdev structure if is not already present and
196  * set name.
197  */
198 static dm_pdev_t *
199 dm_pdev_alloc(const char *name)
200 {
201         dm_pdev_t *dmp;
202
203         dmp = kmalloc(sizeof(*dmp), M_DM, M_WAITOK | M_ZERO);
204         if (dmp == NULL)
205                 return NULL;
206
207         if (name)
208                 strlcpy(dmp->name, name, MAX_DEV_NAME);
209
210         return dmp;
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 dm_dev_remove_ioctl.
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 /*
243  * Decrement pdev reference counter if 0 remove it.
244  */
245 int
246 dm_pdev_decr(dm_pdev_t *dmp)
247 {
248         KKASSERT(dmp != NULL);
249         /*
250          * If this was last reference remove dmp from
251          * global list also.
252          */
253         lockmgr(&dm_pdev_mutex, LK_EXCLUSIVE);
254
255         if (--dmp->ref_cnt == 0) {
256                 TAILQ_REMOVE(&dm_pdev_list, dmp, next_pdev);
257                 lockmgr(&dm_pdev_mutex, LK_RELEASE);
258                 dm_pdev_free(dmp);
259                 return 0;
260         }
261         lockmgr(&dm_pdev_mutex, LK_RELEASE);
262         return 0;
263 }
264
265 uint64_t
266 dm_pdev_get_udev(dm_pdev_t *dmp)
267 {
268         struct vattr va;
269         int ret;
270
271         if (dmp->pdev_vnode == NULL)
272                 return (uint64_t)-1;
273
274         ret = dm_pdev_get_vattr(dmp, &va);
275         if (ret)
276                 return (uint64_t)-1;
277
278         ret = makeudev(va.va_rmajor, va.va_rminor);
279
280         return ret;
281 }
282
283 int
284 dm_pdev_get_vattr(dm_pdev_t *dmp, struct vattr *vap)
285 {
286         int ret;
287
288         if (dmp->pdev_vnode == NULL)
289                 return -1;
290
291         KKASSERT(vap);
292         ret = VOP_GETATTR(dmp->pdev_vnode, vap);
293         if (ret)
294                 return -1;
295
296         return 0;
297 }
298
299 /*
300  * Initialize pdev subsystem.
301  */
302 int
303 dm_pdev_init(void)
304 {
305         TAILQ_INIT(&dm_pdev_list);      /* initialize global pdev list */
306         lockinit(&dm_pdev_mutex, "dmpdev", 0, LK_CANRECURSE);
307
308         return 0;
309 }
310
311 /*
312  * Destroy all existing pdev's in device-mapper.
313  */
314 int
315 dm_pdev_uninit(void)
316 {
317         dm_pdev_t *dmp;
318
319         lockmgr(&dm_pdev_mutex, LK_EXCLUSIVE);
320
321         while ((dmp = TAILQ_FIRST(&dm_pdev_list)) != NULL) {
322                 TAILQ_REMOVE(&dm_pdev_list, dmp, next_pdev);
323                 dm_pdev_free(dmp);
324         }
325         KKASSERT(TAILQ_EMPTY(&dm_pdev_list));
326
327         lockmgr(&dm_pdev_mutex, LK_RELEASE);
328
329         lockuninit(&dm_pdev_mutex);
330         return 0;
331 }