2 * Copyright (c) 2005 Chris Jones
5 * This software was developed for the FreeBSD Project by Chris Jones
6 * thanks to the support of Google's Summer of Code program and
7 * mentoring by Lukas Ertl.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
36 #include <sys/libkern.h>
37 #include <sys/kernel.h>
38 #include <sys/malloc.h>
40 #include <geom/geom.h>
41 #include <geom/vinum/geom_vinum_var.h>
42 #include <geom/vinum/geom_vinum.h>
43 #include <geom/vinum/geom_vinum_share.h>
45 static int gv_rename_drive(struct gv_softc *, struct gctl_req *,
46 struct gv_drive *, char *, int);
47 static int gv_rename_plex(struct gv_softc *, struct gctl_req *,
48 struct gv_plex *, char *, int);
49 static int gv_rename_sd(struct gv_softc *, struct gctl_req *,
50 struct gv_sd *, char *, int);
51 static int gv_rename_vol(struct gv_softc *, struct gctl_req *,
52 struct gv_volume *, char *, int);
55 gv_rename(struct g_geom *gp, struct gctl_req *req)
62 char *newname, *object;
63 int err, *flags, type;
67 flags = gctl_get_paraml(req, "flags", sizeof(*flags));
69 newname = gctl_get_param(req, "newname", NULL);
70 if (newname == NULL) {
71 gctl_error(req, "no new name given");
75 object = gctl_get_param(req, "object", NULL);
77 gctl_error(req, "no object given");
81 type = gv_object_type(sc, object);
84 v = gv_find_vol(sc, object);
86 gctl_error(req, "unknown volume '%s'", object);
89 err = gv_rename_vol(sc, req, v, newname, *flags);
94 p = gv_find_plex(sc, object);
96 gctl_error(req, "unknown plex '%s'", object);
99 err = gv_rename_plex(sc, req, p, newname, *flags);
104 s = gv_find_sd(sc, object);
106 gctl_error(req, "unknown subdisk '%s'", object);
109 err = gv_rename_sd(sc, req, s, newname, *flags);
114 d = gv_find_drive(sc, object);
116 gctl_error(req, "unknown drive '%s'", object);
119 err = gv_rename_drive(sc, req, d, newname, *flags);
124 gctl_error(req, "unknown object '%s'", object);
128 gv_save_config_all(sc);
132 gv_rename_drive(struct gv_softc *sc, struct gctl_req *req, struct gv_drive *d, char *newname, int flags)
137 KASSERT(d != NULL, ("gv_rename_drive: NULL d"));
139 if (gv_object_type(sc, newname) != -1) {
140 gctl_error(req, "drive name '%s' already in use", newname);
144 strncpy(d->name, newname, GV_MAXDRIVENAME);
145 strncpy(d->hdr->label.name, newname, GV_MAXDRIVENAME);
147 /* XXX can we rename providers here? */
149 LIST_FOREACH(s, &d->subdisks, from_drive)
150 strncpy(s->drive, d->name, GV_MAXDRIVENAME);
156 gv_rename_plex(struct gv_softc *sc, struct gctl_req *req, struct gv_plex *p, char *newname, int flags)
159 char *plexnum, *plexnump, *oldplex, *oldplexp;
160 char *newsd, *oldsd, *oldsdp;
164 KASSERT(p != NULL, ("gv_rename_plex: NULL p"));
168 if (gv_object_type(sc, newname) != -1) {
169 gctl_error(req, "plex name '%s' already in use", newname);
173 /* Needed for sanity checking. */
174 plexnum = g_malloc(GV_MAXPLEXNAME, M_WAITOK | M_ZERO);
175 strncpy(plexnum, newname, GV_MAXPLEXNAME);
178 oldplex = g_malloc(GV_MAXPLEXNAME, M_WAITOK | M_ZERO);
179 strncpy(oldplex, p->name, GV_MAXPLEXNAME);
183 * Locate the plex number part of the plex names.
185 * XXX: can we be sure that the current plex name has the format
188 strsep(&oldplexp, ".");
189 strsep(&plexnump, ".");
190 if (plexnump == NULL || *plexnump == '\0') {
191 gctl_error(req, "proposed plex name '%s' is not a valid plex "
196 if (strcmp(oldplexp, plexnump)) {
197 gctl_error(req, "current and proposed plex numbers (%s, %s) "
198 "do not match", plexnump, oldplexp);
203 strncpy(p->name, newname, GV_MAXPLEXNAME);
205 /* XXX can we rename providers here? */
207 /* Fix up references and potentially rename subdisks. */
208 LIST_FOREACH(s, &p->subdisks, in_plex) {
209 strncpy(s->plex, p->name, GV_MAXPLEXNAME);
210 if (flags && GV_FLAG_R) {
211 newsd = g_malloc(GV_MAXSDNAME, M_WAITOK | M_ZERO);
212 oldsd = g_malloc(GV_MAXSDNAME, M_WAITOK | M_ZERO);
214 strncpy(oldsd, s->name, GV_MAXSDNAME);
216 * XXX: can we be sure that the current sd name has the
217 * format 'foo.pX.sY'?
219 strsep(&oldsdp, ".");
220 strsep(&oldsdp, ".");
221 snprintf(newsd, GV_MAXSDNAME, "%s.%s", p->name, oldsdp);
222 err = gv_rename_sd(sc, req, s, newsd, flags);
238 * gv_rename_sd: renames a subdisk. Note that the 'flags' argument is ignored,
239 * since there are no structures below a subdisk. Similarly, we don't have to
240 * clean up any references elsewhere to the subdisk's name.
243 gv_rename_sd(struct gv_softc *sc, struct gctl_req *req, struct gv_sd *s, char * newname, int flags)
245 char *new, *newp, *old, *oldp;
249 KASSERT(s != NULL, ("gv_rename_sd: NULL s"));
253 if (gv_object_type(sc, newname) != -1) {
254 gctl_error(req, "subdisk name %s already in use", newname);
258 /* Needed for sanity checking. */
259 new = g_malloc(GV_MAXSDNAME, M_WAITOK | M_ZERO);
260 strncpy(new, newname, GV_MAXSDNAME);
263 old = g_malloc(GV_MAXSDNAME, M_WAITOK | M_ZERO);
264 strncpy(old, s->name, GV_MAXSDNAME);
268 * Locate the sd number part of the sd names.
270 * XXX: can we be sure that the current sd name has the format
276 if (newp == NULL || *newp == '\0') {
277 gctl_error(req, "proposed sd name '%s' is not a valid sd name",
283 if (newp == NULL || *newp == '\0') {
284 gctl_error(req, "proposed sd name '%s' is not a valid sd name",
289 if (strcmp(newp, oldp)) {
290 gctl_error(req, "current and proposed sd numbers (%s, %s) do "
291 "not match", oldp, newp);
296 strncpy(s->name, newname, GV_MAXSDNAME);
298 /* XXX: can we rename providers here? */
308 gv_rename_vol(struct gv_softc *sc, struct gctl_req *req, struct gv_volume *v, char *newname, int flags)
311 char *new, *old, *oldp;
315 KASSERT(v != NULL, ("gv_rename_vol: NULL v"));
317 if (gv_object_type(sc, newname) != -1) {
318 gctl_error(req, "volume name %s already in use", newname);
322 /* Rename the volume. */
323 strncpy(v->name, newname, GV_MAXVOLNAME);
325 /* Fix up references and potentially rename plexes. */
326 LIST_FOREACH(p, &v->plexes, in_volume) {
327 strncpy(p->volume, v->name, GV_MAXVOLNAME);
328 if (flags && GV_FLAG_R) {
329 new = g_malloc(GV_MAXPLEXNAME, M_WAITOK | M_ZERO);
330 old = g_malloc(GV_MAXPLEXNAME, M_WAITOK | M_ZERO);
332 strncpy(old, p->name, GV_MAXPLEXNAME);
334 * XXX: can we be sure that the current plex name has
335 * the format 'foo.pX'?
338 snprintf(new, GV_MAXPLEXNAME, "%s.%s", v->name, oldp);
339 err = gv_rename_plex(sc, req, p, new, flags);