aboutsummaryrefslogtreecommitdiff
path: root/tests-mx32/ioctl_dm.c
blob: 1e831dfe898099a9dd0d19eaf3143378ee040271 (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
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
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
/*
 * Check decoding of DM_* commands of ioctl syscall.
 *
 * Copyright (c) 2016 Mikulas Patocka <mpatocka@redhat.com>
 * Copyright (c) 2016 Eugene Syromyatnikov <evgsyr@gmail.com>
 * Copyright (c) 2016-2017 The strace developers.
 * 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.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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 "tests.h"

#ifdef HAVE_LINUX_DM_IOCTL_H

# include <errno.h>
# include <inttypes.h>
# include <stdbool.h>
# include <stdio.h>
# include <stddef.h>
# include <string.h>
# include <sys/ioctl.h>
# include <linux/ioctl.h>
# include <linux/dm-ioctl.h>

# ifndef VERBOSE
#  define VERBOSE 0
# endif

# define STR32 "AbCdEfGhIjKlMnOpQrStUvWxYz012345"

# define ALIGNED_SIZE(s_, t_) \
	(((s_) + (ALIGNOF(t_) - 1UL)) & ~(ALIGNOF(t_) - 1UL))
# define ALIGNED_OFFSET(t_, m_) \
	ALIGNED_SIZE(offsetof(t_, m_), t_)

#  ifndef DM_DEV_ARM_POLL
#   define DM_DEV_ARM_POLL     _IOWR(DM_IOCTL, 0x10, struct dm_ioctl)
#  endif

static const char str129[] = STR32 STR32 STR32 STR32 "6";

static const __u64 dts_sector_base = (__u64) 0xdeadca75facef157ULL;
static const __u64 dts_sector_step = (__u64) 0x100000001ULL;
static const __u64 dts_length_base = (__u64) 0xbadc0dedda7a1057ULL;
static const __u64 dts_length_step = (__u64) 0x700000007ULL;
static const __s32 dts_status_base = (__s32) 3141592653U;
static const __s32 dts_status_step = 0x1234;

static const size_t min_sizeof_dm_ioctl =
	offsetof(struct dm_ioctl, data);

static struct s {
	struct dm_ioctl ioc;
	union {
		struct {
			struct dm_target_spec target_spec;
			char target_params[256];
		} ts;
		struct {
			struct dm_target_msg target_msg;
			char target_string[256];
		} tm;
		char string[256];
	} u;
} s;

struct dm_table_open_test {
	struct dm_ioctl ioc;
	struct dm_target_spec target0;
	char param0[1];
	struct dm_target_spec target1;
	char param1[2];
	struct dm_target_spec target2;
	char param2[3];
	struct dm_target_spec target3;
	char param3[4];
	struct dm_target_spec target4;
	char param4[5];
	struct dm_target_spec target5;
	char param5[6];
	struct dm_target_spec target6;
	char param6[7];
	struct dm_target_spec target7;
	char param7[8];
	struct dm_target_spec target8;
	char param8[9];
	struct dm_target_spec target9;
	char param9[10];
};

struct dm_target_msg_test {
	struct dm_ioctl ioc;
	struct dm_target_msg msg;
};

struct args {
	unsigned int arg;
	const char *str;
	bool has_params;
	bool has_event_nr;
};


static void
init_s(struct dm_ioctl *s, size_t size, size_t offs)
{
	memset(s, 0, size);
	s->version[0] = DM_VERSION_MAJOR;
	s->version[1] = 1;
	s->version[2] = 2;
	s->data_size = size;
	s->data_start = offs;
	s->dev = 0x1234;
	strcpy(s->name, "nnn");
	strcpy(s->uuid, "uuu");
}

static void
init_dm_target_spec(struct dm_target_spec *ptr, uint32_t id)
{
	ptr->sector_start = dts_sector_base + dts_sector_step * id;
	ptr->length       = dts_length_base + dts_length_step * id;
	ptr->status       = dts_status_base + dts_status_step * id;

	strncpy(ptr->target_type, str129 +
		id % (sizeof(str129) - sizeof(ptr->target_type)),
		id % (sizeof(ptr->target_type) + 1));
	if (id % (sizeof(ptr->target_type) + 1) < sizeof(ptr->target_type))
		ptr->target_type[id % (sizeof(ptr->target_type) + 1)] = '\0';
}

# if VERBOSE
static void
print_dm_target_spec(struct dm_target_spec *ptr, uint32_t id)
{
	printf("{sector_start=%" PRI__u64 ", length=%" PRI__u64 ", "
	       "target_type=\"%.*s\", string=",
	       dts_sector_base + dts_sector_step * id,
	       dts_length_base + dts_length_step * id,
	       (int) (id % (sizeof(ptr->target_type) + 1)),
	       str129 + id % (sizeof(str129) - sizeof(ptr->target_type)));
}
# endif /* VERBOSE */

int
main(void)
{
	static kernel_ulong_t dummy_dm_ioctl1 =
		_IOC(_IOC_READ, DM_IOCTL, 0, 0x1fff);
	static kernel_ulong_t dummy_dm_ioctl2 =
		_IOC(_IOC_READ|_IOC_WRITE, DM_IOCTL, 0xed, 0);
	static kernel_ulong_t dummy_dm_arg =
		(kernel_ulong_t) 0xbadc0dedda7a1057ULL;
	/* We can't check these properly for now */
	static struct args dummy_check_cmds_nodev[] = {
		{ ARG_STR(DM_REMOVE_ALL),    false },
		{ ARG_STR(DM_LIST_DEVICES),  true  },
		{ ARG_STR(DM_LIST_VERSIONS), true  },
	};
	static struct args dummy_check_cmds[] = {
		{ ARG_STR(DM_DEV_CREATE),    false },
		{ ARG_STR(DM_DEV_REMOVE),    false, true },
		{ ARG_STR(DM_DEV_STATUS),    false },
		{ ARG_STR(DM_DEV_WAIT),      true,  true },
		{ ARG_STR(DM_TABLE_CLEAR),   false },
		{ ARG_STR(DM_TABLE_DEPS),    true  },
		{ ARG_STR(DM_TABLE_STATUS),  true  },
		{ ARG_STR(DM_DEV_ARM_POLL),  false },
	};

	struct dm_ioctl *unaligned_dm_arg =
		tail_alloc(offsetof(struct dm_ioctl, data));
	struct dm_ioctl *dm_arg =
		tail_alloc(ALIGNED_OFFSET(struct dm_ioctl, data));
	struct dm_table_open_test *dm_arg_open1 =
		tail_alloc(ALIGNED_OFFSET(struct dm_table_open_test, target1));
	struct dm_table_open_test *dm_arg_open2 =
		tail_alloc(ALIGNED_OFFSET(struct dm_table_open_test, param1));
	struct dm_table_open_test *dm_arg_open3 =
		tail_alloc(ALIGNED_OFFSET(struct dm_table_open_test, target9));
	struct dm_target_msg_test *dm_arg_msg =
		tail_alloc(sizeof(*dm_arg_msg));

	long rc;
	const char *errstr;
	unsigned int i;


	/* Incorrect operation */
	ioctl(-1, _IOW(DM_IOCTL, 0xde, int), dm_arg);
	printf("ioctl(-1, _IOC(_IOC_WRITE, %#x, 0xde, %#zx), %p) = "
	       "-1 EBADF (%m)\n",
	       DM_IOCTL, sizeof(int), dm_arg);

	ioctl(-1, dummy_dm_ioctl1, 0);
	printf("ioctl(-1, _IOC(_IOC_READ, %#x, 0, %#x), 0) = -1 EBADF (%m)\n",
	       DM_IOCTL, (unsigned int) _IOC_SIZE(dummy_dm_ioctl1));

	ioctl(-1, dummy_dm_ioctl2, dummy_dm_arg);
	printf("ioctl(-1, _IOC(_IOC_READ|_IOC_WRITE, %#x, %#x, 0), %#lx) = "
	       "-1 EBADF (%m)\n",
	       DM_IOCTL, (unsigned int) _IOC_NR(dummy_dm_ioctl2),
	       (unsigned long) dummy_dm_arg);


	/* DM_VERSION */
	/* Incorrect pointer */
	ioctl(-1, DM_VERSION, dm_arg + 1);
	printf("ioctl(-1, DM_VERSION, %p) = -1 EBADF (%m)\n", dm_arg + 1);

	/* Incorrect data_size */
	init_s(dm_arg, 0, 0);
	ioctl(-1, DM_VERSION, &s);
	printf("ioctl(-1, DM_VERSION, %p) = -1 EBADF (%m)\n", &s);

	/* Incorrect version */
	init_s(dm_arg, min_sizeof_dm_ioctl, 0);
	dm_arg->version[0] = 0xbadc0ded;
	dm_arg->version[1] = 0xbadc0dee;
	dm_arg->version[2] = 0xbadc0def;
	ioctl(-1, DM_VERSION, dm_arg);
	printf("ioctl(-1, DM_VERSION, {version=%u.%u.%u"
	       " /* unsupported device mapper ABI version */}) = "
	       "-1 EBADF (%m)\n", 0xbadc0ded, 0xbadc0dee, 0xbadc0def);

	/* Incorrect data_size */
	init_s(dm_arg, 14, 64);
	ioctl(-1, DM_VERSION, dm_arg);
	printf("ioctl(-1, DM_VERSION, {version=4.1.2, data_size=14"
	       " /* data_size too small */}) = -1 EBADF (%m)\n");

	/* Unterminated name/uuid */
	init_s(dm_arg, min_sizeof_dm_ioctl, 0);
	strncpy(dm_arg->name, str129, sizeof(dm_arg->name));
	strncpy(dm_arg->uuid, str129, sizeof(dm_arg->uuid));
	ioctl(-1, DM_VERSION, dm_arg);
	printf("ioctl(-1, DM_VERSION, {version=4.1.2, data_size=%zu, "
	       "dev=makedev(18, 52), name=\"%.127s\"..., uuid=\"%.128s\"..., "
	       "flags=0}) = -1 EBADF (%m)\n",
	       min_sizeof_dm_ioctl, str129, str129);

	/* Normal call */
	init_s(dm_arg, min_sizeof_dm_ioctl, 0);
	ioctl(-1, DM_VERSION, dm_arg);
	printf("ioctl(-1, DM_VERSION, "
	       "{version=4.1.2, data_size=%zu, "
	       "dev=makedev(18, 52), name=\"nnn\", uuid=\"uuu\", flags=0}) = "
	       "-1 EBADF (%m)\n", min_sizeof_dm_ioctl);

	/* Zero dev, name, uuid */
	init_s(dm_arg, min_sizeof_dm_ioctl, 0);
	dm_arg->data_size = 0xfacefeed;
	dm_arg->dev = 0;
	dm_arg->name[0] = '\0';
	dm_arg->uuid[0] = '\0';
	ioctl(-1, DM_VERSION, dm_arg);
	printf("ioctl(-1, DM_VERSION, "
	       "{version=4.1.2, data_size=%u, flags=0}) = "
	       "-1 EBADF (%m)\n", 0xfacefeed);

	/* Flag */
	init_s(dm_arg, min_sizeof_dm_ioctl, 0);
	dm_arg->flags = 0xffffffff;
	ioctl(-1, DM_VERSION, dm_arg);
	printf("ioctl(-1, DM_VERSION, "
	       "{version=4.1.2, data_size=%zu, "
	       "dev=makedev(18, 52), name=\"nnn\", uuid=\"uuu\", flags="
	       "DM_READONLY_FLAG|DM_SUSPEND_FLAG|DM_EXISTS_FLAG|"
	       "DM_PERSISTENT_DEV_FLAG|DM_STATUS_TABLE_FLAG|"
	       "DM_ACTIVE_PRESENT_FLAG|DM_INACTIVE_PRESENT_FLAG|"
	       "DM_BUFFER_FULL_FLAG|DM_SKIP_BDGET_FLAG|DM_SKIP_LOCKFS_FLAG|"
	       "DM_NOFLUSH_FLAG|DM_QUERY_INACTIVE_TABLE_FLAG|"
	       "DM_UEVENT_GENERATED_FLAG|DM_UUID_FLAG|DM_SECURE_DATA_FLAG|"
	       "DM_DATA_OUT_FLAG|DM_DEFERRED_REMOVE|DM_INTERNAL_SUSPEND_FLAG|"
	       "0xfff80080}) = -1 EBADF (%m)\n",
	       min_sizeof_dm_ioctl);

	/* Normal call */
	init_s(&s.ioc, sizeof(s.ioc), 0);
	ioctl(-1, DM_VERSION, &s);
	printf("ioctl(-1, DM_VERSION, "
	       "{version=4.1.2, data_size=%zu, "
	       "dev=makedev(18, 52), name=\"nnn\", uuid=\"uuu\", flags=0}) = "
	       "-1 EBADF (%m)\n", sizeof(s.ioc));


	/* DM_REMOVE_ALL */
	/* DM_LIST_DEVICES */
	/* DM_LIST_VERSIONS */
	for (i = 0; i < ARRAY_SIZE(dummy_check_cmds_nodev); i++) {
		init_s(dm_arg, min_sizeof_dm_ioctl, 0);
		ioctl(-1, dummy_check_cmds_nodev[i].arg, dm_arg);
		printf("ioctl(-1, %s, {version=4.1.2, data_size=%zu%s, "
		       "flags=0}) = -1 EBADF (%m)\n",
		       dummy_check_cmds_nodev[i].str,
		       min_sizeof_dm_ioctl,
		       dummy_check_cmds_nodev[i].has_params ?
		       ", data_start=0" : "");
	}


	/* DM_DEV_CREATE */
	/* DM_DEV_REMOVE */
	/* DM_DEV_STATUS */
	/* DM_DEV_WAIT */
	/* DM_TABLE_CLEAR */
	/* DM_TABLE_DEPS */
	/* DM_TABLE_STATUS */
	for (i = 0; i < ARRAY_SIZE(dummy_check_cmds); i++) {
		init_s(dm_arg, min_sizeof_dm_ioctl, 0);
		ioctl(-1, dummy_check_cmds[i].arg, dm_arg);
		printf("ioctl(-1, %s, {version=4.1.2, data_size=%zu%s, "
		       "dev=makedev(18, 52), name=\"nnn\", uuid=\"uuu\"%s, "
		       "flags=0}) = -1 EBADF (%m)\n",
		       dummy_check_cmds[i].str, min_sizeof_dm_ioctl,
		       dummy_check_cmds[i].has_params ? ", data_start=0" : "",
		       dummy_check_cmds[i].has_event_nr ? ", event_nr=0" : "");
	}


	/* DM_DEV_SUSPEND */
	init_s(&s.ioc, sizeof(s.ioc), 0);
	s.ioc.flags = DM_SUSPEND_FLAG;
	s.ioc.event_nr = 0xbadc0ded;
	ioctl(-1, DM_DEV_SUSPEND, &s);
	printf("ioctl(-1, DM_DEV_SUSPEND, "
	       "{version=4.1.2, data_size=%zu, "
	       "dev=makedev(18, 52), name=\"nnn\", uuid=\"uuu\", "
	       "flags=DM_SUSPEND_FLAG}) = -1 EBADF (%m)\n", sizeof(s.ioc));

	init_s(&s.ioc, sizeof(s.ioc), 0);
	s.ioc.event_nr = 0xbadc0ded;
	ioctl(-1, DM_DEV_SUSPEND, &s);
	printf("ioctl(-1, DM_DEV_SUSPEND, "
	       "{version=4.1.2, data_size=%zu, dev=makedev(18, 52), "
	       "name=\"nnn\", uuid=\"uuu\", event_nr=3134983661, "
	       "flags=0}) = -1 EBADF (%m)\n", sizeof(s.ioc));


	/* DM_TABLE_LOAD */
	init_s(&s.ioc, sizeof(s), offsetof(struct s, u));
	s.ioc.target_count = 1;
	s.u.ts.target_spec.sector_start = 0x10;
	s.u.ts.target_spec.length = 0x20;
	s.u.ts.target_spec.next =
		sizeof(s.u.ts.target_spec) + sizeof(s.u.ts.target_params);
	strcpy(s.u.ts.target_spec.target_type, "tgt");
	strcpy(s.u.ts.target_params, "tparams");
	ioctl(-1, DM_TABLE_LOAD, &s);
	printf("ioctl(-1, DM_TABLE_LOAD, "
	       "{version=4.1.2, data_size=%u, data_start=%u, "
	       "dev=makedev(18, 52), name=\"nnn\", uuid=\"uuu\", "
	       "target_count=1, flags=0, "
# if VERBOSE
	       "{sector_start=16, length=32, target_type=\"tgt\", "
	       "string=\"tparams\"}"
# else /* !VERBOSE */
	       "..."
# endif /* VERBOSE */
	       "}) = -1 EBADF (%m)\n", s.ioc.data_size, s.ioc.data_start);

	/* No targets */
	init_s(dm_arg, min_sizeof_dm_ioctl, min_sizeof_dm_ioctl);
	dm_arg->data_size = sizeof(*dm_arg);
	dm_arg->target_count = 0;
	ioctl(-1, DM_TABLE_LOAD, dm_arg);
	printf("ioctl(-1, DM_TABLE_LOAD, "
	       "{version=4.1.2, data_size=%zu, data_start=%zu, "
	       "dev=makedev(18, 52), name=\"nnn\", uuid=\"uuu\", "
	       "target_count=0, flags=0}) = -1 EBADF (%m)\n",
	       sizeof(*dm_arg), min_sizeof_dm_ioctl);

	/* Invalid data_start */
	init_s(dm_arg, min_sizeof_dm_ioctl, 0xfffffff8);
	dm_arg->data_size = sizeof(*dm_arg);
	dm_arg->target_count = 1234;
	ioctl(-1, DM_TABLE_LOAD, dm_arg);
	printf("ioctl(-1, DM_TABLE_LOAD, "
	       "{version=4.1.2, data_size=%zu, data_start=%u, "
	       "dev=makedev(18, 52), name=\"nnn\", uuid=\"uuu\", "
	       "target_count=1234, flags=0, "
# if VERBOSE
	       "??? /* misplaced struct dm_target_spec */"
# else
	       "..."
# endif /* VERBOSE */
	       "}) = -1 EBADF (%m)\n", sizeof(*dm_arg), 0xfffffff8);

	/* Inaccessible pointer */
	init_s(&dm_arg_open1->ioc, offsetof(struct dm_table_open_test, target1),
	       offsetof(struct dm_table_open_test, target1));
	dm_arg_open1->ioc.data_size = sizeof(*dm_arg_open1);
	dm_arg_open1->ioc.target_count = 0xdeaddea1;
	ioctl(-1, DM_TABLE_LOAD, dm_arg_open1);
	printf("ioctl(-1, DM_TABLE_LOAD, "
	       "{version=4.1.2, data_size=%zu, data_start=%zu, "
	       "dev=makedev(18, 52), name=\"nnn\", uuid=\"uuu\", "
	       "target_count=3735936673, flags=0, "
# if VERBOSE
	       "%p"
# else /* !VERBOSE */
	       "..."
# endif /* VERBOSE */
	       "}) = -1 EBADF (%m)\n", sizeof(*dm_arg_open1),
	       offsetof(struct dm_table_open_test, target1)
# if VERBOSE
	       , (char *) dm_arg_open1 +
	       offsetof(struct dm_table_open_test, target1)
# endif /* VERBOSE */
	       );

	/* Inaccessible string */
	init_s(&dm_arg_open2->ioc, offsetof(struct dm_table_open_test, param1),
	       offsetof(struct dm_table_open_test, target1));
	dm_arg_open2->ioc.data_size = sizeof(*dm_arg_open2);
	dm_arg_open2->ioc.target_count = 2;
	init_dm_target_spec(&dm_arg_open2->target1, 7);
	dm_arg_open2->target1.next =
		offsetof(struct dm_table_open_test, target3) -
		offsetof(struct dm_table_open_test, target1);
	rc = ioctl(-1, DM_TABLE_LOAD, dm_arg_open2);
	errstr = sprintrc(rc);
	printf("ioctl(-1, DM_TABLE_LOAD, "
	       "{version=4.1.2, data_size=%zu, data_start=%zu, "
	       "dev=makedev(18, 52), name=\"nnn\", uuid=\"uuu\", "
	       "target_count=2, flags=0, ",
	       sizeof(*dm_arg_open2),
	       offsetof(struct dm_table_open_test, target1));
# if VERBOSE
	print_dm_target_spec(&dm_arg_open2->target1, 7);
	printf("%p}, %p",
	       (char *) dm_arg_open2 +
	       offsetof(struct dm_table_open_test, param1),
	       (char *) dm_arg_open2 +
	       offsetof(struct dm_table_open_test, target3));
# else /* !VERBOSE */
	printf("...");
# endif /* VERBOSE */
	printf("}) = %s\n", errstr);

	/* Incorrect next */
	init_s(&dm_arg_open3->ioc, offsetof(struct dm_table_open_test, target5),
	       offsetof(struct dm_table_open_test, target0));
	dm_arg_open3->ioc.target_count = 4;

	init_dm_target_spec(&dm_arg_open3->target0, 9);
	dm_arg_open3->target0.next =
		offsetof(struct dm_table_open_test, target1) -
		offsetof(struct dm_table_open_test, target0);
	dm_arg_open3->param0[0] = '\0';

	init_dm_target_spec(&dm_arg_open3->target1, 15);
	dm_arg_open3->target1.next =
		offsetof(struct dm_table_open_test, target3) -
		offsetof(struct dm_table_open_test, target1);
	dm_arg_open3->param1[0] = '\377';
	dm_arg_open3->param1[1] = '\0';

	init_dm_target_spec(&dm_arg_open3->target3, 42);
	dm_arg_open3->target3.next = 0xdeadbeef;
	dm_arg_open3->param3[0] = '\1';
	dm_arg_open3->param3[1] = '\2';
	dm_arg_open3->param3[2] = '\0';

	rc = ioctl(-1, DM_TABLE_LOAD, dm_arg_open3);
	errstr = sprintrc(rc);
	printf("ioctl(-1, DM_TABLE_LOAD, "
	       "{version=4.1.2, data_size=%zu, data_start=%zu, "
	       "dev=makedev(18, 52), name=\"nnn\", uuid=\"uuu\", "
	       "target_count=4, flags=0, ",
	       offsetof(struct dm_table_open_test, target5),
	       offsetof(struct dm_table_open_test, target0));
# if VERBOSE
	print_dm_target_spec(&dm_arg_open3->target0, 9);
	printf("\"\"}, ");
	print_dm_target_spec(&dm_arg_open3->target1, 15);
	printf("\"\\377\"}, ");
	print_dm_target_spec(&dm_arg_open3->target1, 42);
	printf("\"\\1\\2\"}, ??? /* misplaced struct dm_target_spec */");
# else /* !VERBOSE */
	printf("...");
# endif /* VERBOSE */
	printf("}) = %s\n", errstr);

	#define FILL_DM_TARGET(id, id_next) \
		do { \
			init_dm_target_spec(&dm_arg_open3->target##id, id); \
			dm_arg_open3->target##id.next = \
				offsetof(struct dm_table_open_test, \
					 target##id_next) - \
				offsetof(struct dm_table_open_test, \
					 target##id); \
			strncpy(dm_arg_open3->param##id, str129 + id * 2, id); \
			dm_arg_open3->param##id[id] = '\0'; \
		} while (0)
	#define PRINT_DM_TARGET(id) \
		do { \
			print_dm_target_spec(&dm_arg_open3->target##id, id); \
			printf("\"%.*s\"}, ", id, str129 + id * 2); \
		} while (0)

	/* max_strlen limit */
	init_s(&dm_arg_open3->ioc, offsetof(struct dm_table_open_test, target9),
	       offsetof(struct dm_table_open_test, target0));
	dm_arg_open3->ioc.data_size = sizeof(*dm_arg_open3);
	dm_arg_open3->ioc.target_count = 0xbadc0ded;
	FILL_DM_TARGET(0, 1);
	FILL_DM_TARGET(1, 2);
	FILL_DM_TARGET(2, 3);
	FILL_DM_TARGET(3, 4);
	FILL_DM_TARGET(4, 5);
	FILL_DM_TARGET(5, 6);
	FILL_DM_TARGET(6, 7);
	FILL_DM_TARGET(7, 8);
	FILL_DM_TARGET(8, 9);
	rc = ioctl(-1, DM_TABLE_LOAD, dm_arg_open3);
	errstr = sprintrc(rc);
	printf("ioctl(-1, DM_TABLE_LOAD, "
	       "{version=4.1.2, data_size=%zu, data_start=%zu, "
	       "dev=makedev(18, 52), name=\"nnn\", uuid=\"uuu\", "
	       "target_count=3134983661, flags=0, ",
	       sizeof(*dm_arg_open3),
	       offsetof(struct dm_table_open_test, target0));
# if VERBOSE
	PRINT_DM_TARGET(0);
	PRINT_DM_TARGET(1);
	PRINT_DM_TARGET(2);
	PRINT_DM_TARGET(3);
	PRINT_DM_TARGET(4);
	PRINT_DM_TARGET(5);
	PRINT_DM_TARGET(6);
	PRINT_DM_TARGET(7);
	PRINT_DM_TARGET(8);
# endif /* VERBOSE */
	printf("...}) = %s\n", errstr);


	/* DM_TARGET_MSG */
	init_s(&s.ioc, sizeof(s), offsetof(struct s, u));
	s.u.tm.target_msg.sector = 0x1234;
	strcpy(s.u.string + offsetof(struct dm_target_msg, message),
	       "long target msg");
	ioctl(-1, DM_TARGET_MSG, &s);
	printf("ioctl(-1, DM_TARGET_MSG, "
	       "{version=4.1.2, data_size=%u, data_start=%u, "
	       "dev=makedev(18, 52), name=\"nnn\", uuid=\"uuu\", flags=0, "
# if VERBOSE
	       "{sector=4660, message=\"long targ\"...}"
# else /* !VERBOSE */
	       "..."
# endif /* VERBOSE */
	       "}) = -1 EBADF (%m)\n",
	       s.ioc.data_size, s.ioc.data_start);

	/* Invalid data_start */
	init_s(dm_arg, min_sizeof_dm_ioctl, min_sizeof_dm_ioctl);
	dm_arg->data_size = sizeof(*dm_arg);
	ioctl(-1, DM_TARGET_MSG, dm_arg);
	printf("ioctl(-1, DM_TARGET_MSG, "
	       "{version=4.1.2, data_size=%zu, data_start=%zu, "
	       "dev=makedev(18, 52), name=\"nnn\", uuid=\"uuu\", flags=0, "
# if VERBOSE
	       "??? /* misplaced struct dm_target_msg */"
# else /* !VERBOSE */
	       "..."
# endif /* VERBOSE */
	       "}) = -1 EBADF (%m)\n",
	       sizeof(*dm_arg), min_sizeof_dm_ioctl);

	/* Invalid data_start */
	init_s(dm_arg, min_sizeof_dm_ioctl, 0xffffffff);
	dm_arg->data_size = sizeof(*dm_arg);
	ioctl(-1, DM_TARGET_MSG, dm_arg);
	printf("ioctl(-1, DM_TARGET_MSG, "
	       "{version=4.1.2, data_size=%zu, data_start=%u, "
	       "dev=makedev(18, 52), name=\"nnn\", uuid=\"uuu\", flags=0, "
# if VERBOSE
	       "??? /* misplaced struct dm_target_msg */"
# else /* !VERBOSE */
	       "..."
# endif /* VERBOSE */
	       "}) = -1 EBADF (%m)\n",
	       sizeof(*dm_arg), 0xffffffff);

	/* Inaccessible pointer */
	init_s(dm_arg, min_sizeof_dm_ioctl, 0);
	dm_arg->data_size = sizeof(*dm_arg) + sizeof(struct dm_target_msg);
	dm_arg->data_start = sizeof(*dm_arg);
	ioctl(-1, DM_TARGET_MSG, dm_arg);
	printf("ioctl(-1, DM_TARGET_MSG, "
	       "{version=4.1.2, data_size=%zu, data_start=%zu, "
	       "dev=makedev(18, 52), name=\"nnn\", uuid=\"uuu\", flags=0, "
# if VERBOSE
	       "%p"
# else /* !VERBOSE */
	       "..."
# endif /* VERBOSE */
	       "}) = -1 EBADF (%m)\n",
	       sizeof(*dm_arg) + sizeof(struct dm_target_msg),
	       sizeof(*dm_arg)
# if VERBOSE
	       , (char *) dm_arg + sizeof(*dm_arg)
# endif /* VERBOSE */
	       );

	/* Inaccessible string */
	init_s(&dm_arg_msg->ioc, sizeof(*dm_arg_msg),
	       offsetof(struct dm_target_msg_test, msg));
	dm_arg_msg->ioc.data_size = sizeof(*dm_arg_msg) + 1;
	dm_arg_msg->msg.sector = (__u64) 0xdeadbeeffacef157ULL;
	rc = ioctl(-1, DM_TARGET_MSG, dm_arg_msg);
	errstr = sprintrc(rc);
	printf("ioctl(-1, DM_TARGET_MSG, "
	       "{version=4.1.2, data_size=%zu, data_start=%zu, "
	       "dev=makedev(18, 52), name=\"nnn\", uuid=\"uuu\", flags=0, ",
	       sizeof(*dm_arg_msg) + 1,
	       offsetof(struct dm_target_msg_test, msg));
# if VERBOSE
	printf("{sector=%" PRI__u64 ", message=%p}",
	       (__u64) 0xdeadbeeffacef157ULL,
	       (char *) dm_arg_msg +
	       offsetof(struct dm_target_msg_test, msg.message));
# else /* !VERBOSE */
	printf("...");
# endif /* VERBOSE */
	printf("}) = %s\n", errstr);

	/* Zero-sied string */
	init_s(&dm_arg_msg->ioc, sizeof(*dm_arg_msg),
	       offsetof(struct dm_target_msg_test, msg));
	dm_arg_msg->msg.sector = (__u64) 0xdeadbeeffacef157ULL;
	rc = ioctl(-1, DM_TARGET_MSG, dm_arg_msg);
	errstr = sprintrc(rc);
	printf("ioctl(-1, DM_TARGET_MSG, "
	       "{version=4.1.2, data_size=%zu, data_start=%zu, "
	       "dev=makedev(18, 52), name=\"nnn\", uuid=\"uuu\", flags=0, ",
	       sizeof(*dm_arg_msg), offsetof(struct dm_target_msg_test, msg));
# if VERBOSE
	printf("{sector=%" PRI__u64 ", message=\"\"}",
	       (__u64) 0xdeadbeeffacef157ULL);
# else /* !VERBOSE */
	printf("...");
# endif /* VERBOSE */
	printf("}) = %s\n", errstr);


	/* DM_DEV_SET_GEOMETRY */
	init_s(&s.ioc, sizeof(s), offsetof(struct s, u));
	strcpy(s.u.string, "10 20 30 40");
	ioctl(-1, DM_DEV_SET_GEOMETRY, &s);
	printf("ioctl(-1, DM_DEV_SET_GEOMETRY, "
	       "{version=4.1.2, data_size=%u, data_start=%u, "
	       "dev=makedev(18, 52), name=\"nnn\", uuid=\"uuu\", flags=0, "
# if VERBOSE
	       "string=\"10 20 30 \"..."
# else /* !VERBOSE */
	       "..."
# endif /* VERBOSE */
	       "}) = -1 EBADF (%m)\n",
	       s.ioc.data_size, s.ioc.data_start);


	/* DM_DEV_RENAME */
	/* Inaccessible data */
	init_s(dm_arg, min_sizeof_dm_ioctl, min_sizeof_dm_ioctl);
	dm_arg->data_size = sizeof(*dm_arg);
	memcpy(unaligned_dm_arg, dm_arg, offsetof(struct dm_ioctl, data));
	ioctl(-1, DM_DEV_RENAME, unaligned_dm_arg);
	printf("ioctl(-1, DM_DEV_RENAME, "
	       "{version=4.1.2, data_size=%zu, data_start=%zu, "
	       "dev=makedev(18, 52), name=\"nnn\", uuid=\"uuu\", event_nr=0, "
	       "flags=0, "
# if VERBOSE
	       "string=%p"
# else /* !VERBOSE */
	       "..."
# endif /* VERBOSE */
	       "}) = -1 EBADF (%m)\n",
	       sizeof(*unaligned_dm_arg), min_sizeof_dm_ioctl
# if VERBOSE
	       , (char *) unaligned_dm_arg + min_sizeof_dm_ioctl
# endif /* VERBOSE */
	       );

	/* Incorrect data_start data */
	init_s(&s.ioc, sizeof(s), offsetof(struct s, u));
	s.ioc.data_start = 0xdeadbeef;
	ioctl(-1, DM_DEV_RENAME, &s);
	printf("ioctl(-1, DM_DEV_RENAME, "
	       "{version=4.1.2, data_size=%u, data_start=3735928559, "
	       "dev=makedev(18, 52), name=\"nnn\", uuid=\"uuu\", event_nr=0, "
	       "flags=0, "
# if VERBOSE
	       "??? /* misplaced string */"
# else /* !VERBOSE */
	       "..."
# endif /* VERBOSE */
	       "}) = -1 EBADF (%m)\n",
	       s.ioc.data_size);

	/* Strange but still valid data_start */
	init_s(&s.ioc, sizeof(s), offsetof(struct s, u));
	/* Curiously, this is a valid structure */
	s.ioc.data_start = offsetof(struct dm_ioctl, name) + 1;
	ioctl(-1, DM_DEV_RENAME, &s);
	printf("ioctl(-1, DM_DEV_RENAME, "
	       "{version=4.1.2, data_size=%u, data_start=%zu, "
	       "dev=makedev(18, 52), name=\"nnn\", uuid=\"uuu\", event_nr=0, "
	       "flags=0, "
# if VERBOSE
	       "string=\"nn\""
# else /* !VERBOSE */
	       "..."
# endif /* VERBOSE */
	       "}) = -1 EBADF (%m)\n",
	       s.ioc.data_size,
	       offsetof(struct dm_ioctl, name) + 1);

	/* Correct data */
	init_s(&s.ioc, sizeof(s), offsetof(struct s, u));
	strcpy(s.u.string, "new long name");
	ioctl(-1, DM_DEV_RENAME, &s);
	printf("ioctl(-1, DM_DEV_RENAME, "
	       "{version=4.1.2, data_size=%u, data_start=%u, "
	       "dev=makedev(18, 52), name=\"nnn\", uuid=\"uuu\", event_nr=0, "
	       "flags=0, "
# if VERBOSE
	       "string=\"new long \"..."
# else /* !VERBOSE */
	       "..."
# endif /* VERBOSE */
	       "}) = -1 EBADF (%m)\n",
	       s.ioc.data_size, s.ioc.data_start);


	/* DM_TABLE_LOAD */
	init_s(&s.ioc, sizeof(s), offsetof(struct s, u));
	s.ioc.target_count = -1U;
	ioctl(-1, DM_TABLE_LOAD, &s);
	printf("ioctl(-1, DM_TABLE_LOAD, "
	       "{version=4.1.2, data_size=%u, data_start=%u, "
	       "dev=makedev(18, 52), name=\"nnn\", uuid=\"uuu\", "
	       "target_count=4294967295, flags=0, "
# if VERBOSE
	       "{sector_start=0, length=0, target_type=\"\", string=\"\"}"
	       ", ??? /* misplaced struct dm_target_spec */"
# else
	       "..."
# endif /* VERBOSE */
	       "}) = -1 EBADF (%m)\n",
	       s.ioc.data_size, s.ioc.data_start);

	puts("+++ exited with 0 +++");
	return 0;
}

#else /* !HAVE_LINUX_DM_IOCTL_H */

SKIP_MAIN_UNDEFINED("HAVE_LINUX_DM_IOCTL_H")

#endif /* HAVE_LINUX_DM_IOCTL_H */