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