summaryrefslogtreecommitdiffstats
path: root/sys/geom/vinum/geom_vinum_rename.c
blob: 50501d2dd8f05f6a5bb6a68679a391cbef17eace (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/*-
 *  Copyright (c) 2005 Chris Jones
 *  All rights reserved.
 *
 * This software was developed for the FreeBSD Project by Chris Jones
 * thanks to the support of Google's Summer of Code program and
 * mentoring by Lukas Ertl.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/param.h>
#include <sys/libkern.h>
#include <sys/kernel.h>
#include <sys/malloc.h>

#include <geom/geom.h>
#include <geom/vinum/geom_vinum_var.h>
#include <geom/vinum/geom_vinum.h>
#include <geom/vinum/geom_vinum_share.h>

static int	gv_rename_drive(struct gv_softc *, struct gctl_req *,
		    struct gv_drive *, char *, int);
static int	gv_rename_plex(struct gv_softc *, struct gctl_req *,
		    struct gv_plex *, char *, int);
static int	gv_rename_sd(struct gv_softc *, struct gctl_req *,
		    struct gv_sd *, char *, int);
static int	gv_rename_vol(struct gv_softc *, struct gctl_req *,
		    struct gv_volume *, char *, int);

void
gv_rename(struct g_geom *gp, struct gctl_req *req)
{
	struct gv_softc *sc;
	struct gv_volume *v;
	struct gv_plex *p;
	struct gv_sd *s;
	struct gv_drive *d;
	char *newname, *object;
	int err, *flags, type;

	sc = gp->softc;

	flags = gctl_get_paraml(req, "flags", sizeof(*flags));

	newname = gctl_get_param(req, "newname", NULL);
	if (newname == NULL) {
		gctl_error(req, "no new name given");
		return;
	}

	object = gctl_get_param(req, "object", NULL);
	if (object == NULL) {
		gctl_error(req, "no object given");
		return;
	}

	type = gv_object_type(sc, object);
	switch (type) {
	case GV_TYPE_VOL:
		v = gv_find_vol(sc, object);
		if (v == NULL) 	{
			gctl_error(req, "unknown volume '%s'", object);
			return;
		}
		err = gv_rename_vol(sc, req, v, newname, *flags);
		if (err)
			return;
		break;
	case GV_TYPE_PLEX:
		p = gv_find_plex(sc, object);
		if (p == NULL) {
			gctl_error(req, "unknown plex '%s'", object);
			return;
		}
		err = gv_rename_plex(sc, req, p, newname, *flags);
		if (err)
			return;
		break;
	case GV_TYPE_SD:
		s = gv_find_sd(sc, object);
		if (s == NULL) {
			gctl_error(req, "unknown subdisk '%s'", object);
			return;
		}
		err = gv_rename_sd(sc, req, s, newname, *flags);
		if (err)
			return;
		break;
	case GV_TYPE_DRIVE:
		d = gv_find_drive(sc, object);
		if (d == NULL) {
			gctl_error(req, "unknown drive '%s'", object);
			return;
		}
		err = gv_rename_drive(sc, req, d, newname, *flags);
		if (err)
			return;
		break;
	default:
		gctl_error(req, "unknown object '%s'", object);
		return;
	}

	gv_save_config_all(sc);
}

static int
gv_rename_drive(struct gv_softc *sc, struct gctl_req *req, struct gv_drive *d, char *newname, int flags)
{
	struct gv_sd *s;

	g_topology_assert();
	KASSERT(d != NULL, ("gv_rename_drive: NULL d"));

	if (gv_object_type(sc, newname) != -1) {
		gctl_error(req, "drive name '%s' already in use", newname);
		return (-1);
	}

	strncpy(d->name, newname, GV_MAXDRIVENAME);
	strncpy(d->hdr->label.name, newname, GV_MAXDRIVENAME);

	/* XXX can we rename providers here? */

	LIST_FOREACH(s, &d->subdisks, from_drive)
		strncpy(s->drive, d->name, GV_MAXDRIVENAME);

	return (0);
}

static int	
gv_rename_plex(struct gv_softc *sc, struct gctl_req *req, struct gv_plex *p, char *newname, int flags)
{
	struct gv_sd *s;
	char *plexnum, *plexnump, *oldplex, *oldplexp;
	char *newsd, *oldsd, *oldsdp;
	int err;

	g_topology_assert();
	KASSERT(p != NULL, ("gv_rename_plex: NULL p"));

	err = 0;

	if (gv_object_type(sc, newname) != -1) {
		gctl_error(req, "plex name '%s' already in use", newname);
		return (-1);
	}

	/* Needed for sanity checking. */
	plexnum = g_malloc(GV_MAXPLEXNAME, M_WAITOK | M_ZERO);
	strncpy(plexnum, newname, GV_MAXPLEXNAME);
	plexnump = plexnum;

	oldplex = g_malloc(GV_MAXPLEXNAME, M_WAITOK | M_ZERO);
	strncpy(oldplex, p->name, GV_MAXPLEXNAME);
	oldplexp = oldplex;

	/*
	 * Locate the plex number part of the plex names.
	 *
	 * XXX: can we be sure that the current plex name has the format
	 * 'foo.pX'?
	 */
	strsep(&oldplexp, ".");
	strsep(&plexnump, ".");
	if (plexnump == NULL || *plexnump == '\0') {
		gctl_error(req, "proposed plex name '%s' is not a valid plex "
		    "name", newname);
		err = -1;
		goto failure;
	}
	if (strcmp(oldplexp, plexnump)) {
		gctl_error(req, "current and proposed plex numbers (%s, %s) "
		    "do not match", plexnump, oldplexp);
		err = -1;
		goto failure;
	}

	strncpy(p->name, newname, GV_MAXPLEXNAME);

	/* XXX can we rename providers here? */

	/* Fix up references and potentially rename subdisks. */
	LIST_FOREACH(s, &p->subdisks, in_plex) {
		strncpy(s->plex, p->name, GV_MAXPLEXNAME);
		if (flags && GV_FLAG_R) {
			newsd = g_malloc(GV_MAXSDNAME, M_WAITOK | M_ZERO);
			oldsd = g_malloc(GV_MAXSDNAME, M_WAITOK | M_ZERO);
			oldsdp = oldsd;
			strncpy(oldsd, s->name, GV_MAXSDNAME);
			/*
			 * XXX: can we be sure that the current sd name has the
			 * format 'foo.pX.sY'?
			 */
			strsep(&oldsdp, ".");
			strsep(&oldsdp, ".");
			snprintf(newsd, GV_MAXSDNAME, "%s.%s", p->name, oldsdp);
			err = gv_rename_sd(sc, req, s, newsd, flags);
			g_free(newsd);
			g_free(oldsd);
			if (err)
				goto failure;
		}
	}

failure:
	g_free(plexnum);
	g_free(oldplex);

	return (err);
}

/*
 * gv_rename_sd: renames a subdisk.  Note that the 'flags' argument is ignored,
 * since there are no structures below a subdisk.  Similarly, we don't have to
 * clean up any references elsewhere to the subdisk's name.
 */
static int
gv_rename_sd(struct gv_softc *sc, struct gctl_req *req, struct gv_sd *s, char * newname, int flags)
{
	char *new, *newp, *old, *oldp;
	int err;

	g_topology_assert();
	KASSERT(s != NULL, ("gv_rename_sd: NULL s"));

	err = 0;

	if (gv_object_type(sc, newname) != -1) {
		gctl_error(req, "subdisk name %s already in use", newname);
		return (-1);
	}

	/* Needed for sanity checking. */
	new = g_malloc(GV_MAXSDNAME, M_WAITOK | M_ZERO);
	strncpy(new, newname, GV_MAXSDNAME);
	newp = new;

	old = g_malloc(GV_MAXSDNAME, M_WAITOK | M_ZERO);
	strncpy(old, s->name, GV_MAXSDNAME);
	oldp = old;

	/*
	 * Locate the sd number part of the sd names.
	 *
	 * XXX: can we be sure that the current sd name has the format
	 * 'foo.pX.sY'?
	 */
	strsep(&oldp, ".");
	strsep(&oldp, ".");
	strsep(&newp, ".");
	if (newp == NULL || *newp == '\0') {
		gctl_error(req, "proposed sd name '%s' is not a valid sd name",
		    newname);
		err = -1;
		goto fail;
	}
	strsep(&newp, ".");
	if (newp == NULL || *newp == '\0') {
		gctl_error(req, "proposed sd name '%s' is not a valid sd name",
		    newname);
		err = -1;
		goto fail;
	}
	if (strcmp(newp, oldp)) {
		gctl_error(req, "current and proposed sd numbers (%s, %s) do "
		    "not match", oldp, newp);
		err = -1;
		goto fail;
	}

	strncpy(s->name, newname, GV_MAXSDNAME);

	/* XXX: can we rename providers here? */

fail:
	g_free(new);
	g_free(old);

	return (err);
}

static int
gv_rename_vol(struct gv_softc *sc, struct gctl_req *req, struct gv_volume *v, char *newname, int flags)
{
	struct gv_plex *p;
	char *new, *old, *oldp;
	int err;

	g_topology_assert();
	KASSERT(v != NULL, ("gv_rename_vol: NULL v"));

	if (gv_object_type(sc, newname) != -1) {
		gctl_error(req, "volume name %s already in use", newname);
		return (-1);
	}

	/* Rename the volume. */
	strncpy(v->name, newname, GV_MAXVOLNAME);

	/* Fix up references and potentially rename plexes. */
	LIST_FOREACH(p, &v->plexes, in_volume) {
		strncpy(p->volume, v->name, GV_MAXVOLNAME);
		if (flags && GV_FLAG_R) {
			new = g_malloc(GV_MAXPLEXNAME, M_WAITOK | M_ZERO);
			old = g_malloc(GV_MAXPLEXNAME, M_WAITOK | M_ZERO);
			oldp = old;
			strncpy(old, p->name, GV_MAXPLEXNAME);
			/*
			 * XXX: can we be sure that the current plex name has
			 * the format 'foo.pX'?
			 */
			strsep(&oldp, ".");
			snprintf(new, GV_MAXPLEXNAME, "%s.%s", v->name, oldp);
			err = gv_rename_plex(sc, req, p, new, flags);
			g_free(new);
			g_free(old);
			if (err)
				return (err);
		}
	}

	return (0);
}
OpenPOWER on IntegriCloud