]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sbin/nvmecontrol/sanitize.c
sys: Automated cleanup of cdefs and other formatting
[FreeBSD/FreeBSD.git] / sbin / nvmecontrol / sanitize.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (C) 2019 Alexander Motin <mav@FreeBSD.org>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27
28 #include <sys/param.h>
29 #include <sys/ioccom.h>
30
31 #include <ctype.h>
32 #include <err.h>
33 #include <fcntl.h>
34 #include <stdbool.h>
35 #include <stddef.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <sysexits.h>
40 #include <unistd.h>
41
42 #include "nvmecontrol.h"
43
44 /* Tables for command line parsing */
45
46 static cmd_fn_t sanitize;
47
48 static struct options {
49         bool            ause;
50         bool            ndas;
51         bool            oipbp;
52         bool            reportonly;
53         uint8_t         owpass;
54         uint32_t        ovrpat;
55         const char      *sanact;
56         const char      *dev;
57 } opt = {
58         .ause = false,
59         .ndas = false,
60         .oipbp = false,
61         .reportonly = false,
62         .owpass = 1,
63         .ovrpat = 0,
64         .sanact = NULL,
65         .dev = NULL,
66 };
67
68 static const struct opts sanitize_opts[] = {
69 #define OPT(l, s, t, opt, addr, desc) { l, s, t, &opt.addr, desc }
70         OPT("ause", 'U', arg_none, opt, ause,
71             "Allow Unrestricted Sanitize Exit"),
72         OPT("ndas", 'd', arg_none, opt, ndas,
73             "No Deallocate After Sanitize"),
74         OPT("oipbp", 'I', arg_none, opt, oipbp,
75             "Overwrite Invert Pattern Between Passes"),
76         OPT("reportonly", 'r', arg_none, opt, reportonly,
77             "Report previous sanitize status"),
78         OPT("owpass", 'c', arg_uint8, opt, owpass,
79             "Overwrite Pass Count"),
80         OPT("ovrpat", 'p', arg_uint32, opt, ovrpat,
81             "Overwrite Pattern"),
82         OPT("sanact", 'a', arg_string, opt, sanact,
83             "Sanitize Action (block, overwrite, crypto)"),
84         { NULL, 0, arg_none, NULL, NULL }
85 };
86 #undef OPT
87
88 static const struct args sanitize_args[] = {
89         { arg_string, &opt.dev, "controller-id" },
90         { arg_none, NULL, NULL },
91 };
92
93 static struct cmd sanitize_cmd = {
94         .name = "sanitize",
95         .fn = sanitize,
96         .descr = "Sanitize NVM subsystem",
97         .ctx_size = sizeof(opt),
98         .opts = sanitize_opts,
99         .args = sanitize_args,
100 };
101
102 CMD_COMMAND(sanitize_cmd);
103
104 /* End of tables for command line parsing */
105
106 static void
107 sanitize(const struct cmd *f, int argc, char *argv[])
108 {
109         struct nvme_controller_data     cd;
110         struct nvme_pt_command          pt;
111         struct nvme_sanitize_status_page ss;
112         char                            *path;
113         uint32_t                        nsid;
114         int                             sanact = 0, fd, delay = 1;
115
116         if (arg_parse(argc, argv, f))
117                 return;
118
119         if (opt.sanact == NULL) {
120                 if (!opt.reportonly) {
121                         fprintf(stderr, "Sanitize Action is not specified\n");
122                         arg_help(argc, argv, f);
123                 }
124         } else {
125                 if (strcmp(opt.sanact, "exitfailure") == 0)
126                         sanact = 1;
127                 else if (strcmp(opt.sanact, "block") == 0)
128                         sanact = 2;
129                 else if (strcmp(opt.sanact, "overwrite") == 0)
130                         sanact = 3;
131                 else if (strcmp(opt.sanact, "crypto") == 0)
132                         sanact = 4;
133                 else {
134                         fprintf(stderr, "Incorrect Sanitize Action value\n");
135                         arg_help(argc, argv, f);
136                 }
137         }
138         if (opt.owpass == 0 || opt.owpass > 16) {
139                 fprintf(stderr, "Incorrect Overwrite Pass Count value\n");
140                 arg_help(argc, argv, f);
141         }
142
143         open_dev(opt.dev, &fd, 1, 1);
144         get_nsid(fd, &path, &nsid);
145         if (nsid != 0) {
146                 close(fd);
147                 open_dev(path, &fd, 1, 1);
148         }
149         free(path);
150
151         if (opt.reportonly)
152                 goto wait;
153
154         /* Check that controller can execute this command. */
155         if (read_controller_data(fd, &cd))
156                 errx(EX_IOERR, "Identify request failed");
157         if (((cd.sanicap >> NVME_CTRLR_DATA_SANICAP_BES_SHIFT) &
158              NVME_CTRLR_DATA_SANICAP_BES_MASK) == 0 && sanact == 2)
159                 errx(EX_UNAVAILABLE, "controller does not support Block Erase");
160         if (((cd.sanicap >> NVME_CTRLR_DATA_SANICAP_OWS_SHIFT) &
161              NVME_CTRLR_DATA_SANICAP_OWS_MASK) == 0 && sanact == 3)
162                 errx(EX_UNAVAILABLE, "controller does not support Overwrite");
163         if (((cd.sanicap >> NVME_CTRLR_DATA_SANICAP_CES_SHIFT) &
164              NVME_CTRLR_DATA_SANICAP_CES_MASK) == 0 && sanact == 4)
165                 errx(EX_UNAVAILABLE, "controller does not support Crypto Erase");
166
167         /*
168          * If controller supports only one namespace, we may sanitize it.
169          * If there can be more, make user explicit in his commands.
170          */
171         if (nsid != 0 && cd.nn > 1)
172                 errx(EX_UNAVAILABLE, "can't sanitize one of namespaces, specify controller");
173
174         memset(&pt, 0, sizeof(pt));
175         pt.cmd.opc = NVME_OPC_SANITIZE;
176         pt.cmd.cdw10 = htole32((opt.ndas << 9) | (opt.oipbp << 8) |
177             ((opt.owpass & 0xf) << 4) | (opt.ause << 3) | sanact);
178         pt.cmd.cdw11 = htole32(opt.ovrpat);
179
180         if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0)
181                 err(EX_IOERR, "sanitize request failed");
182
183         if (nvme_completion_is_error(&pt.cpl))
184                 errx(EX_IOERR, "sanitize request returned error");
185
186 wait:
187         read_logpage(fd, NVME_LOG_SANITIZE_STATUS,
188             NVME_GLOBAL_NAMESPACE_TAG, 0, 0, 0, &ss, sizeof(ss));
189         switch ((ss.sstat >> NVME_SS_PAGE_SSTAT_STATUS_SHIFT) &
190             NVME_SS_PAGE_SSTAT_STATUS_MASK) {
191         case NVME_SS_PAGE_SSTAT_STATUS_NEVER:
192                 printf("Never sanitized");
193                 break;
194         case NVME_SS_PAGE_SSTAT_STATUS_COMPLETED:
195                 printf("Sanitize completed");
196                 break;
197         case NVME_SS_PAGE_SSTAT_STATUS_INPROG:
198                 printf("Sanitize in progress: %u%% (%u/65535)\r",
199                     (ss.sprog * 100 + 32768) / 65536, ss.sprog);
200                 fflush(stdout);
201                 if (delay < 16)
202                         delay++;
203                 sleep(delay);
204                 goto wait;
205         case NVME_SS_PAGE_SSTAT_STATUS_FAILED:
206                 printf("Sanitize failed");
207                 break;
208         case NVME_SS_PAGE_SSTAT_STATUS_COMPLETEDWD:
209                 printf("Sanitize completed with deallocation");
210                 break;
211         default:
212                 printf("Sanitize status unknown");
213                 break;
214         }
215         if (delay > 1)
216                 printf("                       ");
217         printf("\n");
218
219         close(fd);
220         exit(0);
221 }