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