File: o2cb_scandisk.c

package info (click to toggle)
ocfs2-tools 1.8.4-4+deb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 6,312 kB
  • sloc: ansic: 85,325; sh: 5,702; python: 2,380; makefile: 1,265
file content (206 lines) | stat: -rw-r--r-- 4,454 bytes parent folder | download
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/* -*- mode: c; c-basic-offset: 8; -*-
 * vim: noexpandtab sw=8 ts=8 sts=0:
 *
 * o2cb_scandisk.c
 *
 * Reads all the partitions and get the ocfs2 uuids
 *
 * Copyright (C) 2010 Oracle.  All rights reserved.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public
 * License, version 2,  as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 */

#include "o2cb_scandisk.h"
#include "ocfs2/ocfs2.h"
#include "tools-internal/verbose.h"

struct hb_devices {
	struct list_head hb_list;
	char *hb_path;
};

struct scan_context {
	struct list_head sc_devlist;
	int sc_rescan;
};

static int fill_desc(char *device, struct o2cb_region_desc *reg,
		     struct o2cb_cluster_desc *cluster)
{
	ocfs2_filesys *fs;
	errcode_t ret;

	ret = ocfs2_open(device, OCFS2_FLAG_RO | OCFS2_FLAG_HEARTBEAT_DEV_OK,
			 0, 0, &fs);
	if (ret)
		return ret;

	ret = ocfs2_fill_heartbeat_desc(fs, reg);
	if (!ret)
		ret = ocfs2_fill_cluster_desc(fs, cluster);

	if (!ret) {
		/* TODO free this alloc... or not */
		reg->r_name = strdup(reg->r_name);
		reg->r_device_name = strdup(reg->r_device_name);
	}

	ocfs2_close(fs);

	return ret;
}

static void get_device_uuids(struct scan_context *ctxt, struct list_head *hbdevs)
{
	struct o2cb_device *od;
	struct list_head *pos, *pos1;
	struct hb_devices *hb;
	struct o2cb_region_desc rd;
	struct o2cb_cluster_desc cd;
	int numhbdevs = 0;

	list_for_each(pos, hbdevs) {
		++numhbdevs;
	}

	if (!numhbdevs)
		return;

	list_for_each(pos, &ctxt->sc_devlist) {
		hb = list_entry(pos, struct hb_devices, hb_list);

		if (fill_desc(hb->hb_path, &rd, &cd))
			continue;

		list_for_each(pos1, hbdevs) {
			od = list_entry(pos1, struct o2cb_device, od_list);
			if (od->od_flags & O2CB_DEVICE_FOUND)
				continue;
			if (strcmp(rd.r_name, od->od_uuid))
				continue;
			od->od_flags |= O2CB_DEVICE_FOUND;
			memcpy(&od->od_region, &rd, sizeof(od->od_region));
			memcpy(&od->od_cluster, &cd, sizeof(od->od_cluster));

			verbosef(VL_DEBUG, "Region %s matched to device %s\n",
				 rd.r_name, rd.r_device_name);
			--numhbdevs;
			break;
		}
		if (!numhbdevs)
			break;
	}
}

static void free_scan_context(struct scan_context *ctxt)
{
	struct list_head *pos, *pos1;
	struct hb_devices *hb;

	if (!ctxt)
		return ;

	list_for_each_safe(pos, pos1, &ctxt->sc_devlist) {
		hb = list_entry(pos, struct hb_devices, hb_list);
		list_del(pos);
		free(hb);
	}
}

static void add_to_list(struct list_head *device_list, struct devnode *node)
{
	struct devpath *path;
	struct hb_devices *hb;
	int add = 0;

	path = node->devpath;
	while (path) {
		if (node->mapper)
			add = !strncmp(path->path, "/dev/mapper/", 12);
		else {
			add = !strncmp(path->path, "/dev/sd", 7);
			if (!add)
				add = !strncmp(path->path, "/dev/loop", 9);
			if (!add)
				add = !strncmp(path->path, "/dev/xvd", 8);
			if (!add)
				add = !strncmp(path->path, "/dev/vd", 7);
		}
		if (add) {
			hb = malloc(sizeof(struct hb_devices));
			if (hb) {
				hb->hb_path = strdup(path->path);
				list_add_tail(&hb->hb_list, device_list);
				break;
			}
		}
		path = path->next;
	}
}

static void filter_devices(struct devnode *node, void *user_data)
{
	struct scan_context *ctxt = user_data;

	/* No information in sysfs?  Ignore it! */
	if (!node->sysfsattrs.sysfs)
		return;

	/* Not a disk?  Ignore it! */
	if (!node->sysfsattrs.disk)
		return;

	/* It's part of some other device?  Ignore it! */
	if (node->sysfsattrs.holders)
		return;

	/*
	 * No path in /dev?  Well, udev probably hasn't gotten there. Trigger
	 * a rescan
	 */
	if (!node->devpath) {
		ctxt->sc_rescan = 1;
		return;
	}

	add_to_list(&ctxt->sc_devlist, node);
}

void o2cb_scandisk(struct list_head *hbdevs)
{
	struct devlisthead *dev = NULL;
	int delay = 1;
	struct scan_context scan_ctxt, *ctxt = &scan_ctxt;

	INIT_LIST_HEAD(&ctxt->sc_devlist);

	do {
		ctxt->sc_rescan = 0;
		if (delay > 5)
			break;

		if (dev) {
			free_scan_context(ctxt);
			free_dev_list(dev);
			sleep(delay);
			delay += 2;
		}

		dev = scan_for_dev(NULL, 5, filter_devices, ctxt);
		if (!dev)
			goto bail;
	} while (ctxt->sc_rescan);

	get_device_uuids(ctxt, hbdevs);

bail:
	free_scan_context(ctxt);
	free_dev_list(dev);
}