g_label.c revision 135522
1/*-
2 * Copyright (c) 2004 Pawel Jakub Dawidek <pjd@FreeBSD.org>
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 THE AUTHORS 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 THE AUTHORS 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#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: head/sys/geom/label/g_label.c 135522 2004-09-20 17:26:25Z pjd $");
29
30#include <sys/param.h>
31#include <sys/systm.h>
32#include <sys/kernel.h>
33#include <sys/module.h>
34#include <sys/lock.h>
35#include <sys/mutex.h>
36#include <sys/bio.h>
37#include <sys/sysctl.h>
38#include <sys/malloc.h>
39#include <geom/geom.h>
40#include <geom/geom_slice.h>
41#include <geom/label/g_label.h>
42
43
44SYSCTL_DECL(_kern_geom);
45SYSCTL_NODE(_kern_geom, OID_AUTO, label, CTLFLAG_RW, 0, "GEOM_LABEL stuff");
46u_int g_label_debug = 0;
47TUNABLE_INT("kern.geom.label.debug", &g_label_debug);
48SYSCTL_UINT(_kern_geom_label, OID_AUTO, debug, CTLFLAG_RW, &g_label_debug, 0,
49    "Debug level");
50
51static int g_label_destroy(struct g_geom *gp, boolean_t force);
52static struct g_geom *g_label_taste(struct g_class *mp, struct g_provider *pp,
53    int flags __unused);
54static void g_label_config(struct gctl_req *req, struct g_class *mp,
55    const char *verb);
56
57struct g_class g_label_class = {
58	.name = G_LABEL_CLASS_NAME,
59	.version = G_VERSION,
60	.ctlreq = g_label_config,
61	.taste = g_label_taste
62};
63
64/*
65 * To add a new file system where you want to look for volume labels,
66 * you have to:
67 * 1. Add a file which implements looking for volume labels.
68 * 2. Add an 'extern const struct g_label_desc g_label_<your file system>;'.
69 * 3. Add an element to the table below '&g_label_<your_file_system>,'.
70 */
71const struct g_label_desc *g_labels[] = {
72	&g_label_ufs,
73	&g_label_iso9660,
74	&g_label_msdosfs,
75	NULL
76};
77
78
79static void
80g_label_orphan(struct g_consumer *cp __unused)
81{
82
83	KASSERT(1 == 0, ("%s called?", __func__));
84}
85
86static void
87g_label_start(struct bio *bp __unused)
88{
89
90	KASSERT(1 == 0, ("%s called?", __func__));
91}
92
93static int
94g_label_access(struct g_provider *pp __unused, int dr __unused, int dw __unused,
95    int de __unused)
96{
97
98	KASSERT(1 == 0, ("%s called", __func__));
99	return (EOPNOTSUPP);
100}
101
102static struct g_geom *
103g_label_create(struct gctl_req *req, struct g_class *mp, struct g_provider *pp,
104    const char *label, const char *dir, off_t mediasize)
105{
106	struct g_geom *gp;
107	struct g_provider *pp2;
108	struct g_consumer *cp;
109	char name[64];
110
111	g_topology_assert();
112
113	gp = NULL;
114	cp = NULL;
115	snprintf(name, sizeof(name), "%s/%s", dir, label);
116	LIST_FOREACH(gp, &mp->geom, geom) {
117		pp2 = LIST_FIRST(&gp->provider);
118		if (pp2 == NULL)
119			continue;
120		if (strcmp(pp2->name, name) == 0) {
121			G_LABEL_DEBUG(1, "Label %s(%s) already exists (%s).",
122			    label, name, pp->name);
123			if (req != NULL) {
124				gctl_error(req, "Provider %s already exists.",
125				    name);
126			}
127			return (NULL);
128		}
129	}
130	gp = g_slice_new(mp, 1, pp, &cp, NULL, 0, NULL);
131	if (gp == NULL) {
132		G_LABEL_DEBUG(0, "Cannot create slice %s.", label);
133		if (req != NULL)
134			gctl_error(req, "Cannot create slice %s.", label);
135		return (NULL);
136	}
137	g_access(cp, -1, 0, 0);
138	g_slice_config(gp, 0, G_SLICE_CONFIG_SET, (off_t)0, mediasize,
139	    pp->sectorsize, name);
140	G_LABEL_DEBUG(0, "Label for provider %s is %s.", pp->name, name);
141	return (gp);
142}
143
144static int
145g_label_destroy(struct g_geom *gp, boolean_t force)
146{
147	struct g_provider *pp;
148
149	g_topology_assert();
150	pp = LIST_FIRST(&gp->provider);
151	if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) {
152		if (force) {
153			G_LABEL_DEBUG(0, "Provider %s is still open, so it "
154			    "can't be definitely removed.", pp->name);
155		} else {
156			G_LABEL_DEBUG(1,
157			    "Provider %s is still open (r%dw%de%d).", pp->name,
158			    pp->acr, pp->acw, pp->ace);
159			return (EBUSY);
160		}
161	} else {
162		G_LABEL_DEBUG(0, "Label %s removed.",
163		    LIST_FIRST(&gp->provider)->name);
164	}
165	g_slice_spoiled(LIST_FIRST(&gp->consumer));
166	return (0);
167}
168
169static int
170g_label_read_metadata(struct g_consumer *cp, struct g_label_metadata *md)
171{
172	struct g_provider *pp;
173	u_char *buf;
174	int error;
175
176	g_topology_assert();
177
178	pp = cp->provider;
179	g_topology_unlock();
180	buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize,
181	    &error);
182	g_topology_lock();
183	if (buf == NULL)
184		return (error);
185	/* Decode metadata. */
186	label_metadata_decode(buf, md);
187	g_free(buf);
188
189	return (0);
190}
191
192static struct g_geom *
193g_label_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
194{
195	struct g_label_metadata md;
196	struct g_consumer *cp;
197	struct g_geom *gp;
198	int i;
199
200	g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name);
201	g_topology_assert();
202
203	G_LABEL_DEBUG(2, "Tasting %s.", pp->name);
204
205	if (strcmp(pp->geom->class->name, mp->name) == 0)
206		return (NULL);
207
208	gp = g_new_geomf(mp, "label:taste");
209	gp->start = g_label_start;
210	gp->access = g_label_access;
211	gp->orphan = g_label_orphan;
212	cp = g_new_consumer(gp);
213	g_attach(cp, pp);
214	if (g_access(cp, 1, 0, 0) != 0)
215		goto end;
216	do {
217		if (g_label_read_metadata(cp, &md) != 0)
218			break;
219		if (strcmp(md.md_magic, G_LABEL_MAGIC) != 0)
220			break;
221		if (md.md_version > G_LABEL_VERSION) {
222			printf("geom_label.ko module is too old to handle %s.\n",
223			    pp->name);
224			break;
225		}
226		g_label_create(NULL, mp, pp, md.md_label, G_LABEL_DIR,
227		    pp->mediasize - pp->sectorsize);
228	} while (0);
229	for (i = 0; g_labels[i] != NULL; i++) {
230		char label[64];
231
232		g_topology_unlock();
233		g_labels[i]->ld_taste(cp, label, sizeof(label));
234		g_topology_lock();
235		if (label[0] == '\0')
236			continue;
237		g_label_create(NULL, mp, pp, label, g_labels[i]->ld_dir,
238		    pp->mediasize);
239	}
240	g_access(cp, -1, 0, 0);
241end:
242	g_detach(cp);
243	g_destroy_consumer(cp);
244	g_destroy_geom(gp);
245	return (NULL);
246}
247
248static void
249g_label_ctl_create(struct gctl_req *req, struct g_class *mp)
250{
251	struct g_provider *pp;
252	const char *name;
253	int *nargs;
254
255	g_topology_assert();
256
257	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
258	if (nargs == NULL) {
259		gctl_error(req, "No '%s' argument", "nargs");
260		return;
261	}
262	if (*nargs != 2) {
263		gctl_error(req, "Invalid number of argument.");
264		return;
265	}
266	/*
267	 * arg1 is the name of provider.
268	 */
269	name = gctl_get_asciiparam(req, "arg1");
270	if (name == NULL) {
271		gctl_error(req, "No 'arg%d' argument", 1);
272		return;
273	}
274	if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
275		name += strlen("/dev/");
276	pp = g_provider_by_name(name);
277	if (pp == NULL) {
278		G_LABEL_DEBUG(1, "Provider %s is invalid.", name);
279		gctl_error(req, "Provider %s is invalid.", name);
280		return;
281	}
282	/*
283	 * arg0 is the label.
284	 */
285	name = gctl_get_asciiparam(req, "arg0");
286	if (name == NULL) {
287		gctl_error(req, "No 'arg%d' argument", 0);
288		return;
289	}
290	g_label_create(req, mp, pp, name, G_LABEL_DIR, pp->mediasize);
291}
292
293static const char *
294g_label_skip_dir(const char *name)
295{
296	char path[64];
297	u_int i;
298
299	if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
300		name += strlen("/dev/");
301	if (strncmp(name, G_LABEL_DIR "/", strlen(G_LABEL_DIR "/")) == 0)
302		name += strlen(G_LABEL_DIR "/");
303	for (i = 0; g_labels[i] != NULL; i++) {
304		snprintf(path, sizeof(path), "%s/", g_labels[i]->ld_dir);
305		if (strncmp(name, path, strlen(path)) == 0) {
306			name += strlen(path);
307			break;
308		}
309	}
310	return (name);
311}
312
313static struct g_geom *
314g_label_find_geom(struct g_class *mp, const char *name)
315{
316	struct g_geom *gp;
317	struct g_provider *pp;
318	const char *pname;
319
320	name = g_label_skip_dir(name);
321	LIST_FOREACH(gp, &mp->geom, geom) {
322		pp = LIST_FIRST(&gp->provider);
323		pname = g_label_skip_dir(pp->name);
324		if (strcmp(pname, name) == 0)
325			return (gp);
326	}
327	return (NULL);
328}
329
330static void
331g_label_ctl_destroy(struct gctl_req *req, struct g_class *mp)
332{
333	int *nargs, *force, error, i;
334	struct g_geom *gp;
335	const char *name;
336	char param[16];
337
338	g_topology_assert();
339
340	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
341	if (nargs == NULL) {
342		gctl_error(req, "No '%s' argument", "nargs");
343		return;
344	}
345	if (*nargs <= 0) {
346		gctl_error(req, "Missing device(s).");
347		return;
348	}
349	force = gctl_get_paraml(req, "force", sizeof(*force));
350	if (force == NULL) {
351		gctl_error(req, "No 'force' argument");
352		return;
353	}
354
355	for (i = 0; i < *nargs; i++) {
356		snprintf(param, sizeof(param), "arg%d", i);
357		name = gctl_get_asciiparam(req, param);
358		if (name == NULL) {
359			gctl_error(req, "No 'arg%d' argument", i);
360			return;
361		}
362		gp = g_label_find_geom(mp, name);
363		if (gp == NULL) {
364			G_LABEL_DEBUG(1, "Label %s is invalid.", name);
365			gctl_error(req, "Label %s is invalid.", name);
366			return;
367		}
368		error = g_label_destroy(gp, *force);
369		if (error != 0) {
370			gctl_error(req, "Cannot destroy label %s (error=%d).",
371			    LIST_FIRST(&gp->provider)->name, error);
372			return;
373		}
374	}
375}
376
377static void
378g_label_config(struct gctl_req *req, struct g_class *mp, const char *verb)
379{
380	uint32_t *version;
381
382	g_topology_assert();
383
384	version = gctl_get_paraml(req, "version", sizeof(*version));
385	if (version == NULL) {
386		gctl_error(req, "No '%s' argument.", "version");
387		return;
388	}
389	if (*version != G_LABEL_VERSION) {
390		gctl_error(req, "Userland and kernel parts are out of sync.");
391		return;
392	}
393
394	if (strcmp(verb, "create") == 0) {
395		g_label_ctl_create(req, mp);
396		return;
397	} else if (strcmp(verb, "destroy") == 0 ||
398	    strcmp(verb, "stop") == 0) {
399		g_label_ctl_destroy(req, mp);
400		return;
401	}
402
403	gctl_error(req, "Unknown verb.");
404}
405
406DECLARE_GEOM_CLASS(g_label_class, g_label);
407