aboutsummaryrefslogtreecommitdiffstats
path: root/bcache.c
blob: f99d2dcf5efeb774b194f3fe7d7d8415ce4013e4 (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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
// SPDX-License-Identifier: GPL-2.0
// Author: Shaoxiong Li <dahefanteng@gmail.com>

#include <stdio.h>
#include <inttypes.h>
#include <string.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <getopt.h>
#include <regex.h>
#include "bcache.h"
#include "lib.h"
#include "make.h"
#include <locale.h>
#include "list.h"
#include <limits.h>
#include <assert.h>

#include "features.h"
#include "show.h"

#define BCACHE_TOOLS_VERSION	"1.1"

bool bad_uuid(char *uuid)
{
	const char *pattern =
	    "^[a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12}$";
	regex_t reg;
	int status;
	regmatch_t regmatche;

	if (regcomp(&reg, pattern, REG_EXTENDED) != 0)
		fprintf(stderr, "Error happen when check uuid format:%m\n");
	status = regexec(&reg, uuid, 1, &regmatche, 0);
	regfree(&reg);
	if (status == REG_NOMATCH)
		return true;
	else
		return false;
}

bool bad_dev(char **devname)
{

	char *ptr = realpath(*devname, NULL);

	if (ptr == NULL) {
		fprintf(stderr, "Error:Failed to resolve device name\n");
		return true;
	}
	*devname = ptr;
	char *pattern = "^/dev/[a-zA-Z0-9-]*$";
	regex_t reg;
	int status;
	regmatch_t regmatche;

	if (regcomp(&reg, pattern, REG_EXTENDED) != 0) {
		fprintf(stderr,
			"Error happen when check device name format:%m\n");
	}
	status = regexec(&reg, *devname, 1, &regmatche, 0);
	regfree(&reg);
	if (status == REG_NOMATCH)
		return true;
	else
		return false;
}


int main_usage(void)
{
	fprintf(stderr,
		"Usage:bcache [SUBCMD]\n"
		"	show		show all bcache devices in this host\n"
		"	tree		show active bcache devices in this host\n"
		"	make		make regular device to bcache device\n"
		"	register	register device to kernel\n"
		"	unregister	unregister device from kernel\n"
		"	attach		attach backend device(data device) to cache device\n"
		"	detach		detach backend device(data device) from cache device\n"
		"	set-cachemode	set cachemode for backend device\n"
		"	set-label	set label for backend device\n");
	return EXIT_FAILURE;
}

int show_usage(void)
{
	fprintf(stderr,
		"Usage:	show [option]\n"
		"	show overall information about all devices\n"
		"	-d	--device {devname}	show the detail infomation about this device\n"
		"	-m	--more			show overall information about all devices with detail info\n"
		"	-h	--help			show help information\n");
	return EXIT_FAILURE;
}

int tree_usage(void)
{
	fprintf(stderr,
		"Usage: tree	show active bcache devices in this host\n");
	return EXIT_FAILURE;
}

int register_usage(void)
{
	fprintf(stderr,
		"Usage:register devicename		register device as bcache device to kernel\n");
	return EXIT_FAILURE;
}

int unregister_usage(void)
{
	fprintf(stderr,
		"Usage:unregister devicename		unregister device from kernel\n");
	return EXIT_FAILURE;
}

int attach_usage(void)
{
	fprintf(stderr, "Usage:attach cset_uuid|cachedevice datadevice\n");
	return EXIT_FAILURE;
}

int detach_usage(void)
{
	fprintf(stderr, "Usage:detach devicename\n");
	return EXIT_FAILURE;
}

int setcachemode_usage(void)
{
	fprintf(stderr, "Usage:set-cachemode devicename modetype\n");
	return EXIT_FAILURE;
}

int setlabel_usage(void)
{
	fprintf(stderr,
		"Usage:set-label devicename label\n(only for backend device)\n");
	return EXIT_FAILURE;
}

int version_usagee(void)
{
	fprintf(stderr,
		"Usage: version		display software version\n");
	return EXIT_FAILURE;
}

void replace_line(char **dest, const char *from, const char *to)
{
	assert(strlen(from) == strlen(to));
	char sub[4096] = "";
	char new[4096] = "";

	strcpy(sub, *dest);
	while (1) {
		char *tmp = strpbrk(sub, from);

		if (tmp != NULL) {
			strcpy(new, tmp);
			strcpy(sub, tmp + strlen(from));
		} else
			break;
	}
	if (strlen(new) > 0) {
		strncpy(new, to, strlen(to));
		sprintf(*dest + strlen(*dest) - strlen(new), new, strlen(new));
	}
}

int tree(void)
{
	char *out;
	const char *begin = ".\n";
	const char *middle = "├─";
	const char *tail = "└─";
	struct list_head head;
	struct dev *devs, *tmp, *n, *m;

	INIT_LIST_HEAD(&head);
	int ret;

	out = (char *)malloc(4096);
	if (out == NULL) {
		fprintf(stderr, "Error: fail to allocate memory buffer\n");
		return 1;
	}

	ret = list_bdevs(&head);
	if (ret != 0) {
		free(out);
		fprintf(stderr, "Failed to list devices\n");
		return ret;
	}
	sprintf(out, "%s", begin);
	list_for_each_entry_safe(devs, n, &head, dev_list) {
		if ((devs->version == BCACHE_SB_VERSION_CDEV
		     || devs->version == BCACHE_SB_VERSION_CDEV_WITH_UUID
		     || devs->version == BCACHE_SB_VERSION_CDEV_WITH_FEATURES)
		    && strcmp(devs->state, BCACHE_BASIC_STATE_ACTIVE) == 0) {
			sprintf(out + strlen(out), "%s\n", devs->name);
			list_for_each_entry_safe(tmp, m, &head, dev_list) {
				if (strcmp(devs->cset, tmp->attachuuid) ==
				    0) {
					replace_line(&out, tail, middle);
					sprintf(out + strlen(out), "%s%s %s\n",
						tail, tmp->name, tmp->bname);
				}
			}
		}
	}
	if (strlen(out) > strlen(begin))
		printf("%s", out);
	free_dev(&head);
	free(out);
	return 0;
}

int attach_both(char *cdev, char *backdev)
{
	struct bdev bd;
	struct cdev cd;
	int type = 1;
	int ret;
	char buf[100];

	ret = detail_dev(backdev, &bd, &cd, &type);
	if (ret != 0)
		return ret;
	if (type != BCACHE_SB_VERSION_BDEV
	    && type != BCACHE_SB_VERSION_BDEV_WITH_OFFSET
	    && type != BCACHE_SB_VERSION_BDEV_WITH_FEATURES) {
		fprintf(stderr, "%s is not an backend device\n", backdev);
		return 1;
	}
	if (strcmp(bd.base.attachuuid, BCACHE_BNAME_NOT_EXIST) != 0) {
		fprintf(stderr,
			"This device have attached to another cset\n");
		return 1;
	}

	if (strlen(cdev) != 36) {
		ret = detail_dev(cdev, &bd, &cd, &type);
		if (type != BCACHE_SB_VERSION_CDEV
		    && type != BCACHE_SB_VERSION_CDEV_WITH_UUID
		    && type != BCACHE_SB_VERSION_CDEV_WITH_FEATURES) {
			fprintf(stderr, "%s is not an cache device\n", cdev);
			return 1;
		}
		strcpy(buf, cd.base.cset);
	} else {
		strcpy(buf, cdev);
	}
	return attach_backdev(buf, backdev);
}

bool has_permission(void)
{
	uid_t euid = geteuid();

	if (euid != 0)
		return false;
	return true;
}

int main(int argc, char **argv)
{
	char *subcmd;

	if (!has_permission()) {
		fprintf(stderr,
		"Only root or users who has root priviledges can run this command\n");
		return 1;
	}
	if (argc < 2) {
		main_usage();
		return 1;
	}
	subcmd = argv[1];
	argc--;
	argv += 1;
	char *devname = NULL;
	if (strcmp(subcmd, "make") == 0)
		return make_bcache(argc, argv);
	else if (strcmp(subcmd, "show") == 0) {
		int o = 0;
		int more = 0;
		int device = 0;
		int help = 0;

		static struct option long_options[] = {
			{"more", no_argument, 0, 'm'},
			{"help", no_argument, 0, 'h'},
			{"device", required_argument, 0, 'd'},
			{0, 0, 0, 0}
		};
		int option_index = 0;

		while ((o =
			getopt_long(argc, argv, "hmd:", long_options,
				    &option_index)) != EOF) {
			switch (o) {
			case 'd':
				devname = optarg;
				device = 1;
				break;
			case 'm':
				more = 1;
				break;
			case 'h':
				help = 1;
				break;
			case '?':
				return 1;
			}
		}
		argc -= optind;
		if (help || argc != 0) {
			return show_usage();
		} else if (more) {
			return show_bdevs_detail();
		} else if (device) {
			if (bad_dev(&devname)) {
				fprintf(stderr,
					"Error:Wrong device name found\n");
				return 1;
			}
			return detail_single(devname);
		} else {
			return show_bdevs();
		}
	} else if (strcmp(subcmd, "tree") == 0) {
		if (argc != 1)
			return tree_usage();
		return tree();
	} else if (strcmp(subcmd, "register") == 0) {
		if (argc != 2 || strcmp(argv[1], "-h") == 0)
			return register_usage();
		devname = argv[1];
		if (bad_dev(&devname)) {
			fprintf(stderr, "Error:Wrong device name found\n");
			return 1;
		}
		return register_dev(devname);
	} else if (strcmp(subcmd, "unregister") == 0) {
		if (argc != 2 || strcmp(argv[1], "-h") == 0)
			return unregister_usage();
		devname = argv[1];
		if (bad_dev(&devname)) {
			fprintf(stderr, "Error:Wrong device name found\n");
			return 1;
		}
		struct bdev bd;
		struct cdev cd;
		int type = 1;
		int ret;

		ret = detail_dev(devname, &bd, &cd, &type);
		if (ret != 0)
			return ret;
		if (type == BCACHE_SB_VERSION_BDEV
		    || type == BCACHE_SB_VERSION_BDEV_WITH_OFFSET
		    || type == BCACHE_SB_VERSION_BDEV_WITH_FEATURES) {
			return stop_backdev(devname);
		} else if (type == BCACHE_SB_VERSION_CDEV
			   || type == BCACHE_SB_VERSION_CDEV_WITH_UUID
			   || type == BCACHE_SB_VERSION_CDEV_WITH_FEATURES) {
			return unregister_cset(cd.base.cset);
		}
		return 1;
	} else if (strcmp(subcmd, "attach") == 0) {
		if (argc != 3 || strcmp(argv[1], "-h") == 0)
			return attach_usage();
		devname = argv[2];
		char *attachto = argv[1];

		if ((bad_uuid(attachto) && bad_dev(&attachto))
			|| bad_dev(&devname)) {
			fprintf(stderr,
			"Error:Wrong device name or cache_set uuid found\n");
			return 1;
		}
		return attach_both(attachto, devname);
	} else if (strcmp(subcmd, "detach") == 0) {
		if (argc != 2 || strcmp(argv[1], "-h") == 0)
			return detach_usage();
		devname = argv[1];
		if (bad_dev(&devname)) {
			fprintf(stderr, "Error:Wrong device name found\n");
			return 1;
		}
		return detach_backdev(devname);
	} else if (strcmp(subcmd, "set-cachemode") == 0) {
		if (argc != 3)
			return setcachemode_usage();
		devname = argv[1];
		if (bad_dev(&devname)) {
			fprintf(stderr, "Error:Wrong device name found\n");
			return 1;
		}
		struct bdev bd;
		struct cdev cd;
		int type = 1;
		int ret;

		ret = detail_dev(devname, &bd, &cd, &type);
		if (ret != 0) {
			fprintf(stderr,
			"This device doesn't exist or failed to receive info from this device\n");
			return ret;
		}
		if (type != BCACHE_SB_VERSION_BDEV
		    && type != BCACHE_SB_VERSION_BDEV_WITH_OFFSET
		    && type != BCACHE_SB_VERSION_BDEV_WITH_FEATURES) {
			fprintf(stderr,
				"Only backend device is suppported\n");
			return 1;
		}
		return set_backdev_cachemode(devname, argv[2]);
	} else if (strcmp(subcmd, "set-label") == 0) {
		if (argc != 3)
			return setlabel_usage();
		devname = argv[1];
		if (bad_dev(&devname)) {
			fprintf(stderr, "Error:Wrong device name found\n");
			return 1;
		}
		struct bdev bd;
		struct cdev cd;
		int type = 5;
		int ret;

		ret = detail_dev(devname, &bd, &cd, &type);
		if (ret != 0) {
			fprintf(stderr,
		"This device doesn't exist or failed to receive info from this device\n");
			return ret;
		}
		if (type != BCACHE_SB_VERSION_BDEV
		    && type != BCACHE_SB_VERSION_BDEV_WITH_OFFSET
		    && type != BCACHE_SB_VERSION_BDEV_WITH_FEATURES) {
			fprintf(stderr,
				"Only backend device is suppported\n");
			return 1;
		}
		if (strlen(argv[2]) >= SB_LABEL_SIZE) {
			fprintf(stderr, "Label is too long\n");
			return 1;
		}
		return set_label(devname, argv[2]);
	} else if (strcmp(subcmd, "version") == 0) {
		if (argc != 1)
			return version_usagee();
		printf("bcache-tools %s\n", BCACHE_TOOLS_VERSION);

		return 0;
	}

	main_usage();
	return 0;
}