1 /* $NetBSD: dm_pdev.c,v 1.6 2010/01/04 00:19:08 haad Exp $ */ 2 3 /* 4 * Copyright (c) 2010-2011 Alex Hornung <alex@alexhornung.com> 5 * Copyright (c) 2008 The NetBSD Foundation, Inc. 6 * All rights reserved. 7 * 8 * This code is derived from software contributed to The NetBSD Foundation 9 * by Adam Hamsik. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 * POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 #include <sys/types.h> 34 #include <sys/param.h> 35 36 #include <sys/disk.h> 37 #include <sys/fcntl.h> 38 #include <sys/malloc.h> 39 #include <sys/namei.h> 40 #include <sys/vnode.h> 41 #include <sys/nlookup.h> 42 43 #include <dev/disk/dm/dm.h> 44 45 SLIST_HEAD(dm_pdevs, dm_pdev) dm_pdev_list; 46 47 struct lock dm_pdev_mutex; 48 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 *); 52 53 /* 54 * Find used pdev with name == dm_pdev_name. 55 * needs to be called with the dm_pdev_mutex held. 56 */ 57 static dm_pdev_t * 58 dm_pdev_lookup_name(const char *dm_pdev_name) 59 { 60 dm_pdev_t *dm_pdev; 61 62 KKASSERT(dm_pdev_name != NULL); 63 64 SLIST_FOREACH(dm_pdev, &dm_pdev_list, next_pdev) { 65 if (strcmp(dm_pdev_name, dm_pdev->name) == 0) 66 return dm_pdev; 67 } 68 69 return NULL; 70 } 71 72 static int 73 dm_dk_lookup(const char *dev_name, struct vnode **vpp) 74 { 75 struct nlookupdata nd; 76 int error; 77 78 error = nlookup_init(&nd, dev_name, UIO_SYSSPACE, NLC_FOLLOW); 79 if (error) 80 return error; 81 82 error = vn_open(&nd, NULL, FREAD|FWRITE, 0); 83 if (error) { 84 nlookup_done(&nd); 85 return error; 86 } 87 88 *vpp = nd.nl_open_vp; 89 nd.nl_open_vp = NULL; 90 nlookup_done(&nd); 91 92 return 0; 93 } 94 95 /* 96 * Since dm can have arbitrary stacking on any number of disks and any dm 97 * volume is at least stacked onto another disk, we need to adjust the 98 * dumping offset (which is a raw offset from the beginning of the lowest 99 * physical disk) taking into account the offset of the underlying device 100 * which in turn takes into account the offset below it, etc. 101 * 102 * This function adjusts the dumping offset that is passed to the next 103 * dev_ddump() so it is correct for that underlying device. 104 */ 105 off_t 106 dm_pdev_correct_dump_offset(dm_pdev_t *pdev, off_t offset) 107 { 108 off_t noffset; 109 110 noffset = pdev->pdev_pinfo.reserved_blocks + 111 pdev->pdev_pinfo.media_offset / pdev->pdev_pinfo.media_blksize; 112 noffset *= DEV_BSIZE; 113 noffset += offset; 114 115 return noffset; 116 } 117 118 /* 119 * Create entry for device with name dev_name and open vnode for it. 120 * If entry already exists in global SLIST I will only increment 121 * reference counter. 122 */ 123 dm_pdev_t * 124 dm_pdev_insert(const char *dev_name) 125 { 126 dm_pdev_t *dmp; 127 int error; 128 129 KKASSERT(dev_name != NULL); 130 131 lockmgr(&dm_pdev_mutex, LK_EXCLUSIVE); 132 dmp = dm_pdev_lookup_name(dev_name); 133 134 if (dmp != NULL) { 135 dmp->ref_cnt++; 136 aprint_debug("dmp_pdev_insert pdev %s already in tree\n", dev_name); 137 lockmgr(&dm_pdev_mutex, LK_RELEASE); 138 return dmp; 139 } 140 lockmgr(&dm_pdev_mutex, LK_RELEASE); 141 142 if ((dmp = dm_pdev_alloc(dev_name)) == NULL) 143 return NULL; 144 145 error = dm_dk_lookup(dev_name, &dmp->pdev_vnode); 146 if (error) { 147 aprint_debug("dk_lookup on device: %s failed with error %d!\n", 148 dev_name, error); 149 kfree(dmp, M_DM); 150 return NULL; 151 } 152 dmp->ref_cnt = 1; 153 154 /* 155 * Get us the partinfo from the underlying device, it's needed for 156 * dumps. 157 */ 158 bzero(&dmp->pdev_pinfo, sizeof(dmp->pdev_pinfo)); 159 error = dev_dioctl(dmp->pdev_vnode->v_rdev, DIOCGPART, 160 (void *)&dmp->pdev_pinfo, 0, proc0.p_ucred, NULL, NULL); 161 162 lockmgr(&dm_pdev_mutex, LK_EXCLUSIVE); 163 SLIST_INSERT_HEAD(&dm_pdev_list, dmp, next_pdev); 164 lockmgr(&dm_pdev_mutex, LK_RELEASE); 165 166 return dmp; 167 } 168 169 /* 170 * Allocat new pdev structure if is not already present and 171 * set name. 172 */ 173 static dm_pdev_t * 174 dm_pdev_alloc(const char *name) 175 { 176 dm_pdev_t *dmp; 177 178 if ((dmp = kmalloc(sizeof(dm_pdev_t), M_DM, M_WAITOK | M_ZERO)) == NULL) 179 return NULL; 180 181 strlcpy(dmp->name, name, MAX_DEV_NAME); 182 183 dmp->ref_cnt = 0; 184 dmp->pdev_vnode = NULL; 185 186 return dmp; 187 } 188 /* 189 * Destroy allocated dm_pdev. 190 */ 191 static int 192 dm_pdev_rem(dm_pdev_t * dmp) 193 { 194 int err; 195 196 KKASSERT(dmp != NULL); 197 198 if (dmp->pdev_vnode != NULL) { 199 err = vn_close(dmp->pdev_vnode, FREAD | FWRITE, NULL); 200 if (err != 0) 201 return err; 202 } 203 kfree(dmp, M_DM); 204 dmp = NULL; 205 206 return 0; 207 } 208 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 221 dm_pdev_decr(dm_pdev_t * dmp) 222 { 223 KKASSERT(dmp != NULL); 224 /* 225 * If this was last reference remove dmp from 226 * global list also. 227 */ 228 lockmgr(&dm_pdev_mutex, LK_EXCLUSIVE); 229 230 if (--dmp->ref_cnt == 0) { 231 SLIST_REMOVE(&dm_pdev_list, dmp, dm_pdev, next_pdev); 232 lockmgr(&dm_pdev_mutex, LK_RELEASE); 233 dm_pdev_rem(dmp); 234 return 0; 235 } 236 lockmgr(&dm_pdev_mutex, LK_RELEASE); 237 return 0; 238 } 239 240 /* 241 * Initialize pdev subsystem. 242 */ 243 int 244 dm_pdev_init(void) 245 { 246 SLIST_INIT(&dm_pdev_list); /* initialize global pdev list */ 247 lockinit(&dm_pdev_mutex, "dmpdev", 0, LK_CANRECURSE); 248 249 return 0; 250 } 251 252 /* 253 * Destroy all existing pdev's in device-mapper. 254 */ 255 int 256 dm_pdev_uninit(void) 257 { 258 dm_pdev_t *dm_pdev; 259 260 lockmgr(&dm_pdev_mutex, LK_EXCLUSIVE); 261 while (!SLIST_EMPTY(&dm_pdev_list)) { /* List Deletion. */ 262 263 dm_pdev = SLIST_FIRST(&dm_pdev_list); 264 265 SLIST_REMOVE_HEAD(&dm_pdev_list, next_pdev); 266 267 dm_pdev_rem(dm_pdev); 268 } 269 lockmgr(&dm_pdev_mutex, LK_RELEASE); 270 271 lockuninit(&dm_pdev_mutex); 272 return 0; 273 } 274