Annotation of src/sys/dev/dm/dm_pdev.c, revision 1.1.2.6
1.1.2.6 ! haad 1: /* $NetBSD: dm_pdev.c,v 1.1.2.5 2008/08/19 23:42:11 haad Exp $ */
1.1.2.4 haad 2:
1.1.2.1 haad 3: /*
4: * Copyright (c) 1996, 1997, 1998, 1999, 2002 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/errno.h>
37: #include <sys/ioctl.h>
38: #include <sys/ioccom.h>
39: #include <sys/kmem.h>
40: #include <sys/queue.h>
41:
42: #include <sys/namei.h>
43: #include <sys/vnode.h>
44:
45: #include <dev/dkvar.h>
46:
47: #include "dm.h"
48:
49: static struct dm_pdev *dm_pdev_alloc(const char *);
1.1.2.5 haad 50: static int dm_pdev_rem(struct dm_pdev *);
1.1.2.1 haad 51:
52: /*
53: * XXX. I should use rwlock here.
54: */
55: kmutex_t dm_pdev_mutex;
56:
57: /*
58: * Find used pdev with name == dm_pdev_name.
59: */
60: struct dm_pdev*
61: dm_pdev_lookup_name(const char *dm_pdev_name)
62: {
63: struct dm_pdev *dm_pdev;
64: int dlen; int slen;
65:
1.1.2.3 haad 66: slen = strlen(dm_pdev_name);
1.1.2.1 haad 67:
68: mutex_enter(&dm_pdev_mutex);
69:
70: SLIST_FOREACH(dm_pdev, &dm_pdev_list, next_pdev) {
1.1.2.3 haad 71: dlen = strlen(dm_pdev->name);
1.1.2.1 haad 72:
73: if (slen != dlen)
74: continue;
75:
1.1.2.2 haad 76: if (strncmp(dm_pdev_name, dm_pdev->name, slen) == 0){
1.1.2.1 haad 77: mutex_exit(&dm_pdev_mutex);
78: return dm_pdev;
79: }
80: }
81: mutex_exit(&dm_pdev_mutex);
82:
83: return NULL;
84: }
85:
86: /*
87: * Create entry for device with name dev_name and open vnode for it.
88: * If entry already exists in global SLIST I will only increment
89: * reference counter.
90: */
91: struct dm_pdev*
92: dm_pdev_insert(const char *dev_name)
93: {
94: struct dm_pdev *dmp;
95: int error;
96:
97: dmp = dm_pdev_lookup_name(dev_name);
98:
1.1.2.3 haad 99:
1.1.2.1 haad 100: if (dmp != NULL) {
1.1.2.3 haad 101:
102: mutex_enter(&dm_pdev_mutex);
103:
1.1.2.1 haad 104: dmp->ref_cnt++;
1.1.2.3 haad 105:
106: mutex_exit(&dm_pdev_mutex);
107:
1.1.2.1 haad 108: return dmp;
109: }
110:
111: if ((dmp = dm_pdev_alloc(dev_name)) == NULL)
112: return NULL;
113:
114: error = dk_lookup(dev_name, curlwp, &dmp->pdev_vnode, UIO_SYSSPACE);
115:
116: if (error) {
1.1.2.2 haad 117: aprint_normal("dk_lookup on device: %s failed with error %d!\n",
118: dev_name, error);
1.1.2.1 haad 119:
1.1.2.6 ! haad 120: kmem_free(dmp, sizeof(struct dm_pdev));
! 121:
1.1.2.1 haad 122: return NULL;
123: }
124:
125: dmp->ref_cnt = 1;
1.1.2.3 haad 126:
1.1.2.1 haad 127: mutex_enter(&dm_pdev_mutex);
128:
129: SLIST_INSERT_HEAD(&dm_pdev_list, dmp, next_pdev);
130:
131: mutex_exit(&dm_pdev_mutex);
132:
133: return dmp;
134: }
135:
136: /*
137: * Initialize pdev subsystem.
138: */
139: int
1.1.2.5 haad 140: dm_pdev_init(void)
1.1.2.1 haad 141: {
142:
143: SLIST_INIT(&dm_pdev_list); /* initialize global pdev list */
144:
145: mutex_init(&dm_pdev_mutex,MUTEX_DEFAULT,IPL_NONE);
146:
147: return 0;
148: }
149:
150: /*
151: * Allocat new pdev structure if is not already present and
152: * set name.
153: */
154: static struct dm_pdev*
155: dm_pdev_alloc(const char *name)
156: {
157: struct dm_pdev *dmp;
158:
1.1.2.2 haad 159: if ((dmp = kmem_alloc(sizeof(struct dm_pdev)+strlen(name)+1,
160: KM_NOSLEEP)) == NULL)
1.1.2.1 haad 161: return NULL;
162:
1.1.2.2 haad 163: strlcpy(dmp->name, name, MAX_DEV_NAME);
1.1.2.1 haad 164:
165: dmp->ref_cnt = 0;
166: dmp->pdev_vnode = NULL;
167:
168: return dmp;
169: }
170:
171: /*
1.1.2.5 haad 172: * Destroy allocated dm_pdev. This routine is called with held pdev mutex.
1.1.2.1 haad 173: */
1.1.2.5 haad 174: static int
175: dm_pdev_rem(struct dm_pdev *dmp)
1.1.2.1 haad 176: {
1.1.2.6 ! haad 177: int err;
! 178:
1.1.2.1 haad 179: if (dmp == NULL)
180: return ENOENT;
1.1.2.2 haad 181: if (dmp->pdev_vnode != NULL) {
1.1.2.6 ! haad 182: err = vn_close(dmp->pdev_vnode, FREAD | FWRITE, FSCRED);
! 183: if (err != 0)
! 184: return err;
1.1.2.2 haad 185: }
1.1.2.6 ! haad 186:
1.1.2.5 haad 187: kmem_free(dmp, sizeof(*dmp));
1.1.2.1 haad 188: dmp = NULL;
189:
190: return 0;
191: }
192:
193: /*
1.1.2.5 haad 194: * Destroy all existing pdev's in device-mapper.
195: */
196: int
197: dm_pdev_destroy(void)
198: {
199: struct dm_pdev *dm_pdev;
200:
201: mutex_enter(&dm_pdev_mutex);
202:
203: while (!SLIST_EMPTY(&dm_pdev_list)) { /* List Deletion. */
204:
205: dm_pdev = SLIST_FIRST(&dm_pdev_list);
206:
207: SLIST_REMOVE_HEAD(&dm_pdev_list, next_pdev);
208:
209: dm_pdev_rem(dm_pdev);
210: }
211:
212: mutex_exit(&dm_pdev_mutex);
213:
214: return 0;
215: }
216:
217: /*
1.1.2.1 haad 218: * This funcion is called from dm_dev_remove_ioctl.
219: * When I'm removing device from list, I have to decrement
220: * reference counter. If reference counter is 0 I will remove
221: * dmp from global list and from device list to. And I will CLOSE
222: * dmp vnode too.
223: */
224:
225: /*
226: * I'm guarding only global list with mutex, device should be
227: * locked elsewhere.
228: */
229: int
1.1.2.5 haad 230: dm_pdev_decr(struct dm_pdev *dmp)
1.1.2.1 haad 231: {
1.1.2.5 haad 232: if (dmp == NULL)
1.1.2.1 haad 233: return ENOENT;
234:
1.1.2.3 haad 235: mutex_enter(&dm_pdev_mutex);
1.1.2.5 haad 236:
237: dmp->ref_cnt--;
1.1.2.6 ! haad 238:
1.1.2.5 haad 239: /*
240: * If this was last reference remove dmp from
241: * global list also.
242: */
243: if (dmp->ref_cnt == 0) {
244: SLIST_REMOVE(&dm_pdev_list, dmp, dm_pdev, next_pdev);
245: dm_pdev_rem(dmp);
1.1.2.1 haad 246: }
247:
1.1.2.3 haad 248: mutex_exit(&dm_pdev_mutex);
249:
1.1.2.1 haad 250: return 0;
251: }
CVSweb <webmaster@jp.NetBSD.org>