1 /* $NetBSD: dm_pdev.c,v 1.6 2010/01/04 00:19:08 haad Exp $ */ 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/malloc.h> 38 #include <sys/namei.h> 39 #include <sys/vnode.h> 40 #include <sys/nlookup.h> 41 42 #include "dm.h" 43 44 MALLOC_DECLARE(M_DM); 45 46 SLIST_HEAD(dm_pdevs, dm_pdev) dm_pdev_list; 47 48 struct lock dm_pdev_mutex; 49 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 *); 53 54 /* 55 * Find used pdev with name == dm_pdev_name. 56 */ 57 dm_pdev_t * 58 dm_pdev_lookup_name(const char *dm_pdev_name) 59 { 60 dm_pdev_t *dm_pdev; 61 int dlen; 62 int slen; 63 64 KKASSERT(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; 73 74 if (strncmp(dm_pdev_name, dm_pdev->name, slen) == 0) 75 return dm_pdev; 76 } 77 78 return NULL; 79 } 80 81 static int 82 dm_dk_lookup(const char *dev_name, struct vnode **vpp) 83 { 84 struct nlookupdata nd; 85 int error; 86 87 #ifdef notyet 88 if (this is a root mount) { 89 error = vn_opendisk(dev_name, FREAD|FWRITE, vpp); 90 return error; 91 } 92 #endif 93 94 error = nlookup_init(&nd, dev_name, UIO_SYSSPACE, NLC_FOLLOW); 95 if (error) 96 return error; 97 98 error = vn_open(&nd, NULL, FREAD|FWRITE, 0); 99 *vpp = nd.nl_open_vp; 100 nd.nl_open_vp = NULL; 101 nlookup_done(&nd); 102 103 return 0; 104 } 105 106 /* 107 * Create entry for device with name dev_name and open vnode for it. 108 * If entry already exists in global SLIST I will only increment 109 * reference counter. 110 */ 111 dm_pdev_t * 112 dm_pdev_insert(const char *dev_name) 113 { 114 dm_pdev_t *dmp; 115 int error; 116 117 KKASSERT(dev_name != NULL); 118 119 lockmgr(&dm_pdev_mutex, LK_EXCLUSIVE); 120 dmp = dm_pdev_lookup_name(dev_name); 121 122 if (dmp != NULL) { 123 dmp->ref_cnt++; 124 aprint_debug("dmp_pdev_insert pdev %s already in tree\n", dev_name); 125 lockmgr(&dm_pdev_mutex, LK_RELEASE); 126 return dmp; 127 } 128 lockmgr(&dm_pdev_mutex, LK_RELEASE); 129 130 if ((dmp = dm_pdev_alloc(dev_name)) == NULL) 131 return NULL; 132 133 /* XXX: nlookup and/or vn_opendisk */ 134 error = dm_dk_lookup(dev_name, &dmp->pdev_vnode); 135 if (error) { 136 aprint_debug("dk_lookup on device: %s failed with error %d!\n", 137 dev_name, error); 138 kfree(dmp, M_DM); 139 return NULL; 140 } 141 dmp->ref_cnt = 1; 142 143 lockmgr(&dm_pdev_mutex, LK_EXCLUSIVE); 144 SLIST_INSERT_HEAD(&dm_pdev_list, dmp, next_pdev); 145 lockmgr(&dm_pdev_mutex, LK_RELEASE); 146 147 return dmp; 148 } 149 /* 150 * Initialize pdev subsystem. 151 */ 152 int 153 dm_pdev_init(void) 154 { 155 SLIST_INIT(&dm_pdev_list); /* initialize global pdev list */ 156 lockinit(&dm_pdev_mutex, "dmpdev", 0, LK_CANRECURSE); 157 158 return 0; 159 } 160 /* 161 * Allocat new pdev structure if is not already present and 162 * set name. 163 */ 164 static dm_pdev_t * 165 dm_pdev_alloc(const char *name) 166 { 167 dm_pdev_t *dmp; 168 169 if ((dmp = kmalloc(sizeof(dm_pdev_t), M_DM, M_WAITOK | M_ZERO)) == NULL) 170 return NULL; 171 172 strlcpy(dmp->name, name, MAX_DEV_NAME); 173 174 dmp->ref_cnt = 0; 175 dmp->pdev_vnode = NULL; 176 177 return dmp; 178 } 179 /* 180 * Destroy allocated dm_pdev. 181 */ 182 static int 183 dm_pdev_rem(dm_pdev_t * dmp) 184 { 185 int err; 186 187 KKASSERT(dmp != NULL); 188 189 if (dmp->pdev_vnode != NULL) { 190 err = vn_close(dmp->pdev_vnode, FREAD | FWRITE); 191 if (err != 0) 192 return err; 193 } 194 kfree(dmp, M_DM); 195 dmp = NULL; 196 197 return 0; 198 } 199 /* 200 * Destroy all existing pdev's in device-mapper. 201 */ 202 int 203 dm_pdev_destroy(void) 204 { 205 dm_pdev_t *dm_pdev; 206 207 lockmgr(&dm_pdev_mutex, LK_EXCLUSIVE); 208 while (!SLIST_EMPTY(&dm_pdev_list)) { /* List Deletion. */ 209 210 dm_pdev = SLIST_FIRST(&dm_pdev_list); 211 212 SLIST_REMOVE_HEAD(&dm_pdev_list, next_pdev); 213 214 dm_pdev_rem(dm_pdev); 215 } 216 lockmgr(&dm_pdev_mutex, LK_RELEASE); 217 218 lockuninit(&dm_pdev_mutex); 219 return 0; 220 } 221 /* 222 * This funcion is called from dm_dev_remove_ioctl. 223 * When I'm removing device from list, I have to decrement 224 * reference counter. If reference counter is 0 I will remove 225 * dmp from global list and from device list to. And I will CLOSE 226 * dmp vnode too. 227 */ 228 229 /* 230 * Decrement pdev reference counter if 0 remove it. 231 */ 232 int 233 dm_pdev_decr(dm_pdev_t * dmp) 234 { 235 KKASSERT(dmp != NULL); 236 /* 237 * If this was last reference remove dmp from 238 * global list also. 239 */ 240 lockmgr(&dm_pdev_mutex, LK_EXCLUSIVE); 241 242 if (--dmp->ref_cnt == 0) { 243 SLIST_REMOVE(&dm_pdev_list, dmp, dm_pdev, next_pdev); 244 lockmgr(&dm_pdev_mutex, LK_RELEASE); 245 dm_pdev_rem(dmp); 246 return 0; 247 } 248 lockmgr(&dm_pdev_mutex, LK_RELEASE); 249 return 0; 250 } 251 /*static int 252 dm_pdev_dump_list(void) 253 { 254 dm_pdev_t *dmp; 255 256 aprint_verbose("Dumping dm_pdev_list \n"); 257 258 SLIST_FOREACH(dmp, &dm_pdev_list, next_pdev) { 259 aprint_verbose("dm_pdev_name %s ref_cnt %d list_rf_cnt %d\n", 260 dmp->name, dmp->ref_cnt, dmp->list_ref_cnt); 261 } 262 263 return 0; 264 265 }*/ 266