summaryrefslogtreecommitdiff
path: root/lib/filters/filter-signature.c
blob: de26ef0f29295d338c6a15674103b36a09bfb886 (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
/*
 * Copyright (C) 2004 Luca Berra
 * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
 *
 * This file is part of LVM2.
 *
 * This copyrighted material is made available to anyone wishing to use,
 * modify, copy, or redistribute it subject to the terms and conditions
 * of the GNU Lesser General Public License v.2.1.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include "base/memory/zalloc.h"
#include "lib/misc/lib.h"
#include "lib/filters/filter.h"
#include "lib/commands/toolcontext.h"

#ifdef __linux__

#define BUFSIZE 4096

static int _ignore_signature(struct cmd_context *cmd, struct dev_filter *f __attribute__((unused)),
		      struct device *dev, const char *use_filter_name)
{
	char buf[BUFSIZE];
	int ret = 0;

	if (cmd->filter_nodata_only)
		return 1;

	if (!scan_bcache) {
		/* let pass, call again after scan */
		log_debug_devs("filter signature deferred %s", dev_name(dev));
		dev->flags |= DEV_FILTER_AFTER_SCAN;
		return 1;
	}

	memset(buf, 0, BUFSIZE);

	if (!dev_read_bytes(dev, 0, BUFSIZE, buf)) {
		log_debug_devs("%s: Skipping: error in signature detection",
			       dev_name(dev));
		ret = 0;
		goto out;
	}

	if (dev_is_lvm1(dev, buf, BUFSIZE)) {
		log_debug_devs("%s: Skipping lvm1 device", dev_name(dev));
		ret = 0;
		goto out;
	}

	if (dev_is_pool(dev, buf, BUFSIZE)) {
		log_debug_devs("%s: Skipping gfs-pool device", dev_name(dev));
		ret = 0;
		goto out;
	}
	ret = 1;

out:
	return ret;
}

static void _destroy(struct dev_filter *f)
{
	if (f->use_count)
		log_error(INTERNAL_ERROR "Destroying signature filter while in use %u times.", f->use_count);

	free(f);
}

struct dev_filter *signature_filter_create(struct dev_types *dt)
{
	struct dev_filter *f;

	if (!(f = zalloc(sizeof(*f)))) {
		log_error("md filter allocation failed");
		return NULL;
	}

	f->passes_filter = _ignore_signature;
	f->destroy = _destroy;
	f->use_count = 0;
	f->private = dt;
	f->name = "signature";

	log_debug_devs("signature filter initialised.");

	return f;
}

#else

struct dev_filter *signature_filter_create(struct dev_types *dt)
{
	return NULL;
}

#endif