Deleted Added
full compact
g_label.c (244585) g_label.c (249508)
1/*-
2 * Copyright (c) 2004-2005 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>
1/*-
2 * Copyright (c) 2004-2005 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 244585 2012-12-22 13:43:12Z jh $");
28__FBSDID("$FreeBSD: head/sys/geom/label/g_label.c 249508 2013-04-15 16:09:24Z ivoras $");
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/ctype.h>
38#include <sys/malloc.h>
39#include <sys/libkern.h>
40#include <sys/sbuf.h>
41#include <sys/sysctl.h>
42#include <geom/geom.h>
43#include <geom/geom_slice.h>
44#include <geom/label/g_label.h>
45
46FEATURE(geom_label, "GEOM labeling support");
47
48SYSCTL_DECL(_kern_geom);
49SYSCTL_NODE(_kern_geom, OID_AUTO, label, CTLFLAG_RW, 0, "GEOM_LABEL stuff");
50u_int g_label_debug = 0;
51TUNABLE_INT("kern.geom.label.debug", &g_label_debug);
52SYSCTL_UINT(_kern_geom_label, OID_AUTO, debug, CTLFLAG_RW, &g_label_debug, 0,
53 "Debug level");
54
55static int g_label_destroy_geom(struct gctl_req *req, struct g_class *mp,
56 struct g_geom *gp);
57static int g_label_destroy(struct g_geom *gp, boolean_t force);
58static struct g_geom *g_label_taste(struct g_class *mp, struct g_provider *pp,
59 int flags __unused);
60static void g_label_config(struct gctl_req *req, struct g_class *mp,
61 const char *verb);
62
63struct g_class g_label_class = {
64 .name = G_LABEL_CLASS_NAME,
65 .version = G_VERSION,
66 .ctlreq = g_label_config,
67 .taste = g_label_taste,
68 .destroy_geom = g_label_destroy_geom
69};
70
71/*
72 * To add a new file system where you want to look for volume labels,
73 * you have to:
74 * 1. Add a file g_label_<file system>.c which implements labels recognition.
75 * 2. Add an 'extern const struct g_label_desc g_label_<file system>;' into
76 * g_label.h file.
77 * 3. Add an element to the table below '&g_label_<file system>,'.
78 * 4. Add your file to sys/conf/files.
79 * 5. Add your file to sys/modules/geom/geom_label/Makefile.
80 * 6. Add your file system to manual page sbin/geom/class/label/glabel.8.
81 */
82const struct g_label_desc *g_labels[] = {
83 &g_label_ufs_id,
84 &g_label_ufs_volume,
85 &g_label_iso9660,
86 &g_label_msdosfs,
87 &g_label_ext2fs,
88 &g_label_reiserfs,
89 &g_label_ntfs,
90 &g_label_gpt,
91 &g_label_gpt_uuid,
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/ctype.h>
38#include <sys/malloc.h>
39#include <sys/libkern.h>
40#include <sys/sbuf.h>
41#include <sys/sysctl.h>
42#include <geom/geom.h>
43#include <geom/geom_slice.h>
44#include <geom/label/g_label.h>
45
46FEATURE(geom_label, "GEOM labeling support");
47
48SYSCTL_DECL(_kern_geom);
49SYSCTL_NODE(_kern_geom, OID_AUTO, label, CTLFLAG_RW, 0, "GEOM_LABEL stuff");
50u_int g_label_debug = 0;
51TUNABLE_INT("kern.geom.label.debug", &g_label_debug);
52SYSCTL_UINT(_kern_geom_label, OID_AUTO, debug, CTLFLAG_RW, &g_label_debug, 0,
53 "Debug level");
54
55static int g_label_destroy_geom(struct gctl_req *req, struct g_class *mp,
56 struct g_geom *gp);
57static int g_label_destroy(struct g_geom *gp, boolean_t force);
58static struct g_geom *g_label_taste(struct g_class *mp, struct g_provider *pp,
59 int flags __unused);
60static void g_label_config(struct gctl_req *req, struct g_class *mp,
61 const char *verb);
62
63struct g_class g_label_class = {
64 .name = G_LABEL_CLASS_NAME,
65 .version = G_VERSION,
66 .ctlreq = g_label_config,
67 .taste = g_label_taste,
68 .destroy_geom = g_label_destroy_geom
69};
70
71/*
72 * To add a new file system where you want to look for volume labels,
73 * you have to:
74 * 1. Add a file g_label_<file system>.c which implements labels recognition.
75 * 2. Add an 'extern const struct g_label_desc g_label_<file system>;' into
76 * g_label.h file.
77 * 3. Add an element to the table below '&g_label_<file system>,'.
78 * 4. Add your file to sys/conf/files.
79 * 5. Add your file to sys/modules/geom/geom_label/Makefile.
80 * 6. Add your file system to manual page sbin/geom/class/label/glabel.8.
81 */
82const struct g_label_desc *g_labels[] = {
83 &g_label_ufs_id,
84 &g_label_ufs_volume,
85 &g_label_iso9660,
86 &g_label_msdosfs,
87 &g_label_ext2fs,
88 &g_label_reiserfs,
89 &g_label_ntfs,
90 &g_label_gpt,
91 &g_label_gpt_uuid,
92 &g_label_disk_ident,
92 NULL
93};
94
95
96static int
97g_label_destroy_geom(struct gctl_req *req __unused, struct g_class *mp,
98 struct g_geom *gp __unused)
99{
100
101 /*
102 * XXX: Unloading a class which is using geom_slice:1.56 is currently
103 * XXX: broken, so we deny unloading when we have geoms.
104 */
105 return (EOPNOTSUPP);
106}
107
108static void
109g_label_orphan(struct g_consumer *cp)
110{
111
112 G_LABEL_DEBUG(1, "Label %s removed.",
113 LIST_FIRST(&cp->geom->provider)->name);
114 g_slice_orphan(cp);
115}
116
117static void
118g_label_spoiled(struct g_consumer *cp)
119{
120
121 G_LABEL_DEBUG(1, "Label %s removed.",
122 LIST_FIRST(&cp->geom->provider)->name);
123 g_slice_spoiled(cp);
124}
125
126static int
127g_label_is_name_ok(const char *label)
128{
129 const char *s;
130
131 /* Check if the label starts from ../ */
132 if (strncmp(label, "../", 3) == 0)
133 return (0);
134 /* Check if the label contains /../ */
135 if (strstr(label, "/../") != NULL)
136 return (0);
137 /* Check if the label ends at ../ */
138 if ((s = strstr(label, "/..")) != NULL && s[3] == '\0')
139 return (0);
140 return (1);
141}
142
143static void
144g_label_mangle_name(char *label, size_t size)
145{
146 struct sbuf *sb;
147 const u_char *c;
148
149 sb = sbuf_new(NULL, NULL, size, SBUF_FIXEDLEN);
150 for (c = label; *c != '\0'; c++) {
151 if (!isprint(*c) || isspace(*c) || *c =='"' || *c == '%')
152 sbuf_printf(sb, "%%%02X", *c);
153 else
154 sbuf_putc(sb, *c);
155 }
156 if (sbuf_finish(sb) != 0)
157 label[0] = '\0';
158 else
159 strlcpy(label, sbuf_data(sb), size);
160 sbuf_delete(sb);
161}
162
163static struct g_geom *
164g_label_create(struct gctl_req *req, struct g_class *mp, struct g_provider *pp,
165 const char *label, const char *dir, off_t mediasize)
166{
167 struct g_geom *gp;
168 struct g_provider *pp2;
169 struct g_consumer *cp;
170 char name[64];
171
172 g_topology_assert();
173
174 if (!g_label_is_name_ok(label)) {
175 G_LABEL_DEBUG(0, "%s contains suspicious label, skipping.",
176 pp->name);
177 G_LABEL_DEBUG(1, "%s suspicious label is: %s", pp->name, label);
178 if (req != NULL)
179 gctl_error(req, "Label name %s is invalid.", label);
180 return (NULL);
181 }
182 gp = NULL;
183 cp = NULL;
184 snprintf(name, sizeof(name), "%s/%s", dir, label);
185 LIST_FOREACH(gp, &mp->geom, geom) {
186 pp2 = LIST_FIRST(&gp->provider);
187 if (pp2 == NULL)
188 continue;
189 if ((pp2->flags & G_PF_ORPHAN) != 0)
190 continue;
191 if (strcmp(pp2->name, name) == 0) {
192 G_LABEL_DEBUG(1, "Label %s(%s) already exists (%s).",
193 label, name, pp->name);
194 if (req != NULL) {
195 gctl_error(req, "Provider %s already exists.",
196 name);
197 }
198 return (NULL);
199 }
200 }
201 gp = g_slice_new(mp, 1, pp, &cp, NULL, 0, NULL);
202 if (gp == NULL) {
203 G_LABEL_DEBUG(0, "Cannot create slice %s.", label);
204 if (req != NULL)
205 gctl_error(req, "Cannot create slice %s.", label);
206 return (NULL);
207 }
208 gp->orphan = g_label_orphan;
209 gp->spoiled = g_label_spoiled;
210 g_access(cp, -1, 0, 0);
211 g_slice_config(gp, 0, G_SLICE_CONFIG_SET, (off_t)0, mediasize,
212 pp->sectorsize, "%s", name);
213 G_LABEL_DEBUG(1, "Label for provider %s is %s.", pp->name, name);
214 return (gp);
215}
216
217static int
218g_label_destroy(struct g_geom *gp, boolean_t force)
219{
220 struct g_provider *pp;
221
222 g_topology_assert();
223 pp = LIST_FIRST(&gp->provider);
224 if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) {
225 if (force) {
226 G_LABEL_DEBUG(0, "Provider %s is still open, so it "
227 "can't be definitely removed.", pp->name);
228 } else {
229 G_LABEL_DEBUG(1,
230 "Provider %s is still open (r%dw%de%d).", pp->name,
231 pp->acr, pp->acw, pp->ace);
232 return (EBUSY);
233 }
234 } else if (pp != NULL)
235 G_LABEL_DEBUG(1, "Label %s removed.", pp->name);
236 g_slice_spoiled(LIST_FIRST(&gp->consumer));
237 return (0);
238}
239
240static int
241g_label_read_metadata(struct g_consumer *cp, struct g_label_metadata *md)
242{
243 struct g_provider *pp;
244 u_char *buf;
245 int error;
246
247 g_topology_assert();
248
249 pp = cp->provider;
250 g_topology_unlock();
251 buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize,
252 &error);
253 g_topology_lock();
254 if (buf == NULL)
255 return (error);
256 /* Decode metadata. */
257 label_metadata_decode(buf, md);
258 g_free(buf);
259
260 return (0);
261}
262
263static void
264g_label_orphan_taste(struct g_consumer *cp __unused)
265{
266
267 KASSERT(1 == 0, ("%s called?", __func__));
268}
269
270static void
271g_label_start_taste(struct bio *bp __unused)
272{
273
274 KASSERT(1 == 0, ("%s called?", __func__));
275}
276
277static int
278g_label_access_taste(struct g_provider *pp __unused, int dr __unused,
279 int dw __unused, int de __unused)
280{
281
282 KASSERT(1 == 0, ("%s called", __func__));
283 return (EOPNOTSUPP);
284}
285
286static struct g_geom *
287g_label_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
288{
289 struct g_label_metadata md;
290 struct g_consumer *cp;
291 struct g_geom *gp;
292 int i;
293
294 g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name);
295 g_topology_assert();
296
297 G_LABEL_DEBUG(2, "Tasting %s.", pp->name);
298
299 /* Skip providers that are already open for writing. */
300 if (pp->acw > 0)
301 return (NULL);
302
303 if (strcmp(pp->geom->class->name, mp->name) == 0)
304 return (NULL);
305
306 gp = g_new_geomf(mp, "label:taste");
307 gp->start = g_label_start_taste;
308 gp->access = g_label_access_taste;
309 gp->orphan = g_label_orphan_taste;
310 cp = g_new_consumer(gp);
311 g_attach(cp, pp);
312 if (g_access(cp, 1, 0, 0) != 0)
313 goto end;
314 do {
315 if (g_label_read_metadata(cp, &md) != 0)
316 break;
317 if (strcmp(md.md_magic, G_LABEL_MAGIC) != 0)
318 break;
319 if (md.md_version > G_LABEL_VERSION) {
320 printf("geom_label.ko module is too old to handle %s.\n",
321 pp->name);
322 break;
323 }
324
325 /*
326 * Backward compatibility:
327 */
328 /*
329 * There was no md_provsize field in earlier versions of
330 * metadata.
331 */
332 if (md.md_version < 2)
333 md.md_provsize = pp->mediasize;
334
335 if (md.md_provsize != pp->mediasize)
336 break;
337
338 g_label_create(NULL, mp, pp, md.md_label, G_LABEL_DIR,
339 pp->mediasize - pp->sectorsize);
340 } while (0);
341 for (i = 0; g_labels[i] != NULL; i++) {
93 NULL
94};
95
96
97static int
98g_label_destroy_geom(struct gctl_req *req __unused, struct g_class *mp,
99 struct g_geom *gp __unused)
100{
101
102 /*
103 * XXX: Unloading a class which is using geom_slice:1.56 is currently
104 * XXX: broken, so we deny unloading when we have geoms.
105 */
106 return (EOPNOTSUPP);
107}
108
109static void
110g_label_orphan(struct g_consumer *cp)
111{
112
113 G_LABEL_DEBUG(1, "Label %s removed.",
114 LIST_FIRST(&cp->geom->provider)->name);
115 g_slice_orphan(cp);
116}
117
118static void
119g_label_spoiled(struct g_consumer *cp)
120{
121
122 G_LABEL_DEBUG(1, "Label %s removed.",
123 LIST_FIRST(&cp->geom->provider)->name);
124 g_slice_spoiled(cp);
125}
126
127static int
128g_label_is_name_ok(const char *label)
129{
130 const char *s;
131
132 /* Check if the label starts from ../ */
133 if (strncmp(label, "../", 3) == 0)
134 return (0);
135 /* Check if the label contains /../ */
136 if (strstr(label, "/../") != NULL)
137 return (0);
138 /* Check if the label ends at ../ */
139 if ((s = strstr(label, "/..")) != NULL && s[3] == '\0')
140 return (0);
141 return (1);
142}
143
144static void
145g_label_mangle_name(char *label, size_t size)
146{
147 struct sbuf *sb;
148 const u_char *c;
149
150 sb = sbuf_new(NULL, NULL, size, SBUF_FIXEDLEN);
151 for (c = label; *c != '\0'; c++) {
152 if (!isprint(*c) || isspace(*c) || *c =='"' || *c == '%')
153 sbuf_printf(sb, "%%%02X", *c);
154 else
155 sbuf_putc(sb, *c);
156 }
157 if (sbuf_finish(sb) != 0)
158 label[0] = '\0';
159 else
160 strlcpy(label, sbuf_data(sb), size);
161 sbuf_delete(sb);
162}
163
164static struct g_geom *
165g_label_create(struct gctl_req *req, struct g_class *mp, struct g_provider *pp,
166 const char *label, const char *dir, off_t mediasize)
167{
168 struct g_geom *gp;
169 struct g_provider *pp2;
170 struct g_consumer *cp;
171 char name[64];
172
173 g_topology_assert();
174
175 if (!g_label_is_name_ok(label)) {
176 G_LABEL_DEBUG(0, "%s contains suspicious label, skipping.",
177 pp->name);
178 G_LABEL_DEBUG(1, "%s suspicious label is: %s", pp->name, label);
179 if (req != NULL)
180 gctl_error(req, "Label name %s is invalid.", label);
181 return (NULL);
182 }
183 gp = NULL;
184 cp = NULL;
185 snprintf(name, sizeof(name), "%s/%s", dir, label);
186 LIST_FOREACH(gp, &mp->geom, geom) {
187 pp2 = LIST_FIRST(&gp->provider);
188 if (pp2 == NULL)
189 continue;
190 if ((pp2->flags & G_PF_ORPHAN) != 0)
191 continue;
192 if (strcmp(pp2->name, name) == 0) {
193 G_LABEL_DEBUG(1, "Label %s(%s) already exists (%s).",
194 label, name, pp->name);
195 if (req != NULL) {
196 gctl_error(req, "Provider %s already exists.",
197 name);
198 }
199 return (NULL);
200 }
201 }
202 gp = g_slice_new(mp, 1, pp, &cp, NULL, 0, NULL);
203 if (gp == NULL) {
204 G_LABEL_DEBUG(0, "Cannot create slice %s.", label);
205 if (req != NULL)
206 gctl_error(req, "Cannot create slice %s.", label);
207 return (NULL);
208 }
209 gp->orphan = g_label_orphan;
210 gp->spoiled = g_label_spoiled;
211 g_access(cp, -1, 0, 0);
212 g_slice_config(gp, 0, G_SLICE_CONFIG_SET, (off_t)0, mediasize,
213 pp->sectorsize, "%s", name);
214 G_LABEL_DEBUG(1, "Label for provider %s is %s.", pp->name, name);
215 return (gp);
216}
217
218static int
219g_label_destroy(struct g_geom *gp, boolean_t force)
220{
221 struct g_provider *pp;
222
223 g_topology_assert();
224 pp = LIST_FIRST(&gp->provider);
225 if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) {
226 if (force) {
227 G_LABEL_DEBUG(0, "Provider %s is still open, so it "
228 "can't be definitely removed.", pp->name);
229 } else {
230 G_LABEL_DEBUG(1,
231 "Provider %s is still open (r%dw%de%d).", pp->name,
232 pp->acr, pp->acw, pp->ace);
233 return (EBUSY);
234 }
235 } else if (pp != NULL)
236 G_LABEL_DEBUG(1, "Label %s removed.", pp->name);
237 g_slice_spoiled(LIST_FIRST(&gp->consumer));
238 return (0);
239}
240
241static int
242g_label_read_metadata(struct g_consumer *cp, struct g_label_metadata *md)
243{
244 struct g_provider *pp;
245 u_char *buf;
246 int error;
247
248 g_topology_assert();
249
250 pp = cp->provider;
251 g_topology_unlock();
252 buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize,
253 &error);
254 g_topology_lock();
255 if (buf == NULL)
256 return (error);
257 /* Decode metadata. */
258 label_metadata_decode(buf, md);
259 g_free(buf);
260
261 return (0);
262}
263
264static void
265g_label_orphan_taste(struct g_consumer *cp __unused)
266{
267
268 KASSERT(1 == 0, ("%s called?", __func__));
269}
270
271static void
272g_label_start_taste(struct bio *bp __unused)
273{
274
275 KASSERT(1 == 0, ("%s called?", __func__));
276}
277
278static int
279g_label_access_taste(struct g_provider *pp __unused, int dr __unused,
280 int dw __unused, int de __unused)
281{
282
283 KASSERT(1 == 0, ("%s called", __func__));
284 return (EOPNOTSUPP);
285}
286
287static struct g_geom *
288g_label_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
289{
290 struct g_label_metadata md;
291 struct g_consumer *cp;
292 struct g_geom *gp;
293 int i;
294
295 g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name);
296 g_topology_assert();
297
298 G_LABEL_DEBUG(2, "Tasting %s.", pp->name);
299
300 /* Skip providers that are already open for writing. */
301 if (pp->acw > 0)
302 return (NULL);
303
304 if (strcmp(pp->geom->class->name, mp->name) == 0)
305 return (NULL);
306
307 gp = g_new_geomf(mp, "label:taste");
308 gp->start = g_label_start_taste;
309 gp->access = g_label_access_taste;
310 gp->orphan = g_label_orphan_taste;
311 cp = g_new_consumer(gp);
312 g_attach(cp, pp);
313 if (g_access(cp, 1, 0, 0) != 0)
314 goto end;
315 do {
316 if (g_label_read_metadata(cp, &md) != 0)
317 break;
318 if (strcmp(md.md_magic, G_LABEL_MAGIC) != 0)
319 break;
320 if (md.md_version > G_LABEL_VERSION) {
321 printf("geom_label.ko module is too old to handle %s.\n",
322 pp->name);
323 break;
324 }
325
326 /*
327 * Backward compatibility:
328 */
329 /*
330 * There was no md_provsize field in earlier versions of
331 * metadata.
332 */
333 if (md.md_version < 2)
334 md.md_provsize = pp->mediasize;
335
336 if (md.md_provsize != pp->mediasize)
337 break;
338
339 g_label_create(NULL, mp, pp, md.md_label, G_LABEL_DIR,
340 pp->mediasize - pp->sectorsize);
341 } while (0);
342 for (i = 0; g_labels[i] != NULL; i++) {
342 char label[64];
343 char label[128];
343
344 if (g_labels[i]->ld_enabled == 0)
345 continue;
346 g_topology_unlock();
347 g_labels[i]->ld_taste(cp, label, sizeof(label));
348 g_label_mangle_name(label, sizeof(label));
349 g_topology_lock();
350 if (label[0] == '\0')
351 continue;
352 g_label_create(NULL, mp, pp, label, g_labels[i]->ld_dir,
353 pp->mediasize);
354 }
355 g_access(cp, -1, 0, 0);
356end:
357 g_detach(cp);
358 g_destroy_consumer(cp);
359 g_destroy_geom(gp);
360 return (NULL);
361}
362
363static void
364g_label_ctl_create(struct gctl_req *req, struct g_class *mp)
365{
366 struct g_provider *pp;
367 const char *name;
368 int *nargs;
369
370 g_topology_assert();
371
372 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
373 if (nargs == NULL) {
374 gctl_error(req, "No '%s' argument", "nargs");
375 return;
376 }
377 if (*nargs != 2) {
378 gctl_error(req, "Invalid number of arguments.");
379 return;
380 }
381 /*
382 * arg1 is the name of provider.
383 */
384 name = gctl_get_asciiparam(req, "arg1");
385 if (name == NULL) {
386 gctl_error(req, "No 'arg%d' argument", 1);
387 return;
388 }
389 if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
390 name += strlen("/dev/");
391 pp = g_provider_by_name(name);
392 if (pp == NULL) {
393 G_LABEL_DEBUG(1, "Provider %s is invalid.", name);
394 gctl_error(req, "Provider %s is invalid.", name);
395 return;
396 }
397 /*
398 * arg0 is the label.
399 */
400 name = gctl_get_asciiparam(req, "arg0");
401 if (name == NULL) {
402 gctl_error(req, "No 'arg%d' argument", 0);
403 return;
404 }
405 g_label_create(req, mp, pp, name, G_LABEL_DIR, pp->mediasize);
406}
407
408static const char *
409g_label_skip_dir(const char *name)
410{
411 char path[64];
412 u_int i;
413
414 if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
415 name += strlen("/dev/");
416 if (strncmp(name, G_LABEL_DIR "/", strlen(G_LABEL_DIR "/")) == 0)
417 name += strlen(G_LABEL_DIR "/");
418 for (i = 0; g_labels[i] != NULL; i++) {
419 snprintf(path, sizeof(path), "%s/", g_labels[i]->ld_dir);
420 if (strncmp(name, path, strlen(path)) == 0) {
421 name += strlen(path);
422 break;
423 }
424 }
425 return (name);
426}
427
428static struct g_geom *
429g_label_find_geom(struct g_class *mp, const char *name)
430{
431 struct g_geom *gp;
432 struct g_provider *pp;
433 const char *pname;
434
435 name = g_label_skip_dir(name);
436 LIST_FOREACH(gp, &mp->geom, geom) {
437 pp = LIST_FIRST(&gp->provider);
438 pname = g_label_skip_dir(pp->name);
439 if (strcmp(pname, name) == 0)
440 return (gp);
441 }
442 return (NULL);
443}
444
445static void
446g_label_ctl_destroy(struct gctl_req *req, struct g_class *mp)
447{
448 int *nargs, *force, error, i;
449 struct g_geom *gp;
450 const char *name;
451 char param[16];
452
453 g_topology_assert();
454
455 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
456 if (nargs == NULL) {
457 gctl_error(req, "No '%s' argument", "nargs");
458 return;
459 }
460 if (*nargs <= 0) {
461 gctl_error(req, "Missing device(s).");
462 return;
463 }
464 force = gctl_get_paraml(req, "force", sizeof(*force));
465 if (force == NULL) {
466 gctl_error(req, "No 'force' argument");
467 return;
468 }
469
470 for (i = 0; i < *nargs; i++) {
471 snprintf(param, sizeof(param), "arg%d", i);
472 name = gctl_get_asciiparam(req, param);
473 if (name == NULL) {
474 gctl_error(req, "No 'arg%d' argument", i);
475 return;
476 }
477 gp = g_label_find_geom(mp, name);
478 if (gp == NULL) {
479 G_LABEL_DEBUG(1, "Label %s is invalid.", name);
480 gctl_error(req, "Label %s is invalid.", name);
481 return;
482 }
483 error = g_label_destroy(gp, *force);
484 if (error != 0) {
485 gctl_error(req, "Cannot destroy label %s (error=%d).",
486 LIST_FIRST(&gp->provider)->name, error);
487 return;
488 }
489 }
490}
491
492static void
493g_label_config(struct gctl_req *req, struct g_class *mp, const char *verb)
494{
495 uint32_t *version;
496
497 g_topology_assert();
498
499 version = gctl_get_paraml(req, "version", sizeof(*version));
500 if (version == NULL) {
501 gctl_error(req, "No '%s' argument.", "version");
502 return;
503 }
504 if (*version != G_LABEL_VERSION) {
505 gctl_error(req, "Userland and kernel parts are out of sync.");
506 return;
507 }
508
509 if (strcmp(verb, "create") == 0) {
510 g_label_ctl_create(req, mp);
511 return;
512 } else if (strcmp(verb, "destroy") == 0 ||
513 strcmp(verb, "stop") == 0) {
514 g_label_ctl_destroy(req, mp);
515 return;
516 }
517
518 gctl_error(req, "Unknown verb.");
519}
520
521DECLARE_GEOM_CLASS(g_label_class, g_label);
344
345 if (g_labels[i]->ld_enabled == 0)
346 continue;
347 g_topology_unlock();
348 g_labels[i]->ld_taste(cp, label, sizeof(label));
349 g_label_mangle_name(label, sizeof(label));
350 g_topology_lock();
351 if (label[0] == '\0')
352 continue;
353 g_label_create(NULL, mp, pp, label, g_labels[i]->ld_dir,
354 pp->mediasize);
355 }
356 g_access(cp, -1, 0, 0);
357end:
358 g_detach(cp);
359 g_destroy_consumer(cp);
360 g_destroy_geom(gp);
361 return (NULL);
362}
363
364static void
365g_label_ctl_create(struct gctl_req *req, struct g_class *mp)
366{
367 struct g_provider *pp;
368 const char *name;
369 int *nargs;
370
371 g_topology_assert();
372
373 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
374 if (nargs == NULL) {
375 gctl_error(req, "No '%s' argument", "nargs");
376 return;
377 }
378 if (*nargs != 2) {
379 gctl_error(req, "Invalid number of arguments.");
380 return;
381 }
382 /*
383 * arg1 is the name of provider.
384 */
385 name = gctl_get_asciiparam(req, "arg1");
386 if (name == NULL) {
387 gctl_error(req, "No 'arg%d' argument", 1);
388 return;
389 }
390 if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
391 name += strlen("/dev/");
392 pp = g_provider_by_name(name);
393 if (pp == NULL) {
394 G_LABEL_DEBUG(1, "Provider %s is invalid.", name);
395 gctl_error(req, "Provider %s is invalid.", name);
396 return;
397 }
398 /*
399 * arg0 is the label.
400 */
401 name = gctl_get_asciiparam(req, "arg0");
402 if (name == NULL) {
403 gctl_error(req, "No 'arg%d' argument", 0);
404 return;
405 }
406 g_label_create(req, mp, pp, name, G_LABEL_DIR, pp->mediasize);
407}
408
409static const char *
410g_label_skip_dir(const char *name)
411{
412 char path[64];
413 u_int i;
414
415 if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
416 name += strlen("/dev/");
417 if (strncmp(name, G_LABEL_DIR "/", strlen(G_LABEL_DIR "/")) == 0)
418 name += strlen(G_LABEL_DIR "/");
419 for (i = 0; g_labels[i] != NULL; i++) {
420 snprintf(path, sizeof(path), "%s/", g_labels[i]->ld_dir);
421 if (strncmp(name, path, strlen(path)) == 0) {
422 name += strlen(path);
423 break;
424 }
425 }
426 return (name);
427}
428
429static struct g_geom *
430g_label_find_geom(struct g_class *mp, const char *name)
431{
432 struct g_geom *gp;
433 struct g_provider *pp;
434 const char *pname;
435
436 name = g_label_skip_dir(name);
437 LIST_FOREACH(gp, &mp->geom, geom) {
438 pp = LIST_FIRST(&gp->provider);
439 pname = g_label_skip_dir(pp->name);
440 if (strcmp(pname, name) == 0)
441 return (gp);
442 }
443 return (NULL);
444}
445
446static void
447g_label_ctl_destroy(struct gctl_req *req, struct g_class *mp)
448{
449 int *nargs, *force, error, i;
450 struct g_geom *gp;
451 const char *name;
452 char param[16];
453
454 g_topology_assert();
455
456 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
457 if (nargs == NULL) {
458 gctl_error(req, "No '%s' argument", "nargs");
459 return;
460 }
461 if (*nargs <= 0) {
462 gctl_error(req, "Missing device(s).");
463 return;
464 }
465 force = gctl_get_paraml(req, "force", sizeof(*force));
466 if (force == NULL) {
467 gctl_error(req, "No 'force' argument");
468 return;
469 }
470
471 for (i = 0; i < *nargs; i++) {
472 snprintf(param, sizeof(param), "arg%d", i);
473 name = gctl_get_asciiparam(req, param);
474 if (name == NULL) {
475 gctl_error(req, "No 'arg%d' argument", i);
476 return;
477 }
478 gp = g_label_find_geom(mp, name);
479 if (gp == NULL) {
480 G_LABEL_DEBUG(1, "Label %s is invalid.", name);
481 gctl_error(req, "Label %s is invalid.", name);
482 return;
483 }
484 error = g_label_destroy(gp, *force);
485 if (error != 0) {
486 gctl_error(req, "Cannot destroy label %s (error=%d).",
487 LIST_FIRST(&gp->provider)->name, error);
488 return;
489 }
490 }
491}
492
493static void
494g_label_config(struct gctl_req *req, struct g_class *mp, const char *verb)
495{
496 uint32_t *version;
497
498 g_topology_assert();
499
500 version = gctl_get_paraml(req, "version", sizeof(*version));
501 if (version == NULL) {
502 gctl_error(req, "No '%s' argument.", "version");
503 return;
504 }
505 if (*version != G_LABEL_VERSION) {
506 gctl_error(req, "Userland and kernel parts are out of sync.");
507 return;
508 }
509
510 if (strcmp(verb, "create") == 0) {
511 g_label_ctl_create(req, mp);
512 return;
513 } else if (strcmp(verb, "destroy") == 0 ||
514 strcmp(verb, "stop") == 0) {
515 g_label_ctl_destroy(req, mp);
516 return;
517 }
518
519 gctl_error(req, "Unknown verb.");
520}
521
522DECLARE_GEOM_CLASS(g_label_class, g_label);