|
37 | 37 | #include "cleanup.h" |
38 | 38 | #include "private.h" |
39 | 39 |
|
40 | | -#define NVMF_HOSTID_SIZE 37 |
41 | | - |
42 | | -#define NVMF_HOSTNQN_FILE SYSCONFDIR "/nvme/hostnqn" |
43 | | -#define NVMF_HOSTID_FILE SYSCONFDIR "/nvme/hostid" |
44 | | - |
45 | 40 | const char *nvmf_dev = "/dev/nvme-fabrics"; |
46 | 41 |
|
47 | 42 | /** |
@@ -1384,244 +1379,6 @@ int nvmf_get_discovery_wargs(struct nvme_get_discovery_args *args, |
1384 | 1379 | return 0; |
1385 | 1380 | } |
1386 | 1381 |
|
1387 | | -static int uuid_from_device_tree(char *system_uuid) |
1388 | | -{ |
1389 | | - _cleanup_fd_ int f = -1; |
1390 | | - ssize_t len; |
1391 | | - |
1392 | | - f = open(nvme_uuid_ibm_filename(), O_RDONLY); |
1393 | | - if (f < 0) |
1394 | | - return -ENXIO; |
1395 | | - |
1396 | | - memset(system_uuid, 0, NVME_UUID_LEN_STRING); |
1397 | | - len = read(f, system_uuid, NVME_UUID_LEN_STRING - 1); |
1398 | | - if (len < 0) |
1399 | | - return -ENXIO; |
1400 | | - |
1401 | | - return strlen(system_uuid) ? 0 : -ENXIO; |
1402 | | -} |
1403 | | - |
1404 | | -/* |
1405 | | - * See System Management BIOS (SMBIOS) Reference Specification |
1406 | | - * https://www.dmtf.org/sites/default/files/standards/documents/DSP0134_3.2.0.pdf |
1407 | | - */ |
1408 | | -#define DMI_SYSTEM_INFORMATION 1 |
1409 | | - |
1410 | | -static bool is_dmi_uuid_valid(const char *buf, size_t len) |
1411 | | -{ |
1412 | | - int i; |
1413 | | - |
1414 | | - /* UUID bytes are from byte 8 to 23 */ |
1415 | | - if (len < 24) |
1416 | | - return false; |
1417 | | - |
1418 | | - /* Test it's a invalid UUID with all zeros */ |
1419 | | - for (i = 8; i < 24; i++) { |
1420 | | - if (buf[i]) |
1421 | | - break; |
1422 | | - } |
1423 | | - if (i == 24) |
1424 | | - return false; |
1425 | | - |
1426 | | - return true; |
1427 | | -} |
1428 | | - |
1429 | | -static int uuid_from_dmi_entries(char *system_uuid) |
1430 | | -{ |
1431 | | - _cleanup_dir_ DIR *d = NULL; |
1432 | | - const char *entries_dir = nvme_dmi_entries_dir(); |
1433 | | - int f; |
1434 | | - struct dirent *de; |
1435 | | - char buf[512] = {0}; |
1436 | | - |
1437 | | - system_uuid[0] = '\0'; |
1438 | | - d = opendir(entries_dir); |
1439 | | - if (!d) |
1440 | | - return -ENXIO; |
1441 | | - while ((de = readdir(d))) { |
1442 | | - char filename[PATH_MAX]; |
1443 | | - int len, type; |
1444 | | - |
1445 | | - if (de->d_name[0] == '.') |
1446 | | - continue; |
1447 | | - sprintf(filename, "%s/%s/type", entries_dir, de->d_name); |
1448 | | - f = open(filename, O_RDONLY); |
1449 | | - if (f < 0) |
1450 | | - continue; |
1451 | | - len = read(f, buf, 512); |
1452 | | - close(f); |
1453 | | - if (len <= 0) |
1454 | | - continue; |
1455 | | - if (sscanf(buf, "%d", &type) != 1) |
1456 | | - continue; |
1457 | | - if (type != DMI_SYSTEM_INFORMATION) |
1458 | | - continue; |
1459 | | - sprintf(filename, "%s/%s/raw", entries_dir, de->d_name); |
1460 | | - f = open(filename, O_RDONLY); |
1461 | | - if (f < 0) |
1462 | | - continue; |
1463 | | - len = read(f, buf, 512); |
1464 | | - close(f); |
1465 | | - if (len <= 0) |
1466 | | - continue; |
1467 | | - |
1468 | | - if (!is_dmi_uuid_valid(buf, len)) |
1469 | | - continue; |
1470 | | - |
1471 | | - /* Sigh. https://en.wikipedia.org/wiki/Overengineering */ |
1472 | | - /* DMTF SMBIOS 3.0 Section 7.2.1 System UUID */ |
1473 | | - sprintf(system_uuid, |
1474 | | - "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-" |
1475 | | - "%02x%02x%02x%02x%02x%02x", |
1476 | | - (uint8_t)buf[8 + 3], (uint8_t)buf[8 + 2], |
1477 | | - (uint8_t)buf[8 + 1], (uint8_t)buf[8 + 0], |
1478 | | - (uint8_t)buf[8 + 5], (uint8_t)buf[8 + 4], |
1479 | | - (uint8_t)buf[8 + 7], (uint8_t)buf[8 + 6], |
1480 | | - (uint8_t)buf[8 + 8], (uint8_t)buf[8 + 9], |
1481 | | - (uint8_t)buf[8 + 10], (uint8_t)buf[8 + 11], |
1482 | | - (uint8_t)buf[8 + 12], (uint8_t)buf[8 + 13], |
1483 | | - (uint8_t)buf[8 + 14], (uint8_t)buf[8 + 15]); |
1484 | | - break; |
1485 | | - } |
1486 | | - return strlen(system_uuid) ? 0 : -ENXIO; |
1487 | | -} |
1488 | | - |
1489 | | -#define PATH_DMI_PROD_UUID "/sys/class/dmi/id/product_uuid" |
1490 | | - |
1491 | | -/** |
1492 | | - * uuid_from_product_uuid() - Get system UUID from product_uuid |
1493 | | - * @system_uuid: Where to save the system UUID. |
1494 | | - * |
1495 | | - * Return: 0 on success, -ENXIO otherwise. |
1496 | | - */ |
1497 | | -static int uuid_from_product_uuid(char *system_uuid) |
1498 | | -{ |
1499 | | - _cleanup_file_ FILE *stream = NULL; |
1500 | | - ssize_t nread; |
1501 | | - _cleanup_free_ char *line = NULL; |
1502 | | - size_t len = 0; |
1503 | | - |
1504 | | - stream = fopen(PATH_DMI_PROD_UUID, "re"); |
1505 | | - if (!stream) |
1506 | | - return -ENXIO; |
1507 | | - system_uuid[0] = '\0'; |
1508 | | - |
1509 | | - nread = getline(&line, &len, stream); |
1510 | | - if (nread != NVME_UUID_LEN_STRING) |
1511 | | - return -ENXIO; |
1512 | | - |
1513 | | - /* The kernel is handling the byte swapping according DMTF |
1514 | | - * SMBIOS 3.0 Section 7.2.1 System UUID */ |
1515 | | - |
1516 | | - memcpy(system_uuid, line, NVME_UUID_LEN_STRING - 1); |
1517 | | - system_uuid[NVME_UUID_LEN_STRING - 1] = '\0'; |
1518 | | - |
1519 | | - return 0; |
1520 | | -} |
1521 | | - |
1522 | | -/** |
1523 | | - * uuid_from_dmi() - read system UUID |
1524 | | - * @system_uuid: buffer for the UUID |
1525 | | - * |
1526 | | - * The system UUID can be read from two different locations: |
1527 | | - * |
1528 | | - * 1) /sys/class/dmi/id/product_uuid |
1529 | | - * 2) /sys/firmware/dmi/entries |
1530 | | - * |
1531 | | - * Note that the second location is not present on Debian-based systems. |
1532 | | - * |
1533 | | - * Return: 0 on success, negative errno otherwise. |
1534 | | - */ |
1535 | | -static int uuid_from_dmi(char *system_uuid) |
1536 | | -{ |
1537 | | - int ret = uuid_from_product_uuid(system_uuid); |
1538 | | - if (ret != 0) |
1539 | | - ret = uuid_from_dmi_entries(system_uuid); |
1540 | | - return ret; |
1541 | | -} |
1542 | | - |
1543 | | -char *nvmf_hostid_generate() |
1544 | | -{ |
1545 | | - int ret; |
1546 | | - char uuid_str[NVME_UUID_LEN_STRING]; |
1547 | | - unsigned char uuid[NVME_UUID_LEN]; |
1548 | | - |
1549 | | - ret = uuid_from_dmi(uuid_str); |
1550 | | - if (ret < 0) |
1551 | | - ret = uuid_from_device_tree(uuid_str); |
1552 | | - if (ret < 0) { |
1553 | | - if (nvme_uuid_random(uuid) < 0) |
1554 | | - memset(uuid, 0, NVME_UUID_LEN); |
1555 | | - nvme_uuid_to_string(uuid, uuid_str); |
1556 | | - } |
1557 | | - |
1558 | | - return strdup(uuid_str); |
1559 | | -} |
1560 | | - |
1561 | | -char *nvmf_hostnqn_generate_from_hostid(char *hostid) |
1562 | | -{ |
1563 | | - char *hid = NULL; |
1564 | | - char *hostnqn; |
1565 | | - int ret; |
1566 | | - |
1567 | | - if (!hostid) |
1568 | | - hostid = hid = nvmf_hostid_generate(); |
1569 | | - |
1570 | | - ret = asprintf(&hostnqn, "nqn.2014-08.org.nvmexpress:uuid:%s", hostid); |
1571 | | - free(hid); |
1572 | | - |
1573 | | - return (ret < 0) ? NULL : hostnqn; |
1574 | | -} |
1575 | | - |
1576 | | -char *nvmf_hostnqn_generate() |
1577 | | -{ |
1578 | | - return nvmf_hostnqn_generate_from_hostid(NULL); |
1579 | | -} |
1580 | | - |
1581 | | -static char *nvmf_read_file(const char *f, int len) |
1582 | | -{ |
1583 | | - char buf[len]; |
1584 | | - _cleanup_fd_ int fd = -1; |
1585 | | - int ret; |
1586 | | - |
1587 | | - fd = open(f, O_RDONLY); |
1588 | | - if (fd < 0) |
1589 | | - return NULL; |
1590 | | - |
1591 | | - memset(buf, 0, len); |
1592 | | - ret = read(fd, buf, len - 1); |
1593 | | - |
1594 | | - if (ret < 0 || !strlen(buf)) |
1595 | | - return NULL; |
1596 | | - return strndup(buf, strcspn(buf, "\n")); |
1597 | | -} |
1598 | | - |
1599 | | -char *nvmf_hostnqn_from_file() |
1600 | | -{ |
1601 | | - char *hostnqn = getenv("LIBNVME_HOSTNQN"); |
1602 | | - |
1603 | | - if (hostnqn) { |
1604 | | - if (!strcmp(hostnqn, "")) |
1605 | | - return NULL; |
1606 | | - return strdup(hostnqn); |
1607 | | - } |
1608 | | - |
1609 | | - return nvmf_read_file(NVMF_HOSTNQN_FILE, NVMF_NQN_SIZE); |
1610 | | -} |
1611 | | - |
1612 | | -char *nvmf_hostid_from_file() |
1613 | | -{ |
1614 | | - char *hostid = getenv("LIBNVME_HOSTID"); |
1615 | | - |
1616 | | - if (hostid) { |
1617 | | - if (!strcmp(hostid, "")) |
1618 | | - return NULL; |
1619 | | - return strdup(hostid); |
1620 | | - } |
1621 | | - |
1622 | | - return nvmf_read_file(NVMF_HOSTID_FILE, NVMF_HOSTID_SIZE); |
1623 | | -} |
1624 | | - |
1625 | 1382 | /** |
1626 | 1383 | * nvmf_get_tel() - Calculate the amount of memory needed for a DIE. |
1627 | 1384 | * @hostsymname: Symbolic name (may be NULL) |
@@ -2668,9 +2425,9 @@ int nvmf_config_modify(struct nvme_global_ctx *ctx, |
2668 | 2425 | struct nvme_ctrl *c; |
2669 | 2426 |
|
2670 | 2427 | if (!fctx->hostnqn) |
2671 | | - fctx->hostnqn = hnqn = nvmf_hostnqn_from_file(); |
| 2428 | + fctx->hostnqn = hnqn = nvme_hostnqn_from_file(); |
2672 | 2429 | if (!fctx->hostid && hnqn) |
2673 | | - fctx->hostid = hid = nvmf_hostid_from_file(); |
| 2430 | + fctx->hostid = hid = nvme_hostid_from_file(); |
2674 | 2431 |
|
2675 | 2432 | h = nvme_lookup_host(ctx, fctx->hostnqn, fctx->hostid); |
2676 | 2433 | if (!h) { |
|
0 commit comments