]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - tests/zfs-tests/cmd/libzfs_input_check/libzfs_input_check.c
Vendor import of openzfs master @ 184df27eef0abdc7ab2105b21257f753834b936b
[FreeBSD/FreeBSD.git] / tests / zfs-tests / cmd / libzfs_input_check / libzfs_input_check.c
1 /*
2  * CDDL HEADER START
3  *
4  * This file and its contents are supplied under the terms of the
5  * Common Development and Distribution License ("CDDL"), version 1.0.
6  * You may only use this file in accordance with the terms of version
7  * 1.0 of the CDDL.
8  *
9  * A full copy of the text of the CDDL should have accompanied this
10  * source.  A copy of the CDDL is also available via the Internet at
11  * http://www.illumos.org/license/CDDL.
12  *
13  * CDDL HEADER END
14  */
15
16 /*
17  * Copyright (c) 2018 by Delphix. All rights reserved.
18  */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <strings.h>
24 #include <libzfs_core.h>
25 #include <libzutil.h>
26
27 #include <sys/nvpair.h>
28 #include <sys/zfs_ioctl.h>
29
30 /*
31  * Test the nvpair inputs for the non-legacy zfs ioctl commands.
32  */
33
34 boolean_t unexpected_failures;
35 int zfs_fd;
36 const char *active_test;
37
38 /*
39  * Tracks which zfs_ioc_t commands were tested
40  */
41 boolean_t ioc_tested[ZFS_IOC_LAST - ZFS_IOC_FIRST];
42
43 /*
44  * Legacy ioctls that are skipped (for now)
45  */
46 static unsigned ioc_skip[] = {
47         ZFS_IOC_POOL_CREATE,
48         ZFS_IOC_POOL_DESTROY,
49         ZFS_IOC_POOL_IMPORT,
50         ZFS_IOC_POOL_EXPORT,
51         ZFS_IOC_POOL_CONFIGS,
52         ZFS_IOC_POOL_STATS,
53         ZFS_IOC_POOL_TRYIMPORT,
54         ZFS_IOC_POOL_SCAN,
55         ZFS_IOC_POOL_FREEZE,
56         ZFS_IOC_POOL_UPGRADE,
57         ZFS_IOC_POOL_GET_HISTORY,
58
59         ZFS_IOC_VDEV_ADD,
60         ZFS_IOC_VDEV_REMOVE,
61         ZFS_IOC_VDEV_SET_STATE,
62         ZFS_IOC_VDEV_ATTACH,
63         ZFS_IOC_VDEV_DETACH,
64         ZFS_IOC_VDEV_SETPATH,
65         ZFS_IOC_VDEV_SETFRU,
66
67         ZFS_IOC_OBJSET_STATS,
68         ZFS_IOC_OBJSET_ZPLPROPS,
69         ZFS_IOC_DATASET_LIST_NEXT,
70         ZFS_IOC_SNAPSHOT_LIST_NEXT,
71         ZFS_IOC_SET_PROP,
72         ZFS_IOC_DESTROY,
73         ZFS_IOC_RENAME,
74         ZFS_IOC_RECV,
75         ZFS_IOC_SEND,
76         ZFS_IOC_INJECT_FAULT,
77         ZFS_IOC_CLEAR_FAULT,
78         ZFS_IOC_INJECT_LIST_NEXT,
79         ZFS_IOC_ERROR_LOG,
80         ZFS_IOC_CLEAR,
81         ZFS_IOC_PROMOTE,
82         ZFS_IOC_DSOBJ_TO_DSNAME,
83         ZFS_IOC_OBJ_TO_PATH,
84         ZFS_IOC_POOL_SET_PROPS,
85         ZFS_IOC_POOL_GET_PROPS,
86         ZFS_IOC_SET_FSACL,
87         ZFS_IOC_GET_FSACL,
88         ZFS_IOC_SHARE,
89         ZFS_IOC_INHERIT_PROP,
90         ZFS_IOC_SMB_ACL,
91         ZFS_IOC_USERSPACE_ONE,
92         ZFS_IOC_USERSPACE_MANY,
93         ZFS_IOC_USERSPACE_UPGRADE,
94         ZFS_IOC_OBJSET_RECVD_PROPS,
95         ZFS_IOC_VDEV_SPLIT,
96         ZFS_IOC_NEXT_OBJ,
97         ZFS_IOC_DIFF,
98         ZFS_IOC_TMP_SNAPSHOT,
99         ZFS_IOC_OBJ_TO_STATS,
100         ZFS_IOC_SPACE_WRITTEN,
101         ZFS_IOC_POOL_REGUID,
102         ZFS_IOC_SEND_PROGRESS,
103         ZFS_IOC_EVENTS_NEXT,
104         ZFS_IOC_EVENTS_CLEAR,
105         ZFS_IOC_EVENTS_SEEK,
106         ZFS_IOC_NEXTBOOT,
107         ZFS_IOC_JAIL,
108         ZFS_IOC_UNJAIL,
109 };
110
111
112 #define IOC_INPUT_TEST(ioc, name, req, opt, err)                \
113         IOC_INPUT_TEST_IMPL(ioc, name, req, opt, err, B_FALSE)
114
115 #define IOC_INPUT_TEST_WILD(ioc, name, req, opt, err)           \
116         IOC_INPUT_TEST_IMPL(ioc, name, req, opt, err, B_TRUE)
117
118 #define IOC_INPUT_TEST_IMPL(ioc, name, req, opt, err, wild)     \
119         do {                                                    \
120                 active_test = __func__ + 5;                     \
121                 ioc_tested[ioc - ZFS_IOC_FIRST] = B_TRUE;       \
122                 lzc_ioctl_test(ioc, name, req, opt, err, wild); \
123         } while (0)
124
125 /*
126  * run a zfs ioctl command, verify expected results and log failures
127  */
128 static void
129 lzc_ioctl_run(zfs_ioc_t ioc, const char *name, nvlist_t *innvl, int expected)
130 {
131         zfs_cmd_t zc = {"\0"};
132         char *packed = NULL;
133         const char *variant;
134         size_t size = 0;
135         int error = 0;
136
137         switch (expected) {
138         case ZFS_ERR_IOC_ARG_UNAVAIL:
139                 variant = "unsupported input";
140                 break;
141         case ZFS_ERR_IOC_ARG_REQUIRED:
142                 variant = "missing input";
143                 break;
144         case ZFS_ERR_IOC_ARG_BADTYPE:
145                 variant = "invalid input type";
146                 break;
147         default:
148                 variant = "valid input";
149                 break;
150         }
151
152         packed = fnvlist_pack(innvl, &size);
153         (void) strlcpy(zc.zc_name, name, sizeof (zc.zc_name));
154         zc.zc_name[sizeof (zc.zc_name) - 1] = '\0';
155         zc.zc_nvlist_src = (uint64_t)(uintptr_t)packed;
156         zc.zc_nvlist_src_size = size;
157         zc.zc_nvlist_dst_size = MAX(size * 2, 128 * 1024);
158         zc.zc_nvlist_dst = (uint64_t)(uintptr_t)malloc(zc.zc_nvlist_dst_size);
159
160         if (zfs_ioctl_fd(zfs_fd, ioc, &zc) != 0)
161                 error = errno;
162
163         if (error != expected) {
164                 unexpected_failures = B_TRUE;
165                 (void) fprintf(stderr, "%s: Unexpected result with %s, "
166                     "error %d (expecting %d)\n",
167                     active_test, variant, error, expected);
168         }
169
170         fnvlist_pack_free(packed, size);
171         free((void *)(uintptr_t)zc.zc_nvlist_dst);
172 }
173
174 /*
175  * Test each ioc for the following ioctl input errors:
176  *   ZFS_ERR_IOC_ARG_UNAVAIL    an input argument is not supported by kernel
177  *   ZFS_ERR_IOC_ARG_REQUIRED   a required input argument is missing
178  *   ZFS_ERR_IOC_ARG_BADTYPE    an input argument has an invalid type
179  */
180 static int
181 lzc_ioctl_test(zfs_ioc_t ioc, const char *name, nvlist_t *required,
182     nvlist_t *optional, int expected_error, boolean_t wildcard)
183 {
184         nvlist_t *input = fnvlist_alloc();
185         nvlist_t *future = fnvlist_alloc();
186         int error = 0;
187
188         if (required != NULL) {
189                 for (nvpair_t *pair = nvlist_next_nvpair(required, NULL);
190                     pair != NULL; pair = nvlist_next_nvpair(required, pair)) {
191                         fnvlist_add_nvpair(input, pair);
192                 }
193         }
194         if (optional != NULL) {
195                 for (nvpair_t *pair = nvlist_next_nvpair(optional, NULL);
196                     pair != NULL; pair = nvlist_next_nvpair(optional, pair)) {
197                         fnvlist_add_nvpair(input, pair);
198                 }
199         }
200
201         /*
202          * Generic input run with 'optional' nvlist pair
203          */
204         if (!wildcard)
205                 fnvlist_add_nvlist(input, "optional", future);
206         lzc_ioctl_run(ioc, name, input, expected_error);
207         if (!wildcard)
208                 fnvlist_remove(input, "optional");
209
210         /*
211          * Bogus input value
212          */
213         if (!wildcard) {
214                 fnvlist_add_string(input, "bogus_input", "bogus");
215                 lzc_ioctl_run(ioc, name, input, ZFS_ERR_IOC_ARG_UNAVAIL);
216                 fnvlist_remove(input, "bogus_input");
217         }
218
219         /*
220          * Missing required inputs
221          */
222         if (required != NULL) {
223                 nvlist_t *empty = fnvlist_alloc();
224                 lzc_ioctl_run(ioc, name, empty, ZFS_ERR_IOC_ARG_REQUIRED);
225                 nvlist_free(empty);
226         }
227
228         /*
229          * Wrong nvpair type
230          */
231         if (required != NULL || optional != NULL) {
232                 /*
233                  * switch the type of one of the input pairs
234                  */
235                 for (nvpair_t *pair = nvlist_next_nvpair(input, NULL);
236                     pair != NULL; pair = nvlist_next_nvpair(input, pair)) {
237                         char pname[MAXNAMELEN];
238                         data_type_t ptype;
239
240                         strlcpy(pname, nvpair_name(pair), sizeof (pname));
241                         pname[sizeof (pname) - 1] = '\0';
242                         ptype = nvpair_type(pair);
243                         fnvlist_remove_nvpair(input, pair);
244
245                         switch (ptype) {
246                         case DATA_TYPE_STRING:
247                                 fnvlist_add_uint64(input, pname, 42);
248                                 break;
249                         default:
250                                 fnvlist_add_string(input, pname, "bogus");
251                                 break;
252                         }
253                 }
254                 lzc_ioctl_run(ioc, name, input, ZFS_ERR_IOC_ARG_BADTYPE);
255         }
256
257         nvlist_free(future);
258         nvlist_free(input);
259
260         return (error);
261 }
262
263 static void
264 test_pool_sync(const char *pool)
265 {
266         nvlist_t *required = fnvlist_alloc();
267
268         fnvlist_add_boolean_value(required, "force", B_TRUE);
269
270         IOC_INPUT_TEST(ZFS_IOC_POOL_SYNC, pool, required, NULL, 0);
271
272         nvlist_free(required);
273 }
274
275 static void
276 test_pool_reopen(const char *pool)
277 {
278         nvlist_t *optional = fnvlist_alloc();
279
280         fnvlist_add_boolean_value(optional, "scrub_restart", B_FALSE);
281
282         IOC_INPUT_TEST(ZFS_IOC_POOL_REOPEN, pool, NULL, optional, 0);
283
284         nvlist_free(optional);
285 }
286
287 static void
288 test_pool_checkpoint(const char *pool)
289 {
290         IOC_INPUT_TEST(ZFS_IOC_POOL_CHECKPOINT, pool, NULL, NULL, 0);
291 }
292
293 static void
294 test_pool_discard_checkpoint(const char *pool)
295 {
296         int err = lzc_pool_checkpoint(pool);
297         if (err == 0 || err == ZFS_ERR_CHECKPOINT_EXISTS)
298                 IOC_INPUT_TEST(ZFS_IOC_POOL_DISCARD_CHECKPOINT, pool, NULL,
299                     NULL, 0);
300 }
301
302 static void
303 test_log_history(const char *pool)
304 {
305         nvlist_t *required = fnvlist_alloc();
306
307         fnvlist_add_string(required, "message", "input check");
308
309         IOC_INPUT_TEST(ZFS_IOC_LOG_HISTORY, pool, required, NULL, 0);
310
311         nvlist_free(required);
312 }
313
314 static void
315 test_create(const char *pool)
316 {
317         char dataset[MAXNAMELEN + 32];
318
319         (void) snprintf(dataset, sizeof (dataset), "%s/create-fs", pool);
320
321         nvlist_t *required = fnvlist_alloc();
322         nvlist_t *optional = fnvlist_alloc();
323         nvlist_t *props = fnvlist_alloc();
324
325         fnvlist_add_int32(required, "type", DMU_OST_ZFS);
326         fnvlist_add_uint64(props, "recordsize", 8192);
327         fnvlist_add_nvlist(optional, "props", props);
328
329         IOC_INPUT_TEST(ZFS_IOC_CREATE, dataset, required, optional, 0);
330
331         nvlist_free(required);
332         nvlist_free(optional);
333 }
334
335 static void
336 test_snapshot(const char *pool, const char *snapshot)
337 {
338         nvlist_t *required = fnvlist_alloc();
339         nvlist_t *optional = fnvlist_alloc();
340         nvlist_t *snaps = fnvlist_alloc();
341         nvlist_t *props = fnvlist_alloc();
342
343         fnvlist_add_boolean(snaps, snapshot);
344         fnvlist_add_nvlist(required, "snaps", snaps);
345
346         fnvlist_add_string(props, "org.openzfs:launch", "September 17th, 2013");
347         fnvlist_add_nvlist(optional, "props", props);
348
349         IOC_INPUT_TEST(ZFS_IOC_SNAPSHOT, pool, required, optional, 0);
350
351         nvlist_free(props);
352         nvlist_free(snaps);
353         nvlist_free(optional);
354         nvlist_free(required);
355 }
356
357 static void
358 test_space_snaps(const char *snapshot)
359 {
360         nvlist_t *required = fnvlist_alloc();
361         fnvlist_add_string(required, "firstsnap", snapshot);
362
363         IOC_INPUT_TEST(ZFS_IOC_SPACE_SNAPS, snapshot, required, NULL, 0);
364
365         nvlist_free(required);
366 }
367
368 static void
369 test_destroy_snaps(const char *pool, const char *snapshot)
370 {
371         nvlist_t *required = fnvlist_alloc();
372         nvlist_t *snaps = fnvlist_alloc();
373
374         fnvlist_add_boolean(snaps, snapshot);
375         fnvlist_add_nvlist(required, "snaps", snaps);
376
377         IOC_INPUT_TEST(ZFS_IOC_DESTROY_SNAPS, pool, required, NULL, 0);
378
379         nvlist_free(snaps);
380         nvlist_free(required);
381 }
382
383
384 static void
385 test_bookmark(const char *pool, const char *snapshot, const char *bookmark)
386 {
387         nvlist_t *required = fnvlist_alloc();
388
389         fnvlist_add_string(required, bookmark, snapshot);
390
391         IOC_INPUT_TEST_WILD(ZFS_IOC_BOOKMARK, pool, required, NULL, 0);
392
393         nvlist_free(required);
394 }
395
396 static void
397 test_get_bookmarks(const char *dataset)
398 {
399         nvlist_t *optional = fnvlist_alloc();
400
401         fnvlist_add_boolean(optional, "guid");
402         fnvlist_add_boolean(optional, "createtxg");
403         fnvlist_add_boolean(optional, "creation");
404
405         IOC_INPUT_TEST_WILD(ZFS_IOC_GET_BOOKMARKS, dataset, NULL, optional, 0);
406
407         nvlist_free(optional);
408 }
409
410 static void
411 test_destroy_bookmarks(const char *pool, const char *bookmark)
412 {
413         nvlist_t *required = fnvlist_alloc();
414
415         fnvlist_add_boolean(required, bookmark);
416
417         IOC_INPUT_TEST_WILD(ZFS_IOC_DESTROY_BOOKMARKS, pool, required, NULL, 0);
418
419         nvlist_free(required);
420 }
421
422 static void
423 test_clone(const char *snapshot, const char *clone)
424 {
425         nvlist_t *required = fnvlist_alloc();
426         nvlist_t *optional = fnvlist_alloc();
427         nvlist_t *props = fnvlist_alloc();
428
429         fnvlist_add_string(required, "origin", snapshot);
430
431         IOC_INPUT_TEST(ZFS_IOC_CLONE, clone, required, NULL, 0);
432
433         nvlist_free(props);
434         nvlist_free(optional);
435         nvlist_free(required);
436 }
437
438 static void
439 test_rollback(const char *dataset, const char *snapshot)
440 {
441         nvlist_t *optional = fnvlist_alloc();
442
443         fnvlist_add_string(optional, "target", snapshot);
444
445         IOC_INPUT_TEST(ZFS_IOC_ROLLBACK, dataset, NULL, optional, B_FALSE);
446
447         nvlist_free(optional);
448 }
449
450 static void
451 test_hold(const char *pool, const char *snapshot)
452 {
453         nvlist_t *required = fnvlist_alloc();
454         nvlist_t *optional = fnvlist_alloc();
455         nvlist_t *holds = fnvlist_alloc();
456
457         fnvlist_add_string(holds, snapshot, "libzfs_check_hold");
458         fnvlist_add_nvlist(required, "holds", holds);
459         fnvlist_add_int32(optional, "cleanup_fd", zfs_fd);
460
461         IOC_INPUT_TEST(ZFS_IOC_HOLD, pool, required, optional, 0);
462
463         nvlist_free(holds);
464         nvlist_free(optional);
465         nvlist_free(required);
466 }
467
468 static void
469 test_get_holds(const char *snapshot)
470 {
471         IOC_INPUT_TEST(ZFS_IOC_GET_HOLDS, snapshot, NULL, NULL, 0);
472 }
473
474 static void
475 test_release(const char *pool, const char *snapshot)
476 {
477         nvlist_t *required = fnvlist_alloc();
478         nvlist_t *release = fnvlist_alloc();
479
480         fnvlist_add_boolean(release, "libzfs_check_hold");
481         fnvlist_add_nvlist(required, snapshot, release);
482
483         IOC_INPUT_TEST_WILD(ZFS_IOC_RELEASE, pool, required, NULL, 0);
484
485         nvlist_free(release);
486         nvlist_free(required);
487 }
488
489
490 static void
491 test_send_new(const char *snapshot, int fd)
492 {
493         nvlist_t *required = fnvlist_alloc();
494         nvlist_t *optional = fnvlist_alloc();
495
496         fnvlist_add_int32(required, "fd", fd);
497
498         fnvlist_add_boolean(optional, "largeblockok");
499         fnvlist_add_boolean(optional, "embedok");
500         fnvlist_add_boolean(optional, "compressok");
501         fnvlist_add_boolean(optional, "rawok");
502
503         /*
504          * TODO - Resumable send is harder to set up. So we currently
505          * ignore testing for that variant.
506          */
507 #if 0
508         fnvlist_add_string(optional, "fromsnap", from);
509         fnvlist_add_uint64(optional, "resume_object", resumeobj);
510         fnvlist_add_uint64(optional, "resume_offset", offset);
511         fnvlist_add_boolean(optional, "savedok");
512 #endif
513         IOC_INPUT_TEST(ZFS_IOC_SEND_NEW, snapshot, required, optional, 0);
514
515         nvlist_free(optional);
516         nvlist_free(required);
517 }
518
519 static void
520 test_recv_new(const char *dataset, int fd)
521 {
522         dmu_replay_record_t drr = { 0 };
523         nvlist_t *required = fnvlist_alloc();
524         nvlist_t *optional = fnvlist_alloc();
525         nvlist_t *props = fnvlist_alloc();
526         char snapshot[MAXNAMELEN + 32];
527         ssize_t count;
528
529         int cleanup_fd = open(ZFS_DEV, O_RDWR);
530
531         (void) snprintf(snapshot, sizeof (snapshot), "%s@replicant", dataset);
532
533         count = pread(fd, &drr, sizeof (drr), 0);
534         if (count != sizeof (drr)) {
535                 (void) fprintf(stderr, "could not read stream: %s\n",
536                     strerror(errno));
537         }
538
539         fnvlist_add_string(required, "snapname", snapshot);
540         fnvlist_add_byte_array(required, "begin_record", (uchar_t *)&drr,
541             sizeof (drr));
542         fnvlist_add_int32(required, "input_fd", fd);
543
544         fnvlist_add_string(props, "org.openzfs:launch", "September 17th, 2013");
545         fnvlist_add_nvlist(optional, "localprops", props);
546         fnvlist_add_boolean(optional, "force");
547         fnvlist_add_int32(optional, "cleanup_fd", cleanup_fd);
548
549         /*
550          * TODO - Resumable receive is harder to set up. So we currently
551          * ignore testing for one.
552          */
553 #if 0
554         fnvlist_add_nvlist(optional, "props", recvdprops);
555         fnvlist_add_string(optional, "origin", origin);
556         fnvlist_add_boolean(optional, "resumable");
557         fnvlist_add_uint64(optional, "action_handle", *action_handle);
558 #endif
559         IOC_INPUT_TEST(ZFS_IOC_RECV_NEW, dataset, required, optional,
560             ZFS_ERR_STREAM_TRUNCATED);
561
562         nvlist_free(props);
563         nvlist_free(optional);
564         nvlist_free(required);
565
566         (void) close(cleanup_fd);
567 }
568
569 static void
570 test_send_space(const char *snapshot1, const char *snapshot2)
571 {
572         nvlist_t *optional = fnvlist_alloc();
573
574         fnvlist_add_string(optional, "from", snapshot1);
575         fnvlist_add_boolean(optional, "largeblockok");
576         fnvlist_add_boolean(optional, "embedok");
577         fnvlist_add_boolean(optional, "compressok");
578         fnvlist_add_boolean(optional, "rawok");
579
580         IOC_INPUT_TEST(ZFS_IOC_SEND_SPACE, snapshot2, NULL, optional, 0);
581
582         nvlist_free(optional);
583 }
584
585 static void
586 test_remap(const char *dataset)
587 {
588         IOC_INPUT_TEST(ZFS_IOC_REMAP, dataset, NULL, NULL, 0);
589 }
590
591 static void
592 test_channel_program(const char *pool)
593 {
594         const char *program =
595             "arg = ...\n"
596             "argv = arg[\"argv\"]\n"
597             "return argv[1]";
598         char *const argv[1] = { "Hello World!" };
599         nvlist_t *required = fnvlist_alloc();
600         nvlist_t *optional = fnvlist_alloc();
601         nvlist_t *args = fnvlist_alloc();
602
603         fnvlist_add_string(required, "program", program);
604         fnvlist_add_string_array(args, "argv", argv, 1);
605         fnvlist_add_nvlist(required, "arg", args);
606
607         fnvlist_add_boolean_value(optional, "sync", B_TRUE);
608         fnvlist_add_uint64(optional, "instrlimit", 1000 * 1000);
609         fnvlist_add_uint64(optional, "memlimit", 8192 * 1024);
610
611         IOC_INPUT_TEST(ZFS_IOC_CHANNEL_PROGRAM, pool, required, optional, 0);
612
613         nvlist_free(args);
614         nvlist_free(optional);
615         nvlist_free(required);
616 }
617
618 #define WRAPPING_KEY_LEN        32
619
620 static void
621 test_load_key(const char *dataset)
622 {
623         nvlist_t *required = fnvlist_alloc();
624         nvlist_t *optional = fnvlist_alloc();
625         nvlist_t *hidden = fnvlist_alloc();
626         uint8_t keydata[WRAPPING_KEY_LEN] = {0};
627
628         fnvlist_add_uint8_array(hidden, "wkeydata", keydata, sizeof (keydata));
629         fnvlist_add_nvlist(required, "hidden_args", hidden);
630         fnvlist_add_boolean(optional, "noop");
631
632         IOC_INPUT_TEST(ZFS_IOC_LOAD_KEY, dataset, required, optional, EINVAL);
633         nvlist_free(hidden);
634         nvlist_free(optional);
635         nvlist_free(required);
636 }
637
638 static void
639 test_change_key(const char *dataset)
640 {
641         IOC_INPUT_TEST(ZFS_IOC_CHANGE_KEY, dataset, NULL, NULL, EINVAL);
642 }
643
644 static void
645 test_unload_key(const char *dataset)
646 {
647         IOC_INPUT_TEST(ZFS_IOC_UNLOAD_KEY, dataset, NULL, NULL, EACCES);
648 }
649
650 static void
651 test_vdev_initialize(const char *pool)
652 {
653         nvlist_t *required = fnvlist_alloc();
654         nvlist_t *vdev_guids = fnvlist_alloc();
655
656         fnvlist_add_uint64(vdev_guids, "path", 0xdeadbeefdeadbeef);
657         fnvlist_add_uint64(required, ZPOOL_INITIALIZE_COMMAND,
658             POOL_INITIALIZE_START);
659         fnvlist_add_nvlist(required, ZPOOL_INITIALIZE_VDEVS, vdev_guids);
660
661         IOC_INPUT_TEST(ZFS_IOC_POOL_INITIALIZE, pool, required, NULL, EINVAL);
662         nvlist_free(vdev_guids);
663         nvlist_free(required);
664 }
665
666 static void
667 test_vdev_trim(const char *pool)
668 {
669         nvlist_t *required = fnvlist_alloc();
670         nvlist_t *optional = fnvlist_alloc();
671         nvlist_t *vdev_guids = fnvlist_alloc();
672
673         fnvlist_add_uint64(vdev_guids, "path", 0xdeadbeefdeadbeef);
674         fnvlist_add_uint64(required, ZPOOL_TRIM_COMMAND, POOL_TRIM_START);
675         fnvlist_add_nvlist(required, ZPOOL_TRIM_VDEVS, vdev_guids);
676         fnvlist_add_uint64(optional, ZPOOL_TRIM_RATE, 1ULL << 30);
677         fnvlist_add_boolean_value(optional, ZPOOL_TRIM_SECURE, B_TRUE);
678
679         IOC_INPUT_TEST(ZFS_IOC_POOL_TRIM, pool, required, optional, EINVAL);
680         nvlist_free(vdev_guids);
681         nvlist_free(optional);
682         nvlist_free(required);
683 }
684
685 static int
686 zfs_destroy(const char *dataset)
687 {
688         zfs_cmd_t zc = {"\0"};
689         int err;
690
691         (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
692         zc.zc_name[sizeof (zc.zc_name) - 1] = '\0';
693         err = zfs_ioctl_fd(zfs_fd, ZFS_IOC_DESTROY, &zc);
694
695         return (err == 0 ? 0 : errno);
696 }
697
698 static void
699 test_redact(const char *snapshot1, const char *snapshot2)
700 {
701         nvlist_t *required = fnvlist_alloc();
702         nvlist_t *snapnv = fnvlist_alloc();
703         char bookmark[MAXNAMELEN + 32];
704
705         fnvlist_add_string(required, "bookname", "testbookmark");
706         fnvlist_add_boolean(snapnv, snapshot2);
707         fnvlist_add_nvlist(required, "snapnv", snapnv);
708
709         IOC_INPUT_TEST(ZFS_IOC_REDACT, snapshot1, required, NULL, 0);
710
711         nvlist_free(snapnv);
712         nvlist_free(required);
713
714         strlcpy(bookmark, snapshot1, sizeof (bookmark));
715         *strchr(bookmark, '@') = '\0';
716         strlcat(bookmark, "#testbookmark", sizeof (bookmark) -
717             strlen(bookmark));
718         zfs_destroy(bookmark);
719 }
720
721 static void
722 test_get_bookmark_props(const char *bookmark)
723 {
724         IOC_INPUT_TEST(ZFS_IOC_GET_BOOKMARK_PROPS, bookmark, NULL, NULL, 0);
725 }
726
727 static void
728 test_wait(const char *pool)
729 {
730         nvlist_t *required = fnvlist_alloc();
731         nvlist_t *optional = fnvlist_alloc();
732
733         fnvlist_add_int32(required, "wait_activity", 2);
734         fnvlist_add_uint64(optional, "wait_tag", 0xdeadbeefdeadbeef);
735
736         IOC_INPUT_TEST(ZFS_IOC_WAIT, pool, required, optional, EINVAL);
737
738         nvlist_free(required);
739         nvlist_free(optional);
740 }
741
742 static void
743 test_wait_fs(const char *dataset)
744 {
745         nvlist_t *required = fnvlist_alloc();
746
747         fnvlist_add_int32(required, "wait_activity", 2);
748
749         IOC_INPUT_TEST(ZFS_IOC_WAIT_FS, dataset, required, NULL, EINVAL);
750
751         nvlist_free(required);
752 }
753
754 static void
755 test_get_bootenv(const char *pool)
756 {
757         IOC_INPUT_TEST(ZFS_IOC_GET_BOOTENV, pool, NULL, NULL, 0);
758 }
759
760 static void
761 test_set_bootenv(const char *pool)
762 {
763         nvlist_t *required = fnvlist_alloc();
764
765         fnvlist_add_string(required, "envmap", "test");
766
767         IOC_INPUT_TEST(ZFS_IOC_SET_BOOTENV, pool, required, NULL, 0);
768
769         nvlist_free(required);
770 }
771
772 static void
773 zfs_ioc_input_tests(const char *pool)
774 {
775         char filepath[] = "/tmp/ioc_test_file_XXXXXX";
776         char dataset[ZFS_MAX_DATASET_NAME_LEN];
777         char snapbase[ZFS_MAX_DATASET_NAME_LEN + 32];
778         char snapshot[ZFS_MAX_DATASET_NAME_LEN + 32];
779         char bookmark[ZFS_MAX_DATASET_NAME_LEN + 32];
780         char backup[ZFS_MAX_DATASET_NAME_LEN];
781         char clone[ZFS_MAX_DATASET_NAME_LEN];
782         char clonesnap[ZFS_MAX_DATASET_NAME_LEN + 32];
783         int tmpfd, err;
784
785         /*
786          * Setup names and create a working dataset
787          */
788         (void) snprintf(dataset, sizeof (dataset), "%s/test-fs", pool);
789         (void) snprintf(snapbase, sizeof (snapbase), "%s@snapbase", dataset);
790         (void) snprintf(snapshot, sizeof (snapshot), "%s@snapshot", dataset);
791         (void) snprintf(bookmark, sizeof (bookmark), "%s#bookmark", dataset);
792         (void) snprintf(clone, sizeof (clone), "%s/test-fs-clone", pool);
793         (void) snprintf(clonesnap, sizeof (clonesnap), "%s@snap", clone);
794         (void) snprintf(backup, sizeof (backup), "%s/backup", pool);
795
796         err = lzc_create(dataset, DMU_OST_ZFS, NULL, NULL, 0);
797         if (err) {
798                 (void) fprintf(stderr, "could not create '%s': %s\n",
799                     dataset, strerror(errno));
800                 exit(2);
801         }
802
803         tmpfd = mkstemp(filepath);
804         if (tmpfd < 0) {
805                 (void) fprintf(stderr, "could not create '%s': %s\n",
806                     filepath, strerror(errno));
807                 exit(2);
808         }
809
810         /*
811          * run a test for each ioctl
812          * Note that some test build on previous test operations
813          */
814         test_pool_sync(pool);
815         test_pool_reopen(pool);
816         test_pool_checkpoint(pool);
817         test_pool_discard_checkpoint(pool);
818         test_log_history(pool);
819
820         test_create(dataset);
821         test_snapshot(pool, snapbase);
822         test_snapshot(pool, snapshot);
823
824         test_space_snaps(snapshot);
825         test_send_space(snapbase, snapshot);
826         test_send_new(snapshot, tmpfd);
827         test_recv_new(backup, tmpfd);
828
829         test_bookmark(pool, snapshot, bookmark);
830         test_get_bookmarks(dataset);
831         test_get_bookmark_props(bookmark);
832         test_destroy_bookmarks(pool, bookmark);
833
834         test_hold(pool, snapshot);
835         test_get_holds(snapshot);
836         test_release(pool, snapshot);
837
838         test_clone(snapshot, clone);
839         test_snapshot(pool, clonesnap);
840         test_redact(snapshot, clonesnap);
841         zfs_destroy(clonesnap);
842         zfs_destroy(clone);
843
844         test_rollback(dataset, snapshot);
845         test_destroy_snaps(pool, snapshot);
846         test_destroy_snaps(pool, snapbase);
847
848         test_remap(dataset);
849         test_channel_program(pool);
850
851         test_load_key(dataset);
852         test_change_key(dataset);
853         test_unload_key(dataset);
854
855         test_vdev_initialize(pool);
856         test_vdev_trim(pool);
857
858         test_wait(pool);
859         test_wait_fs(dataset);
860
861         test_set_bootenv(pool);
862         test_get_bootenv(pool);
863
864         /*
865          * cleanup
866          */
867         zfs_cmd_t zc = {"\0"};
868
869         nvlist_t *snaps = fnvlist_alloc();
870         fnvlist_add_boolean(snaps, snapshot);
871         (void) lzc_destroy_snaps(snaps, B_FALSE, NULL);
872         nvlist_free(snaps);
873
874         (void) zfs_destroy(dataset);
875         (void) zfs_destroy(backup);
876
877         (void) close(tmpfd);
878         (void) unlink(filepath);
879
880         /*
881          * All the unused slots should yield ZFS_ERR_IOC_CMD_UNAVAIL
882          */
883         for (int i = 0; i < ARRAY_SIZE(ioc_skip); i++) {
884                 if (ioc_tested[ioc_skip[i] - ZFS_IOC_FIRST])
885                         (void) fprintf(stderr, "cmd %d tested, not skipped!\n",
886                             (int)(ioc_skip[i] - ZFS_IOC_FIRST));
887
888                 ioc_tested[ioc_skip[i] - ZFS_IOC_FIRST] = B_TRUE;
889         }
890
891         (void) strlcpy(zc.zc_name, pool, sizeof (zc.zc_name));
892         zc.zc_name[sizeof (zc.zc_name) - 1] = '\0';
893
894         for (unsigned ioc = ZFS_IOC_FIRST; ioc < ZFS_IOC_LAST; ioc++) {
895                 unsigned cmd = ioc - ZFS_IOC_FIRST;
896
897                 if (ioc_tested[cmd])
898                         continue;
899
900                 if (zfs_ioctl_fd(zfs_fd, ioc, &zc) != 0 &&
901                     errno != ZFS_ERR_IOC_CMD_UNAVAIL) {
902                         (void) fprintf(stderr, "cmd %d is missing a test case "
903                             "(%d)\n", cmd, errno);
904                 }
905         }
906 }
907
908 enum zfs_ioc_ref {
909 #ifdef __FreeBSD__
910         ZFS_IOC_BASE = 0,
911 #else
912         ZFS_IOC_BASE = ('Z' << 8),
913 #endif
914         ZFS_IOC_PLATFORM_BASE = ZFS_IOC_BASE + 0x80,
915 };
916
917 /*
918  * Canonical reference check of /dev/zfs ioctl numbers.
919  * These cannot change and new ioctl numbers must be appended.
920  */
921 static boolean_t
922 validate_ioc_values(void)
923 {
924         boolean_t result = B_TRUE;
925
926 #define CHECK(expr) do { \
927         if (!(expr)) { \
928                 result = B_FALSE; \
929                 fprintf(stderr, "(%s) === FALSE\n", #expr); \
930         } \
931 } while (0)
932
933         CHECK(ZFS_IOC_BASE + 0 == ZFS_IOC_POOL_CREATE);
934         CHECK(ZFS_IOC_BASE + 1 == ZFS_IOC_POOL_DESTROY);
935         CHECK(ZFS_IOC_BASE + 2 == ZFS_IOC_POOL_IMPORT);
936         CHECK(ZFS_IOC_BASE + 3 == ZFS_IOC_POOL_EXPORT);
937         CHECK(ZFS_IOC_BASE + 4 == ZFS_IOC_POOL_CONFIGS);
938         CHECK(ZFS_IOC_BASE + 5 == ZFS_IOC_POOL_STATS);
939         CHECK(ZFS_IOC_BASE + 6 == ZFS_IOC_POOL_TRYIMPORT);
940         CHECK(ZFS_IOC_BASE + 7 == ZFS_IOC_POOL_SCAN);
941         CHECK(ZFS_IOC_BASE + 8 == ZFS_IOC_POOL_FREEZE);
942         CHECK(ZFS_IOC_BASE + 9 == ZFS_IOC_POOL_UPGRADE);
943         CHECK(ZFS_IOC_BASE + 10 == ZFS_IOC_POOL_GET_HISTORY);
944         CHECK(ZFS_IOC_BASE + 11 == ZFS_IOC_VDEV_ADD);
945         CHECK(ZFS_IOC_BASE + 12 == ZFS_IOC_VDEV_REMOVE);
946         CHECK(ZFS_IOC_BASE + 13 == ZFS_IOC_VDEV_SET_STATE);
947         CHECK(ZFS_IOC_BASE + 14 == ZFS_IOC_VDEV_ATTACH);
948         CHECK(ZFS_IOC_BASE + 15 == ZFS_IOC_VDEV_DETACH);
949         CHECK(ZFS_IOC_BASE + 16 == ZFS_IOC_VDEV_SETPATH);
950         CHECK(ZFS_IOC_BASE + 17 == ZFS_IOC_VDEV_SETFRU);
951         CHECK(ZFS_IOC_BASE + 18 == ZFS_IOC_OBJSET_STATS);
952         CHECK(ZFS_IOC_BASE + 19 == ZFS_IOC_OBJSET_ZPLPROPS);
953         CHECK(ZFS_IOC_BASE + 20 == ZFS_IOC_DATASET_LIST_NEXT);
954         CHECK(ZFS_IOC_BASE + 21 == ZFS_IOC_SNAPSHOT_LIST_NEXT);
955         CHECK(ZFS_IOC_BASE + 22 == ZFS_IOC_SET_PROP);
956         CHECK(ZFS_IOC_BASE + 23 == ZFS_IOC_CREATE);
957         CHECK(ZFS_IOC_BASE + 24 == ZFS_IOC_DESTROY);
958         CHECK(ZFS_IOC_BASE + 25 == ZFS_IOC_ROLLBACK);
959         CHECK(ZFS_IOC_BASE + 26 == ZFS_IOC_RENAME);
960         CHECK(ZFS_IOC_BASE + 27 == ZFS_IOC_RECV);
961         CHECK(ZFS_IOC_BASE + 28 == ZFS_IOC_SEND);
962         CHECK(ZFS_IOC_BASE + 29 == ZFS_IOC_INJECT_FAULT);
963         CHECK(ZFS_IOC_BASE + 30 == ZFS_IOC_CLEAR_FAULT);
964         CHECK(ZFS_IOC_BASE + 31 == ZFS_IOC_INJECT_LIST_NEXT);
965         CHECK(ZFS_IOC_BASE + 32 == ZFS_IOC_ERROR_LOG);
966         CHECK(ZFS_IOC_BASE + 33 == ZFS_IOC_CLEAR);
967         CHECK(ZFS_IOC_BASE + 34 == ZFS_IOC_PROMOTE);
968         CHECK(ZFS_IOC_BASE + 35 == ZFS_IOC_SNAPSHOT);
969         CHECK(ZFS_IOC_BASE + 36 == ZFS_IOC_DSOBJ_TO_DSNAME);
970         CHECK(ZFS_IOC_BASE + 37 == ZFS_IOC_OBJ_TO_PATH);
971         CHECK(ZFS_IOC_BASE + 38 == ZFS_IOC_POOL_SET_PROPS);
972         CHECK(ZFS_IOC_BASE + 39 == ZFS_IOC_POOL_GET_PROPS);
973         CHECK(ZFS_IOC_BASE + 40 == ZFS_IOC_SET_FSACL);
974         CHECK(ZFS_IOC_BASE + 41 == ZFS_IOC_GET_FSACL);
975         CHECK(ZFS_IOC_BASE + 42 == ZFS_IOC_SHARE);
976         CHECK(ZFS_IOC_BASE + 43 == ZFS_IOC_INHERIT_PROP);
977         CHECK(ZFS_IOC_BASE + 44 == ZFS_IOC_SMB_ACL);
978         CHECK(ZFS_IOC_BASE + 45 == ZFS_IOC_USERSPACE_ONE);
979         CHECK(ZFS_IOC_BASE + 46 == ZFS_IOC_USERSPACE_MANY);
980         CHECK(ZFS_IOC_BASE + 47 == ZFS_IOC_USERSPACE_UPGRADE);
981         CHECK(ZFS_IOC_BASE + 48 == ZFS_IOC_HOLD);
982         CHECK(ZFS_IOC_BASE + 49 == ZFS_IOC_RELEASE);
983         CHECK(ZFS_IOC_BASE + 50 == ZFS_IOC_GET_HOLDS);
984         CHECK(ZFS_IOC_BASE + 51 == ZFS_IOC_OBJSET_RECVD_PROPS);
985         CHECK(ZFS_IOC_BASE + 52 == ZFS_IOC_VDEV_SPLIT);
986         CHECK(ZFS_IOC_BASE + 53 == ZFS_IOC_NEXT_OBJ);
987         CHECK(ZFS_IOC_BASE + 54 == ZFS_IOC_DIFF);
988         CHECK(ZFS_IOC_BASE + 55 == ZFS_IOC_TMP_SNAPSHOT);
989         CHECK(ZFS_IOC_BASE + 56 == ZFS_IOC_OBJ_TO_STATS);
990         CHECK(ZFS_IOC_BASE + 57 == ZFS_IOC_SPACE_WRITTEN);
991         CHECK(ZFS_IOC_BASE + 58 == ZFS_IOC_SPACE_SNAPS);
992         CHECK(ZFS_IOC_BASE + 59 == ZFS_IOC_DESTROY_SNAPS);
993         CHECK(ZFS_IOC_BASE + 60 == ZFS_IOC_POOL_REGUID);
994         CHECK(ZFS_IOC_BASE + 61 == ZFS_IOC_POOL_REOPEN);
995         CHECK(ZFS_IOC_BASE + 62 == ZFS_IOC_SEND_PROGRESS);
996         CHECK(ZFS_IOC_BASE + 63 == ZFS_IOC_LOG_HISTORY);
997         CHECK(ZFS_IOC_BASE + 64 == ZFS_IOC_SEND_NEW);
998         CHECK(ZFS_IOC_BASE + 65 == ZFS_IOC_SEND_SPACE);
999         CHECK(ZFS_IOC_BASE + 66 == ZFS_IOC_CLONE);
1000         CHECK(ZFS_IOC_BASE + 67 == ZFS_IOC_BOOKMARK);
1001         CHECK(ZFS_IOC_BASE + 68 == ZFS_IOC_GET_BOOKMARKS);
1002         CHECK(ZFS_IOC_BASE + 69 == ZFS_IOC_DESTROY_BOOKMARKS);
1003         CHECK(ZFS_IOC_BASE + 70 == ZFS_IOC_RECV_NEW);
1004         CHECK(ZFS_IOC_BASE + 71 == ZFS_IOC_POOL_SYNC);
1005         CHECK(ZFS_IOC_BASE + 72 == ZFS_IOC_CHANNEL_PROGRAM);
1006         CHECK(ZFS_IOC_BASE + 73 == ZFS_IOC_LOAD_KEY);
1007         CHECK(ZFS_IOC_BASE + 74 == ZFS_IOC_UNLOAD_KEY);
1008         CHECK(ZFS_IOC_BASE + 75 == ZFS_IOC_CHANGE_KEY);
1009         CHECK(ZFS_IOC_BASE + 76 == ZFS_IOC_REMAP);
1010         CHECK(ZFS_IOC_BASE + 77 == ZFS_IOC_POOL_CHECKPOINT);
1011         CHECK(ZFS_IOC_BASE + 78 == ZFS_IOC_POOL_DISCARD_CHECKPOINT);
1012         CHECK(ZFS_IOC_BASE + 79 == ZFS_IOC_POOL_INITIALIZE);
1013         CHECK(ZFS_IOC_BASE + 80 == ZFS_IOC_POOL_TRIM);
1014         CHECK(ZFS_IOC_BASE + 81 == ZFS_IOC_REDACT);
1015         CHECK(ZFS_IOC_BASE + 82 == ZFS_IOC_GET_BOOKMARK_PROPS);
1016         CHECK(ZFS_IOC_BASE + 83 == ZFS_IOC_WAIT);
1017         CHECK(ZFS_IOC_BASE + 84 == ZFS_IOC_WAIT_FS);
1018         CHECK(ZFS_IOC_PLATFORM_BASE + 1 == ZFS_IOC_EVENTS_NEXT);
1019         CHECK(ZFS_IOC_PLATFORM_BASE + 2 == ZFS_IOC_EVENTS_CLEAR);
1020         CHECK(ZFS_IOC_PLATFORM_BASE + 3 == ZFS_IOC_EVENTS_SEEK);
1021         CHECK(ZFS_IOC_PLATFORM_BASE + 4 == ZFS_IOC_NEXTBOOT);
1022         CHECK(ZFS_IOC_PLATFORM_BASE + 5 == ZFS_IOC_JAIL);
1023         CHECK(ZFS_IOC_PLATFORM_BASE + 6 == ZFS_IOC_UNJAIL);
1024         CHECK(ZFS_IOC_PLATFORM_BASE + 7 == ZFS_IOC_SET_BOOTENV);
1025         CHECK(ZFS_IOC_PLATFORM_BASE + 8 == ZFS_IOC_GET_BOOTENV);
1026
1027 #undef CHECK
1028
1029         return (result);
1030 }
1031
1032 int
1033 main(int argc, const char *argv[])
1034 {
1035         if (argc != 2) {
1036                 (void) fprintf(stderr, "usage: %s <pool>\n", argv[0]);
1037                 exit(2);
1038         }
1039
1040         if (!validate_ioc_values()) {
1041                 (void) fprintf(stderr, "WARNING: zfs_ioc_t has binary "
1042                     "incompatible command values\n");
1043                 exit(3);
1044         }
1045
1046         (void) libzfs_core_init();
1047         zfs_fd = open(ZFS_DEV, O_RDWR);
1048         if (zfs_fd < 0) {
1049                 (void) fprintf(stderr, "open: %s\n", strerror(errno));
1050                 libzfs_core_fini();
1051                 exit(2);
1052         }
1053
1054         zfs_ioc_input_tests(argv[1]);
1055
1056         (void) close(zfs_fd);
1057         libzfs_core_fini();
1058
1059         return (unexpected_failures);
1060 }