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