<commit_before>/*-
 * Copyright (c) 2004-2005 <NAME> <<EMAIL>>
 * All rights reserved.
 *
 * 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 THE AUTHORS 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 THE AUTHORS 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/systm.h>
#include <sys/kernel.h>
#include <sys/module.h>
#include <sys/lock.h>
#include <sys/mutex.h>
#include <sys/bio.h>
#include <sys/sysctl.h>
#include <sys/malloc.h>
#include <geom/geom.h>
#include <geom/geom_slice.h>
#include <geom/label/g_label.h>


SYSCTL_DECL(_kern_geom);
SYSCTL_NODE(_kern_geom, OID_AUTO, label, CTLFLAG_RW, 0, "GEOM_LABEL stuff");
u_int g_label_debug = 0;
TUNABLE_INT("kern.geom.label.debug", &g_label_debug);
SYSCTL_UINT(_kern_geom_label, OID_AUTO, debug, CTLFLAG_RW, &g_label_debug, 0,
    "Debug level");

static int g_label_destroy_geom(struct gctl_req *req, struct g_class *mp,
    struct g_geom *gp);
static int g_label_destroy(struct g_geom *gp, boolean_t force);
static struct g_geom *g_label_taste(struct g_class *mp, struct g_provider *pp,
    int flags __unused);
static void g_label_config(struct gctl_req *req, struct g_class *mp,
    const char *verb);

struct g_class g_label_class = {
	.name = G_LABEL_CLASS_NAME,
	.version = G_VERSION,
	.ctlreq = g_label_config,
	.taste = g_label_taste,
	.destroy_geom = g_label_destroy_geom
};

/*
 * To add a new file system where you want to look for volume labels,
 * you have to:
 * 1. Add a file g_label_<file system>.c which implements labels recognition.
 * 2. Add an 'extern const struct g_label_desc g_label_<file system>;' into
 *    g_label.h file.
 * 3. Add an element to the table below '&g_label_<file system>,'.
 * 4. Add your file to sys/conf/files.
 * 5. Add your file to sys/modules/geom/geom_label/Makefile.
 * 6. Add your file system to manual page sbin/geom/class/label/glabel.8.
 */
const struct g_label_desc *g_labels[] = {
	&g_label_ufs,
	&g_label_iso9660,
	&g_label_msdosfs,
	&g_label_ext2fs,
	&g_label_reiserfs,
	&g_label_ntfs,
	NULL
};


static int
g_label_destroy_geom(struct gctl_req *req __unused, struct g_class *mp,
    struct g_geom *gp __unused)
{

	/*
	 * XXX: Unloading a class which is using geom_slice:1.56 is currently
	 * XXX: broken, so we deny unloading when we have geoms.
	 */
	return (EOPNOTSUPP);
}

static void
g_label_orphan(struct g_consumer *cp)
{

	G_LABEL_DEBUG(0, "Label %s removed.",
	    LIST_FIRST(&cp->geom->provider)->name);
	g_slice_orphan(cp);
}

static void
g_label_spoiled(struct g_consumer *cp)
{

	G_LABEL_DEBUG(0, "Label %s removed.",
	    LIST_FIRST(&cp->geom->provider)->name);
	g_slice_spoiled(cp);
}

static struct g_geom *
g_label_create(struct gctl_req *req, struct g_class *mp, struct g_provider *pp,
    const char *label, const char *dir, off_t mediasize)
{
	struct g_geom *gp;
	struct g_provider *pp2;
	struct g_consumer *cp;
	char name[64];

	g_topology_assert();

	gp = NULL;
	cp = NULL;
	snprintf(name, sizeof(name), "%s/%s", dir, label);
	LIST_FOREACH(gp, &mp->geom, geom) {
		pp2 = LIST_FIRST(&gp->provider);
		if (pp2 == NULL)
			continue;
		if (strcmp(pp2->name, name) == 0) {
			G_LABEL_DEBUG(1, "Label %s(%s) already exists (%s).",
			    label, name, pp->name);
			if (req != NULL) {
				gctl_error(req, "Provider %s already exists.",
				    name);
			}
			return (NULL);
		}
	}<commit_msg>MFC:	sys/geom/label/g_label.c	1.21<commit_after>/*-
 * Copyright (c) 2004-2005 <NAME> <<EMAIL>>
 * All rights reserved.
 *
 * 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 THE AUTHORS 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 THE AUTHORS 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/systm.h>
#include <sys/kernel.h>
#include <sys/module.h>
#include <sys/lock.h>
#include <sys/mutex.h>
#include <sys/bio.h>
#include <sys/sysctl.h>
#include <sys/malloc.h>
#include <sys/libkern.h>
#include <geom/geom.h>
#include <geom/geom_slice.h>
#include <geom/label/g_label.h>


SYSCTL_DECL(_kern_geom);
SYSCTL_NODE(_kern_geom, OID_AUTO, label, CTLFLAG_RW, 0, "GEOM_LABEL stuff");
u_int g_label_debug = 0;
TUNABLE_INT("kern.geom.label.debug", &g_label_debug);
SYSCTL_UINT(_kern_geom_label, OID_AUTO, debug, CTLFLAG_RW, &g_label_debug, 0,
    "Debug level");

static int g_label_destroy_geom(struct gctl_req *req, struct g_class *mp,
    struct g_geom *gp);
static int g_label_destroy(struct g_geom *gp, boolean_t force);
static struct g_geom *g_label_taste(struct g_class *mp, struct g_provider *pp,
    int flags __unused);
static void g_label_config(struct gctl_req *req, struct g_class *mp,
    const char *verb);

struct g_class g_label_class = {
	.name = G_LABEL_CLASS_NAME,
	.version = G_VERSION,
	.ctlreq = g_label_config,
	.taste = g_label_taste,
	.destroy_geom = g_label_destroy_geom
};

/*
 * To add a new file system where you want to look for volume labels,
 * you have to:
 * 1. Add a file g_label_<file system>.c which implements labels recognition.
 * 2. Add an 'extern const struct g_label_desc g_label_<file system>;' into
 *    g_label.h file.
 * 3. Add an element to the table below '&g_label_<file system>,'.
 * 4. Add your file to sys/conf/files.
 * 5. Add your file to sys/modules/geom/geom_label/Makefile.
 * 6. Add your file system to manual page sbin/geom/class/label/glabel.8.
 */
const struct g_label_desc *g_labels[] = {
	&g_label_ufs,
	&g_label_iso9660,
	&g_label_msdosfs,
	&g_label_ext2fs,
	&g_label_reiserfs,
	&g_label_ntfs,
	NULL
};


static int
g_label_destroy_geom(struct gctl_req *req __unused, struct g_class *mp,
    struct g_geom *gp __unused)
{

	/*
	 * XXX: Unloading a class which is using geom_slice:1.56 is currently
	 * XXX: broken, so we deny unloading when we have geoms.
	 */
	return (EOPNOTSUPP);
}

static void
g_label_orphan(struct g_consumer *cp)
{

	G_LABEL_DEBUG(0, "Label %s removed.",
	    LIST_FIRST(&cp->geom->provider)->name);
	g_slice_orphan(cp);
}

static void
g_label_spoiled(struct g_consumer *cp)
{

	G_LABEL_DEBUG(0, "Label %s removed.",
	    LIST_FIRST(&cp->geom->provider)->name);
	g_slice_spoiled(cp);
}

static int
g_label_is_name_ok(const char *label)
{
	const char *s;

	/* Check is the label starts from ../ */
	if (strncmp(label, "../", 3) == 0)
		return (0);
	/* Check is the label contains /../ */
	if (strstr(label, "/../") != NULL)
		return (0);
	/* Check is the label ends at ../ */
	if ((s = strstr(label, "/..")) != NULL && s[3] == '\0')
		return (0);
	return (1);
}

static struct g_geom *
g_label_create(struct gctl_req *req, struct g_class *mp, struct g_provider *pp,
    const char *label, const char *dir, off_t mediasize)
{
	struct g_geom *gp;
	struct g_provider *pp2;
	struct g_consumer *cp;
	char name[64];

	g_topology_assert();

	if (!g_label_is_name_ok(label)) {
		G_LABEL_DEBUG(0, "%s contains suspicious label, skipping.",
		    pp->name);
		G_LABEL_DEBUG(1, "%s suspicious label is: %s", pp->name, label);
		return (NULL);
	}
	gp = NULL;
	cp = NULL;
	snprintf(name, sizeof(name), "%s/%s", dir, label);
	LIST_FOREACH(gp, &mp->geom, geom) {
		pp2 = LIST_FIRST(&gp->provider);
		if (pp2 == NULL)
			continue;
		if (strcmp(pp2->name, name) == 0) {
			G_LABEL_DEBUG(1, "Label %s(%s) already exists (%s).",
			    label, name, pp->name);
			if (req != NULL) {
				gctl_error(req, "Provider %s already exists.",
				    name);
			}
			return (NULL);
		}
	}
{'fraction_non_alphanumeric': 0.11703228476821192, 'fraction_numerical': 0.009209437086092715, 'mean_word_length': 4.238482384823849, 'pattern_counts': {'":': 0, '<': 42, '<?xml version=': 0, '>': 64, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3186709', 'n_tokens_mistral': 3622, 'n_tokens_neox': 3389, 'n_words': 1081}