]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - 6/sys/geom/vinum/geom_vinum_rm.c
merge fix for boot-time hang on centos' xen
[FreeBSD/FreeBSD.git] / 6 / sys / geom / vinum / geom_vinum_rm.c
1 /*-
2  *  Copyright (c) 2004 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/kernel.h>
34 #include <sys/malloc.h>
35
36 #include <geom/geom.h>
37 #include <geom/vinum/geom_vinum_var.h>
38 #include <geom/vinum/geom_vinum.h>
39 #include <geom/vinum/geom_vinum_share.h>
40
41 static void     gv_free_sd(struct gv_sd *);
42 static int      gv_rm_drive(struct gv_softc *, struct gctl_req *,
43                     struct gv_drive *, int);
44 static int      gv_rm_plex(struct gv_softc *, struct gctl_req *,
45                     struct gv_plex *, int);
46 static int      gv_rm_vol(struct gv_softc *, struct gctl_req *,
47                     struct gv_volume *, int);
48
49 /* General 'remove' routine. */
50 void
51 gv_remove(struct g_geom *gp, struct gctl_req *req)
52 {
53         struct gv_softc *sc;
54         struct gv_volume *v;
55         struct gv_plex *p;
56         struct gv_sd *s;
57         struct gv_drive *d;
58         int *argc, *flags;
59         char *argv, buf[20];
60         int i, type, err;
61
62         argc = gctl_get_paraml(req, "argc", sizeof(*argc));
63         flags = gctl_get_paraml(req, "flags", sizeof(*flags));
64
65         if (argc == NULL || *argc == 0) {
66                 gctl_error(req, "no arguments given");
67                 return;
68         }
69
70         sc = gp->softc;
71
72         for (i = 0; i < *argc; i++) {
73                 snprintf(buf, sizeof(buf), "argv%d", i);
74                 argv = gctl_get_param(req, buf, NULL);
75                 if (argv == NULL)
76                         continue;
77                 type = gv_object_type(sc, argv);
78                 switch (type) {
79                 case GV_TYPE_VOL:
80                         v = gv_find_vol(sc, argv);
81                         if (v == NULL) {
82                                 gctl_error(req, "unknown volume '%s'", argv);
83                                 return;
84                         }
85                         err = gv_rm_vol(sc, req, v, *flags);
86                         if (err)
87                                 return;
88                         break;
89                 case GV_TYPE_PLEX:
90                         p = gv_find_plex(sc, argv);
91                         if (p == NULL) {
92                                 gctl_error(req, "unknown plex '%s'", argv);
93                                 return;
94                         }
95                         err = gv_rm_plex(sc, req, p, *flags);
96                         if (err)
97                                 return;
98                         break;
99                 case GV_TYPE_SD:
100                         s = gv_find_sd(sc, argv);
101                         if (s == NULL) {
102                                 gctl_error(req, "unknown subdisk '%s'", argv);
103                                 return;
104                         }
105                         err = gv_rm_sd(sc, req, s, *flags);
106                         if (err)
107                                 return;
108                         break;
109                 case GV_TYPE_DRIVE:
110                         d = gv_find_drive(sc, argv);
111                         if (d == NULL) {
112                                 gctl_error(req, "unknown drive '%s'", argv);
113                                 return;
114                         }
115                         err = gv_rm_drive(sc, req, d, *flags);
116                         if (err)
117                                 return;
118                         break;
119                 default:
120                         gctl_error(req, "unknown object '%s'", argv);
121                         return;
122                 }
123         }
124
125         gv_save_config_all(sc);
126 }
127
128 /* Remove a volume. */
129 static int
130 gv_rm_vol(struct gv_softc *sc, struct gctl_req *req, struct gv_volume *v, int flags)
131 {
132         struct g_geom *gp;
133         struct gv_plex *p, *p2;
134         int err;
135
136         g_topology_assert();
137         KASSERT(v != NULL, ("gv_rm_vol: NULL v"));
138
139         /* If this volume has plexes, we want a recursive removal. */
140         if (!LIST_EMPTY(&v->plexes) && !(flags & GV_FLAG_R)) {
141                 gctl_error(req, "volume '%s' has attached plexes", v->name);
142                 return (-1);
143         }
144
145         gp = v->geom;
146
147         /* Check if any of our consumers is open. */
148         if (gp != NULL && gv_is_open(gp)) {
149                 gctl_error(req, "volume '%s' is busy", v->name);
150                 return (-1);
151         }
152
153         /* Remove the plexes our volume has. */
154         LIST_FOREACH_SAFE(p, &v->plexes, in_volume, p2) {
155                 v->plexcount--;
156                 LIST_REMOVE(p, in_volume);
157                 p->vol_sc = NULL;
158
159                 err = gv_rm_plex(sc, req, p, flags);
160                 if (err)
161                         return (err);
162         }
163
164         /* Clean up and let our geom fade away. */
165         LIST_REMOVE(v, volume);
166         gv_kill_vol_thread(v);
167         g_free(v);
168         if (gp != NULL) {
169                 gp->softc = NULL;
170                 g_wither_geom(gp, ENXIO);
171         }
172
173         return (0);
174 }
175
176 /* Remove a plex. */
177 static int
178 gv_rm_plex(struct gv_softc *sc, struct gctl_req *req, struct gv_plex *p, int flags)
179 {
180         struct g_geom *gp;
181         struct gv_sd *s, *s2;
182         int err;
183
184         g_topology_assert();
185
186         KASSERT(p != NULL, ("gv_rm_plex: NULL p"));
187
188         /* If this plex has subdisks, we want a recursive removal. */
189         if (!LIST_EMPTY(&p->subdisks) && !(flags & GV_FLAG_R)) {
190                 gctl_error(req, "plex '%s' has attached subdisks", p->name);
191                 return (-1);
192         }
193
194         if (p->vol_sc != NULL && p->vol_sc->plexcount == 1) {
195                 gctl_error(req, "plex '%s' is still attached to volume '%s'",
196                     p->name, p->volume);
197                 return (-1);
198         }
199
200         gp = p->geom;
201
202         /* Check if any of our consumers is open. */
203         if (gp != NULL && gv_is_open(gp)) {
204                 gctl_error(req, "plex '%s' is busy", p->name);
205                 return (-1);
206         }
207
208         /* Remove the subdisks our plex has. */
209         LIST_FOREACH_SAFE(s, &p->subdisks, in_plex, s2) {
210                 p->sdcount--;
211 #if 0
212                 LIST_REMOVE(s, in_plex);
213                 s->plex_sc = NULL;
214 #endif
215
216                 err = gv_rm_sd(sc, req, s, flags);
217                 if (err)
218                         return (err);
219         }
220
221         /* Clean up and let our geom fade away. */
222         LIST_REMOVE(p, plex);
223         if (p->vol_sc != NULL) {
224                 p->vol_sc->plexcount--;
225                 LIST_REMOVE(p, in_volume);
226                 p->vol_sc = NULL;
227         }
228
229         gv_kill_plex_thread(p);
230         g_free(p);
231
232         if (gp != NULL) {
233                 gp->softc = NULL;
234                 g_wither_geom(gp, ENXIO);
235         }
236
237         return (0);
238 }
239
240 /* Remove a subdisk. */
241 int
242 gv_rm_sd(struct gv_softc *sc, struct gctl_req *req, struct gv_sd *s, int flags)
243 {
244         struct g_provider *pp;
245
246         KASSERT(s != NULL, ("gv_rm_sd: NULL s"));
247
248         pp = s->provider;
249
250         /* Clean up. */
251         if (s->plex_sc)
252                 LIST_REMOVE(s, in_plex);
253         if (s->drive_sc)
254                 LIST_REMOVE(s, from_drive);
255         LIST_REMOVE(s, sd);
256         gv_free_sd(s);
257         g_free(s);
258
259         /* If the subdisk has a provider we need to clean up this one too. */
260         if (pp != NULL) {
261                 pp->flags |= G_PF_WITHER;
262                 g_orphan_provider(pp, ENXIO);
263         }
264
265         return (0);
266 }
267
268 /* Remove a drive. */
269 static int
270 gv_rm_drive(struct gv_softc *sc, struct gctl_req *req, struct gv_drive *d, int flags)
271 {
272         struct g_geom *gp;
273         struct g_consumer *cp;
274         struct gv_freelist *fl, *fl2;
275         struct gv_plex *p;
276         struct gv_sd *s, *s2;
277         struct gv_volume *v;
278         int err;
279
280         KASSERT(d != NULL, ("gv_rm_drive: NULL d"));
281         gp = d->geom;
282         KASSERT(gp != NULL, ("gv_rm_drive: NULL gp"));
283
284         /* We don't allow to remove open drives. */
285         if (gv_is_open(gp)) {
286                 gctl_error(req, "drive '%s' is open", d->name);
287                 return (-1);
288         }
289
290         /* A drive with subdisks needs a recursive removal. */
291         if (!LIST_EMPTY(&d->subdisks) && !(flags & GV_FLAG_R)) {
292                 gctl_error(req, "drive '%s' still has subdisks", d->name);
293                 return (-1);
294         }
295
296         cp = LIST_FIRST(&gp->consumer);
297         err = g_access(cp, 0, 1, 0);
298         if (err) {
299                 printf("GEOM_VINUM: gv_rm_drive: couldn't access '%s', errno: "
300                     "%d\n", cp->provider->name, err);
301                 return (err);
302         }
303
304         /* Clear the Vinum Magic. */
305         d->hdr->magic = GV_NOMAGIC;
306         g_topology_unlock();
307         err = g_write_data(cp, GV_HDR_OFFSET, d->hdr, GV_HDR_LEN);
308         if (err) {
309                 printf("GEOM_VINUM: gv_rm_drive: couldn't write header to '%s'"
310                     ", errno: %d\n", cp->provider->name, err);
311                 d->hdr->magic = GV_MAGIC;
312         }
313         g_topology_lock();
314         g_access(cp, 0, -1, 0);
315
316         /* Remove all associated subdisks, plexes, volumes. */
317         if (!LIST_EMPTY(&d->subdisks)) {
318                 LIST_FOREACH_SAFE(s, &d->subdisks, from_drive, s2) {
319                         p = s->plex_sc;
320                         if (p != NULL) {
321                                 v = p->vol_sc;
322                                 if (v != NULL)
323                                         gv_rm_vol(sc, req, v, flags);
324                         }
325                 }
326         }
327
328         /* Clean up. */
329         LIST_FOREACH_SAFE(fl, &d->freelist, freelist, fl2) {
330                 LIST_REMOVE(fl, freelist);
331                 g_free(fl);
332         }
333         LIST_REMOVE(d, drive);
334
335         gv_kill_drive_thread(d);
336         gp = d->geom;
337         d->geom = NULL;
338         g_free(d->hdr);
339         g_free(d);
340         gv_save_config_all(sc);
341         g_wither_geom(gp, ENXIO);
342
343         return (err);
344 }
345
346 static void
347 gv_free_sd(struct gv_sd *s)
348 {
349         struct gv_drive *d;
350         struct gv_freelist *fl, *fl2;
351
352         KASSERT(s != NULL, ("gv_free_sd: NULL s"));
353
354         d = s->drive_sc;
355         if (d == NULL)
356                 return;
357
358         /*
359          * First, find the free slot that's immediately before or after this
360          * subdisk.
361          */
362         fl = NULL;
363         LIST_FOREACH(fl, &d->freelist, freelist) {
364                 if (fl->offset == s->drive_offset + s->size)
365                         break;
366                 if (fl->offset + fl->size == s->drive_offset)
367                         break;
368         }
369
370         /* If there is no free slot behind this subdisk, so create one. */
371         if (fl == NULL) {
372
373                 fl = g_malloc(sizeof(*fl), M_WAITOK | M_ZERO);
374                 fl->size = s->size;
375                 fl->offset = s->drive_offset;
376
377                 if (d->freelist_entries == 0) {
378                         LIST_INSERT_HEAD(&d->freelist, fl, freelist);
379                 } else {
380                         LIST_FOREACH(fl2, &d->freelist, freelist) {
381                                 if (fl->offset < fl2->offset) {
382                                         LIST_INSERT_BEFORE(fl2, fl, freelist);
383                                         break;
384                                 } else if (LIST_NEXT(fl2, freelist) == NULL) {
385                                         LIST_INSERT_AFTER(fl2, fl, freelist);
386                                         break;
387                                 }
388                         }
389                 }
390
391                 d->freelist_entries++;
392
393         /* Expand the free slot we just found. */
394         } else {
395                 fl->size += s->size;
396                 if (fl->offset > s->drive_offset)
397                         fl->offset = s->drive_offset;
398         }
399
400         d->avail += s->size;
401 }