| 1 |
/* |
| 2 |
* Copyright (C) 2014-2021 Red Hat, Inc. |
| 3 |
* |
| 4 |
* This library is free software; you can redistribute it and/or |
| 5 |
* modify it under the terms of the GNU Lesser General Public |
| 6 |
* License as published by the Free Software Foundation; either |
| 7 |
* version 2.1 of the License, or (at your option) any later version. |
| 8 |
* |
| 9 |
* This library is distributed in the hope that it will be useful, |
| 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 |
* Lesser General Public License for more details. |
| 13 |
* |
| 14 |
* You should have received a copy of the GNU Lesser General Public |
| 15 |
* License along with this library; if not, see <http://www.gnu.org/licenses/>. |
| 16 |
* |
| 17 |
* Author: Tomas Bzatek <[email protected]> |
| 18 |
*/ |
| 19 |
|
| 20 |
#include <glib.h> |
| 21 |
#include <string.h> |
| 22 |
#include <stdio.h> |
| 23 |
#include <unistd.h> |
| 24 |
#include <sys/ioctl.h> |
| 25 |
#include <sys/stat.h> |
| 26 |
#include <errno.h> |
| 27 |
#include <fcntl.h> |
| 28 |
#include <malloc.h> |
| 29 |
#include <linux/fs.h> |
| 30 |
|
| 31 |
#include <libnvme.h> |
| 32 |
|
| 33 |
#include <blockdev/utils.h> |
| 34 |
#include <check_deps.h> |
| 35 |
#include "nvme.h" |
| 36 |
#include "nvme-private.h" |
| 37 |
|
| 38 |
|
| 39 |
/** |
| 40 |
* bd_nvme_device_self_test: |
| 41 |
* @device: a NVMe controller or namespace device (e.g. `/dev/nvme0`) |
| 42 |
* @action: self-test action to take. |
| 43 |
* @error: (out) (nullable): place to store error (if any) |
| 44 |
* |
| 45 |
* Initiates or aborts the Device Self-test operation on the controller or a namespace, |
| 46 |
* distinguished by the @device path specified. In case a controller device |
| 47 |
* is specified then the self-test operation would include all active namespaces. |
| 48 |
* |
| 49 |
* To abort a running operation, pass #BD_NVME_SELF_TEST_ACTION_ABORT as @action. |
| 50 |
* To retrieve progress of a current running operation, check the self-test log using |
| 51 |
* bd_nvme_get_self_test_log(). |
| 52 |
* |
| 53 |
* Returns: %TRUE if the device self-test command was issued successfully, |
| 54 |
* %FALSE otherwise with @error set. |
| 55 |
* |
| 56 |
* Tech category: %BD_NVME_TECH_NVME-%BD_NVME_TECH_MODE_MANAGE |
| 57 |
*/ |
| 58 |
gboolean bd_nvme_device_self_test (const gchar *device, BDNVMESelfTestAction action, GError **error) { |
| 59 |
int ret; |
| 60 |
struct nvme_dev_self_test_args args = { |
| 61 |
.args_size = sizeof(args), |
| 62 |
.result = NULL, |
| 63 |
.timeout = NVME_DEFAULT_IOCTL_TIMEOUT, |
| 64 |
.nsid = 0xffffffff, |
| 65 |
}; |
| 66 |
|
| 67 |
switch (action) { |
| 68 |
case BD_NVME_SELF_TEST_ACTION_SHORT: |
| 69 |
args.stc = NVME_DST_STC_SHORT; |
| 70 |
break; |
| 71 |
case BD_NVME_SELF_TEST_ACTION_EXTENDED: |
| 72 |
args.stc = NVME_DST_STC_LONG; |
| 73 |
break; |
| 74 |
case BD_NVME_SELF_TEST_ACTION_VENDOR_SPECIFIC: |
| 75 |
args.stc = NVME_DST_STC_VS; |
| 76 |
break; |
| 77 |
case BD_NVME_SELF_TEST_ACTION_ABORT: |
| 78 |
args.stc = NVME_DST_STC_ABORT; |
| 79 |
break; |
| 80 |
default: |
| 81 |
g_set_error (error, BD_NVME_ERROR, BD_NVME_ERROR_INVALID_ARGUMENT, |
| 82 |
"Invalid value specified for the self-test action: %d", action); |
| 83 |
return FALSE; |
| 84 |
} |
| 85 |
|
| 86 |
/* open the block device */ |
| 87 |
args.fd = _open_dev (device, error); |
| 88 |
if (args.fd < 0) |
| 89 |
return FALSE; |
| 90 |
|
| 91 |
/* get Namespace Identifier (NSID) for the @device (NVME_IOCTL_ID) */ |
| 92 |
ret = nvme_get_nsid (args.fd, &args.nsid); |
| 93 |
if (ret < 0 && errno == ENOTTY) |
| 94 |
/* not a block device, assuming controller character device */ |
| 95 |
args.nsid = 0xffffffff; |
| 96 |
else if (ret != 0) { |
| 97 |
_nvme_status_to_error (ret, FALSE, error); |
| 98 |
g_prefix_error (error, "Error getting Namespace Identifier (NSID): "); |
| 99 |
close (args.fd); |
| 100 |
return FALSE; |
| 101 |
} |
| 102 |
|
| 103 |
ret = nvme_dev_self_test (&args); |
| 104 |
if (ret != 0) { |
| 105 |
_nvme_status_to_error (ret, FALSE, error); |
| 106 |
g_prefix_error (error, "NVMe Device Self-test command error: "); |
| 107 |
close (args.fd); |
| 108 |
return FALSE; |
| 109 |
} |
| 110 |
close (args.fd); |
| 111 |
|
| 112 |
return TRUE; |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
/* returns 0xff in case of error (the NVMe standard defines total of 16 flba records) */ |
| 117 |
static __u8 find_lbaf_for_size (int fd, __u32 nsid, guint16 lba_data_size, guint16 metadata_size, GError **error) { |
| 118 |
int ret; |
| 119 |
struct nvme_id_ns *ns_info; |
| 120 |
__u8 flbas = 0; |
| 121 |
guint i; |
| 122 |
|
| 123 |
/* TODO: find first attached namespace instead of hardcoding NSID = 1 */ |
| 124 |
ns_info = _nvme_alloc (sizeof (struct nvme_id_ns), error); |
| 125 |
if (!ns_info) |
| 126 |
return 0xff; |
| 127 |
ret = nvme_identify_ns (fd, nsid == 0xffffffff ? 1 : nsid, ns_info); |
| 128 |
if (ret != 0) { |
| 129 |
_nvme_status_to_error (ret, FALSE, error); |
| 130 |
g_prefix_error (error, "NVMe Identify Namespace command error: "); |
| 131 |
free (ns_info); |
| 132 |
return 0xff; |
| 133 |
} |
| 134 |
|
| 135 |
/* return currently used lbaf */ |
| 136 |
if (lba_data_size == 0) { |
| 137 |
nvme_id_ns_flbas_to_lbaf_inuse (ns_info->flbas, &flbas); |
| 138 |
free (ns_info); |
| 139 |
return flbas; |
| 140 |
} |
| 141 |
|
| 142 |
for (i = 0; i <= ns_info->nlbaf + ns_info->nulbaf; i++) |
| 143 |
if (1UL << ns_info->lbaf[i].ds == lba_data_size && GUINT16_FROM_LE (ns_info->lbaf[i].ms) == metadata_size) { |
| 144 |
free (ns_info); |
| 145 |
return i; |
| 146 |
} |
| 147 |
|
| 148 |
g_set_error_literal (error, BD_NVME_ERROR, BD_NVME_ERROR_INVALID_ARGUMENT, |
| 149 |
"Couldn't match desired LBA data block size in a device supported LBA format data sizes"); |
| 150 |
free (ns_info); |
| 151 |
return 0xff; |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* bd_nvme_format: |
| 156 |
* @device: NVMe namespace or controller device to format (e.g. `/dev/nvme0n1`) |
| 157 |
* @lba_data_size: desired LBA data size (i.e. a sector size) in bytes or `0` to keep current. See #BDNVMELBAFormat and bd_nvme_get_namespace_info(). |
| 158 |
* @metadata_size: desired metadata size in bytes or `0` for default. See #BDNVMELBAFormat and bd_nvme_get_namespace_info(). |
| 159 |
* @secure_erase: optional secure erase action to take. |
| 160 |
* @error: (out) (nullable): place to store error (if any) |
| 161 |
* |
| 162 |
* Performs low level format of the NVM media, destroying all data and metadata for either |
| 163 |
* a specific namespace or all attached namespaces to the controller. Use this command |
| 164 |
* to change LBA sector size. Optional secure erase method can be specified as well. |
| 165 |
* |
| 166 |
* Supported LBA data sizes for a given namespace can be listed using the bd_nvme_get_namespace_info() |
| 167 |
* call. In case of a special value `0` the current LBA format for a given namespace will be |
| 168 |
* retained. When called on a controller device the first namespace is used as a reference. |
| 169 |
* |
| 170 |
* Note that the NVMe controller may define a Format NVM attribute indicating that the format |
| 171 |
* operation would apply to all namespaces and a format (excluding secure erase) of any |
| 172 |
* namespace results in a format of all namespaces in the NVM subsystem. In such case and |
| 173 |
* when @device is a namespace block device the #BD_NVME_ERROR_WOULD_FORMAT_ALL_NS error |
| 174 |
* is returned to prevent further damage. This is then supposed to be handled by the caller |
| 175 |
* and bd_nvme_format() is supposed to be called on a controller device instead. |
| 176 |
* |
| 177 |
* This call blocks until the format operation has finished. To retrieve progress |
| 178 |
* of a current running operation, check the namespace info using bd_nvme_get_namespace_info(). |
| 179 |
* |
| 180 |
* Returns: %TRUE if the format command finished successfully, %FALSE otherwise with @error set. |
| 181 |
* |
| 182 |
* Tech category: %BD_NVME_TECH_NVME-%BD_NVME_TECH_MODE_MANAGE |
| 183 |
*/ |
| 184 |
gboolean bd_nvme_format (const gchar *device, guint16 lba_data_size, guint16 metadata_size, BDNVMEFormatSecureErase secure_erase, GError **error) { |
| 185 |
int ret; |
| 186 |
gboolean ctrl_device = FALSE; |
| 187 |
struct nvme_id_ctrl *ctrl_id; |
| 188 |
struct nvme_format_nvm_args args = { |
| 189 |
.args_size = sizeof(args), |
| 190 |
.result = NULL, |
| 191 |
.timeout = NVME_DEFAULT_IOCTL_TIMEOUT, |
| 192 |
.nsid = 0xffffffff, |
| 193 |
.mset = NVME_FORMAT_MSET_SEPARATE /* 0 */, |
| 194 |
.pi = NVME_FORMAT_PI_DISABLE /* 0 */, |
| 195 |
.pil = NVME_FORMAT_PIL_LAST /* 0 */, |
| 196 |
.ses = NVME_FORMAT_SES_NONE, |
| 197 |
}; |
| 198 |
|
| 199 |
/* open the block device */ |
| 200 |
args.fd = _open_dev (device, error); |
| 201 |
if (args.fd < 0) |
| 202 |
return FALSE; |
| 203 |
|
| 204 |
ret = nvme_get_nsid (args.fd, &args.nsid); |
| 205 |
if (ret < 0 && errno == ENOTTY) { |
| 206 |
/* not a block device, assuming controller character device */ |
| 207 |
args.nsid = 0xffffffff; |
| 208 |
ctrl_device = TRUE; |
| 209 |
} else if (ret != 0) { |
| 210 |
_nvme_status_to_error (ret, FALSE, error); |
| 211 |
g_prefix_error (error, "Error getting Namespace Identifier (NSID): "); |
| 212 |
close (args.fd); |
| 213 |
return FALSE; |
| 214 |
} |
| 215 |
|
| 216 |
/* check the FNA controller bit when formatting a single namespace */ |
| 217 |
if (! ctrl_device) { |
| 218 |
ctrl_id = _nvme_alloc (sizeof (struct nvme_id_ctrl), error); |
| 219 |
if (!ctrl_id) { |
| 220 |
close (args.fd); |
| 221 |
return FALSE; |
| 222 |
} |
| 223 |
ret = nvme_identify_ctrl (args.fd, ctrl_id); |
| 224 |
if (ret != 0) { |
| 225 |
_nvme_status_to_error (ret, FALSE, error); |
| 226 |
g_prefix_error (error, "NVMe Identify Controller command error: "); |
| 227 |
close (args.fd); |
| 228 |
free (ctrl_id); |
| 229 |
return FALSE; |
| 230 |
} |
| 231 |
/* from nvme-cli: |
| 232 |
* FNA bit 0 set to 1: all namespaces ... shall be configured with the same |
| 233 |
* attributes and a format (excluding secure erase) of any namespace results in a |
| 234 |
* format of all namespaces. |
| 235 |
*/ |
| 236 |
if ((ctrl_id->fna & NVME_CTRL_FNA_FMT_ALL_NAMESPACES) == NVME_CTRL_FNA_FMT_ALL_NAMESPACES) { |
| 237 |
/* tell user that it would format other namespaces and that bd_nvme_format() |
| 238 |
* should be called on a controller device instead */ |
| 239 |
g_set_error_literal (error, BD_NVME_ERROR, BD_NVME_ERROR_WOULD_FORMAT_ALL_NS, |
| 240 |
"The NVMe controller indicates it would format all namespaces."); |
| 241 |
close (args.fd); |
| 242 |
free (ctrl_id); |
| 243 |
return FALSE; |
| 244 |
} |
| 245 |
free (ctrl_id); |
| 246 |
} |
| 247 |
|
| 248 |
/* find out the desired LBA data format index */ |
| 249 |
args.lbaf = find_lbaf_for_size (args.fd, args.nsid, lba_data_size, metadata_size, error); |
| 250 |
if (args.lbaf == 0xff) { |
| 251 |
close (args.fd); |
| 252 |
return FALSE; |
| 253 |
} |
| 254 |
|
| 255 |
switch (secure_erase) { |
| 256 |
case BD_NVME_FORMAT_SECURE_ERASE_USER_DATA: |
| 257 |
args.ses = NVME_FORMAT_SES_USER_DATA_ERASE; |
| 258 |
break; |
| 259 |
case BD_NVME_FORMAT_SECURE_ERASE_CRYPTO: |
| 260 |
args.ses = NVME_FORMAT_SES_CRYPTO_ERASE; |
| 261 |
break; |
| 262 |
case BD_NVME_FORMAT_SECURE_ERASE_NONE: |
| 263 |
default: |
| 264 |
args.ses = NVME_FORMAT_SES_NONE; |
| 265 |
} |
| 266 |
|
| 267 |
ret = nvme_format_nvm (&args); |
| 268 |
if (ret != 0) { |
| 269 |
_nvme_status_to_error (ret, FALSE, error); |
| 270 |
g_prefix_error (error, "Format NVM command error: "); |
| 271 |
close (args.fd); |
| 272 |
return FALSE; |
| 273 |
} |
| 274 |
|
| 275 |
/* rescan the namespaces if block size has changed */ |
| 276 |
if (ctrl_device) { |
| 277 |
if (ioctl (args.fd, NVME_IOCTL_RESCAN) < 0) { |
| 278 |
g_set_error (error, BD_NVME_ERROR, BD_NVME_ERROR_FAILED, |
| 279 |
"Failed to rescan namespaces after format: %s", strerror_l (errno, _C_LOCALE)); |
| 280 |
close (args.fd); |
| 281 |
return FALSE; |
| 282 |
} |
| 283 |
} else { |
| 284 |
if (lba_data_size != 0) { |
| 285 |
/* from nvme-cli: |
| 286 |
* If block size has been changed by the format command up there, we should notify it to |
| 287 |
* kernel blkdev to update its own block size to the given one because blkdev will not |
| 288 |
* update by itself without re-opening fd. |
| 289 |
*/ |
| 290 |
int block_size = lba_data_size; |
| 291 |
|
| 292 |
if (ioctl (args.fd, BLKBSZSET, &block_size) < 0) { |
| 293 |
g_set_error (error, BD_NVME_ERROR, BD_NVME_ERROR_FAILED, |
| 294 |
"Failed to set block size to %d after format: %s", block_size, strerror_l (errno, _C_LOCALE)); |
| 295 |
close (args.fd); |
| 296 |
return FALSE; |
| 297 |
} |
| 298 |
|
| 299 |
if (ioctl (args.fd, BLKRRPART) < 0) { |
| 300 |
g_set_error (error, BD_NVME_ERROR, BD_NVME_ERROR_FAILED, |
| 301 |
"Failed to re-read partition table after format: %s", strerror_l (errno, _C_LOCALE)); |
| 302 |
close (args.fd); |
| 303 |
return FALSE; |
| 304 |
} |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
close (args.fd); |
| 309 |
return TRUE; |
| 310 |
} |
| 311 |
|
| 312 |
/** |
| 313 |
* bd_nvme_sanitize: |
| 314 |
* @device: NVMe namespace or controller device to format (e.g. `/dev/nvme0n1`) |
| 315 |
* @action: the sanitize action to perform. |
| 316 |
* @no_dealloc: instruct the controller to not deallocate the affected media area. |
| 317 |
* @overwrite_pass_count: number of overwrite passes [1-15] or 0 for the default (16 passes). |
| 318 |
* @overwrite_pattern: a 32-bit pattern used for the Overwrite sanitize operation. |
| 319 |
* @overwrite_invert_pattern: invert the overwrite pattern between passes. |
| 320 |
* @error: (out) (nullable): place to store error (if any) |
| 321 |
* |
| 322 |
* Starts a sanitize operation or recovers from a previously failed sanitize operation. |
| 323 |
* By definition, a sanitize operation alters all user data in the NVM subsystem such |
| 324 |
* that recovery of any previous user data from any cache, the non-volatile media, |
| 325 |
* or any Controller Memory Buffer is not possible. The scope of a sanitize operation |
| 326 |
* is all locations in the NVM subsystem that are able to contain user data, including |
| 327 |
* caches, Persistent Memory Regions, and unallocated or deallocated areas of the media. |
| 328 |
* |
| 329 |
* Once started, a sanitize operation is not able to be aborted and continues after |
| 330 |
* a Controller Level Reset including across power cycles. Once the sanitize operation |
| 331 |
* has run the media affected may not be immediately ready for use unless additional |
| 332 |
* media modification mechanism is run. This is often vendor specific and also depends |
| 333 |
* on the sanitize method (@action) used. Callers to this sanitize operation should |
| 334 |
* set @no_dealloc to %TRUE for the added convenience. |
| 335 |
* |
| 336 |
* The controller also ignores Critical Warning(s) in the SMART / Health Information |
| 337 |
* log page (e.g., read only mode) and attempts to complete the sanitize operation requested. |
| 338 |
* |
| 339 |
* This call returns immediately and the actual sanitize operation is performed |
| 340 |
* in the background. Use bd_nvme_get_sanitize_log() to retrieve status and progress |
| 341 |
* of a running sanitize operation. In case a sanitize operation fails the controller |
| 342 |
* may restrict its operation until a subsequent sanitize operation is started |
| 343 |
* (i.e. retried) or an #BD_NVME_SANITIZE_ACTION_EXIT_FAILURE action is used |
| 344 |
* to acknowledge the failure explicitly. |
| 345 |
* |
| 346 |
* The @overwrite_pass_count, @overwrite_pattern and @overwrite_invert_pattern |
| 347 |
* arguments are only valid when @action is #BD_NVME_SANITIZE_ACTION_OVERWRITE. |
| 348 |
* |
| 349 |
* The sanitize operation is set to run under the Allow Unrestricted Sanitize Exit |
| 350 |
* mode. |
| 351 |
* |
| 352 |
* Returns: %TRUE if the sanitize command was successfully submitted, %FALSE otherwise with @error set. |
| 353 |
* |
| 354 |
* Tech category: %BD_NVME_TECH_NVME-%BD_NVME_TECH_MODE_MANAGE |
| 355 |
*/ |
| 356 |
gboolean bd_nvme_sanitize (const gchar *device, BDNVMESanitizeAction action, gboolean no_dealloc, gint overwrite_pass_count, guint32 overwrite_pattern, gboolean overwrite_invert_pattern, GError **error) { |
| 357 |
int ret; |
| 358 |
struct nvme_sanitize_nvm_args args = { |
| 359 |
.args_size = sizeof(args), |
| 360 |
.result = NULL, |
| 361 |
.timeout = NVME_DEFAULT_IOCTL_TIMEOUT, |
| 362 |
.ause = TRUE, |
| 363 |
.owpass = overwrite_pass_count, |
| 364 |
.oipbp = overwrite_invert_pattern, |
| 365 |
.nodas = no_dealloc, |
| 366 |
.ovrpat = GUINT32_TO_LE (overwrite_pattern), |
| 367 |
}; |
| 368 |
|
| 369 |
switch (action) { |
| 370 |
case BD_NVME_SANITIZE_ACTION_EXIT_FAILURE: |
| 371 |
args.sanact = NVME_SANITIZE_SANACT_EXIT_FAILURE; |
| 372 |
break; |
| 373 |
case BD_NVME_SANITIZE_ACTION_BLOCK_ERASE: |
| 374 |
args.sanact = NVME_SANITIZE_SANACT_START_BLOCK_ERASE; |
| 375 |
break; |
| 376 |
case BD_NVME_SANITIZE_ACTION_OVERWRITE: |
| 377 |
args.sanact = NVME_SANITIZE_SANACT_START_OVERWRITE; |
| 378 |
break; |
| 379 |
case BD_NVME_SANITIZE_ACTION_CRYPTO_ERASE: |
| 380 |
args.sanact = NVME_SANITIZE_SANACT_START_CRYPTO_ERASE; |
| 381 |
break; |
| 382 |
default: |
| 383 |
g_set_error (error, BD_NVME_ERROR, BD_NVME_ERROR_INVALID_ARGUMENT, |
| 384 |
"Invalid value specified for the sanitize action: %d", action); |
| 385 |
return FALSE; |
| 386 |
} |
| 387 |
|
| 388 |
/* open the block device */ |
| 389 |
args.fd = _open_dev (device, error); |
| 390 |
if (args.fd < 0) |
| 391 |
return FALSE; |
| 392 |
|
| 393 |
ret = nvme_sanitize_nvm (&args); |
| 394 |
if (ret != 0) { |
| 395 |
_nvme_status_to_error (ret, FALSE, error); |
| 396 |
g_prefix_error (error, "Sanitize command error: "); |
| 397 |
close (args.fd); |
| 398 |
return FALSE; |
| 399 |
} |
| 400 |
|
| 401 |
close (args.fd); |
| 402 |
return TRUE; |
| 403 |
} |