2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2005 Chris Jones
7 * This software was developed for the FreeBSD Project by Chris Jones
8 * thanks to the support of Google's Summer of Code program and
9 * mentoring by Lukas Ertl.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
20 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include <sys/param.h>
38 #include <sys/libkern.h>
39 #include <sys/malloc.h>
41 #include <geom/geom.h>
42 #include <geom/vinum/geom_vinum_var.h>
43 #include <geom/vinum/geom_vinum.h>
46 gv_rename(struct g_geom *gp, struct gctl_req *req)
53 char *newname, *object, *name;
58 flags = gctl_get_paraml(req, "flags", sizeof(*flags));
60 gctl_error(req, "no flags given");
64 newname = gctl_get_param(req, "newname", NULL);
65 if (newname == NULL) {
66 gctl_error(req, "no new name given");
70 object = gctl_get_param(req, "object", NULL);
72 gctl_error(req, "no object given");
76 type = gv_object_type(sc, object);
79 v = gv_find_vol(sc, object);
81 gctl_error(req, "unknown volume '%s'", object);
84 name = g_malloc(GV_MAXVOLNAME, M_WAITOK | M_ZERO);
85 strlcpy(name, newname, GV_MAXVOLNAME);
86 gv_post_event(sc, GV_EVENT_RENAME_VOL, v, name, *flags, 0);
89 p = gv_find_plex(sc, object);
91 gctl_error(req, "unknown plex '%s'", object);
94 name = g_malloc(GV_MAXPLEXNAME, M_WAITOK | M_ZERO);
95 strlcpy(name, newname, GV_MAXPLEXNAME);
96 gv_post_event(sc, GV_EVENT_RENAME_PLEX, p, name, *flags, 0);
99 s = gv_find_sd(sc, object);
101 gctl_error(req, "unknown subdisk '%s'", object);
104 name = g_malloc(GV_MAXSDNAME, M_WAITOK | M_ZERO);
105 strlcpy(name, newname, GV_MAXSDNAME);
106 gv_post_event(sc, GV_EVENT_RENAME_SD, s, name, *flags, 0);
109 d = gv_find_drive(sc, object);
111 gctl_error(req, "unknown drive '%s'", object);
114 name = g_malloc(GV_MAXDRIVENAME, M_WAITOK | M_ZERO);
115 strlcpy(name, newname, GV_MAXDRIVENAME);
116 gv_post_event(sc, GV_EVENT_RENAME_DRIVE, d, name, *flags, 0);
119 gctl_error(req, "unknown object '%s'", object);
125 gv_rename_drive(struct gv_softc *sc, struct gv_drive *d, char *newname,
130 KASSERT(d != NULL, ("gv_rename_drive: NULL d"));
132 if (gv_object_type(sc, newname) != GV_ERR_NOTFOUND) {
133 G_VINUM_DEBUG(1, "drive name '%s' already in use", newname);
134 return (GV_ERR_NAMETAKEN);
137 strlcpy(d->name, newname, sizeof(d->name));
139 strlcpy(d->hdr->label.name, newname, sizeof(d->hdr->label.name));
141 LIST_FOREACH(s, &d->subdisks, from_drive)
142 strlcpy(s->drive, d->name, sizeof(s->drive));
148 gv_rename_plex(struct gv_softc *sc, struct gv_plex *p, char *newname, int flags)
150 char newsd[GV_MAXSDNAME];
155 KASSERT(p != NULL, ("gv_rename_plex: NULL p"));
157 if (gv_object_type(sc, newname) != GV_ERR_NOTFOUND) {
158 G_VINUM_DEBUG(1, "plex name '%s' already in use", newname);
159 return (GV_ERR_NAMETAKEN);
163 * Locate the plex number part of the plex names.
164 * XXX: might be a good idea to sanitize input a bit more
166 ptr = strrchr(newname, '.');
168 G_VINUM_DEBUG(0, "proposed plex name '%s' is not a valid plex "
170 return (GV_ERR_INVNAME);
173 strlcpy(p->name, newname, sizeof(p->name));
175 /* Fix up references and potentially rename subdisks. */
176 LIST_FOREACH(s, &p->subdisks, in_plex) {
177 strlcpy(s->plex, p->name, sizeof(s->plex));
178 if (flags & GV_FLAG_R) {
180 * Look for the two last dots in the string, and assume
181 * that the old value was ok.
183 ptr = strrchr(s->name, '.');
185 return (GV_ERR_INVNAME);
187 snprintf(newsd, sizeof(newsd), "%s.%s", p->name, ptr);
188 err = gv_rename_sd(sc, s, newsd, flags);
197 * gv_rename_sd: renames a subdisk. Note that the 'flags' argument is ignored,
198 * since there are no structures below a subdisk. Similarly, we don't have to
199 * clean up any references elsewhere to the subdisk's name.
202 gv_rename_sd(struct gv_softc *sc, struct gv_sd *s, char *newname, int flags)
206 KASSERT(s != NULL, ("gv_rename_sd: NULL s"));
208 if (gv_object_type(sc, newname) != GV_ERR_NOTFOUND) {
209 G_VINUM_DEBUG(1, "subdisk name %s already in use", newname);
210 return (GV_ERR_NAMETAKEN);
213 /* Locate the sd number part of the sd names. */
214 dot1 = strchr(newname, '.');
215 if (dot1 == NULL || (dot2 = strchr(dot1 + 1, '.')) == NULL) {
216 G_VINUM_DEBUG(0, "proposed sd name '%s' is not a valid sd name",
218 return (GV_ERR_INVNAME);
220 strlcpy(s->name, newname, sizeof(s->name));
225 gv_rename_vol(struct gv_softc *sc, struct gv_volume *v, char *newname,
228 struct g_provider *pp;
230 char newplex[GV_MAXPLEXNAME], *ptr;
233 KASSERT(v != NULL, ("gv_rename_vol: NULL v"));
235 KASSERT(pp != NULL, ("gv_rename_vol: NULL pp"));
237 if (gv_object_type(sc, newname) != GV_ERR_NOTFOUND) {
238 G_VINUM_DEBUG(1, "volume name %s already in use", newname);
239 return (GV_ERR_NAMETAKEN);
242 /* Rename the volume. */
243 strlcpy(v->name, newname, sizeof(v->name));
245 /* Fix up references and potentially rename plexes. */
246 LIST_FOREACH(p, &v->plexes, in_volume) {
247 strlcpy(p->volume, v->name, sizeof(p->volume));
248 if (flags & GV_FLAG_R) {
250 * Look for the last dot in the string, and assume that
251 * the old value was ok.
253 ptr = strrchr(p->name, '.');
255 snprintf(newplex, sizeof(newplex), "%s.%s", v->name, ptr);
256 err = gv_rename_plex(sc, p, newplex, flags);