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