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/malloc.h>
39 #include <geom/geom.h>
40 #include <geom/vinum/geom_vinum_var.h>
41 #include <geom/vinum/geom_vinum.h>
44 gv_rename(struct g_geom *gp, struct gctl_req *req)
51 char *newname, *object, *name;
56 flags = gctl_get_paraml(req, "flags", sizeof(*flags));
58 gctl_error(req, "no flags given");
62 newname = gctl_get_param(req, "newname", NULL);
63 if (newname == NULL) {
64 gctl_error(req, "no new name given");
68 object = gctl_get_param(req, "object", NULL);
70 gctl_error(req, "no object given");
74 type = gv_object_type(sc, object);
77 v = gv_find_vol(sc, object);
79 gctl_error(req, "unknown volume '%s'", object);
82 name = g_malloc(GV_MAXVOLNAME, M_WAITOK | M_ZERO);
83 strlcpy(name, newname, GV_MAXVOLNAME);
84 gv_post_event(sc, GV_EVENT_RENAME_VOL, v, name, *flags, 0);
87 p = gv_find_plex(sc, object);
89 gctl_error(req, "unknown plex '%s'", object);
92 name = g_malloc(GV_MAXPLEXNAME, M_WAITOK | M_ZERO);
93 strlcpy(name, newname, GV_MAXPLEXNAME);
94 gv_post_event(sc, GV_EVENT_RENAME_PLEX, p, name, *flags, 0);
97 s = gv_find_sd(sc, object);
99 gctl_error(req, "unknown subdisk '%s'", object);
102 name = g_malloc(GV_MAXSDNAME, M_WAITOK | M_ZERO);
103 strlcpy(name, newname, GV_MAXSDNAME);
104 gv_post_event(sc, GV_EVENT_RENAME_SD, s, name, *flags, 0);
107 d = gv_find_drive(sc, object);
109 gctl_error(req, "unknown drive '%s'", object);
112 name = g_malloc(GV_MAXDRIVENAME, M_WAITOK | M_ZERO);
113 strlcpy(name, newname, GV_MAXDRIVENAME);
114 gv_post_event(sc, GV_EVENT_RENAME_DRIVE, d, name, *flags, 0);
117 gctl_error(req, "unknown object '%s'", object);
123 gv_rename_drive(struct gv_softc *sc, struct gv_drive *d, char *newname,
128 KASSERT(d != NULL, ("gv_rename_drive: NULL d"));
130 if (gv_object_type(sc, newname) != GV_ERR_NOTFOUND) {
131 G_VINUM_DEBUG(1, "drive name '%s' already in use", newname);
132 return (GV_ERR_NAMETAKEN);
135 strlcpy(d->name, newname, sizeof(d->name));
137 strlcpy(d->hdr->label.name, newname, sizeof(d->hdr->label.name));
139 LIST_FOREACH(s, &d->subdisks, from_drive)
140 strlcpy(s->drive, d->name, sizeof(s->drive));
146 gv_rename_plex(struct gv_softc *sc, struct gv_plex *p, char *newname, int flags)
148 char newsd[GV_MAXSDNAME];
153 KASSERT(p != NULL, ("gv_rename_plex: NULL p"));
155 if (gv_object_type(sc, newname) != GV_ERR_NOTFOUND) {
156 G_VINUM_DEBUG(1, "plex name '%s' already in use", newname);
157 return (GV_ERR_NAMETAKEN);
161 * Locate the plex number part of the plex names.
162 * XXX: might be a good idea to sanitize input a bit more
164 ptr = strrchr(newname, '.');
166 G_VINUM_DEBUG(0, "proposed plex name '%s' is not a valid plex "
168 return (GV_ERR_INVNAME);
171 strlcpy(p->name, newname, sizeof(p->name));
173 /* Fix up references and potentially rename subdisks. */
174 LIST_FOREACH(s, &p->subdisks, in_plex) {
175 strlcpy(s->plex, p->name, sizeof(s->plex));
176 if (flags & GV_FLAG_R) {
178 * Look for the two last dots in the string, and assume
179 * that the old value was ok.
181 ptr = strrchr(s->name, '.');
183 return (GV_ERR_INVNAME);
185 snprintf(newsd, sizeof(newsd), "%s.%s", p->name, ptr);
186 err = gv_rename_sd(sc, s, newsd, flags);
195 * gv_rename_sd: renames a subdisk. Note that the 'flags' argument is ignored,
196 * since there are no structures below a subdisk. Similarly, we don't have to
197 * clean up any references elsewhere to the subdisk's name.
200 gv_rename_sd(struct gv_softc *sc, struct gv_sd *s, char *newname, int flags)
204 KASSERT(s != NULL, ("gv_rename_sd: NULL s"));
206 if (gv_object_type(sc, newname) != GV_ERR_NOTFOUND) {
207 G_VINUM_DEBUG(1, "subdisk name %s already in use", newname);
208 return (GV_ERR_NAMETAKEN);
211 /* Locate the sd number part of the sd names. */
212 dot1 = strchr(newname, '.');
213 if (dot1 == NULL || (dot2 = strchr(dot1 + 1, '.')) == NULL) {
214 G_VINUM_DEBUG(0, "proposed sd name '%s' is not a valid sd name",
216 return (GV_ERR_INVNAME);
218 strlcpy(s->name, newname, sizeof(s->name));
223 gv_rename_vol(struct gv_softc *sc, struct gv_volume *v, char *newname,
226 struct g_provider *pp;
228 char newplex[GV_MAXPLEXNAME], *ptr;
231 KASSERT(v != NULL, ("gv_rename_vol: NULL v"));
233 KASSERT(pp != NULL, ("gv_rename_vol: NULL pp"));
235 if (gv_object_type(sc, newname) != GV_ERR_NOTFOUND) {
236 G_VINUM_DEBUG(1, "volume name %s already in use", newname);
237 return (GV_ERR_NAMETAKEN);
240 /* Rename the volume. */
241 strlcpy(v->name, newname, sizeof(v->name));
243 /* Fix up references and potentially rename plexes. */
244 LIST_FOREACH(p, &v->plexes, in_volume) {
245 strlcpy(p->volume, v->name, sizeof(p->volume));
246 if (flags & GV_FLAG_R) {
248 * Look for the last dot in the string, and assume that
249 * the old value was ok.
251 ptr = strrchr(p->name, '.');
253 snprintf(newplex, sizeof(newplex), "%s.%s", v->name, ptr);
254 err = gv_rename_plex(sc, p, newplex, flags);