1 /*- 2 * Copyright (c) 2004, 2007 Lukas Ertl 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 */ 27 28 #include <sys/cdefs.h> 29 __FBSDID("$FreeBSD$"); 30 31 #include <sys/param.h> 32 #include <sys/libkern.h> 33 #include <sys/malloc.h> 34 35 #include <geom/geom.h> 36 #include <geom/vinum/geom_vinum_var.h> 37 #include <geom/vinum/geom_vinum.h> 38 39 /* General 'remove' routine. */ 40 void 41 gv_remove(struct g_geom *gp, struct gctl_req *req) 42 { 43 struct gv_softc *sc; 44 struct gv_volume *v; 45 struct gv_plex *p; 46 struct gv_sd *s; 47 struct gv_drive *d; 48 int *argc, *flags; 49 char *argv, buf[20]; 50 int i, type; 51 52 argc = gctl_get_paraml(req, "argc", sizeof(*argc)); 53 54 if (argc == NULL || *argc == 0) { 55 gctl_error(req, "no arguments given"); 56 return; 57 } 58 59 flags = gctl_get_paraml(req, "flags", sizeof(*flags)); 60 if (flags == NULL) { 61 gctl_error(req, "no flags given"); 62 return; 63 } 64 65 sc = gp->softc; 66 67 /* XXX config locking */ 68 69 for (i = 0; i < *argc; i++) { 70 snprintf(buf, sizeof(buf), "argv%d", i); 71 argv = gctl_get_param(req, buf, NULL); 72 if (argv == NULL) 73 continue; 74 type = gv_object_type(sc, argv); 75 switch (type) { 76 case GV_TYPE_VOL: 77 v = gv_find_vol(sc, argv); 78 79 /* 80 * If this volume has plexes, we want a recursive 81 * removal. 82 */ 83 if (!LIST_EMPTY(&v->plexes) && !(*flags & GV_FLAG_R)) { 84 gctl_error(req, "volume '%s' has attached " 85 "plexes - need recursive removal", v->name); 86 return; 87 } 88 89 gv_post_event(sc, GV_EVENT_RM_VOLUME, v, NULL, 0, 0); 90 break; 91 92 case GV_TYPE_PLEX: 93 p = gv_find_plex(sc, argv); 94 95 /* 96 * If this plex has subdisks, we want a recursive 97 * removal. 98 */ 99 if (!LIST_EMPTY(&p->subdisks) && 100 !(*flags & GV_FLAG_R)) { 101 gctl_error(req, "plex '%s' has attached " 102 "subdisks - need recursive removal", 103 p->name); 104 return; 105 } 106 107 /* Don't allow removal of the only plex of a volume. */ 108 if (p->vol_sc != NULL && p->vol_sc->plexcount == 1) { 109 gctl_error(req, "plex '%s' is still attached " 110 "to volume '%s'", p->name, p->volume); 111 return; 112 } 113 114 gv_post_event(sc, GV_EVENT_RM_PLEX, p, NULL, 0, 0); 115 break; 116 117 case GV_TYPE_SD: 118 s = gv_find_sd(sc, argv); 119 120 /* Don't allow removal if attached to a plex. */ 121 if (s->plex_sc != NULL) { 122 gctl_error(req, "subdisk '%s' is still attached" 123 " to plex '%s'", s->name, s->plex_sc->name); 124 return; 125 } 126 127 gv_post_event(sc, GV_EVENT_RM_SD, s, NULL, 0, 0); 128 break; 129 130 case GV_TYPE_DRIVE: 131 d = gv_find_drive(sc, argv); 132 /* We don't allow to remove open drives. */ 133 if (gv_consumer_is_open(d->consumer) && 134 !(*flags & GV_FLAG_F)) { 135 gctl_error(req, "drive '%s' is open", d->name); 136 return; 137 } 138 139 /* A drive with subdisks needs a recursive removal. */ 140 /* if (!LIST_EMPTY(&d->subdisks) && 141 !(*flags & GV_FLAG_R)) { 142 gctl_error(req, "drive '%s' still has subdisks" 143 " - need recursive removal", d->name); 144 return; 145 }*/ 146 147 gv_post_event(sc, GV_EVENT_RM_DRIVE, d, NULL, *flags, 148 0); 149 break; 150 151 default: 152 gctl_error(req, "unknown object '%s'", argv); 153 return; 154 } 155 } 156 157 gv_post_event(sc, GV_EVENT_SAVE_CONFIG, sc, NULL, 0, 0); 158 } 159 160 /* Resets configuration */ 161 int 162 gv_resetconfig(struct gv_softc *sc) 163 { 164 struct gv_drive *d, *d2; 165 struct gv_volume *v, *v2; 166 struct gv_plex *p, *p2; 167 struct gv_sd *s, *s2; 168 169 /* First make sure nothing is open. */ 170 LIST_FOREACH_SAFE(d, &sc->drives, drive, d2) { 171 if (gv_consumer_is_open(d->consumer)) { 172 return (GV_ERR_ISBUSY); 173 } 174 } 175 176 /* Make sure nothing is going on internally. */ 177 LIST_FOREACH_SAFE(p, &sc->plexes, plex, p2) { 178 if (p->flags & (GV_PLEX_REBUILDING | GV_PLEX_GROWING)) 179 return (GV_ERR_ISBUSY); 180 } 181 182 /* Then if not, we remove everything. */ 183 LIST_FOREACH_SAFE(s, &sc->subdisks, sd, s2) 184 gv_rm_sd(sc, s); 185 LIST_FOREACH_SAFE(d, &sc->drives, drive, d2) 186 gv_rm_drive(sc, d, 0); 187 LIST_FOREACH_SAFE(p, &sc->plexes, plex, p2) 188 gv_rm_plex(sc, p); 189 LIST_FOREACH_SAFE(v, &sc->volumes, volume, v2) 190 gv_rm_vol(sc, v); 191 192 gv_post_event(sc, GV_EVENT_SAVE_CONFIG, sc, NULL, 0, 0); 193 194 return (0); 195 } 196 197 /* Remove a volume. */ 198 void 199 gv_rm_vol(struct gv_softc *sc, struct gv_volume *v) 200 { 201 struct g_provider *pp; 202 struct gv_plex *p, *p2; 203 204 KASSERT(v != NULL, ("gv_rm_vol: NULL v")); 205 pp = v->provider; 206 KASSERT(pp != NULL, ("gv_rm_vol: NULL pp")); 207 208 /* Check if any of our consumers is open. */ 209 if (gv_provider_is_open(pp)) { 210 G_VINUM_DEBUG(0, "unable to remove %s: volume still in use", 211 v->name); 212 return; 213 } 214 215 /* Remove the plexes our volume has. */ 216 LIST_FOREACH_SAFE(p, &v->plexes, in_volume, p2) 217 gv_rm_plex(sc, p); 218 219 /* Clean up. */ 220 LIST_REMOVE(v, volume); 221 g_free(v); 222 223 /* Get rid of the volume's provider. */ 224 if (pp != NULL) { 225 g_topology_lock(); 226 pp->flags |= G_PF_WITHER; 227 g_orphan_provider(pp, ENXIO); 228 g_topology_unlock(); 229 } 230 } 231 232 /* Remove a plex. */ 233 void 234 gv_rm_plex(struct gv_softc *sc, struct gv_plex *p) 235 { 236 struct gv_volume *v; 237 struct gv_sd *s, *s2; 238 239 KASSERT(p != NULL, ("gv_rm_plex: NULL p")); 240 v = p->vol_sc; 241 242 /* Check if any of our consumers is open. */ 243 if (v != NULL && gv_provider_is_open(v->provider) && v->plexcount < 2) { 244 G_VINUM_DEBUG(0, "unable to remove %s: volume still in use", 245 p->name); 246 return; 247 } 248 249 /* Remove the subdisks our plex has. */ 250 LIST_FOREACH_SAFE(s, &p->subdisks, in_plex, s2) 251 gv_rm_sd(sc, s); 252 253 v = p->vol_sc; 254 /* Clean up and let our geom fade away. */ 255 LIST_REMOVE(p, plex); 256 if (p->vol_sc != NULL) { 257 p->vol_sc->plexcount--; 258 LIST_REMOVE(p, in_volume); 259 p->vol_sc = NULL; 260 /* Correctly update the volume size. */ 261 gv_update_vol_size(v, gv_vol_size(v)); 262 } 263 264 g_free(p); 265 } 266 267 /* Remove a subdisk. */ 268 void 269 gv_rm_sd(struct gv_softc *sc, struct gv_sd *s) 270 { 271 struct gv_plex *p; 272 struct gv_volume *v; 273 274 KASSERT(s != NULL, ("gv_rm_sd: NULL s")); 275 276 p = s->plex_sc; 277 v = NULL; 278 279 /* Clean up. */ 280 if (p != NULL) { 281 LIST_REMOVE(s, in_plex); 282 s->plex_sc = NULL; 283 p->sdcount--; 284 /* Update the plexsize. */ 285 p->size = gv_plex_size(p); 286 v = p->vol_sc; 287 if (v != NULL) { 288 /* Update the size of our plex' volume. */ 289 gv_update_vol_size(v, gv_vol_size(v)); 290 } 291 } 292 if (s->drive_sc && !(s->drive_sc->flags & GV_DRIVE_REFERENCED)) 293 LIST_REMOVE(s, from_drive); 294 LIST_REMOVE(s, sd); 295 gv_free_sd(s); 296 g_free(s); 297 } 298 299 /* Remove a drive. */ 300 void 301 gv_rm_drive(struct gv_softc *sc, struct gv_drive *d, int flags) 302 { 303 struct g_consumer *cp; 304 struct gv_freelist *fl, *fl2; 305 struct gv_plex *p; 306 struct gv_sd *s, *s2; 307 struct gv_volume *v; 308 struct gv_drive *d2; 309 int err; 310 311 KASSERT(d != NULL, ("gv_rm_drive: NULL d")); 312 313 cp = d->consumer; 314 315 if (cp != NULL) { 316 g_topology_lock(); 317 err = g_access(cp, 0, 1, 0); 318 g_topology_unlock(); 319 320 if (err) { 321 G_VINUM_DEBUG(0, "%s: unable to access '%s', " 322 "errno: %d", __func__, cp->provider->name, err); 323 return; 324 } 325 326 /* Clear the Vinum Magic. */ 327 d->hdr->magic = GV_NOMAGIC; 328 err = gv_write_header(cp, d->hdr); 329 if (err) 330 G_VINUM_DEBUG(0, "gv_rm_drive: error writing header to" 331 " '%s', errno: %d", cp->provider->name, err); 332 333 g_topology_lock(); 334 g_access(cp, -cp->acr, -cp->acw, -cp->ace); 335 g_detach(cp); 336 g_destroy_consumer(cp); 337 g_topology_unlock(); 338 } 339 340 /* Remove all associated subdisks, plexes, volumes. */ 341 if (flags & GV_FLAG_R) { 342 if (!LIST_EMPTY(&d->subdisks)) { 343 LIST_FOREACH_SAFE(s, &d->subdisks, from_drive, s2) { 344 p = s->plex_sc; 345 if (p != NULL) { 346 v = p->vol_sc; 347 if (v != NULL) 348 gv_rm_vol(sc, v); 349 } 350 } 351 } 352 } 353 354 /* Clean up. */ 355 LIST_FOREACH_SAFE(fl, &d->freelist, freelist, fl2) { 356 LIST_REMOVE(fl, freelist); 357 g_free(fl); 358 } 359 360 LIST_REMOVE(d, drive); 361 g_free(d->hdr); 362 363 /* Put ourself into referenced state if we have subdisks. */ 364 if (d->sdcount > 0) { 365 d->consumer = NULL; 366 d->hdr = NULL; 367 d->flags |= GV_DRIVE_REFERENCED; 368 snprintf(d->device, sizeof(d->device), "???"); 369 d->size = 0; 370 d->avail = 0; 371 d->freelist_entries = 0; 372 LIST_FOREACH(s, &d->subdisks, from_drive) { 373 s->flags |= GV_SD_TASTED; 374 gv_set_sd_state(s, GV_SD_DOWN, GV_SETSTATE_FORCE); 375 } 376 /* Shuffle around so we keep gv_is_newer happy. */ 377 LIST_REMOVE(d, drive); 378 d2 = LIST_FIRST(&sc->drives); 379 if (d2 == NULL) 380 LIST_INSERT_HEAD(&sc->drives, d, drive); 381 else 382 LIST_INSERT_AFTER(d2, d, drive); 383 return; 384 } 385 g_free(d); 386 387 gv_save_config(sc); 388 } 389