xref: /illumos-gate/usr/src/test/nvme-tests/tests/libnvme/device-empty.c (revision 489f6310fe8952e87fc1dce8af87990fcfd90f18)
1 /*
2  * This file and its contents are supplied under the terms of the
3  * Common Development and Distribution License ("CDDL"), version 1.0.
4  * You may only use this file in accordance with the terms of version
5  * 1.0 of the CDDL.
6  *
7  * A full copy of the text of the CDDL should have accompanied this
8  * source.  A copy of the CDDL is also available via the Internet at
9  * http://www.illumos.org/license/CDDL.
10  */
11 
12 /*
13  * Copyright 2025 Oxide Computer Company
14  */
15 
16 /*
17  * Destroy all namsepaces on an NVMe device. This is used for test setup.
18  */
19 
20 #include <err.h>
21 #include "libnvme_test_common.h"
22 
23 static bool
24 device_empty_cb(nvme_ctrl_t *ctrl, const nvme_ns_disc_t *disc,
25     void *arg)
26 {
27 	const uint32_t nsid = nvme_ns_disc_nsid(disc);
28 
29 	if (!libnvme_test_setup_ns(ctrl, NVME_NS_DISC_F_ALL, nsid,
30 	    UINT32_MAX)) {
31 		exit(EXIT_FAILURE);
32 	}
33 
34 	return (true);
35 }
36 
37 int
38 main(void)
39 {
40 	nvme_t *nvme;
41 	nvme_ctrl_t *ctrl;
42 
43 	libnvme_test_init(&nvme, &ctrl);
44 	if (!nvme_ctrl_lock(ctrl, NVME_LOCK_L_WRITE, NVME_LOCK_F_DONT_BLOCK)) {
45 		libnvme_test_ctrl_fatal(ctrl, "failed to obtain write lock");
46 	}
47 
48 	if (!nvme_ns_discover(ctrl, NVME_NS_DISC_F_ALL,
49 	    device_empty_cb, NULL)) {
50 		libnvme_test_ctrl_fatal(ctrl, "failed to iterate namespaces");
51 	}
52 
53 	nvme_ctrl_unlock(ctrl);
54 	nvme_ctrl_fini(ctrl);
55 	nvme_fini(nvme);
56 
57 	return (EXIT_SUCCESS);
58 }
59