Annotation of src/sys/dev/dm/dm_pdev.c, revision 1.1.2.3
1.1.2.1 haad 1: /*
2: * Copyright (c) 1996, 1997, 1998, 1999, 2002 The NetBSD Foundation, Inc.
3: * All rights reserved.
4: *
5: * This code is derived from software contributed to The NetBSD Foundation
6: * by Adam Hamsik.
7: *
8: * Redistribution and use in source and binary forms, with or without
9: * modification, are permitted provided that the following conditions
10: * are met:
11: * 1. Redistributions of source code must retain the above copyright
12: * notice, this list of conditions and the following disclaimer.
13: * 2. Redistributions in binary form must reproduce the above copyright
14: * notice, this list of conditions and the following disclaimer in the
15: * documentation and/or other materials provided with the distribution.
16: *
17: * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18: * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19: * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21: * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27: * POSSIBILITY OF SUCH DAMAGE.
28: */
29:
30: #include <sys/types.h>
31: #include <sys/param.h>
32:
33: #include <sys/disk.h>
34: #include <sys/errno.h>
35: #include <sys/ioctl.h>
36: #include <sys/ioccom.h>
37: #include <sys/kmem.h>
38: #include <sys/queue.h>
39:
40: #include <sys/namei.h>
41: #include <sys/vnode.h>
42:
43: #include <dev/dkvar.h>
44:
45: #include "dm.h"
46:
47: static struct dm_pdev *dm_pdev_alloc(const char *);
48:
49: /*
50: * XXX. I should use rwlock here.
51: */
52: kmutex_t dm_pdev_mutex;
53:
54: /*
55: * Find used pdev with name == dm_pdev_name.
56: */
57: struct dm_pdev*
58: dm_pdev_lookup_name(const char *dm_pdev_name)
59: {
60: struct dm_pdev *dm_pdev;
61: int dlen; int slen;
62:
1.1.2.3 ! haad 63: slen = strlen(dm_pdev_name);
1.1.2.1 haad 64:
65: mutex_enter(&dm_pdev_mutex);
66:
67: SLIST_FOREACH(dm_pdev, &dm_pdev_list, next_pdev) {
1.1.2.3 ! haad 68: dlen = strlen(dm_pdev->name);
1.1.2.1 haad 69:
70: if (slen != dlen)
71: continue;
72:
1.1.2.2 haad 73: if (strncmp(dm_pdev_name, dm_pdev->name, slen) == 0){
1.1.2.1 haad 74: mutex_exit(&dm_pdev_mutex);
75: return dm_pdev;
76: }
77: }
78: mutex_exit(&dm_pdev_mutex);
79:
80: return NULL;
81: }
82:
83: /*
84: * Create entry for device with name dev_name and open vnode for it.
85: * If entry already exists in global SLIST I will only increment
86: * reference counter.
87: */
88: struct dm_pdev*
89: dm_pdev_insert(const char *dev_name)
90: {
91: struct dm_pdev *dmp;
92: int error;
93:
94: dmp = dm_pdev_lookup_name(dev_name);
95:
1.1.2.3 ! haad 96:
1.1.2.1 haad 97: if (dmp != NULL) {
1.1.2.3 ! haad 98:
! 99: mutex_enter(&dm_pdev_mutex);
! 100:
1.1.2.1 haad 101: dmp->ref_cnt++;
1.1.2.3 ! haad 102:
! 103: mutex_exit(&dm_pdev_mutex);
! 104:
1.1.2.1 haad 105: return dmp;
106: }
107:
108: if ((dmp = dm_pdev_alloc(dev_name)) == NULL)
109: return NULL;
110:
111: error = dk_lookup(dev_name, curlwp, &dmp->pdev_vnode, UIO_SYSSPACE);
112:
113: if (error) {
1.1.2.2 haad 114: aprint_normal("dk_lookup on device: %s failed with error %d!\n",
115: dev_name, error);
1.1.2.1 haad 116:
117: return NULL;
118: }
119:
120: dmp->ref_cnt = 1;
1.1.2.3 ! haad 121:
1.1.2.1 haad 122: mutex_enter(&dm_pdev_mutex);
123:
124: SLIST_INSERT_HEAD(&dm_pdev_list, dmp, next_pdev);
125:
126: mutex_exit(&dm_pdev_mutex);
127:
128: return dmp;
129: }
130:
131: /*
132: * Initialize pdev subsystem.
133: */
134: int
135: dm_pdev_init()
136: {
137:
138: SLIST_INIT(&dm_pdev_list); /* initialize global pdev list */
139:
140: mutex_init(&dm_pdev_mutex,MUTEX_DEFAULT,IPL_NONE);
141:
142: return 0;
143: }
144:
145: /*
146: * Allocat new pdev structure if is not already present and
147: * set name.
148: */
149: static struct dm_pdev*
150: dm_pdev_alloc(const char *name)
151: {
152: struct dm_pdev *dmp;
153:
1.1.2.2 haad 154: if ((dmp = kmem_alloc(sizeof(struct dm_pdev)+strlen(name)+1,
155: KM_NOSLEEP)) == NULL)
1.1.2.1 haad 156: return NULL;
157:
1.1.2.2 haad 158: strlcpy(dmp->name, name, MAX_DEV_NAME);
1.1.2.1 haad 159:
160: dmp->ref_cnt = 0;
161: dmp->pdev_vnode = NULL;
162:
163: return dmp;
164: }
165:
166: /*
167: * Destroy allocated dm_pdev.
168: */
169: int
170: dm_pdev_destroy(struct dm_pdev *dmp)
171: {
172:
173: if (dmp == NULL)
174: return ENOENT;
175:
1.1.2.2 haad 176: if (dmp->pdev_vnode != NULL) {
177: VOP_CLOSE(dmp->pdev_vnode, FREAD|FWRITE, FSCRED);
178: vput(dmp->pdev_vnode);
179: }
1.1.2.1 haad 180: kmem_free(dmp,sizeof(*dmp));
181:
182: dmp = NULL;
183:
184: return 0;
185: }
186:
187: /*
188: * This funcion is called from dm_dev_remove_ioctl.
189: * When I'm removing device from list, I have to decrement
190: * reference counter. If reference counter is 0 I will remove
191: * dmp from global list and from device list to. And I will CLOSE
192: * dmp vnode too.
193: */
194:
195: /*
196: * I'm guarding only global list with mutex, device should be
197: * locked elsewhere.
198: */
199: int
200: dm_pdev_decr(struct dm_pdevs *head)
201: {
202: struct dm_pdev *dmp;
203: if (head == NULL)
204: return ENOENT;
205:
1.1.2.3 ! haad 206: mutex_enter(&dm_pdev_mutex);
! 207:
1.1.2.2 haad 208: SLIST_FOREACH(dmp, head, next_pdev) {
1.1.2.1 haad 209:
210: dmp->ref_cnt--;
211:
1.1.2.2 haad 212: SLIST_REMOVE(head, dmp, dm_pdev, next_pdev);
1.1.2.1 haad 213:
1.1.2.2 haad 214: /*
215: * If this was last reference remove dmp from
216: * global list also.
217: */
1.1.2.1 haad 218: if (dmp->ref_cnt == 0) {
1.1.2.2 haad 219: SLIST_REMOVE(&dm_pdev_list, dmp, dm_pdev, next_pdev);
1.1.2.1 haad 220: dm_pdev_destroy(dmp);
221: }
222: }
223:
1.1.2.3 ! haad 224: mutex_exit(&dm_pdev_mutex);
! 225:
1.1.2.1 haad 226: return 0;
227: }
228:
CVSweb <webmaster@jp.NetBSD.org>