Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
/*
 * Copyright (c) 2010 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Adam Hamsik.
 *
 * 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 NETBSD FOUNDATION, INC. 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 FOUNDATION 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/types.h>
#include <sys/param.h>

#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <prop/proplib.h>

#include <dm.h>

#ifdef RUMP_ACTION
#include <rump/rump.h>
#include <rump/rumpclient.h>
#include <rump/rump_syscalls.h>
#endif

/* dmctl command is used to communicate with device-mapper driver in NetBSD
 * it uses libdm library to create and send required data to kernel.
 *
 * Main purpose of dmctl is to add posibility to use device-mapper driver
 * from outside of LVM scope.
 */

#define DMCTL_CMD_REQ_NODEVNAME 0	/* Command do not require device name */
#define DMCTL_CMD_REQ_DEVNAME	1	/* Command require device name to work */
#define DMCTL_CMD_REQ_NEWNAME	2	/* Command require device name and
					   newname to work */
struct command {
	const char *cmd_name;
	const char *cmd_help;
	const char *ioctl_cmd_name;
	int min_args;
	int (*cmd_func)(int, char *[], libdm_task_t);
};

static const   char *dvname;            /* device name */
static const   char *cmdname;           /* command user issued */

static char * parse_stdin(char *);

static int dmctl_get_version(int, char *[], libdm_task_t);
static int dmctl_get_targets(int, char *[], libdm_task_t);
static int dmctl_get_device_info(int, char *[], libdm_task_t);
static int dmctl_create_dev(int, char *[], libdm_task_t);
static int dmctl_dev_rename(int, char *[], libdm_task_t);
static int dmctl_dev_remove(int, char *[], libdm_task_t);
static int dmctl_dev_resume(int, char *[], libdm_task_t);
static int dmctl_dev_suspend(int, char *[], libdm_task_t);
static int dmctl_dev_deps(int, char *[], libdm_task_t);
static int dmctl_list_devices(int, char *[], libdm_task_t);
static int dmctl_table_reload(int, char *[], libdm_task_t);
static int dmctl_table_status(int, char *[], libdm_task_t);
__dead static void usage(void);

static struct command commands[] = {
	{ "version",
	  "Print driver and lib version.",
	  NULL, DMCTL_CMD_REQ_NODEVNAME,
	  dmctl_get_version },
	{ "targets",
	  "List available kernel targets.",
	  NULL, DMCTL_CMD_REQ_NODEVNAME,
	  dmctl_get_targets },
	{ "create",
	  "Create device with [dm device name].",
	  NULL, DMCTL_CMD_REQ_DEVNAME,
	  dmctl_create_dev },
	{ "ls",
	  "List existing dm devices.",
	  "names", DMCTL_CMD_REQ_NODEVNAME,
	  dmctl_list_devices },
	{ "info",
	  "Get info about device with [dm device name].",
	  NULL, DMCTL_CMD_REQ_DEVNAME,
	  dmctl_get_device_info },
	{ "rename",
	  "Rename device with [dm device name] to  [dm device new name].",
	  NULL, DMCTL_CMD_REQ_NEWNAME,
	  dmctl_dev_rename },
	{ "remove",
	  "Remove device with [dm device name].",
	  NULL, DMCTL_CMD_REQ_DEVNAME,
	  dmctl_dev_remove },
	{ "resume",
	  "Resume IO on dm device [dm device name].",
	  NULL, DMCTL_CMD_REQ_DEVNAME,
	  dmctl_dev_resume },
	{ "suspend",
	  "Suspend IO on dm device [dm device name].",
	  NULL, DMCTL_CMD_REQ_DEVNAME,
	  dmctl_dev_suspend },
	{ "deps",
	  "Print physical dependiences for dm device [dm device name].",
	  NULL, DMCTL_CMD_REQ_DEVNAME,
	  dmctl_dev_deps },
	{ "reload",
	  "Switch active and passive tables for device with [dm device name].",
	  NULL, DMCTL_CMD_REQ_DEVNAME,
	  dmctl_table_reload },
	{ "status",
	  "Print status for device with [dm device name].",
	  "table", DMCTL_CMD_REQ_DEVNAME,
	  dmctl_table_status },
	{ "table",
	  "Print active table for device with [dm device name].",
	  NULL, DMCTL_CMD_REQ_DEVNAME,
	  dmctl_table_status },
	{ NULL,
	  NULL,
	  NULL, 0,
	  NULL },
};

int
main(int argc, char *argv[])
{
	int i;
	int oargc;
	libdm_task_t task;

	oargc = 0;

#ifdef RUMP_ACTION
	if (rumpclient_init() == -1)
	  err(EXIT_FAILURE, "rump client init failed");
#endif

	/* Must have at least: device command */
	if (argc < 2)
		usage();

	/* Skip program name, get and skip device name and command. */
	cmdname = argv[1];
	if (argc > 2) {
		oargc = 1;
		dvname = argv[2];
	}

	if (argc > 3) {
		argv += 3;
		argc -= 3;
		oargc = 2;
	} else {
		argv = 0;
		argc = 0;
	}

	for (i = 0; commands[i].cmd_name != NULL; i++)
		if (strcmp(cmdname, commands[i].cmd_name) == 0)
			break;

	if (commands[i].cmd_name == NULL)
		errx(EXIT_FAILURE, "unknown command: %s", cmdname);

	if (commands[i].ioctl_cmd_name != NULL)
		cmdname = commands[i].ioctl_cmd_name;

	if (oargc != commands[i].min_args) {
		(void)fprintf(stderr, "Insufficient number of arguments for "
		    "command: %s specified\n", commands[i].cmd_name);
		usage();
	}

	/*
	 * Create libdm task, and pass it to command handler later.
	 * Don't release it here because it will be replaced by different
	 * dictionary received from kernel after libdm_task_run.
	 */
	task = libdm_task_create(cmdname);

	(*commands[i].cmd_func)(argc, argv, task);

	return 0;
}

/*
 * Print library and kernel driver versions if command can be used only when
 * major, minor number of library version is <= kernel.
 */
static int
dmctl_get_version(int argc __unused, char *argv[] __unused, libdm_task_t task)
{
	uint32_t ver[3];

	(void)libdm_task_get_cmd_version(task, ver, sizeof(ver));

	printf("Library protocol version %d:%d:%d\n", ver[0], ver[1], ver[2]);

	if (libdm_task_run(task) != 0)
		err(EXIT_FAILURE, "dmctl_get_version: libdm_task_run failed.");

	(void)libdm_task_get_cmd_version(task, ver, 3);
	printf("Kernel protocol version %d:%d:%d\n",ver[0], ver[1], ver[2]);

	libdm_task_destroy(task);
	return 0;
}

/*
 * Get list of available targets from kernel and print them.
 */
static int
dmctl_get_targets(int argc __unused, char *argv[] __unused, libdm_task_t task)
{
	libdm_cmd_t cmd;
	libdm_iter_t iter;
	libdm_target_t target;
	uint32_t ver[3];

	if (libdm_task_run(task) != 0)
		err(EXIT_FAILURE, "dmctl_get_targets: libdm_task_run failed.");

	if ((cmd = libdm_task_get_cmd(task)) == NULL)
		return ENOENT;

	iter = libdm_cmd_iter_create(cmd);

	while((target = libdm_cmd_get_target(iter)) != NULL){
		printf("Target name: %s\n", libdm_target_get_name(target));

		libdm_target_get_version(target, ver, sizeof(ver));
		printf("Target version %d.%d.%d\n\n", ver[0], ver[1], ver[2]);

		libdm_target_destroy(target);
	}

	libdm_iter_destroy(iter);
	libdm_cmd_destroy(cmd);
	libdm_task_destroy(task);

	return 0;
}

/*
 * Create device with name used as second parameter.
 * TODO: Support for UUIDs here.
 */
static int
dmctl_create_dev(int argc __unused, char *argv[] __unused, libdm_task_t task)
{

	libdm_task_set_name(dvname, task);

	if (libdm_task_run(task) != 0)
		err(EXIT_FAILURE, "dmctl_create_dev: libdm_task_run failed.");

	libdm_task_destroy(task);
	return 0;
}

/*
 * Get basic device info from device-mapper driver.
 */
static int
dmctl_get_device_info(int argc __unused, char *argv[] __unused, libdm_task_t task)
{

	libdm_task_set_name(dvname, task);

	if (libdm_task_run(task) != 0)
		err(EXIT_FAILURE, "%s: libdm_task_run failed", __func__);

	printf("Printing Device info for:\n");
	printf("Device name: \t\t%s\n", libdm_task_get_name(task));
	printf("Device uuid: \t\t%s\n", libdm_task_get_uuid(task));
	printf("Device minor: \t\t%d\n", libdm_task_get_minor(task));
	printf("Device target number: \t%d\n", libdm_task_get_target_num(task));
	printf("Device flags: \t\t%d\n", libdm_task_get_flags(task));

	libdm_task_destroy(task);
	return 0;
}

/*
 * List all device in device-mapper driver.
 */
static int
dmctl_list_devices(int argc __unused, char *argv[] __unused, libdm_task_t task)
{
	libdm_cmd_t cmd;
	libdm_iter_t iter;
	libdm_dev_t dev;

	if (libdm_task_run(task) != 0)
		err(EXIT_FAILURE, "dmctl_list_devices: libdm_task_run failed.");

	if ((cmd = libdm_task_get_cmd(task)) == NULL)
		return ENOENT;

	iter = libdm_cmd_iter_create(cmd);

	while((dev = libdm_cmd_get_dev(iter)) != NULL){
		printf("Device name: %s, device minor: %d \n",
		    libdm_dev_get_name(dev), libdm_dev_get_minor(dev));
		libdm_dev_destroy(dev);
	}

	libdm_iter_destroy(iter);
	libdm_cmd_destroy(cmd);
	libdm_task_destroy(task);

	return 0;
}

/*
 * Rename device to new name
 */
static int
dmctl_dev_rename(int argc __unused, char *argv[], libdm_task_t task)
{
	libdm_cmd_t cmd;

	libdm_task_set_name(dvname, task);

	cmd = libdm_cmd_create();
	libdm_dev_set_newname(argv[0], cmd);
	libdm_task_set_cmd(cmd, task);

	if (libdm_task_run(task) != 0)
		err(EXIT_FAILURE, "dmctl_dev_rename: libdm_task_run failed.");

	libdm_cmd_destroy(cmd);
	libdm_task_destroy(task);

	return 0;
}

/*
 * Remove device from dm device list.
 */
static int
dmctl_dev_remove(int argc __unused, char *argv[] __unused, libdm_task_t task)
{

	if (dvname == NULL)
		return (ENOENT);

	libdm_task_set_name(dvname, task);

	if (libdm_task_run(task) != 0)
		err(EXIT_FAILURE, "dmctl_dev_remove: libdm_task_run failed.");

	libdm_task_destroy(task);
	return 0;
}

/*
 * Resume device which was suspended or created right now.
 * Replace table in "active slot" witg table in "inactive slot".
 */
static int
dmctl_dev_resume(int argc __unused, char *argv[] __unused, libdm_task_t task)
{

	libdm_task_set_name(dvname, task);

	if (libdm_task_run(task) != 0)
		err(EXIT_FAILURE, "dmctl_dev_resume: libdm_task_run failed.");

	libdm_task_destroy(task);
	return 0;
}

/*
 * Resume device which was suspended or created right now.
 * Replace table in "active slot" with table in "inactive slot".
 */
static int
dmctl_dev_suspend(int argc __unused, char *argv[] __unused, libdm_task_t task)
{

	libdm_task_set_name(dvname, task);
	libdm_task_set_suspend_flag(task);

	if (libdm_task_run(task) != 0)
		err(EXIT_FAILURE, "dmctl_dev_suspend: libdm_task_run failed.");

	libdm_task_destroy(task);
	return 0;
}

/*
 * Get device dependiences from device-mapper. Device dependency is physical
 * device on which dm device depends.
 */
static int
dmctl_dev_deps(int argc __unused, char *argv[] __unused, libdm_task_t task)
{
	libdm_cmd_t cmd;
	libdm_iter_t iter;
	dev_t dev_deps;

	libdm_task_set_name(dvname, task);

	if (libdm_task_run(task) != 0)
		err(EXIT_FAILURE, "dmctl_dev_deps: libdm_task_run failed.");

	if ((cmd = libdm_task_get_cmd(task)) == NULL)
		return ENOENT;

	iter = libdm_cmd_iter_create(cmd);

	printf("Device %s dependiences \n", dvname);

	while((dev_deps = libdm_cmd_get_deps(iter)) != 0)
		printf("major: %d minor: %d\n", major(dev_deps), minor(dev_deps));

	libdm_iter_destroy(iter);
	libdm_cmd_destroy(cmd);
	libdm_task_destroy(task);

	return 0;
}

/*
 * Reload device table to get new one to use.
 */
static int
dmctl_table_reload(int argc, char *argv[], libdm_task_t task)
{
	libdm_cmd_t cmd;
	libdm_table_t table;

	char *params;
	char *file_path;
	char target[128];
	int len;
	uint64_t start, length;

	file_path = NULL;
	params = NULL;

	cmd = libdm_cmd_create();

	libdm_task_set_name(dvname, task);

	if (argc != 0)
		file_path = argv[0];

	while ((params = parse_stdin(file_path)) != NULL) {
		table = libdm_table_create();

		sscanf(params, "%"PRIu64" %"PRIu64" %s %n", &start, &length, target, &len);

		libdm_table_set_start(start, table);
		libdm_table_set_length(length, table);
		libdm_table_set_target(target, table);
		libdm_table_set_params(params + len, table);
		libdm_cmd_set_table(table, cmd);

		libdm_table_destroy(table);

		free(params);
	}

	libdm_task_set_cmd(cmd, task);

	if (libdm_task_run(task) != 0)
		err(EXIT_FAILURE, "libdm_task_run: from dmctl_table_reload failed.");

	libdm_cmd_destroy(cmd);
	libdm_task_destroy(task);
	free(params);

	return 0;
}

/*
 * Get table status from device.
 */
static int
dmctl_table_status(int argc __unused, char *argv[] __unused, libdm_task_t task)
{
	libdm_cmd_t cmd;
	libdm_table_t table;
	libdm_iter_t iter;

	libdm_task_set_name(dvname, task);
	libdm_task_set_status_flag(task);

	if (libdm_task_run(task) != 0)
		err(EXIT_FAILURE, "libdm_task_run");

	if ((cmd = libdm_task_get_cmd(task)) == NULL)
		return ENOENT;

	iter = libdm_cmd_iter_create(cmd);

	printf("Getting device table for device %s\n", dvname);

	while ((table = libdm_cmd_get_table(iter)) != NULL) {
		printf("%10"PRIu64" %10"PRIu64" %s\n",
			libdm_table_get_start(table),
			libdm_table_get_length(table),
			libdm_table_get_target(table));
		libdm_table_destroy(table);
	}

	libdm_iter_destroy(iter);
	libdm_cmd_destroy(cmd);
	libdm_task_destroy(task);

	return 0;
}

static void
usage(void)
{
	int i;

	(void)fprintf(stderr, "usage: %s command [dm device name]\n"
	    "Available commands are:\n ", getprogname());
	for (i = 0; commands[i].cmd_name != NULL; i++)
		(void)fprintf(stderr, "\t%s\t%s\n", commands[i].cmd_name, commands[i].cmd_help);
	exit(EXIT_FAILURE);
}

static char *
parse_stdin(char *file_path)
{
	char *buf, *lbuf;
	size_t len;
	FILE *fp;

	lbuf = NULL;

	if (file_path) {
		if ((fp = fopen(file_path, "r")) == NULL)
			err(ENOENT, "Cannot open table file");
	} else
		fp = stdin;

	if ((buf = fgetln(fp, &len)) == NULL)
		return NULL;

	if (buf[len - 1] != '\n')
		len++;

	if ((lbuf = malloc(len)) == NULL)
		err(EXIT_FAILURE, "malloc");

	memcpy(lbuf, buf, len);
	lbuf[len - 1] = '\0';

	return lbuf;
}