No empty .Rs/.Re
[netbsd-mini2440.git] / sys / dev / dm / dm_pdev.c
bloba2324a01ba12d5582982d7fb71908fb0544704d4
1 /* $NetBSD: dm_pdev.c,v 1.5 2010/01/03 12:53:00 haad Exp $ */
3 /*
4 * Copyright (c) 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Adam Hamsik.
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.
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.
32 #include <sys/types.h>
33 #include <sys/param.h>
35 #include <sys/disk.h>
36 #include <sys/fcntl.h>
37 #include <sys/kmem.h>
38 #include <sys/namei.h>
39 #include <sys/vnode.h>
41 #include <dev/dkvar.h>
43 #include "dm.h"
45 SLIST_HEAD(dm_pdevs, dm_pdev) dm_pdev_list;
47 kmutex_t dm_pdev_mutex;
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 *);
54 * Find used pdev with name == dm_pdev_name.
56 dm_pdev_t *
57 dm_pdev_lookup_name(const char *dm_pdev_name)
59 dm_pdev_t *dm_pdev;
60 int dlen;
61 int slen;
63 KASSERT(dm_pdev_name != NULL);
65 slen = strlen(dm_pdev_name);
67 SLIST_FOREACH(dm_pdev, &dm_pdev_list, next_pdev) {
68 dlen = strlen(dm_pdev->name);
70 if (slen != dlen)
71 continue;
73 if (strncmp(dm_pdev_name, dm_pdev->name, slen) == 0)
74 return dm_pdev;
77 return NULL;
80 * Create entry for device with name dev_name and open vnode for it.
81 * If entry already exists in global SLIST I will only increment
82 * reference counter.
84 dm_pdev_t *
85 dm_pdev_insert(const char *dev_name)
87 dm_pdev_t *dmp;
88 int error;
90 KASSERT(dev_name != NULL);
92 mutex_enter(&dm_pdev_mutex);
93 dmp = dm_pdev_lookup_name(dev_name);
95 if (dmp != NULL) {
96 dmp->ref_cnt++;
97 aprint_debug("dmp_pdev_insert pdev %s already in tree\n", dev_name);
98 mutex_exit(&dm_pdev_mutex);
99 return dmp;
101 mutex_exit(&dm_pdev_mutex);
103 if ((dmp = dm_pdev_alloc(dev_name)) == NULL)
104 return NULL;
106 error = dk_lookup(dev_name, curlwp, &dmp->pdev_vnode, UIO_SYSSPACE);
107 if (error) {
108 aprint_debug("dk_lookup on device: %s failed with error %d!\n",
109 dev_name, error);
110 kmem_free(dmp, sizeof(dm_pdev_t));
111 return NULL;
113 dmp->ref_cnt = 1;
115 mutex_enter(&dm_pdev_mutex);
116 SLIST_INSERT_HEAD(&dm_pdev_list, dmp, next_pdev);
117 mutex_exit(&dm_pdev_mutex);
119 return dmp;
122 * Initialize pdev subsystem.
125 dm_pdev_init(void)
127 SLIST_INIT(&dm_pdev_list); /* initialize global pdev list */
128 mutex_init(&dm_pdev_mutex, MUTEX_DEFAULT, IPL_NONE);
130 return 0;
133 * Allocat new pdev structure if is not already present and
134 * set name.
136 static dm_pdev_t *
137 dm_pdev_alloc(const char *name)
139 dm_pdev_t *dmp;
141 if ((dmp = kmem_zalloc(sizeof(dm_pdev_t), KM_SLEEP)) == NULL)
142 return NULL;
144 strlcpy(dmp->name, name, MAX_DEV_NAME);
146 dmp->ref_cnt = 0;
147 dmp->pdev_vnode = NULL;
149 return dmp;
152 * Destroy allocated dm_pdev.
154 static int
155 dm_pdev_rem(dm_pdev_t * dmp)
157 int err;
159 KASSERT(dmp != NULL);
161 if (dmp->pdev_vnode != NULL) {
162 err = vn_close(dmp->pdev_vnode, FREAD | FWRITE, FSCRED);
163 if (err != 0)
164 return err;
166 kmem_free(dmp, sizeof(*dmp));
167 dmp = NULL;
169 return 0;
172 * Destroy all existing pdev's in device-mapper.
175 dm_pdev_destroy(void)
177 dm_pdev_t *dm_pdev;
179 mutex_enter(&dm_pdev_mutex);
180 while (!SLIST_EMPTY(&dm_pdev_list)) { /* List Deletion. */
182 dm_pdev = SLIST_FIRST(&dm_pdev_list);
184 SLIST_REMOVE_HEAD(&dm_pdev_list, next_pdev);
186 dm_pdev_rem(dm_pdev);
188 mutex_exit(&dm_pdev_mutex);
190 mutex_destroy(&dm_pdev_mutex);
191 return 0;
194 * This funcion is called from dm_dev_remove_ioctl.
195 * When I'm removing device from list, I have to decrement
196 * reference counter. If reference counter is 0 I will remove
197 * dmp from global list and from device list to. And I will CLOSE
198 * dmp vnode too.
202 * Decrement pdev reference counter if 0 remove it.
205 dm_pdev_decr(dm_pdev_t * dmp)
207 KASSERT(dmp != NULL);
209 * If this was last reference remove dmp from
210 * global list also.
212 mutex_enter(&dm_pdev_mutex);
214 if (--dmp->ref_cnt == 0) {
215 SLIST_REMOVE(&dm_pdev_list, dmp, dm_pdev, next_pdev);
216 mutex_exit(&dm_pdev_mutex);
217 dm_pdev_rem(dmp);
218 return 0;
220 mutex_exit(&dm_pdev_mutex);
221 return 0;
223 /*static int
224 dm_pdev_dump_list(void)
226 dm_pdev_t *dmp;
228 aprint_verbose("Dumping dm_pdev_list \n");
230 SLIST_FOREACH(dmp, &dm_pdev_list, next_pdev) {
231 aprint_verbose("dm_pdev_name %s ref_cnt %d list_rf_cnt %d\n",
232 dmp->name, dmp->ref_cnt, dmp->list_ref_cnt);
235 return 0;