From 7552ed51f78ca1ad311228f76e90cf8c120ee2f6 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 4 Oct 2023 14:52:51 +0200 Subject: Adding upstream version 2.6. Signed-off-by: Daniel Baumann --- .github/workflows/appimage.yml | 40 +- .github/workflows/build.yml | 55 +- .github/workflows/coverage.yml | 21 + .github/workflows/release.yml | 2 +- Documentation/nvme-admin-passthru.html | 2 +- Documentation/nvme-ana-log.html | 2 +- Documentation/nvme-attach-ns.html | 2 +- Documentation/nvme-boot-part-log.html | 2 +- Documentation/nvme-capacity-mgmt.html | 2 +- Documentation/nvme-changed-ns-list-log.html | 2 +- Documentation/nvme-cmdset-ind-id-ns.html | 2 +- Documentation/nvme-compare.html | 2 +- Documentation/nvme-connect-all.html | 2 +- Documentation/nvme-connect.html | 2 +- Documentation/nvme-copy.html | 2 +- Documentation/nvme-create-ns.html | 14 +- Documentation/nvme-create-ns.txt | 5 + Documentation/nvme-delete-ns.html | 2 +- Documentation/nvme-dera-stat.html | 2 +- Documentation/nvme-detach-ns.html | 2 +- Documentation/nvme-device-self-test.html | 2 +- Documentation/nvme-dim.html | 2 +- Documentation/nvme-dir-receive.html | 2 +- Documentation/nvme-dir-send.html | 2 +- Documentation/nvme-disconnect-all.html | 2 +- Documentation/nvme-disconnect.html | 2 +- Documentation/nvme-discover.html | 2 +- Documentation/nvme-dsm.html | 2 +- Documentation/nvme-effects-log.html | 2 +- Documentation/nvme-endurance-event-agg-log.html | 2 +- Documentation/nvme-endurance-log.html | 2 +- Documentation/nvme-error-log.html | 2 +- Documentation/nvme-fdp-configs.html | 2 +- Documentation/nvme-fdp-events.html | 2 +- Documentation/nvme-fdp-set-events.html | 2 +- Documentation/nvme-fdp-stats.html | 2 +- Documentation/nvme-fdp-status.html | 2 +- Documentation/nvme-fdp-update.html | 2 +- Documentation/nvme-fdp-usage.html | 2 +- Documentation/nvme-fid-support-effects-log.html | 2 +- Documentation/nvme-flush.html | 2 +- Documentation/nvme-format.html | 2 +- Documentation/nvme-fw-commit.html | 2 +- Documentation/nvme-fw-download.html | 2 +- Documentation/nvme-fw-log.html | 2 +- Documentation/nvme-gen-hostnqn.html | 2 +- Documentation/nvme-get-feature.html | 2 +- Documentation/nvme-get-lba-status.html | 2 +- Documentation/nvme-get-log.html | 2 +- Documentation/nvme-get-ns-id.html | 2 +- Documentation/nvme-get-property.html | 2 +- Documentation/nvme-help.html | 2 +- Documentation/nvme-huawei-id-ctrl.html | 2 +- Documentation/nvme-huawei-list.html | 2 +- Documentation/nvme-id-ctrl.html | 2 +- Documentation/nvme-id-domain.html | 2 +- Documentation/nvme-id-iocs.html | 2 +- Documentation/nvme-id-ns.html | 2 +- Documentation/nvme-id-nvmset.html | 2 +- Documentation/nvme-inspur-nvme-vendor-log.html | 2 +- Documentation/nvme-intel-id-ctrl.html | 2 +- Documentation/nvme-intel-internal-log.html | 2 +- Documentation/nvme-intel-lat-stats.html | 2 +- Documentation/nvme-intel-market-name.html | 2 +- Documentation/nvme-intel-smart-log-add.html | 2 +- Documentation/nvme-intel-temp-stats.html | 2 +- Documentation/nvme-io-mgmt-recv.html | 2 +- Documentation/nvme-io-mgmt-send.html | 2 +- Documentation/nvme-io-passthru.html | 2 +- Documentation/nvme-lba-status-log.html | 2 +- Documentation/nvme-list-ctrl.html | 2 +- Documentation/nvme-list-endgrp.html | 2 +- Documentation/nvme-list-ns.html | 2 +- Documentation/nvme-list-subsys.html | 2 +- Documentation/nvme-list.html | 2 +- Documentation/nvme-lockdown.html | 2 +- Documentation/nvme-mi-cmd-support-effects-log.html | 2 +- Documentation/nvme-micron-clear-pcie-errors.html | 2 +- Documentation/nvme-micron-internal-log.html | 2 +- Documentation/nvme-micron-nand-stats.html | 2 +- Documentation/nvme-micron-pcie-stats.html | 2 +- Documentation/nvme-micron-selective-download.html | 2 +- Documentation/nvme-micron-smart-add-log.html | 2 +- Documentation/nvme-micron-temperature-stats.html | 2 +- Documentation/nvme-netapp-ontapdevices.html | 2 +- Documentation/nvme-netapp-smdevices.html | 2 +- Documentation/nvme-ns-descs.html | 2 +- Documentation/nvme-ns-rescan.html | 2 +- Documentation/nvme-nvm-id-ctrl.html | 2 +- Documentation/nvme-nvme-mi-recv.html | 2 +- Documentation/nvme-nvme-mi-send.html | 2 +- .../nvme-ocp-clear-fw-activate-history.html | 2 +- ...-ocp-clear-pcie-correctable-error-counters.html | 2 +- Documentation/nvme-ocp-eol-plp-failure-mode.html | 2 +- Documentation/nvme-ocp-error-recovery-log.txt | 2 +- Documentation/nvme-ocp-latency-monitor-log.html | 2 +- .../nvme-ocp-set-dssd-power-state-feature.txt | 42 + Documentation/nvme-ocp-smart-add-log.html | 2 +- Documentation/nvme-persistent-event-log.html | 2 +- Documentation/nvme-phy-rx-eom-log.txt | 59 + Documentation/nvme-pred-lat-event-agg-log.html | 2 +- Documentation/nvme-predictable-lat-log.html | 2 +- Documentation/nvme-primary-ctrl-caps.html | 2 +- Documentation/nvme-read.html | 2 +- Documentation/nvme-reset.html | 2 +- Documentation/nvme-resv-acquire.html | 2 +- Documentation/nvme-resv-notif-log.html | 2 +- Documentation/nvme-resv-register.html | 2 +- Documentation/nvme-resv-release.html | 2 +- Documentation/nvme-resv-report.html | 2 +- Documentation/nvme-rpmb.html | 2 +- Documentation/nvme-sanitize-log.html | 2 +- Documentation/nvme-sanitize.html | 2 +- .../nvme-seagate-clear-fw-activate-history.html | 2 +- ...nvme-seagate-clear-pcie-correctable-errors.html | 2 +- .../nvme-seagate-cloud-SSD-plugin-version.html | 2 +- Documentation/nvme-seagate-get-ctrl-tele.html | 2 +- Documentation/nvme-seagate-get-host-tele.html | 2 +- Documentation/nvme-seagate-help.html | 2 +- Documentation/nvme-seagate-plugin-version.html | 2 +- Documentation/nvme-seagate-version.html | 2 +- .../nvme-seagate-vs-fw-activate-history.html | 2 +- Documentation/nvme-seagate-vs-internal-log.html | 2 +- Documentation/nvme-seagate-vs-log-page-sup.html | 2 +- Documentation/nvme-seagate-vs-pcie-stats.html | 2 +- Documentation/nvme-seagate-vs-smart-add-log.html | 2 +- .../nvme-seagate-vs-temperature-stats.html | 2 +- Documentation/nvme-security-recv.html | 2 +- Documentation/nvme-security-send.html | 2 +- Documentation/nvme-self-test-log.html | 2 +- Documentation/nvme-set-feature.html | 2 +- Documentation/nvme-set-property.html | 2 +- Documentation/nvme-show-hostnqn.html | 2 +- Documentation/nvme-show-regs.html | 2 +- Documentation/nvme-show-topology.html | 2 +- Documentation/nvme-smart-log.html | 2 +- Documentation/nvme-subsystem-reset.html | 2 +- Documentation/nvme-supported-log-pages.html | 2 +- Documentation/nvme-telemetry-log.html | 2 +- ...nvme-toshiba-clear-pcie-correctable-errors.html | 2 +- Documentation/nvme-toshiba-vs-internal-log.html | 2 +- Documentation/nvme-toshiba-vs-smart-add-log.html | 2 +- Documentation/nvme-transcend-badblock.html | 2 +- Documentation/nvme-transcend-healthvalue.html | 2 +- Documentation/nvme-verify.html | 2 +- .../nvme-virtium-save-smart-to-vtview-log.html | 2 +- Documentation/nvme-virtium-show-identify.html | 2 +- Documentation/nvme-wdc-cap-diag.html | 2 +- Documentation/nvme-wdc-capabilities.html | 2 +- Documentation/nvme-wdc-clear-assert-dump.html | 2 +- .../nvme-wdc-clear-fw-activate-history.html | 2 +- .../nvme-wdc-clear-pcie-correctable-errors.html | 2 +- .../nvme-wdc-cloud-SSD-plugin-version.html | 2 +- Documentation/nvme-wdc-cloud-boot-SSD-version.html | 2 +- Documentation/nvme-wdc-drive-essentials.html | 2 +- Documentation/nvme-wdc-drive-log.html | 2 +- Documentation/nvme-wdc-drive-resize.html | 2 +- Documentation/nvme-wdc-enc-get-log.html | 2 +- Documentation/nvme-wdc-get-crash-dump.html | 2 +- .../nvme-wdc-get-dev-capabilities-log.html | 2 +- Documentation/nvme-wdc-get-drive-status.html | 2 +- Documentation/nvme-wdc-get-error-recovery-log.html | 2 +- .../nvme-wdc-get-latency-monitor-log.html | 2 +- Documentation/nvme-wdc-get-pfail-dump.html | 2 +- .../nvme-wdc-get-unsupported-reqs-log.html | 2 +- Documentation/nvme-wdc-id-ctrl.html | 2 +- Documentation/nvme-wdc-log-page-directory.html | 2 +- Documentation/nvme-wdc-namespace-resize.html | 2 +- Documentation/nvme-wdc-purge-monitor.html | 2 +- Documentation/nvme-wdc-purge.html | 2 +- Documentation/nvme-wdc-vs-cloud-log.html | 2 +- Documentation/nvme-wdc-vs-device-waf.html | 2 +- Documentation/nvme-wdc-vs-drive-info.html | 2 +- .../nvme-wdc-vs-error-reason-identifier.html | 2 +- Documentation/nvme-wdc-vs-fw-activate-history.html | 2 +- Documentation/nvme-wdc-vs-hw-rev-log.html | 2 +- Documentation/nvme-wdc-vs-internal-log.html | 2 +- Documentation/nvme-wdc-vs-nand-stats.html | 2 +- Documentation/nvme-wdc-vs-smart-add-log.html | 2 +- .../nvme-wdc-vs-telemetry-controller-option.html | 2 +- Documentation/nvme-wdc-vs-temperature-stats.html | 2 +- Documentation/nvme-write-uncor.html | 2 +- Documentation/nvme-write-zeroes.html | 2 +- Documentation/nvme-write.html | 2 +- Documentation/nvme-zns-changed-zone-list.html | 2 +- Documentation/nvme-zns-close-zone.html | 2 +- Documentation/nvme-zns-finish-zone.html | 2 +- Documentation/nvme-zns-id-ctrl.html | 2 +- Documentation/nvme-zns-id-ns.html | 2 +- Documentation/nvme-zns-offline-zone.html | 2 +- Documentation/nvme-zns-open-zone.html | 2 +- Documentation/nvme-zns-report-zones.html | 2 +- Documentation/nvme-zns-reset-zone.html | 2 +- Documentation/nvme-zns-set-zone-desc.html | 2 +- Documentation/nvme-zns-zone-append.html | 2 +- Documentation/nvme-zns-zone-mgmt-recv.html | 2 +- Documentation/nvme-zns-zone-mgmt-send.html | 2 +- Documentation/nvme.html | 2 +- README.md | 4 +- ccan/ccan/build_assert/_info | 49 - ccan/ccan/check_type/_info | 33 - ccan/ccan/compiler/LICENSE | 1 + ccan/ccan/compiler/compiler.h | 317 ++ ccan/ccan/container_of/_info | 65 - ccan/ccan/endian/_info | 55 - ccan/ccan/hash/LICENSE | 1 + ccan/ccan/hash/hash.c | 926 +++++ ccan/ccan/hash/hash.h | 313 ++ ccan/ccan/htable/LICENSE | 1 + ccan/ccan/htable/htable.c | 491 +++ ccan/ccan/htable/htable.h | 290 ++ ccan/ccan/htable/htable_type.h | 188 + ccan/ccan/ilog/LICENSE | 1 + ccan/ccan/ilog/ilog.c | 141 + ccan/ccan/ilog/ilog.h | 154 + ccan/ccan/likely/LICENSE | 1 + ccan/ccan/likely/likely.c | 136 + ccan/ccan/likely/likely.h | 111 + ccan/ccan/list/_info | 72 - ccan/ccan/short_types/LICENSE | 1 + ccan/ccan/short_types/short_types.h | 35 + ccan/ccan/str/_info | 52 - ccan/ccan/strset/strset.c | 309 ++ ccan/ccan/strset/strset.h | 167 + ccan/ccan/typesafe_cb/LICENSE | 1 + ccan/ccan/typesafe_cb/typesafe_cb.h | 134 + ccan/licenses/LGPL-2.1 | 510 +++ ccan/meson.build | 5 + completions/_nvme | 25 +- completions/bash-nvme-completion.sh | 12 +- fabrics.c | 145 +- fabrics.h | 2 +- meson.build | 9 +- nbft.c | 4 +- nvme-builtin.h | 1 + nvme-print-binary.c | 17 +- nvme-print-json.c | 185 +- nvme-print-stdout.c | 619 +++- nvme-print.c | 18 +- nvme-print.h | 6 +- nvme-rpmb.c | 4 +- nvme-wrap.c | 15 +- nvme-wrap.h | 5 +- nvme.c | 3802 +++++++++----------- nvme.h | 14 +- .../udev-rules/71-nvmf-iopolicy-netapp.rules.in | 4 +- plugins/intel/intel-nvme.c | 6 +- plugins/micron/micron-nvme.c | 4 +- plugins/ocp/ocp-nvme.c | 97 + plugins/ocp/ocp-nvme.h | 3 +- plugins/scaleflux/sfx-nvme.c | 8 +- plugins/solidigm/solidigm-telemetry/nlog.c | 5 +- plugins/wdc/wdc-nvme.c | 34 +- plugins/zns/zns.c | 24 +- scripts/build.sh | 41 +- subprojects/libnvme.wrap | 2 +- unit/test-uint128.c | 22 +- util/argconfig.c | 96 +- util/argconfig.h | 10 +- util/cleanup.h | 16 + util/json.h | 10 +- util/types.c | 7 +- 262 files changed, 7432 insertions(+), 3022 deletions(-) create mode 100644 .github/workflows/coverage.yml create mode 100644 Documentation/nvme-ocp-set-dssd-power-state-feature.txt create mode 100644 Documentation/nvme-phy-rx-eom-log.txt delete mode 100644 ccan/ccan/build_assert/_info delete mode 100644 ccan/ccan/check_type/_info create mode 120000 ccan/ccan/compiler/LICENSE create mode 100644 ccan/ccan/compiler/compiler.h delete mode 100644 ccan/ccan/container_of/_info delete mode 100644 ccan/ccan/endian/_info create mode 120000 ccan/ccan/hash/LICENSE create mode 100644 ccan/ccan/hash/hash.c create mode 100644 ccan/ccan/hash/hash.h create mode 120000 ccan/ccan/htable/LICENSE create mode 100644 ccan/ccan/htable/htable.c create mode 100644 ccan/ccan/htable/htable.h create mode 100644 ccan/ccan/htable/htable_type.h create mode 120000 ccan/ccan/ilog/LICENSE create mode 100644 ccan/ccan/ilog/ilog.c create mode 100644 ccan/ccan/ilog/ilog.h create mode 120000 ccan/ccan/likely/LICENSE create mode 100644 ccan/ccan/likely/likely.c create mode 100644 ccan/ccan/likely/likely.h delete mode 100644 ccan/ccan/list/_info create mode 120000 ccan/ccan/short_types/LICENSE create mode 100644 ccan/ccan/short_types/short_types.h delete mode 100644 ccan/ccan/str/_info create mode 100644 ccan/ccan/strset/strset.c create mode 100644 ccan/ccan/strset/strset.h create mode 120000 ccan/ccan/typesafe_cb/LICENSE create mode 100644 ccan/ccan/typesafe_cb/typesafe_cb.h create mode 100644 ccan/licenses/LGPL-2.1 diff --git a/.github/workflows/appimage.yml b/.github/workflows/appimage.yml index f7b7ae0..526c879 100644 --- a/.github/workflows/appimage.yml +++ b/.github/workflows/appimage.yml @@ -13,32 +13,22 @@ jobs: build-appimage: name: build AppImage runs-on: ubuntu-latest + container: + image: ghcr.io/igaw/linux-nvme/debian:latest steps: - - uses: actions/checkout@v3 - - name: install dependencies - run: sudo apt-get install libjson-c-dev libssl-dev libdbus-1-dev libhugetlbfs-dev - - uses: actions/setup-python@v4 - with: - python-version: '3.x' - - uses: BSFishy/meson-build@v1.0.3 - with: - setup-options: > - --werror - --buildtype=release - --prefix=/usr - --force-fallback-for=libnvme - -Dlibnvme:werror=false - action: install - meson-version: 0.61.2 - - name: build AppImage - uses: AppImageCrafters/build-appimage@v1.3 - with: - recipe: .github/AppImageBuilder.yml - - uses: actions/upload-artifact@v3 - name: upload artifacts to github - with: - name: AppImage - path: '*.AppImage*' + - uses: actions/checkout@v4 + - name: build + run: | + scripts/build.sh appimage + - name: build AppImage + uses: AppImageCrafters/build-appimage@v1.3 + with: + recipe: .github/AppImageBuilder.yml + - uses: actions/upload-artifact@v3 + name: upload artifacts to github + with: + name: AppImage + path: '*.AppImage*' deploy-appimage: name: deploy AppImage diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index e3e2fd4..9903645 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -17,12 +17,9 @@ jobs: compiler: [gcc, clang] buildtype: [debug, release] container: - image: ghcr.io/igaw/linux-nvme/debian:0.30 + image: ghcr.io/igaw/linux-nvme/debian.python:latest steps: - - uses: actions/checkout@v3 - - uses: actions/setup-python@v4 - with: - python-version: '3.x' + - uses: actions/checkout@v4 - name: build run: | scripts/build.sh -b ${{ matrix.buildtype }} -c ${{ matrix.compiler }} @@ -40,33 +37,22 @@ jobs: matrix: include: - arch: armhf - port: armhf - compiler: gcc-arm-linux-gnueabihf - packages: - arch: s390x - port: s390x - compiler: gcc-s390x-linux-gnu - packages: libgcc-s1:s390x - arch: ppc64le - port: ppc64el - compiler: gcc-powerpc64le-linux-gnu - packges: steps: - - uses: actions/checkout@v3 - - name: set up arm architecture - run: | - export release=$(lsb_release -c -s) - sudo dpkg --add-architecture ${{ matrix.port }} - sudo sed -i -e 's/deb http/deb [arch=amd64] http/g' /etc/apt/sources.list - sudo dd of=/etc/apt/sources.list.d/${{ matrix.arch }}.list < diff --git a/Documentation/nvme-ana-log.html b/Documentation/nvme-ana-log.html index b3d2ef5..db71a5c 100644 --- a/Documentation/nvme-ana-log.html +++ b/Documentation/nvme-ana-log.html @@ -823,7 +823,7 @@ Print the ANA log page in a human readable format: diff --git a/Documentation/nvme-attach-ns.html b/Documentation/nvme-attach-ns.html index a90e3dd..127aa33 100644 --- a/Documentation/nvme-attach-ns.html +++ b/Documentation/nvme-attach-ns.html @@ -817,7 +817,7 @@ controller identifiers.

diff --git a/Documentation/nvme-boot-part-log.html b/Documentation/nvme-boot-part-log.html index b327078..02b0cdc 100644 --- a/Documentation/nvme-boot-part-log.html +++ b/Documentation/nvme-boot-part-log.html @@ -835,7 +835,7 @@ Retrieve Boot Partition data to boot_part_log.bin diff --git a/Documentation/nvme-capacity-mgmt.html b/Documentation/nvme-capacity-mgmt.html index e0fdc4a..3d17b8b 100644 --- a/Documentation/nvme-capacity-mgmt.html +++ b/Documentation/nvme-capacity-mgmt.html @@ -839,7 +839,7 @@ device (ex: /dev/nvme0), or a namespace block device (ex: /dev/nvme0n1).

diff --git a/Documentation/nvme-changed-ns-list-log.html b/Documentation/nvme-changed-ns-list-log.html index 13b9f37..15ec1b9 100644 --- a/Documentation/nvme-changed-ns-list-log.html +++ b/Documentation/nvme-changed-ns-list-log.html @@ -835,7 +835,7 @@ Print the raw Changed Namespace List log to a file: diff --git a/Documentation/nvme-cmdset-ind-id-ns.html b/Documentation/nvme-cmdset-ind-id-ns.html index c4459b9..387a375 100644 --- a/Documentation/nvme-cmdset-ind-id-ns.html +++ b/Documentation/nvme-cmdset-ind-id-ns.html @@ -879,7 +879,7 @@ Have the program return the raw structure in binary: diff --git a/Documentation/nvme-compare.html b/Documentation/nvme-compare.html index 4adf97e..9ee2c38 100644 --- a/Documentation/nvme-compare.html +++ b/Documentation/nvme-compare.html @@ -1095,7 +1095,7 @@ metadata is passes.

diff --git a/Documentation/nvme-connect-all.html b/Documentation/nvme-connect-all.html index 4124714..ced16c3 100644 --- a/Documentation/nvme-connect-all.html +++ b/Documentation/nvme-connect-all.html @@ -1253,7 +1253,7 @@ nvme-connect(1)

diff --git a/Documentation/nvme-connect.html b/Documentation/nvme-connect.html index 4007df2..7b0c2e6 100644 --- a/Documentation/nvme-connect.html +++ b/Documentation/nvme-connect.html @@ -1211,7 +1211,7 @@ and Christoph Hellwig

diff --git a/Documentation/nvme-copy.html b/Documentation/nvme-copy.html index 875304c..e9f7ee3 100644 --- a/Documentation/nvme-copy.html +++ b/Documentation/nvme-copy.html @@ -982,7 +982,7 @@ logical block ranges to a single consecutive destination logical block range.

diff --git a/Documentation/nvme-create-ns.html b/Documentation/nvme-create-ns.html index fe15e74..c36f5b0 100644 --- a/Documentation/nvme-create-ns.html +++ b/Documentation/nvme-create-ns.html @@ -756,6 +756,7 @@ nvme-create-ns(1) Manual Page [--nmic=<nmic> | -m <nmic>] [--anagrp-id=<anagrpid> | -a <anagrpid>] [--nvmset-id=<nvmsetid> | -i <nvmsetid>] + [--endg-id=<endgid> | -e <endgid>] [--csi=<command_set_identifier> | -y <command_set_identifier>] [--lbstm=<lbstm> | -l <lbstm>] [--nphndls=<nphndls> | -n <nphndls>] @@ -866,6 +867,17 @@ device (ex: /dev/nvme0), or a namespace block device (ex: /dev/nvme0n1).

+-e <endgid> +
+
+--endg-id=<endgid> +
+
+

+ This field specifies the identifier of the endurance group. +

+
+
-y <command_set_identifier>
@@ -1033,7 +1045,7 @@ Create a namespace: diff --git a/Documentation/nvme-create-ns.txt b/Documentation/nvme-create-ns.txt index 5d1355d..28046b6 100644 --- a/Documentation/nvme-create-ns.txt +++ b/Documentation/nvme-create-ns.txt @@ -15,6 +15,7 @@ SYNOPSIS [--nmic= | -m ] [--anagrp-id= | -a ] [--nvmset-id= | -i ] + [--endg-id= | -e ] [--csi= | -y ] [--lbstm= | -l ] [--nphndls= | -n ] @@ -69,6 +70,10 @@ OPTIONS --nvmset-id=:: This field specifies the identifier of the NVM Set. +-e :: +--endg-id=:: + This field specifies the identifier of the endurance group. + -y :: --csi=:: This field specifies the identifier of command set. diff --git a/Documentation/nvme-delete-ns.html b/Documentation/nvme-delete-ns.html index 7004e10..7db9073 100644 --- a/Documentation/nvme-delete-ns.html +++ b/Documentation/nvme-delete-ns.html @@ -799,7 +799,7 @@ The '--namespace-id' option is mandatory.

diff --git a/Documentation/nvme-dera-stat.html b/Documentation/nvme-dera-stat.html index 41946f7..0dee6a3 100644 --- a/Documentation/nvme-dera-stat.html +++ b/Documentation/nvme-dera-stat.html @@ -797,7 +797,7 @@ Print the Dera Device status and Additional SMART log page in a human readable f diff --git a/Documentation/nvme-detach-ns.html b/Documentation/nvme-detach-ns.html index 2e8bcc1..0b6e583 100644 --- a/Documentation/nvme-detach-ns.html +++ b/Documentation/nvme-detach-ns.html @@ -810,7 +810,7 @@ controller identifiers.

diff --git a/Documentation/nvme-device-self-test.html b/Documentation/nvme-device-self-test.html index e5dfb38..f2180a9 100644 --- a/Documentation/nvme-device-self-test.html +++ b/Documentation/nvme-device-self-test.html @@ -848,7 +848,7 @@ Abort the device self-test operation in the namespace-id 1: diff --git a/Documentation/nvme-dim.html b/Documentation/nvme-dim.html index f5eb82c..e31d59a 100644 --- a/Documentation/nvme-dim.html +++ b/Documentation/nvme-dim.html @@ -863,7 +863,7 @@ Deregister from Central Discovery Controller (CDC) associated with nvme4 diff --git a/Documentation/nvme-dir-receive.html b/Documentation/nvme-dir-receive.html index e73882f..64ecda3 100644 --- a/Documentation/nvme-dir-receive.html +++ b/Documentation/nvme-dir-receive.html @@ -969,7 +969,7 @@ Get streams directive status : diff --git a/Documentation/nvme-dir-send.html b/Documentation/nvme-dir-send.html index c2acdb3..20d0c63 100644 --- a/Documentation/nvme-dir-send.html +++ b/Documentation/nvme-dir-send.html @@ -982,7 +982,7 @@ Release stream ID 3 : diff --git a/Documentation/nvme-disconnect-all.html b/Documentation/nvme-disconnect-all.html index afc08e0..d1a0402 100644 --- a/Documentation/nvme-disconnect-all.html +++ b/Documentation/nvme-disconnect-all.html @@ -795,7 +795,7 @@ Disconnect all existing nvme controllers: diff --git a/Documentation/nvme-disconnect.html b/Documentation/nvme-disconnect.html index 87df800..8c80005 100644 --- a/Documentation/nvme-disconnect.html +++ b/Documentation/nvme-disconnect.html @@ -839,7 +839,7 @@ Disconnect the controller nvme4 diff --git a/Documentation/nvme-discover.html b/Documentation/nvme-discover.html index bce1ae8..dfd3b79 100644 --- a/Documentation/nvme-discover.html +++ b/Documentation/nvme-discover.html @@ -1303,7 +1303,7 @@ nvme-connect-all(1)

diff --git a/Documentation/nvme-dsm.html b/Documentation/nvme-dsm.html index 58c1764..e6312a6 100644 --- a/Documentation/nvme-dsm.html +++ b/Documentation/nvme-dsm.html @@ -893,7 +893,7 @@ any settings from the flags may have provided.

diff --git a/Documentation/nvme-effects-log.html b/Documentation/nvme-effects-log.html index 23dc161..8a08210 100644 --- a/Documentation/nvme-effects-log.html +++ b/Documentation/nvme-effects-log.html @@ -847,7 +847,7 @@ Have the program return the raw structure in binary: diff --git a/Documentation/nvme-endurance-event-agg-log.html b/Documentation/nvme-endurance-event-agg-log.html index 771ce5a..f34dfa9 100644 --- a/Documentation/nvme-endurance-event-agg-log.html +++ b/Documentation/nvme-endurance-event-agg-log.html @@ -851,7 +851,7 @@ Print the raw Endurance log to a file: diff --git a/Documentation/nvme-endurance-log.html b/Documentation/nvme-endurance-log.html index 109bdf1..c286558 100644 --- a/Documentation/nvme-endurance-log.html +++ b/Documentation/nvme-endurance-log.html @@ -834,7 +834,7 @@ Print the raw Endurance log to a file: diff --git a/Documentation/nvme-error-log.html b/Documentation/nvme-error-log.html index d83eb97..148f4af 100644 --- a/Documentation/nvme-error-log.html +++ b/Documentation/nvme-error-log.html @@ -849,7 +849,7 @@ Print the raw output to a file: diff --git a/Documentation/nvme-fdp-configs.html b/Documentation/nvme-fdp-configs.html index 7035659..4ae6e1c 100644 --- a/Documentation/nvme-fdp-configs.html +++ b/Documentation/nvme-fdp-configs.html @@ -827,7 +827,7 @@ the possible configurations for Flexible Data Placement.

diff --git a/Documentation/nvme-fdp-events.html b/Documentation/nvme-fdp-events.html index 972d185..c7833d6 100644 --- a/Documentation/nvme-fdp-events.html +++ b/Documentation/nvme-fdp-events.html @@ -827,7 +827,7 @@ Units and media usage in an Endurance Group.

diff --git a/Documentation/nvme-fdp-set-events.html b/Documentation/nvme-fdp-set-events.html index 4541a6c..accaae6 100644 --- a/Documentation/nvme-fdp-set-events.html +++ b/Documentation/nvme-fdp-set-events.html @@ -817,7 +817,7 @@ Handle.

diff --git a/Documentation/nvme-fdp-stats.html b/Documentation/nvme-fdp-stats.html index 892bcf9..d5c4762 100644 --- a/Documentation/nvme-fdp-stats.html +++ b/Documentation/nvme-fdp-stats.html @@ -815,7 +815,7 @@ the life of the FDP configuration in an Endurance Group.

diff --git a/Documentation/nvme-fdp-status.html b/Documentation/nvme-fdp-status.html index d53f4f9..2ec10bf 100644 --- a/Documentation/nvme-fdp-status.html +++ b/Documentation/nvme-fdp-status.html @@ -815,7 +815,7 @@ are accessible by the specified namespace.

diff --git a/Documentation/nvme-fdp-update.html b/Documentation/nvme-fdp-update.html index 63e8042..0bcdeb5 100644 --- a/Documentation/nvme-fdp-update.html +++ b/Documentation/nvme-fdp-update.html @@ -802,7 +802,7 @@ a different Reclaim Unit accessible by the specified namespace.

diff --git a/Documentation/nvme-fdp-usage.html b/Documentation/nvme-fdp-usage.html index e789004..40adfdc 100644 --- a/Documentation/nvme-fdp-usage.html +++ b/Documentation/nvme-fdp-usage.html @@ -816,7 +816,7 @@ Endurance Group.

diff --git a/Documentation/nvme-fid-support-effects-log.html b/Documentation/nvme-fid-support-effects-log.html index ef42d4c..acd7e3b 100644 --- a/Documentation/nvme-fid-support-effects-log.html +++ b/Documentation/nvme-fid-support-effects-log.html @@ -814,7 +814,7 @@ raw buffer may be printed to stdout.

diff --git a/Documentation/nvme-flush.html b/Documentation/nvme-flush.html index d4f3dd1..5095761 100644 --- a/Documentation/nvme-flush.html +++ b/Documentation/nvme-flush.html @@ -800,7 +800,7 @@ any namespace.

diff --git a/Documentation/nvme-format.html b/Documentation/nvme-format.html index b617bb8..6afcbc2 100644 --- a/Documentation/nvme-format.html +++ b/Documentation/nvme-format.html @@ -1035,7 +1035,7 @@ information: diff --git a/Documentation/nvme-fw-commit.html b/Documentation/nvme-fw-commit.html index 3b84432..a76b0e1 100644 --- a/Documentation/nvme-fw-commit.html +++ b/Documentation/nvme-fw-commit.html @@ -905,7 +905,7 @@ commit the last downloaded fw to slot 1. diff --git a/Documentation/nvme-fw-download.html b/Documentation/nvme-fw-download.html index 2ca7499..b4ff3a7 100644 --- a/Documentation/nvme-fw-download.html +++ b/Documentation/nvme-fw-download.html @@ -852,7 +852,7 @@ Transfer a firmware size 128KiB at a time: diff --git a/Documentation/nvme-fw-log.html b/Documentation/nvme-fw-log.html index 37d313d..83eafeb 100644 --- a/Documentation/nvme-fw-log.html +++ b/Documentation/nvme-fw-log.html @@ -835,7 +835,7 @@ Print the log firmware to a file: diff --git a/Documentation/nvme-gen-hostnqn.html b/Documentation/nvme-gen-hostnqn.html index 4ae8da7..4eedd6d 100644 --- a/Documentation/nvme-gen-hostnqn.html +++ b/Documentation/nvme-gen-hostnqn.html @@ -785,7 +785,7 @@ and prints it to stdout.

diff --git a/Documentation/nvme-get-feature.html b/Documentation/nvme-get-feature.html index 7752e56..fc34d16 100644 --- a/Documentation/nvme-get-feature.html +++ b/Documentation/nvme-get-feature.html @@ -977,7 +977,7 @@ format: diff --git a/Documentation/nvme-get-lba-status.html b/Documentation/nvme-get-lba-status.html index c1a03a4..58a06ef 100644 --- a/Documentation/nvme-get-lba-status.html +++ b/Documentation/nvme-get-lba-status.html @@ -896,7 +896,7 @@ Get LBA Status of the namespace 1 from SLBA 10 for the max Dwords of 0x1000 diff --git a/Documentation/nvme-get-log.html b/Documentation/nvme-get-log.html index 6480f1b..1b4957c 100644 --- a/Documentation/nvme-get-log.html +++ b/Documentation/nvme-get-log.html @@ -973,7 +973,7 @@ Have the program return the raw log page in binary: diff --git a/Documentation/nvme-get-ns-id.html b/Documentation/nvme-get-ns-id.html index e4f0838..37cfc2c 100644 --- a/Documentation/nvme-get-ns-id.html +++ b/Documentation/nvme-get-ns-id.html @@ -794,7 +794,7 @@ Shows the namespace id for the given block device: diff --git a/Documentation/nvme-get-property.html b/Documentation/nvme-get-property.html index f6ebe4f..5f7cdd3 100644 --- a/Documentation/nvme-get-property.html +++ b/Documentation/nvme-get-property.html @@ -843,7 +843,7 @@ Then look for NVMe Fabrics command (0x7f) at trace diff --git a/Documentation/nvme-help.html b/Documentation/nvme-help.html index 102d647..2d93925 100644 --- a/Documentation/nvme-help.html +++ b/Documentation/nvme-help.html @@ -794,7 +794,7 @@ Show help for nvme smart log: diff --git a/Documentation/nvme-huawei-id-ctrl.html b/Documentation/nvme-huawei-id-ctrl.html index 608a2ac..2a4e7cd 100644 --- a/Documentation/nvme-huawei-id-ctrl.html +++ b/Documentation/nvme-huawei-id-ctrl.html @@ -855,7 +855,7 @@ fields in a human readable format: diff --git a/Documentation/nvme-huawei-list.html b/Documentation/nvme-huawei-list.html index ef99034..7c866d3 100644 --- a/Documentation/nvme-huawei-list.html +++ b/Documentation/nvme-huawei-list.html @@ -797,7 +797,7 @@ for those Huawei devices as well as some pertinent information about them.

diff --git a/Documentation/nvme-id-ctrl.html b/Documentation/nvme-id-ctrl.html index 5de0ca2..e845a2a 100644 --- a/Documentation/nvme-id-ctrl.html +++ b/Documentation/nvme-id-ctrl.html @@ -910,7 +910,7 @@ int main(int argc, char **argv) diff --git a/Documentation/nvme-id-domain.html b/Documentation/nvme-id-domain.html index 02dfee8..decc53a 100644 --- a/Documentation/nvme-id-domain.html +++ b/Documentation/nvme-id-domain.html @@ -812,7 +812,7 @@ device (ex: /dev/nvme0), or a namespace block device (ex: /dev/nvme0n1).

diff --git a/Documentation/nvme-id-iocs.html b/Documentation/nvme-id-iocs.html index f03b979..c3b2dc1 100644 --- a/Documentation/nvme-id-iocs.html +++ b/Documentation/nvme-id-iocs.html @@ -844,7 +844,7 @@ show the fields in human readable format diff --git a/Documentation/nvme-id-ns.html b/Documentation/nvme-id-ns.html index 5ada031..f28f969 100644 --- a/Documentation/nvme-id-ns.html +++ b/Documentation/nvme-id-ns.html @@ -955,7 +955,7 @@ int main(int argc, char **argv) diff --git a/Documentation/nvme-id-nvmset.html b/Documentation/nvme-id-nvmset.html index 7c75fc2..4da3bf7 100644 --- a/Documentation/nvme-id-nvmset.html +++ b/Documentation/nvme-id-nvmset.html @@ -851,7 +851,7 @@ as shown in the above example, or you can 'cat' a saved output buff diff --git a/Documentation/nvme-inspur-nvme-vendor-log.html b/Documentation/nvme-inspur-nvme-vendor-log.html index ccc8e9d..99f8840 100644 --- a/Documentation/nvme-inspur-nvme-vendor-log.html +++ b/Documentation/nvme-inspur-nvme-vendor-log.html @@ -796,7 +796,7 @@ Print the Inspur Device Vendor log page in a human readable format: diff --git a/Documentation/nvme-intel-id-ctrl.html b/Documentation/nvme-intel-id-ctrl.html index 32a720d..30c9e1a 100644 --- a/Documentation/nvme-intel-id-ctrl.html +++ b/Documentation/nvme-intel-id-ctrl.html @@ -853,7 +853,7 @@ fields in a human readable format: diff --git a/Documentation/nvme-intel-internal-log.html b/Documentation/nvme-intel-internal-log.html index 34bbcfe..060b62d 100644 --- a/Documentation/nvme-intel-internal-log.html +++ b/Documentation/nvme-intel-internal-log.html @@ -873,7 +873,7 @@ Gets the event log from the device and saves to defined file: diff --git a/Documentation/nvme-intel-lat-stats.html b/Documentation/nvme-intel-lat-stats.html index ee9c0df..3d72747 100644 --- a/Documentation/nvme-intel-lat-stats.html +++ b/Documentation/nvme-intel-lat-stats.html @@ -832,7 +832,7 @@ Get the write statistics diff --git a/Documentation/nvme-intel-market-name.html b/Documentation/nvme-intel-market-name.html index e29a60d..0e0e596 100644 --- a/Documentation/nvme-intel-market-name.html +++ b/Documentation/nvme-intel-market-name.html @@ -813,7 +813,7 @@ Get the marketing name diff --git a/Documentation/nvme-intel-smart-log-add.html b/Documentation/nvme-intel-smart-log-add.html index af356af..0af652b 100644 --- a/Documentation/nvme-intel-smart-log-add.html +++ b/Documentation/nvme-intel-smart-log-add.html @@ -850,7 +850,7 @@ Print the raw Intel Additional SMART log to a file: diff --git a/Documentation/nvme-intel-temp-stats.html b/Documentation/nvme-intel-temp-stats.html index 04de67b..d485ae2 100644 --- a/Documentation/nvme-intel-temp-stats.html +++ b/Documentation/nvme-intel-temp-stats.html @@ -822,7 +822,7 @@ Print the raw SMART log to a file: diff --git a/Documentation/nvme-io-mgmt-recv.html b/Documentation/nvme-io-mgmt-recv.html index 3c00355..72ace1b 100644 --- a/Documentation/nvme-io-mgmt-recv.html +++ b/Documentation/nvme-io-mgmt-recv.html @@ -846,7 +846,7 @@ a hex dump, or binary.

diff --git a/Documentation/nvme-io-mgmt-send.html b/Documentation/nvme-io-mgmt-send.html index a625024..7c455e1 100644 --- a/Documentation/nvme-io-mgmt-send.html +++ b/Documentation/nvme-io-mgmt-send.html @@ -845,7 +845,7 @@ convenience parameters to produce the binary payload.

diff --git a/Documentation/nvme-io-passthru.html b/Documentation/nvme-io-passthru.html index f65a513..09c58d8 100644 --- a/Documentation/nvme-io-passthru.html +++ b/Documentation/nvme-io-passthru.html @@ -993,7 +993,7 @@ printed to stdout for another program to parse.

diff --git a/Documentation/nvme-lba-status-log.html b/Documentation/nvme-lba-status-log.html index 76ef5c7..72274ba 100644 --- a/Documentation/nvme-lba-status-log.html +++ b/Documentation/nvme-lba-status-log.html @@ -831,7 +831,7 @@ NVME diff --git a/Documentation/nvme-list-ctrl.html b/Documentation/nvme-list-ctrl.html index 3ad61a4..68d540f 100644 --- a/Documentation/nvme-list-ctrl.html +++ b/Documentation/nvme-list-ctrl.html @@ -828,7 +828,7 @@ OPTIONS diff --git a/Documentation/nvme-list-endgrp.html b/Documentation/nvme-list-endgrp.html index c377757..caa86f8 100644 --- a/Documentation/nvme-list-endgrp.html +++ b/Documentation/nvme-list-endgrp.html @@ -815,7 +815,7 @@ than or equal to the value specified in the CDW11.ENDGID field.

diff --git a/Documentation/nvme-list-ns.html b/Documentation/nvme-list-ns.html index b06c669..b679420 100644 --- a/Documentation/nvme-list-ns.html +++ b/Documentation/nvme-list-ns.html @@ -859,7 +859,7 @@ Print the namespaces present for NVM Command Set in normal format diff --git a/Documentation/nvme-list-subsys.html b/Documentation/nvme-list-subsys.html index 0b82591..9e0fbec 100644 --- a/Documentation/nvme-list-subsys.html +++ b/Documentation/nvme-list-subsys.html @@ -854,7 +854,7 @@ nvme-subsys1 - NQN=nvmf-test2 diff --git a/Documentation/nvme-list.html b/Documentation/nvme-list.html index 976c90d..cfa91f3 100644 --- a/Documentation/nvme-list.html +++ b/Documentation/nvme-list.html @@ -816,7 +816,7 @@ for those devices as well as some pertinent information about them.

diff --git a/Documentation/nvme-lockdown.html b/Documentation/nvme-lockdown.html index f572ee1..7c75f20 100644 --- a/Documentation/nvme-lockdown.html +++ b/Documentation/nvme-lockdown.html @@ -850,7 +850,7 @@ Identifier.

diff --git a/Documentation/nvme-mi-cmd-support-effects-log.html b/Documentation/nvme-mi-cmd-support-effects-log.html index ef4846b..38ff067 100644 --- a/Documentation/nvme-mi-cmd-support-effects-log.html +++ b/Documentation/nvme-mi-cmd-support-effects-log.html @@ -815,7 +815,7 @@ raw buffer may be printed to stdout.

diff --git a/Documentation/nvme-micron-clear-pcie-errors.html b/Documentation/nvme-micron-clear-pcie-errors.html index 8458851..1b954df 100644 --- a/Documentation/nvme-micron-clear-pcie-errors.html +++ b/Documentation/nvme-micron-clear-pcie-errors.html @@ -798,7 +798,7 @@ Retrieve NAND statistics information diff --git a/Documentation/nvme-micron-internal-log.html b/Documentation/nvme-micron-internal-log.html index 02a1985..de274c2 100644 --- a/Documentation/nvme-micron-internal-log.html +++ b/Documentation/nvme-micron-internal-log.html @@ -813,7 +813,7 @@ Gets the logs from the device and saves to micron_logs.zip file diff --git a/Documentation/nvme-micron-nand-stats.html b/Documentation/nvme-micron-nand-stats.html index 919b86f..c3279b8 100644 --- a/Documentation/nvme-micron-nand-stats.html +++ b/Documentation/nvme-micron-nand-stats.html @@ -799,7 +799,7 @@ Retrieve NAND statistics information diff --git a/Documentation/nvme-micron-pcie-stats.html b/Documentation/nvme-micron-pcie-stats.html index 9efb712..4c64010 100644 --- a/Documentation/nvme-micron-pcie-stats.html +++ b/Documentation/nvme-micron-pcie-stats.html @@ -799,7 +799,7 @@ Retrieve PCIe error information diff --git a/Documentation/nvme-micron-selective-download.html b/Documentation/nvme-micron-selective-download.html index d041cb0..da0910e 100644 --- a/Documentation/nvme-micron-selective-download.html +++ b/Documentation/nvme-micron-selective-download.html @@ -867,7 +867,7 @@ Update eeprom, OOB and main firmware diff --git a/Documentation/nvme-micron-smart-add-log.html b/Documentation/nvme-micron-smart-add-log.html index c87e321..b4a9025 100644 --- a/Documentation/nvme-micron-smart-add-log.html +++ b/Documentation/nvme-micron-smart-add-log.html @@ -808,7 +808,7 @@ Retrieve NAND/extended SMART data and display in json format diff --git a/Documentation/nvme-micron-temperature-stats.html b/Documentation/nvme-micron-temperature-stats.html index 061aad0..68f9f61 100644 --- a/Documentation/nvme-micron-temperature-stats.html +++ b/Documentation/nvme-micron-temperature-stats.html @@ -799,7 +799,7 @@ Retrieve temperature information diff --git a/Documentation/nvme-netapp-ontapdevices.html b/Documentation/nvme-netapp-ontapdevices.html index 6292259..be92dc2 100644 --- a/Documentation/nvme-netapp-ontapdevices.html +++ b/Documentation/nvme-netapp-ontapdevices.html @@ -807,7 +807,7 @@ Display information, in a column-based format, for ONTAP devices. diff --git a/Documentation/nvme-netapp-smdevices.html b/Documentation/nvme-netapp-smdevices.html index b8e896d..8546e9d 100644 --- a/Documentation/nvme-netapp-smdevices.html +++ b/Documentation/nvme-netapp-smdevices.html @@ -809,7 +809,7 @@ namespace. diff --git a/Documentation/nvme-ns-descs.html b/Documentation/nvme-ns-descs.html index 192432b..8388846 100644 --- a/Documentation/nvme-ns-descs.html +++ b/Documentation/nvme-ns-descs.html @@ -857,7 +857,7 @@ Have the program return the raw structure in binary: diff --git a/Documentation/nvme-ns-rescan.html b/Documentation/nvme-ns-rescan.html index 7202c4b..0386119 100644 --- a/Documentation/nvme-ns-rescan.html +++ b/Documentation/nvme-ns-rescan.html @@ -794,7 +794,7 @@ Rescans the nvme namespaces. diff --git a/Documentation/nvme-nvm-id-ctrl.html b/Documentation/nvme-nvm-id-ctrl.html index 25be91a..8acedc2 100644 --- a/Documentation/nvme-nvm-id-ctrl.html +++ b/Documentation/nvme-nvm-id-ctrl.html @@ -821,7 +821,7 @@ Show the output in json format diff --git a/Documentation/nvme-nvme-mi-recv.html b/Documentation/nvme-nvme-mi-recv.html index 7c8ce29..800d716 100644 --- a/Documentation/nvme-nvme-mi-recv.html +++ b/Documentation/nvme-nvme-mi-recv.html @@ -882,7 +882,7 @@ Has the program issue a nvme-mi-recv to execute the VPD read. diff --git a/Documentation/nvme-nvme-mi-send.html b/Documentation/nvme-nvme-mi-send.html index 00a6e26..09fb05e 100644 --- a/Documentation/nvme-nvme-mi-send.html +++ b/Documentation/nvme-nvme-mi-send.html @@ -882,7 +882,7 @@ Has the program issue a nvme-mi-send to execute the VPD write. diff --git a/Documentation/nvme-ocp-clear-fw-activate-history.html b/Documentation/nvme-ocp-clear-fw-activate-history.html index 24825f8..8894de2 100644 --- a/Documentation/nvme-ocp-clear-fw-activate-history.html +++ b/Documentation/nvme-ocp-clear-fw-activate-history.html @@ -817,7 +817,7 @@ Clears OCP Firmware Activation History Log for the device: diff --git a/Documentation/nvme-ocp-clear-pcie-correctable-error-counters.html b/Documentation/nvme-ocp-clear-pcie-correctable-error-counters.html index 48d3b9a..60a98dc 100644 --- a/Documentation/nvme-ocp-clear-pcie-correctable-error-counters.html +++ b/Documentation/nvme-ocp-clear-pcie-correctable-error-counters.html @@ -817,7 +817,7 @@ Clears PCIe correctable error counters Log for the device: diff --git a/Documentation/nvme-ocp-eol-plp-failure-mode.html b/Documentation/nvme-ocp-eol-plp-failure-mode.html index 8b43ad7..ebc4436 100644 --- a/Documentation/nvme-ocp-eol-plp-failure-mode.html +++ b/Documentation/nvme-ocp-eol-plp-failure-mode.html @@ -884,7 +884,7 @@ Has the program issue a eol-plp-failure-mode to retrieve the 0xC2 get features. diff --git a/Documentation/nvme-ocp-error-recovery-log.txt b/Documentation/nvme-ocp-error-recovery-log.txt index edbf4e6..1ce9dd1 100644 --- a/Documentation/nvme-ocp-error-recovery-log.txt +++ b/Documentation/nvme-ocp-error-recovery-log.txt @@ -34,7 +34,7 @@ EXAMPLES * Has the program issue a error-recovery-log command to retrieve the 0xC1 log page. + ------------ -# nvme ocp unsupported-reqs-log /dev/nvme0 -o normal +# nvme ocp error-recovery-log /dev/nvme0 -o normal ------------ NVME diff --git a/Documentation/nvme-ocp-latency-monitor-log.html b/Documentation/nvme-ocp-latency-monitor-log.html index 88282ad..420a35f 100644 --- a/Documentation/nvme-ocp-latency-monitor-log.html +++ b/Documentation/nvme-ocp-latency-monitor-log.html @@ -811,7 +811,7 @@ Displays the get latency monitor log for the device: diff --git a/Documentation/nvme-ocp-set-dssd-power-state-feature.txt b/Documentation/nvme-ocp-set-dssd-power-state-feature.txt new file mode 100644 index 0000000..7dfe9ea --- /dev/null +++ b/Documentation/nvme-ocp-set-dssd-power-state-feature.txt @@ -0,0 +1,42 @@ +set-dssd-power-state-feature(1) +=============================== + +NAME +---- +nvme-ocp-set-dssd-power-state-feature - Set DSSD Power State + +SYNOPSIS +-------- +[verse] +'nvme ocp set-dssd-power-state-feature' [--power-state= | -p ] [--no-uuid | -n] + [--save | -s] + +DESCRIPTION +----------- +For the NVMe device given, retrieves OCP DSSD Power state Feature + +The parameter is mandatory and may be either the NVMe character +device (ex: /dev/nvme0) or block device (ex: /dev/nvme0n1). + +This will only work on OCP compliant devices supporting this feature. +Results for any other device are undefined. + +On success it returns 0, error code otherwise. + +OPTIONS +------- +-p :: +--power-state=:: + DSSD Power State to set in watts. + +EXAMPLES +-------- +* Has the program issue a set-dssd-power-state-feature command to set DSSD Power State to set in watts. ++ +------------ +# nvme ocp set-dssd-power-state-feature /dev/nvme0 -p -s -n +------------ + +NVME +---- +Part of the nvme-user suite. diff --git a/Documentation/nvme-ocp-smart-add-log.html b/Documentation/nvme-ocp-smart-add-log.html index 9757c3d..b41fd2c 100644 --- a/Documentation/nvme-ocp-smart-add-log.html +++ b/Documentation/nvme-ocp-smart-add-log.html @@ -812,7 +812,7 @@ Has the program issue a smart-add-log command to retrieve the 0xC0 log page. diff --git a/Documentation/nvme-persistent-event-log.html b/Documentation/nvme-persistent-event-log.html index f960bea..d64081d 100644 --- a/Documentation/nvme-persistent-event-log.html +++ b/Documentation/nvme-persistent-event-log.html @@ -867,7 +867,7 @@ Print the raw persistent event log to a file: diff --git a/Documentation/nvme-phy-rx-eom-log.txt b/Documentation/nvme-phy-rx-eom-log.txt new file mode 100644 index 0000000..015c851 --- /dev/null +++ b/Documentation/nvme-phy-rx-eom-log.txt @@ -0,0 +1,59 @@ +nvme-phy-rx-eom-log(1) +====================== + +NAME +---- +nvme-phy-rx-eom-log - Retrieves a Physical Interface Receiver Eye Opening Measurement log page from an NVMe device + +SYNOPSIS +-------- +[verse] +'nvme phy-rx-eom-log' [--lsp= | -s ] + [--controller= | -c ] + [--output-format= | -o ] + +DESCRIPTION +----------- +Retrieves a Physical Interface Receiver Eye Opening Measurement log page from +an NVMe device and provides the returned structure. + +The parameter is mandatory and may be either the NVMe character +device (ex: /dev/nvme0), or a namespace block device (ex: /dev/nvme0n1). + +On success it returns 0, error code otherwise. + +OPTIONS +------- +-s :: +--lsp=:: + The log specified field configuring the controller's action to take + during processing of the command and the measurement quality. + +-c :: +--controller=:: + Controller ID of the controller associated wit the PCIe port to be + measured. + +-o :: +--output-format=:: + Set the reporting format to 'normal', 'json', or + 'binary'. Only one output format can be used at a time. + +EXAMPLES +-------- +* Start a best quality measurement and retrieve the log page header ++ +------------ +# nvme phy-rx-eom-log /dev/nvme0 --lsp=10 +------------ + +* Retrieve a finished best quality measurement on controller with ID 3 ++ +------------ +# nvme phy-rx-eom-log /dev/nvme0 --lsp=2 --controller=3 +------------ + + +NVME +---- +Part of the nvme-user suite diff --git a/Documentation/nvme-pred-lat-event-agg-log.html b/Documentation/nvme-pred-lat-event-agg-log.html index bead028..c2ea7fe 100644 --- a/Documentation/nvme-pred-lat-event-agg-log.html +++ b/Documentation/nvme-pred-lat-event-agg-log.html @@ -863,7 +863,7 @@ Print the raw Predictable Latency Event Aggregate log to a file: diff --git a/Documentation/nvme-predictable-lat-log.html b/Documentation/nvme-predictable-lat-log.html index 5419947..b74dbbf 100644 --- a/Documentation/nvme-predictable-lat-log.html +++ b/Documentation/nvme-predictable-lat-log.html @@ -850,7 +850,7 @@ Print the raw Predictable latency per NVM set log to a file: diff --git a/Documentation/nvme-primary-ctrl-caps.html b/Documentation/nvme-primary-ctrl-caps.html index dfeec00..8af0344 100644 --- a/Documentation/nvme-primary-ctrl-caps.html +++ b/Documentation/nvme-primary-ctrl-caps.html @@ -835,7 +835,7 @@ fields in a human readable format: diff --git a/Documentation/nvme-read.html b/Documentation/nvme-read.html index a881ec2..2904f3d 100644 --- a/Documentation/nvme-read.html +++ b/Documentation/nvme-read.html @@ -1068,7 +1068,7 @@ metadata is passes.

diff --git a/Documentation/nvme-reset.html b/Documentation/nvme-reset.html index 1296dc7..74f963a 100644 --- a/Documentation/nvme-reset.html +++ b/Documentation/nvme-reset.html @@ -794,7 +794,7 @@ Resets the controller. diff --git a/Documentation/nvme-resv-acquire.html b/Documentation/nvme-resv-acquire.html index fbbd31d..12a3526 100644 --- a/Documentation/nvme-resv-acquire.html +++ b/Documentation/nvme-resv-acquire.html @@ -948,7 +948,7 @@ cellspacing="0" cellpadding="4"> diff --git a/Documentation/nvme-resv-notif-log.html b/Documentation/nvme-resv-notif-log.html index 05775f5..ef5cfea 100644 --- a/Documentation/nvme-resv-notif-log.html +++ b/Documentation/nvme-resv-notif-log.html @@ -822,7 +822,7 @@ Print the output in json format: diff --git a/Documentation/nvme-resv-register.html b/Documentation/nvme-resv-register.html index ed2481e..072acf2 100644 --- a/Documentation/nvme-resv-register.html +++ b/Documentation/nvme-resv-register.html @@ -937,7 +937,7 @@ cellspacing="0" cellpadding="4"> diff --git a/Documentation/nvme-resv-release.html b/Documentation/nvme-resv-release.html index c02dd1b..1504945 100644 --- a/Documentation/nvme-resv-release.html +++ b/Documentation/nvme-resv-release.html @@ -930,7 +930,7 @@ cellspacing="0" cellpadding="4"> diff --git a/Documentation/nvme-resv-report.html b/Documentation/nvme-resv-report.html index 0a0e33b..0f085f9 100644 --- a/Documentation/nvme-resv-report.html +++ b/Documentation/nvme-resv-report.html @@ -855,7 +855,7 @@ Controller data structure for each such controller).

diff --git a/Documentation/nvme-rpmb.html b/Documentation/nvme-rpmb.html index 490fd07..97d80a1 100644 --- a/Documentation/nvme-rpmb.html +++ b/Documentation/nvme-rpmb.html @@ -1001,7 +1001,7 @@ data onto output.bin diff --git a/Documentation/nvme-sanitize-log.html b/Documentation/nvme-sanitize-log.html index d421e62..4ed2e90 100644 --- a/Documentation/nvme-sanitize-log.html +++ b/Documentation/nvme-sanitize-log.html @@ -892,7 +892,7 @@ Has the program issue Sanitize-log Command : diff --git a/Documentation/nvme-sanitize.html b/Documentation/nvme-sanitize.html index a9f3e99..32b0e04 100644 --- a/Documentation/nvme-sanitize.html +++ b/Documentation/nvme-sanitize.html @@ -938,7 +938,7 @@ Has the program issue Sanitize Command : diff --git a/Documentation/nvme-seagate-clear-fw-activate-history.html b/Documentation/nvme-seagate-clear-fw-activate-history.html index dbba892..6dc0d10 100644 --- a/Documentation/nvme-seagate-clear-fw-activate-history.html +++ b/Documentation/nvme-seagate-clear-fw-activate-history.html @@ -792,7 +792,7 @@ nvme block device (ex: /dev/nvme0n1).

diff --git a/Documentation/nvme-seagate-clear-pcie-correctable-errors.html b/Documentation/nvme-seagate-clear-pcie-correctable-errors.html index 32cde9c..feb6ad6 100644 --- a/Documentation/nvme-seagate-clear-pcie-correctable-errors.html +++ b/Documentation/nvme-seagate-clear-pcie-correctable-errors.html @@ -802,7 +802,7 @@ nvme block device (ex: /dev/nvme0n1).

diff --git a/Documentation/nvme-seagate-cloud-SSD-plugin-version.html b/Documentation/nvme-seagate-cloud-SSD-plugin-version.html index a70616a..bc93aad 100644 --- a/Documentation/nvme-seagate-cloud-SSD-plugin-version.html +++ b/Documentation/nvme-seagate-cloud-SSD-plugin-version.html @@ -787,7 +787,7 @@ nvme-seagate-cloud-SSD-plugin-version (1) Manual Page diff --git a/Documentation/nvme-seagate-get-ctrl-tele.html b/Documentation/nvme-seagate-get-ctrl-tele.html index 9455a72..4374c55 100644 --- a/Documentation/nvme-seagate-get-ctrl-tele.html +++ b/Documentation/nvme-seagate-get-ctrl-tele.html @@ -813,7 +813,7 @@ nvme block device (ex: /dev/nvme0n1).

diff --git a/Documentation/nvme-seagate-get-host-tele.html b/Documentation/nvme-seagate-get-host-tele.html index 84d5a36..da3b81b 100644 --- a/Documentation/nvme-seagate-get-host-tele.html +++ b/Documentation/nvme-seagate-get-host-tele.html @@ -825,7 +825,7 @@ nvme block device (ex: /dev/nvme0n1).

diff --git a/Documentation/nvme-seagate-help.html b/Documentation/nvme-seagate-help.html index eb24703..304d08b 100644 --- a/Documentation/nvme-seagate-help.html +++ b/Documentation/nvme-seagate-help.html @@ -812,7 +812,7 @@ help Display this help diff --git a/Documentation/nvme-seagate-plugin-version.html b/Documentation/nvme-seagate-plugin-version.html index 2e188e3..5a65c48 100644 --- a/Documentation/nvme-seagate-plugin-version.html +++ b/Documentation/nvme-seagate-plugin-version.html @@ -787,7 +787,7 @@ nvme-seagate-plugin-version(1) Manual Page diff --git a/Documentation/nvme-seagate-version.html b/Documentation/nvme-seagate-version.html index e8bee0d..62b9af0 100644 --- a/Documentation/nvme-seagate-version.html +++ b/Documentation/nvme-seagate-version.html @@ -787,7 +787,7 @@ nvme-seagate-version(1) Manual Page diff --git a/Documentation/nvme-seagate-vs-fw-activate-history.html b/Documentation/nvme-seagate-vs-fw-activate-history.html index bfc9572..0f45465 100644 --- a/Documentation/nvme-seagate-vs-fw-activate-history.html +++ b/Documentation/nvme-seagate-vs-fw-activate-history.html @@ -813,7 +813,7 @@ nvme block device (ex: /dev/nvme0n1).

diff --git a/Documentation/nvme-seagate-vs-internal-log.html b/Documentation/nvme-seagate-vs-internal-log.html index 19283d1..35e42bb 100644 --- a/Documentation/nvme-seagate-vs-internal-log.html +++ b/Documentation/nvme-seagate-vs-internal-log.html @@ -813,7 +813,7 @@ nvme block device (ex: /dev/nvme0n1).

diff --git a/Documentation/nvme-seagate-vs-log-page-sup.html b/Documentation/nvme-seagate-vs-log-page-sup.html index e8be1aa..44765aa 100644 --- a/Documentation/nvme-seagate-vs-log-page-sup.html +++ b/Documentation/nvme-seagate-vs-log-page-sup.html @@ -814,7 +814,7 @@ LogPage-Id LogPage-Name diff --git a/Documentation/nvme-seagate-vs-pcie-stats.html b/Documentation/nvme-seagate-vs-pcie-stats.html index c85cf25..b43cba0 100644 --- a/Documentation/nvme-seagate-vs-pcie-stats.html +++ b/Documentation/nvme-seagate-vs-pcie-stats.html @@ -802,7 +802,7 @@ nvme block device (ex: /dev/nvme0n1).

diff --git a/Documentation/nvme-seagate-vs-smart-add-log.html b/Documentation/nvme-seagate-vs-smart-add-log.html index e34c938..ddd4cd6 100644 --- a/Documentation/nvme-seagate-vs-smart-add-log.html +++ b/Documentation/nvme-seagate-vs-smart-add-log.html @@ -830,7 +830,7 @@ all commands work across all product families.

diff --git a/Documentation/nvme-seagate-vs-temperature-stats.html b/Documentation/nvme-seagate-vs-temperature-stats.html index 2b3ab2a..75e7c65 100644 --- a/Documentation/nvme-seagate-vs-temperature-stats.html +++ b/Documentation/nvme-seagate-vs-temperature-stats.html @@ -802,7 +802,7 @@ nvme block device (ex: /dev/nvme0n1).

diff --git a/Documentation/nvme-security-recv.html b/Documentation/nvme-security-recv.html index 34f1982..3eacdff 100644 --- a/Documentation/nvme-security-recv.html +++ b/Documentation/nvme-security-recv.html @@ -886,7 +886,7 @@ controller reset occurs.

diff --git a/Documentation/nvme-security-send.html b/Documentation/nvme-security-send.html index a345ded..b2ee749 100644 --- a/Documentation/nvme-security-send.html +++ b/Documentation/nvme-security-send.html @@ -872,7 +872,7 @@ Receive command is Security Protocol field dependent as defined in SPC-4.

diff --git a/Documentation/nvme-self-test-log.html b/Documentation/nvme-self-test-log.html index 00c0490..8490729 100644 --- a/Documentation/nvme-self-test-log.html +++ b/Documentation/nvme-self-test-log.html @@ -847,7 +847,7 @@ Get the self-test-log and print it in a json format: diff --git a/Documentation/nvme-set-feature.html b/Documentation/nvme-set-feature.html index 41d8018..7fcb060 100644 --- a/Documentation/nvme-set-feature.html +++ b/Documentation/nvme-set-feature.html @@ -899,7 +899,7 @@ Sets the host id to the ascii string. diff --git a/Documentation/nvme-set-property.html b/Documentation/nvme-set-property.html index 873b83b..ff25412 100644 --- a/Documentation/nvme-set-property.html +++ b/Documentation/nvme-set-property.html @@ -805,7 +805,7 @@ nvme-set-property(1) Manual Page diff --git a/Documentation/nvme-show-hostnqn.html b/Documentation/nvme-show-hostnqn.html index 19ee45e..e31b27d 100644 --- a/Documentation/nvme-show-hostnqn.html +++ b/Documentation/nvme-show-hostnqn.html @@ -785,7 +785,7 @@ this will show the systemd-generated host NQN for the system.

diff --git a/Documentation/nvme-show-regs.html b/Documentation/nvme-show-regs.html index 79ed622..194b73c 100644 --- a/Documentation/nvme-show-regs.html +++ b/Documentation/nvme-show-regs.html @@ -848,7 +848,7 @@ in a json format: diff --git a/Documentation/nvme-show-topology.html b/Documentation/nvme-show-topology.html index 7675a6b..27556a3 100644 --- a/Documentation/nvme-show-topology.html +++ b/Documentation/nvme-show-topology.html @@ -822,7 +822,7 @@ nvme-show-topology(1) Manual Page diff --git a/Documentation/nvme-smart-log.html b/Documentation/nvme-smart-log.html index 5c62ef2..ba0605f 100644 --- a/Documentation/nvme-smart-log.html +++ b/Documentation/nvme-smart-log.html @@ -850,7 +850,7 @@ Print the raw SMART log to a file: diff --git a/Documentation/nvme-subsystem-reset.html b/Documentation/nvme-subsystem-reset.html index 51e27cc..d888938 100644 --- a/Documentation/nvme-subsystem-reset.html +++ b/Documentation/nvme-subsystem-reset.html @@ -794,7 +794,7 @@ Resets the subsystem. diff --git a/Documentation/nvme-supported-log-pages.html b/Documentation/nvme-supported-log-pages.html index 228ee9a..2fe7126 100644 --- a/Documentation/nvme-supported-log-pages.html +++ b/Documentation/nvme-supported-log-pages.html @@ -813,7 +813,7 @@ for each command that is supported.

diff --git a/Documentation/nvme-telemetry-log.html b/Documentation/nvme-telemetry-log.html index bcd352e..5cc5978 100644 --- a/Documentation/nvme-telemetry-log.html +++ b/Documentation/nvme-telemetry-log.html @@ -838,7 +838,7 @@ Retrieve Telemetry Host-Initiated data to telemetry_log.bin diff --git a/Documentation/nvme-toshiba-clear-pcie-correctable-errors.html b/Documentation/nvme-toshiba-clear-pcie-correctable-errors.html index 88b176d..ad20764 100644 --- a/Documentation/nvme-toshiba-clear-pcie-correctable-errors.html +++ b/Documentation/nvme-toshiba-clear-pcie-correctable-errors.html @@ -791,7 +791,7 @@ Clear the PCIe correctable errors count: diff --git a/Documentation/nvme-toshiba-vs-internal-log.html b/Documentation/nvme-toshiba-vs-internal-log.html index e520bfc..32641bb 100644 --- a/Documentation/nvme-toshiba-vs-internal-log.html +++ b/Documentation/nvme-toshiba-vs-internal-log.html @@ -837,7 +837,7 @@ Get the previous log from the device and save to a binary file: diff --git a/Documentation/nvme-toshiba-vs-smart-add-log.html b/Documentation/nvme-toshiba-vs-smart-add-log.html index f9ff173..25dffa5 100644 --- a/Documentation/nvme-toshiba-vs-smart-add-log.html +++ b/Documentation/nvme-toshiba-vs-smart-add-log.html @@ -840,7 +840,7 @@ Get the contents of log page 0xC0 from the device and save to a binary file: diff --git a/Documentation/nvme-transcend-badblock.html b/Documentation/nvme-transcend-badblock.html index f8df9fa..002c564 100644 --- a/Documentation/nvme-transcend-badblock.html +++ b/Documentation/nvme-transcend-badblock.html @@ -796,7 +796,7 @@ Print the Transcend device’s bad blocks in a human readable format: diff --git a/Documentation/nvme-transcend-healthvalue.html b/Documentation/nvme-transcend-healthvalue.html index 51668ee..2bd62ee 100644 --- a/Documentation/nvme-transcend-healthvalue.html +++ b/Documentation/nvme-transcend-healthvalue.html @@ -796,7 +796,7 @@ Print the Transcend Device health value in a human readable format: diff --git a/Documentation/nvme-verify.html b/Documentation/nvme-verify.html index 5616b77..2873604 100644 --- a/Documentation/nvme-verify.html +++ b/Documentation/nvme-verify.html @@ -953,7 +953,7 @@ metadata is passes.

diff --git a/Documentation/nvme-virtium-save-smart-to-vtview-log.html b/Documentation/nvme-virtium-save-smart-to-vtview-log.html index 7140680..b8d3d70 100644 --- a/Documentation/nvme-virtium-save-smart-to-vtview-log.html +++ b/Documentation/nvme-virtium-save-smart-to-vtview-log.html @@ -876,7 +876,7 @@ Just logging: Default logging is run for 20 hours and log every 10 hours. diff --git a/Documentation/nvme-virtium-show-identify.html b/Documentation/nvme-virtium-show-identify.html index ab4cb8e..31b9112 100644 --- a/Documentation/nvme-virtium-show-identify.html +++ b/Documentation/nvme-virtium-show-identify.html @@ -798,7 +798,7 @@ Show Identify Device: diff --git a/Documentation/nvme-wdc-cap-diag.html b/Documentation/nvme-wdc-cap-diag.html index 29a0cd6..b3d496c 100644 --- a/Documentation/nvme-wdc-cap-diag.html +++ b/Documentation/nvme-wdc-cap-diag.html @@ -856,7 +856,7 @@ Gets the capture diagnostics log from the device transferring the data in 16k ch diff --git a/Documentation/nvme-wdc-capabilities.html b/Documentation/nvme-wdc-capabilities.html index 3109d51..f43e981 100644 --- a/Documentation/nvme-wdc-capabilities.html +++ b/Documentation/nvme-wdc-capabilities.html @@ -789,7 +789,7 @@ Displays the capabilities for the device: diff --git a/Documentation/nvme-wdc-clear-assert-dump.html b/Documentation/nvme-wdc-clear-assert-dump.html index a8eed74..922bc67 100644 --- a/Documentation/nvme-wdc-clear-assert-dump.html +++ b/Documentation/nvme-wdc-clear-assert-dump.html @@ -798,7 +798,7 @@ Clears the assert dump (if present): diff --git a/Documentation/nvme-wdc-clear-fw-activate-history.html b/Documentation/nvme-wdc-clear-fw-activate-history.html index ed34302..c23cddb 100644 --- a/Documentation/nvme-wdc-clear-fw-activate-history.html +++ b/Documentation/nvme-wdc-clear-fw-activate-history.html @@ -797,7 +797,7 @@ Clears the firmware activate history table: diff --git a/Documentation/nvme-wdc-clear-pcie-correctable-errors.html b/Documentation/nvme-wdc-clear-pcie-correctable-errors.html index bbbc8d4..a795771 100644 --- a/Documentation/nvme-wdc-clear-pcie-correctable-errors.html +++ b/Documentation/nvme-wdc-clear-pcie-correctable-errors.html @@ -799,7 +799,7 @@ Clears the PCIe Correctable Error Count field returned in the smart-log-add comm diff --git a/Documentation/nvme-wdc-cloud-SSD-plugin-version.html b/Documentation/nvme-wdc-cloud-SSD-plugin-version.html index c94e7a5..4272148 100644 --- a/Documentation/nvme-wdc-cloud-SSD-plugin-version.html +++ b/Documentation/nvme-wdc-cloud-SSD-plugin-version.html @@ -790,7 +790,7 @@ Displays the cloud ssd plugin version for the device: diff --git a/Documentation/nvme-wdc-cloud-boot-SSD-version.html b/Documentation/nvme-wdc-cloud-boot-SSD-version.html index b536657..29c0058 100644 --- a/Documentation/nvme-wdc-cloud-boot-SSD-version.html +++ b/Documentation/nvme-wdc-cloud-boot-SSD-version.html @@ -790,7 +790,7 @@ Displays the cloud boot ssd version for the device: diff --git a/Documentation/nvme-wdc-drive-essentials.html b/Documentation/nvme-wdc-drive-essentials.html index 48c4a83..618ccbf 100644 --- a/Documentation/nvme-wdc-drive-essentials.html +++ b/Documentation/nvme-wdc-drive-essentials.html @@ -821,7 +821,7 @@ Gets the drive essentials data files from the device and saves the tar file to s diff --git a/Documentation/nvme-wdc-drive-log.html b/Documentation/nvme-wdc-drive-log.html index 4c442be..d1a44f3 100644 --- a/Documentation/nvme-wdc-drive-log.html +++ b/Documentation/nvme-wdc-drive-log.html @@ -829,7 +829,7 @@ Gets the drive log from the device and saves to defined file with pathname (e.g. diff --git a/Documentation/nvme-wdc-drive-resize.html b/Documentation/nvme-wdc-drive-resize.html index f1a952f..b38489c 100644 --- a/Documentation/nvme-wdc-drive-resize.html +++ b/Documentation/nvme-wdc-drive-resize.html @@ -810,7 +810,7 @@ Has the program issue WDC Resize Vendor Unique Command : diff --git a/Documentation/nvme-wdc-enc-get-log.html b/Documentation/nvme-wdc-enc-get-log.html index 7ff9cdd..5eae1d3 100644 --- a/Documentation/nvme-wdc-enc-get-log.html +++ b/Documentation/nvme-wdc-enc-get-log.html @@ -832,7 +832,7 @@ Gets the enclosure log from the device based on the log id(0xd2) with default tr diff --git a/Documentation/nvme-wdc-get-crash-dump.html b/Documentation/nvme-wdc-get-crash-dump.html index 639d665..f900b25 100644 --- a/Documentation/nvme-wdc-get-crash-dump.html +++ b/Documentation/nvme-wdc-get-crash-dump.html @@ -830,7 +830,7 @@ Gets the crash dump from the device and saves to defined file with pathname (e.g diff --git a/Documentation/nvme-wdc-get-dev-capabilities-log.html b/Documentation/nvme-wdc-get-dev-capabilities-log.html index d2627dd..6ee46c0 100644 --- a/Documentation/nvme-wdc-get-dev-capabilities-log.html +++ b/Documentation/nvme-wdc-get-dev-capabilities-log.html @@ -816,7 +816,7 @@ Has the program issue WDC get-dev-capabilities-log plugin command : diff --git a/Documentation/nvme-wdc-get-drive-status.html b/Documentation/nvme-wdc-get-drive-status.html index c3d5735..4376b59 100644 --- a/Documentation/nvme-wdc-get-drive-status.html +++ b/Documentation/nvme-wdc-get-drive-status.html @@ -836,7 +836,7 @@ Has the program issue WDC get-drive-status command : diff --git a/Documentation/nvme-wdc-get-error-recovery-log.html b/Documentation/nvme-wdc-get-error-recovery-log.html index 9d2352c..156396b 100644 --- a/Documentation/nvme-wdc-get-error-recovery-log.html +++ b/Documentation/nvme-wdc-get-error-recovery-log.html @@ -816,7 +816,7 @@ Has the program issue WDC get-error-recovery-log plugin command : diff --git a/Documentation/nvme-wdc-get-latency-monitor-log.html b/Documentation/nvme-wdc-get-latency-monitor-log.html index 438217a..2019853 100644 --- a/Documentation/nvme-wdc-get-latency-monitor-log.html +++ b/Documentation/nvme-wdc-get-latency-monitor-log.html @@ -810,7 +810,7 @@ Displays the get latency monitor log for the device: diff --git a/Documentation/nvme-wdc-get-pfail-dump.html b/Documentation/nvme-wdc-get-pfail-dump.html index 20f55a9..4a9010e 100644 --- a/Documentation/nvme-wdc-get-pfail-dump.html +++ b/Documentation/nvme-wdc-get-pfail-dump.html @@ -832,7 +832,7 @@ Gets the pfail crash dump from the device and saves to defined file with pathnam diff --git a/Documentation/nvme-wdc-get-unsupported-reqs-log.html b/Documentation/nvme-wdc-get-unsupported-reqs-log.html index 8645c49..cd63df1 100644 --- a/Documentation/nvme-wdc-get-unsupported-reqs-log.html +++ b/Documentation/nvme-wdc-get-unsupported-reqs-log.html @@ -816,7 +816,7 @@ Has the program issue WDC get-unsupported-reqs-log plugin command : diff --git a/Documentation/nvme-wdc-id-ctrl.html b/Documentation/nvme-wdc-id-ctrl.html index 1ce9fa3..c423221 100644 --- a/Documentation/nvme-wdc-id-ctrl.html +++ b/Documentation/nvme-wdc-id-ctrl.html @@ -856,7 +856,7 @@ fields in a human readable format: diff --git a/Documentation/nvme-wdc-log-page-directory.html b/Documentation/nvme-wdc-log-page-directory.html index 03769b7..3f416de 100644 --- a/Documentation/nvme-wdc-log-page-directory.html +++ b/Documentation/nvme-wdc-log-page-directory.html @@ -812,7 +812,7 @@ WDC log-page-directory example command : diff --git a/Documentation/nvme-wdc-namespace-resize.html b/Documentation/nvme-wdc-namespace-resize.html index 419acc4..1eb751f 100644 --- a/Documentation/nvme-wdc-namespace-resize.html +++ b/Documentation/nvme-wdc-namespace-resize.html @@ -838,7 +838,7 @@ Resizes namespace 2 to 7% of the original TNVMCAP reported value: diff --git a/Documentation/nvme-wdc-purge-monitor.html b/Documentation/nvme-wdc-purge-monitor.html index a96def6..c3fb22e 100644 --- a/Documentation/nvme-wdc-purge-monitor.html +++ b/Documentation/nvme-wdc-purge-monitor.html @@ -837,7 +837,7 @@ Has the program issue WDC Purge-Monitor Vendor Unique Command : diff --git a/Documentation/nvme-wdc-purge.html b/Documentation/nvme-wdc-purge.html index 710914d..9597d3d 100644 --- a/Documentation/nvme-wdc-purge.html +++ b/Documentation/nvme-wdc-purge.html @@ -799,7 +799,7 @@ Has the program issue WDC Purge Vendor Unique Command : diff --git a/Documentation/nvme-wdc-vs-cloud-log.html b/Documentation/nvme-wdc-vs-cloud-log.html index 337ea65..7eae4da 100644 --- a/Documentation/nvme-wdc-vs-cloud-log.html +++ b/Documentation/nvme-wdc-vs-cloud-log.html @@ -828,7 +828,7 @@ Has the program issue WDC vs-cloud-log Vendor Unique Command : diff --git a/Documentation/nvme-wdc-vs-device-waf.html b/Documentation/nvme-wdc-vs-device-waf.html index d62dd6c..73baeae 100644 --- a/Documentation/nvme-wdc-vs-device-waf.html +++ b/Documentation/nvme-wdc-vs-device-waf.html @@ -828,7 +828,7 @@ Has the program issue WDC vs-device-waf plugin Command : diff --git a/Documentation/nvme-wdc-vs-drive-info.html b/Documentation/nvme-wdc-vs-drive-info.html index fe6b5d8..ed6e1fe 100644 --- a/Documentation/nvme-wdc-vs-drive-info.html +++ b/Documentation/nvme-wdc-vs-drive-info.html @@ -795,7 +795,7 @@ on the drive:

diff --git a/Documentation/nvme-wdc-vs-error-reason-identifier.html b/Documentation/nvme-wdc-vs-error-reason-identifier.html index d107c8a..56f64e6 100644 --- a/Documentation/nvme-wdc-vs-error-reason-identifier.html +++ b/Documentation/nvme-wdc-vs-error-reason-identifier.html @@ -836,7 +836,7 @@ Retrieves the controller initiated error reason identifier field and save it in diff --git a/Documentation/nvme-wdc-vs-fw-activate-history.html b/Documentation/nvme-wdc-vs-fw-activate-history.html index 53d3478..b9d997a 100644 --- a/Documentation/nvme-wdc-vs-fw-activate-history.html +++ b/Documentation/nvme-wdc-vs-fw-activate-history.html @@ -868,7 +868,7 @@ Has the program issue WDC vs-fw-activate-history Vendor Unique Command : diff --git a/Documentation/nvme-wdc-vs-hw-rev-log.html b/Documentation/nvme-wdc-vs-hw-rev-log.html index 06dcc28..7bcd303 100644 --- a/Documentation/nvme-wdc-vs-hw-rev-log.html +++ b/Documentation/nvme-wdc-vs-hw-rev-log.html @@ -827,7 +827,7 @@ Has the program issue WDC vs-hw-rev-log plugin Command : diff --git a/Documentation/nvme-wdc-vs-internal-log.html b/Documentation/nvme-wdc-vs-internal-log.html index 2c9a34a..f7c0e0a 100644 --- a/Documentation/nvme-wdc-vs-internal-log.html +++ b/Documentation/nvme-wdc-vs-internal-log.html @@ -950,7 +950,7 @@ Gets the controller telemetry log page to data area 3 from the device and stores diff --git a/Documentation/nvme-wdc-vs-nand-stats.html b/Documentation/nvme-wdc-vs-nand-stats.html index 1ea9f8f..f354711 100644 --- a/Documentation/nvme-wdc-vs-nand-stats.html +++ b/Documentation/nvme-wdc-vs-nand-stats.html @@ -814,7 +814,7 @@ Has the program issue WDC vs-nand-stats Vendor Unique Command : diff --git a/Documentation/nvme-wdc-vs-smart-add-log.html b/Documentation/nvme-wdc-vs-smart-add-log.html index 6abbe25..1c204dd 100644 --- a/Documentation/nvme-wdc-vs-smart-add-log.html +++ b/Documentation/nvme-wdc-vs-smart-add-log.html @@ -927,7 +927,7 @@ Has the program issue WDC vs-smart-add-log Vendor Unique Command for 0xC0 and 0x diff --git a/Documentation/nvme-wdc-vs-telemetry-controller-option.html b/Documentation/nvme-wdc-vs-telemetry-controller-option.html index 8599741..2593a94 100644 --- a/Documentation/nvme-wdc-vs-telemetry-controller-option.html +++ b/Documentation/nvme-wdc-vs-telemetry-controller-option.html @@ -853,7 +853,7 @@ Gets the current status (enabled or disabled) of the controller initiated option diff --git a/Documentation/nvme-wdc-vs-temperature-stats.html b/Documentation/nvme-wdc-vs-temperature-stats.html index 6ba4aa1..775305e 100644 --- a/Documentation/nvme-wdc-vs-temperature-stats.html +++ b/Documentation/nvme-wdc-vs-temperature-stats.html @@ -857,7 +857,7 @@ Displays the temperature stats for the device: diff --git a/Documentation/nvme-write-uncor.html b/Documentation/nvme-write-uncor.html index 4c69628..dd6d6e0 100644 --- a/Documentation/nvme-write-uncor.html +++ b/Documentation/nvme-write-uncor.html @@ -844,7 +844,7 @@ blocks.

diff --git a/Documentation/nvme-write-zeroes.html b/Documentation/nvme-write-zeroes.html index a596cd5..65293cb 100644 --- a/Documentation/nvme-write-zeroes.html +++ b/Documentation/nvme-write-zeroes.html @@ -989,7 +989,7 @@ metadata is passes.

diff --git a/Documentation/nvme-write.html b/Documentation/nvme-write.html index c268201..ed44f0d 100644 --- a/Documentation/nvme-write.html +++ b/Documentation/nvme-write.html @@ -1090,7 +1090,7 @@ metadata is passes.

diff --git a/Documentation/nvme-zns-changed-zone-list.html b/Documentation/nvme-zns-changed-zone-list.html index a2a48c4..befb331 100644 --- a/Documentation/nvme-zns-changed-zone-list.html +++ b/Documentation/nvme-zns-changed-zone-list.html @@ -833,7 +833,7 @@ Show the output in json format diff --git a/Documentation/nvme-zns-close-zone.html b/Documentation/nvme-zns-close-zone.html index 8212189..67c0178 100644 --- a/Documentation/nvme-zns-close-zone.html +++ b/Documentation/nvme-zns-close-zone.html @@ -846,7 +846,7 @@ Close all zones on namespace 1: diff --git a/Documentation/nvme-zns-finish-zone.html b/Documentation/nvme-zns-finish-zone.html index fe8fbfc..be00fcd 100644 --- a/Documentation/nvme-zns-finish-zone.html +++ b/Documentation/nvme-zns-finish-zone.html @@ -847,7 +847,7 @@ Finish all zones on namespace 1: diff --git a/Documentation/nvme-zns-id-ctrl.html b/Documentation/nvme-zns-id-ctrl.html index 7279c65..8e0c56e 100644 --- a/Documentation/nvme-zns-id-ctrl.html +++ b/Documentation/nvme-zns-id-ctrl.html @@ -821,7 +821,7 @@ Show the output in json format diff --git a/Documentation/nvme-zns-id-ns.html b/Documentation/nvme-zns-id-ns.html index 7f56f7e..1b72104 100644 --- a/Documentation/nvme-zns-id-ns.html +++ b/Documentation/nvme-zns-id-ns.html @@ -847,7 +847,7 @@ Show the output in json format with extra details diff --git a/Documentation/nvme-zns-offline-zone.html b/Documentation/nvme-zns-offline-zone.html index 9d12c96..5e2e126 100644 --- a/Documentation/nvme-zns-offline-zone.html +++ b/Documentation/nvme-zns-offline-zone.html @@ -846,7 +846,7 @@ Offline all zones on namespace 1: diff --git a/Documentation/nvme-zns-open-zone.html b/Documentation/nvme-zns-open-zone.html index fda136e..31ab991 100644 --- a/Documentation/nvme-zns-open-zone.html +++ b/Documentation/nvme-zns-open-zone.html @@ -858,7 +858,7 @@ Open the first zone on namespace 1: diff --git a/Documentation/nvme-zns-report-zones.html b/Documentation/nvme-zns-report-zones.html index 79fdb2a..d3107e8 100644 --- a/Documentation/nvme-zns-report-zones.html +++ b/Documentation/nvme-zns-report-zones.html @@ -957,7 +957,7 @@ Show the output in json format with extra details diff --git a/Documentation/nvme-zns-reset-zone.html b/Documentation/nvme-zns-reset-zone.html index 2b3d58c..8f57ec9 100644 --- a/Documentation/nvme-zns-reset-zone.html +++ b/Documentation/nvme-zns-reset-zone.html @@ -847,7 +847,7 @@ Reset the first zone on namespace 1: diff --git a/Documentation/nvme-zns-set-zone-desc.html b/Documentation/nvme-zns-set-zone-desc.html index ddf9779..06c0c3e 100644 --- a/Documentation/nvme-zns-set-zone-desc.html +++ b/Documentation/nvme-zns-set-zone-desc.html @@ -859,7 +859,7 @@ Write "hello world" into the zone descriptor for namespace 1’s first zone diff --git a/Documentation/nvme-zns-zone-append.html b/Documentation/nvme-zns-zone-append.html index 2dd499f..e9c6c96 100644 --- a/Documentation/nvme-zns-zone-append.html +++ b/Documentation/nvme-zns-zone-append.html @@ -940,7 +940,7 @@ Append the data "hello world" into 4k worth of blocks into the zone starting diff --git a/Documentation/nvme-zns-zone-mgmt-recv.html b/Documentation/nvme-zns-zone-mgmt-recv.html index cebdb7e..1427d89 100644 --- a/Documentation/nvme-zns-zone-mgmt-recv.html +++ b/Documentation/nvme-zns-zone-mgmt-recv.html @@ -882,7 +882,7 @@ Binary dump of a report all zones diff --git a/Documentation/nvme-zns-zone-mgmt-send.html b/Documentation/nvme-zns-zone-mgmt-send.html index 424c2ff..c35410f 100644 --- a/Documentation/nvme-zns-zone-mgmt-send.html +++ b/Documentation/nvme-zns-zone-mgmt-send.html @@ -916,7 +916,7 @@ Write "hello world" into the zone descriptor for namespace 1’s first zone diff --git a/Documentation/nvme.html b/Documentation/nvme.html index 9ab55c8..3881c50 100644 --- a/Documentation/nvme.html +++ b/Documentation/nvme.html @@ -2114,7 +2114,7 @@ NVM-Express Site.

diff --git a/README.md b/README.md index d969769..04781e7 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # nvme-cli ![Coverity Scan Build Status](https://scan.coverity.com/projects/24883/badge.svg) -![MesonBuild](https://github.com/linux-nvme/nvme-cli/actions/workflows/meson.yml/badge.svg) +![MesonBuild](https://github.com/linux-nvme/nvme-cli/actions/workflows/build.yml/badge.svg) ![GitHub](https://img.shields.io/github/license/linux-nvme/nvme-cli) NVM-Express user space tooling for Linux. @@ -15,7 +15,7 @@ nvme-cli uses meson as build system. |---------|------------|-------| | libnvme, libnvme-mi| yes | be either installed or included into the build via meson fallback feature | | json-c | optional | recommended, without all plugins are disabled and json-c output format is disabled | - | libhugetblfs | optional | adds support for hugetblfs | + | libhugetlbfs | optional | adds support for hugetlbfs | ### Configuring diff --git a/ccan/ccan/build_assert/_info b/ccan/ccan/build_assert/_info deleted file mode 100644 index 97ebe6c..0000000 --- a/ccan/ccan/build_assert/_info +++ /dev/null @@ -1,49 +0,0 @@ -#include "config.h" -#include -#include - -/** - * build_assert - routines for build-time assertions - * - * This code provides routines which will cause compilation to fail should some - * assertion be untrue: such failures are preferable to run-time assertions, - * but much more limited since they can only depends on compile-time constants. - * - * These assertions are most useful when two parts of the code must be kept in - * sync: it is better to avoid such cases if possible, but seconds best is to - * detect invalid changes at build time. - * - * For example, a tricky piece of code might rely on a certain element being at - * the start of the structure. To ensure that future changes don't break it, - * you would catch such changes in your code like so: - * - * Example: - * #include - * #include - * - * struct foo { - * char string[5]; - * int x; - * }; - * - * static char *foo_string(struct foo *foo) - * { - * // This trick requires that the string be first in the structure - * BUILD_ASSERT(offsetof(struct foo, string) == 0); - * return (char *)foo; - * } - * - * License: CC0 (Public domain) - * Author: Rusty Russell - */ -int main(int argc, char *argv[]) -{ - if (argc != 2) - return 1; - - if (strcmp(argv[1], "depends") == 0) - /* Nothing. */ - return 0; - - return 1; -} diff --git a/ccan/ccan/check_type/_info b/ccan/ccan/check_type/_info deleted file mode 100644 index cc42673..0000000 --- a/ccan/ccan/check_type/_info +++ /dev/null @@ -1,33 +0,0 @@ -#include "config.h" -#include -#include - -/** - * check_type - routines for compile time type checking - * - * C has fairly weak typing: ints get automatically converted to longs, signed - * to unsigned, etc. There are some cases where this is best avoided, and - * these macros provide methods for evoking warnings (or build errors) when - * a precise type isn't used. - * - * On compilers which don't support typeof() these routines are less effective, - * since they have to use sizeof() which can only distiguish between types of - * different size. - * - * License: CC0 (Public domain) - * Author: Rusty Russell - */ -int main(int argc, char *argv[]) -{ - if (argc != 2) - return 1; - - if (strcmp(argv[1], "depends") == 0) { -#if !HAVE_TYPEOF - printf("ccan/build_assert\n"); -#endif - return 0; - } - - return 1; -} diff --git a/ccan/ccan/compiler/LICENSE b/ccan/ccan/compiler/LICENSE new file mode 120000 index 0000000..b7951da --- /dev/null +++ b/ccan/ccan/compiler/LICENSE @@ -0,0 +1 @@ +../../licenses/CC0 \ No newline at end of file diff --git a/ccan/ccan/compiler/compiler.h b/ccan/ccan/compiler/compiler.h new file mode 100644 index 0000000..562b29e --- /dev/null +++ b/ccan/ccan/compiler/compiler.h @@ -0,0 +1,317 @@ +/* CC0 (Public domain) - see LICENSE file for details */ +#ifndef CCAN_COMPILER_H +#define CCAN_COMPILER_H +#include "config.h" + +#ifndef COLD +#if HAVE_ATTRIBUTE_COLD +/** + * COLD - a function is unlikely to be called. + * + * Used to mark an unlikely code path and optimize appropriately. + * It is usually used on logging or error routines. + * + * Example: + * static void COLD moan(const char *reason) + * { + * fprintf(stderr, "Error: %s (%s)\n", reason, strerror(errno)); + * } + */ +#define COLD __attribute__((__cold__)) +#else +#define COLD +#endif +#endif + +#ifndef NORETURN +#if HAVE_ATTRIBUTE_NORETURN +/** + * NORETURN - a function does not return + * + * Used to mark a function which exits; useful for suppressing warnings. + * + * Example: + * static void NORETURN fail(const char *reason) + * { + * fprintf(stderr, "Error: %s (%s)\n", reason, strerror(errno)); + * exit(1); + * } + */ +#define NORETURN __attribute__((__noreturn__)) +#else +#define NORETURN +#endif +#endif + +#ifndef PRINTF_FMT +#if HAVE_ATTRIBUTE_PRINTF +/** + * PRINTF_FMT - a function takes printf-style arguments + * @nfmt: the 1-based number of the function's format argument. + * @narg: the 1-based number of the function's first variable argument. + * + * This allows the compiler to check your parameters as it does for printf(). + * + * Example: + * void PRINTF_FMT(2,3) my_printf(const char *prefix, const char *fmt, ...); + */ +#define PRINTF_FMT(nfmt, narg) \ + __attribute__((format(__printf__, nfmt, narg))) +#else +#define PRINTF_FMT(nfmt, narg) +#endif +#endif + +#ifndef CONST_FUNCTION +#if HAVE_ATTRIBUTE_CONST +/** + * CONST_FUNCTION - a function's return depends only on its argument + * + * This allows the compiler to assume that the function will return the exact + * same value for the exact same arguments. This implies that the function + * must not use global variables, or dereference pointer arguments. + */ +#define CONST_FUNCTION __attribute__((__const__)) +#else +#define CONST_FUNCTION +#endif + +#ifndef PURE_FUNCTION +#if HAVE_ATTRIBUTE_PURE +/** + * PURE_FUNCTION - a function is pure + * + * A pure function is one that has no side effects other than it's return value + * and uses no inputs other than it's arguments and global variables. + */ +#define PURE_FUNCTION __attribute__((__pure__)) +#else +#define PURE_FUNCTION +#endif +#endif +#endif + +#if HAVE_ATTRIBUTE_UNUSED +#ifndef UNNEEDED +/** + * UNNEEDED - a variable/function may not be needed + * + * This suppresses warnings about unused variables or functions, but tells + * the compiler that if it is unused it need not emit it into the source code. + * + * Example: + * // With some preprocessor options, this is unnecessary. + * static UNNEEDED int counter; + * + * // With some preprocessor options, this is unnecessary. + * static UNNEEDED void add_to_counter(int add) + * { + * counter += add; + * } + */ +#define UNNEEDED __attribute__((__unused__)) +#endif + +#ifndef NEEDED +#if HAVE_ATTRIBUTE_USED +/** + * NEEDED - a variable/function is needed + * + * This suppresses warnings about unused variables or functions, but tells + * the compiler that it must exist even if it (seems) unused. + * + * Example: + * // Even if this is unused, these are vital for debugging. + * static NEEDED int counter; + * static NEEDED void dump_counter(void) + * { + * printf("Counter is %i\n", counter); + * } + */ +#define NEEDED __attribute__((__used__)) +#else +/* Before used, unused functions and vars were always emitted. */ +#define NEEDED __attribute__((__unused__)) +#endif +#endif + +#ifndef UNUSED +/** + * UNUSED - a parameter is unused + * + * Some compilers (eg. gcc with -W or -Wunused) warn about unused + * function parameters. This suppresses such warnings and indicates + * to the reader that it's deliberate. + * + * Example: + * // This is used as a callback, so needs to have this prototype. + * static int some_callback(void *unused UNUSED) + * { + * return 0; + * } + */ +#define UNUSED __attribute__((__unused__)) +#endif +#else +#ifndef UNNEEDED +#define UNNEEDED +#endif +#ifndef NEEDED +#define NEEDED +#endif +#ifndef UNUSED +#define UNUSED +#endif +#endif + +#ifndef IS_COMPILE_CONSTANT +#if HAVE_BUILTIN_CONSTANT_P +/** + * IS_COMPILE_CONSTANT - does the compiler know the value of this expression? + * @expr: the expression to evaluate + * + * When an expression manipulation is complicated, it is usually better to + * implement it in a function. However, if the expression being manipulated is + * known at compile time, it is better to have the compiler see the entire + * expression so it can simply substitute the result. + * + * This can be done using the IS_COMPILE_CONSTANT() macro. + * + * Example: + * enum greek { ALPHA, BETA, GAMMA, DELTA, EPSILON }; + * + * // Out-of-line version. + * const char *greek_name(enum greek greek); + * + * // Inline version. + * static inline const char *_greek_name(enum greek greek) + * { + * switch (greek) { + * case ALPHA: return "alpha"; + * case BETA: return "beta"; + * case GAMMA: return "gamma"; + * case DELTA: return "delta"; + * case EPSILON: return "epsilon"; + * default: return "**INVALID**"; + * } + * } + * + * // Use inline if compiler knows answer. Otherwise call function + * // to avoid copies of the same code everywhere. + * #define greek_name(g) \ + * (IS_COMPILE_CONSTANT(greek) ? _greek_name(g) : greek_name(g)) + */ +#define IS_COMPILE_CONSTANT(expr) __builtin_constant_p(expr) +#else +/* If we don't know, assume it's not. */ +#define IS_COMPILE_CONSTANT(expr) 0 +#endif +#endif + +#ifndef WARN_UNUSED_RESULT +#if HAVE_WARN_UNUSED_RESULT +/** + * WARN_UNUSED_RESULT - warn if a function return value is unused. + * + * Used to mark a function where it is extremely unlikely that the caller + * can ignore the result, eg realloc(). + * + * Example: + * // buf param may be freed by this; need return value! + * static char *WARN_UNUSED_RESULT enlarge(char *buf, unsigned *size) + * { + * return realloc(buf, (*size) *= 2); + * } + */ +#define WARN_UNUSED_RESULT __attribute__((__warn_unused_result__)) +#else +#define WARN_UNUSED_RESULT +#endif +#endif + + +#if HAVE_ATTRIBUTE_DEPRECATED +/** + * WARN_DEPRECATED - warn that a function/type/variable is deprecated when used. + * + * Used to mark a function, type or variable should not be used. + * + * Example: + * WARN_DEPRECATED char *oldfunc(char *buf); + */ +#define WARN_DEPRECATED __attribute__((__deprecated__)) +#else +#define WARN_DEPRECATED +#endif + + +#if HAVE_ATTRIBUTE_NONNULL +/** + * NO_NULL_ARGS - specify that no arguments to this function can be NULL. + * + * The compiler will warn if any pointer args are NULL. + * + * Example: + * NO_NULL_ARGS char *my_copy(char *buf); + */ +#define NO_NULL_ARGS __attribute__((__nonnull__)) + +/** + * NON_NULL_ARGS - specify that some arguments to this function can't be NULL. + * @...: 1-based argument numbers for which args can't be NULL. + * + * The compiler will warn if any of the specified pointer args are NULL. + * + * Example: + * char *my_copy2(char *buf, char *maybenull) NON_NULL_ARGS(1); + */ +#define NON_NULL_ARGS(...) __attribute__((__nonnull__(__VA_ARGS__))) +#else +#define NO_NULL_ARGS +#define NON_NULL_ARGS(...) +#endif + +#if HAVE_ATTRIBUTE_RETURNS_NONNULL +/** + * RETURNS_NONNULL - specify that this function cannot return NULL. + * + * Mainly an optimization opportunity, but can also suppress warnings. + * + * Example: + * RETURNS_NONNULL char *my_copy(char *buf); + */ +#define RETURNS_NONNULL __attribute__((__returns_nonnull__)) +#else +#define RETURNS_NONNULL +#endif + +#if HAVE_ATTRIBUTE_SENTINEL +/** + * LAST_ARG_NULL - specify the last argument of a variadic function must be NULL. + * + * The compiler will warn if the last argument isn't NULL. + * + * Example: + * char *join_string(char *buf, ...) LAST_ARG_NULL; + */ +#define LAST_ARG_NULL __attribute__((__sentinel__)) +#else +#define LAST_ARG_NULL +#endif + +#if HAVE_BUILTIN_CPU_SUPPORTS +/** + * cpu_supports - test if current CPU supports the named feature. + * + * This takes a literal string, and currently only works on glibc platforms. + * + * Example: + * if (cpu_supports("mmx")) + * printf("MMX support engaged!\n"); + */ +#define cpu_supports(x) __builtin_cpu_supports(x) +#else +#define cpu_supports(x) 0 +#endif /* HAVE_BUILTIN_CPU_SUPPORTS */ + +#endif /* CCAN_COMPILER_H */ diff --git a/ccan/ccan/container_of/_info b/ccan/ccan/container_of/_info deleted file mode 100644 index b116052..0000000 --- a/ccan/ccan/container_of/_info +++ /dev/null @@ -1,65 +0,0 @@ -#include "config.h" -#include -#include - -/** - * container_of - routine for upcasting - * - * It is often convenient to create code where the caller registers a pointer - * to a generic structure and a callback. The callback might know that the - * pointer points to within a larger structure, and container_of gives a - * convenient and fairly type-safe way of returning to the enclosing structure. - * - * This idiom is an alternative to providing a void * pointer for every - * callback. - * - * Example: - * #include - * #include - * - * struct timer { - * void *members; - * }; - * - * struct info { - * int my_stuff; - * struct timer timer; - * }; - * - * static void my_timer_callback(struct timer *timer) - * { - * struct info *info = container_of(timer, struct info, timer); - * printf("my_stuff is %u\n", info->my_stuff); - * } - * - * static void register_timer(struct timer *timer) - * { - * (void)timer; - * (void)my_timer_callback; - * //... - * } - * - * int main(void) - * { - * struct info info = { .my_stuff = 1 }; - * - * register_timer(&info.timer); - * // ... - * return 0; - * } - * - * License: CC0 (Public domain) - * Author: Rusty Russell - */ -int main(int argc, char *argv[]) -{ - if (argc != 2) - return 1; - - if (strcmp(argv[1], "depends") == 0) { - printf("ccan/check_type\n"); - return 0; - } - - return 1; -} diff --git a/ccan/ccan/endian/_info b/ccan/ccan/endian/_info deleted file mode 100644 index efe5a8b..0000000 --- a/ccan/ccan/endian/_info +++ /dev/null @@ -1,55 +0,0 @@ -#include "config.h" -#include -#include - -/** - * endian - endian conversion macros for simple types - * - * Portable protocols (such as on-disk formats, or network protocols) - * are often defined to be a particular endian: little-endian (least - * significant bytes first) or big-endian (most significant bytes - * first). - * - * Similarly, some CPUs lay out values in memory in little-endian - * order (most commonly, Intel's 8086 and derivatives), or big-endian - * order (almost everyone else). - * - * This module provides conversion routines, inspired by the linux kernel. - * It also provides leint32_t, beint32_t etc typedefs, which are annotated for - * the sparse checker. - * - * Example: - * #include - * #include - * #include - * - * // - * int main(int argc, char *argv[]) - * { - * uint32_t value; - * - * if (argc != 2) - * errx(1, "Usage: %s ", argv[0]); - * - * value = atoi(argv[1]); - * printf("native: %08x\n", value); - * printf("little-endian: %08x\n", cpu_to_le32(value)); - * printf("big-endian: %08x\n", cpu_to_be32(value)); - * printf("byte-reversed: %08x\n", bswap_32(value)); - * exit(0); - * } - * - * License: License: CC0 (Public domain) - * Author: Rusty Russell - */ -int main(int argc, char *argv[]) -{ - if (argc != 2) - return 1; - - if (strcmp(argv[1], "depends") == 0) - /* Nothing */ - return 0; - - return 1; -} diff --git a/ccan/ccan/hash/LICENSE b/ccan/ccan/hash/LICENSE new file mode 120000 index 0000000..b7951da --- /dev/null +++ b/ccan/ccan/hash/LICENSE @@ -0,0 +1 @@ +../../licenses/CC0 \ No newline at end of file diff --git a/ccan/ccan/hash/hash.c b/ccan/ccan/hash/hash.c new file mode 100644 index 0000000..88d88fc --- /dev/null +++ b/ccan/ccan/hash/hash.c @@ -0,0 +1,926 @@ +/* CC0 (Public domain) - see LICENSE file for details */ +/* +------------------------------------------------------------------------------- +lookup3.c, by Bob Jenkins, May 2006, Public Domain. + +These are functions for producing 32-bit hashes for hash table lookup. +hash_word(), hashlittle(), hashlittle2(), hashbig(), mix(), and final() +are externally useful functions. Routines to test the hash are included +if SELF_TEST is defined. You can use this free for any purpose. It's in +the public domain. It has no warranty. + +You probably want to use hashlittle(). hashlittle() and hashbig() +hash byte arrays. hashlittle() is is faster than hashbig() on +little-endian machines. Intel and AMD are little-endian machines. +On second thought, you probably want hashlittle2(), which is identical to +hashlittle() except it returns two 32-bit hashes for the price of one. +You could implement hashbig2() if you wanted but I haven't bothered here. + +If you want to find a hash of, say, exactly 7 integers, do + a = i1; b = i2; c = i3; + mix(a,b,c); + a += i4; b += i5; c += i6; + mix(a,b,c); + a += i7; + final(a,b,c); +then use c as the hash value. If you have a variable length array of +4-byte integers to hash, use hash_word(). If you have a byte array (like +a character string), use hashlittle(). If you have several byte arrays, or +a mix of things, see the comments above hashlittle(). + +Why is this so big? I read 12 bytes at a time into 3 4-byte integers, +then mix those integers. This is fast (you can do a lot more thorough +mixing with 12*3 instructions on 3 integers than you can with 3 instructions +on 1 byte), but shoehorning those bytes into integers efficiently is messy. +------------------------------------------------------------------------------- +*/ +//#define SELF_TEST 1 + +#if 0 +#include /* defines printf for tests */ +#include /* defines time_t for timings in the test */ +#include /* defines uint32_t etc */ +#include /* attempt to define endianness */ + +#ifdef linux +# include /* attempt to define endianness */ +#endif + +/* + * My best guess at if you are big-endian or little-endian. This may + * need adjustment. + */ +#if (defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN) && \ + __BYTE_ORDER == __LITTLE_ENDIAN) || \ + (defined(i386) || defined(__i386__) || defined(__i486__) || \ + defined(__i586__) || defined(__i686__) || defined(__x86_64) || \ + defined(vax) || defined(MIPSEL)) +# define HASH_LITTLE_ENDIAN 1 +# define HASH_BIG_ENDIAN 0 +#elif (defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && \ + __BYTE_ORDER == __BIG_ENDIAN) || \ + (defined(sparc) || defined(POWERPC) || defined(mc68000) || defined(sel)) +# define HASH_LITTLE_ENDIAN 0 +# define HASH_BIG_ENDIAN 1 +#else +# error Unknown endian +#endif +#endif /* old hash.c headers. */ + +#include "hash.h" + +#if HAVE_LITTLE_ENDIAN +#define HASH_LITTLE_ENDIAN 1 +#define HASH_BIG_ENDIAN 0 +#elif HAVE_BIG_ENDIAN +#define HASH_LITTLE_ENDIAN 0 +#define HASH_BIG_ENDIAN 1 +#else +#error Unknown endian +#endif + +#define hashsize(n) ((uint32_t)1<<(n)) +#define hashmask(n) (hashsize(n)-1) +#define rot(x,k) (((x)<<(k)) | ((x)>>(32-(k)))) + +/* +------------------------------------------------------------------------------- +mix -- mix 3 32-bit values reversibly. + +This is reversible, so any information in (a,b,c) before mix() is +still in (a,b,c) after mix(). + +If four pairs of (a,b,c) inputs are run through mix(), or through +mix() in reverse, there are at least 32 bits of the output that +are sometimes the same for one pair and different for another pair. +This was tested for: +* pairs that differed by one bit, by two bits, in any combination + of top bits of (a,b,c), or in any combination of bottom bits of + (a,b,c). +* "differ" is defined as +, -, ^, or ~^. For + and -, I transformed + the output delta to a Gray code (a^(a>>1)) so a string of 1's (as + is commonly produced by subtraction) look like a single 1-bit + difference. +* the base values were pseudorandom, all zero but one bit set, or + all zero plus a counter that starts at zero. + +Some k values for my "a-=c; a^=rot(c,k); c+=b;" arrangement that +satisfy this are + 4 6 8 16 19 4 + 9 15 3 18 27 15 + 14 9 3 7 17 3 +Well, "9 15 3 18 27 15" didn't quite get 32 bits diffing +for "differ" defined as + with a one-bit base and a two-bit delta. I +used http://burtleburtle.net/bob/hash/avalanche.html to choose +the operations, constants, and arrangements of the variables. + +This does not achieve avalanche. There are input bits of (a,b,c) +that fail to affect some output bits of (a,b,c), especially of a. The +most thoroughly mixed value is c, but it doesn't really even achieve +avalanche in c. + +This allows some parallelism. Read-after-writes are good at doubling +the number of bits affected, so the goal of mixing pulls in the opposite +direction as the goal of parallelism. I did what I could. Rotates +seem to cost as much as shifts on every machine I could lay my hands +on, and rotates are much kinder to the top and bottom bits, so I used +rotates. +------------------------------------------------------------------------------- +*/ +#define mix(a,b,c) \ +{ \ + a -= c; a ^= rot(c, 4); c += b; \ + b -= a; b ^= rot(a, 6); a += c; \ + c -= b; c ^= rot(b, 8); b += a; \ + a -= c; a ^= rot(c,16); c += b; \ + b -= a; b ^= rot(a,19); a += c; \ + c -= b; c ^= rot(b, 4); b += a; \ +} + +/* +------------------------------------------------------------------------------- +final -- final mixing of 3 32-bit values (a,b,c) into c + +Pairs of (a,b,c) values differing in only a few bits will usually +produce values of c that look totally different. This was tested for +* pairs that differed by one bit, by two bits, in any combination + of top bits of (a,b,c), or in any combination of bottom bits of + (a,b,c). +* "differ" is defined as +, -, ^, or ~^. For + and -, I transformed + the output delta to a Gray code (a^(a>>1)) so a string of 1's (as + is commonly produced by subtraction) look like a single 1-bit + difference. +* the base values were pseudorandom, all zero but one bit set, or + all zero plus a counter that starts at zero. + +These constants passed: + 14 11 25 16 4 14 24 + 12 14 25 16 4 14 24 +and these came close: + 4 8 15 26 3 22 24 + 10 8 15 26 3 22 24 + 11 8 15 26 3 22 24 +------------------------------------------------------------------------------- +*/ +#define final(a,b,c) \ +{ \ + c ^= b; c -= rot(b,14); \ + a ^= c; a -= rot(c,11); \ + b ^= a; b -= rot(a,25); \ + c ^= b; c -= rot(b,16); \ + a ^= c; a -= rot(c,4); \ + b ^= a; b -= rot(a,14); \ + c ^= b; c -= rot(b,24); \ +} + +/* +-------------------------------------------------------------------- + This works on all machines. To be useful, it requires + -- that the key be an array of uint32_t's, and + -- that the length be the number of uint32_t's in the key + + The function hash_word() is identical to hashlittle() on little-endian + machines, and identical to hashbig() on big-endian machines, + except that the length has to be measured in uint32_ts rather than in + bytes. hashlittle() is more complicated than hash_word() only because + hashlittle() has to dance around fitting the key bytes into registers. +-------------------------------------------------------------------- +*/ +uint32_t hash_u32( +const uint32_t *k, /* the key, an array of uint32_t values */ +size_t length, /* the length of the key, in uint32_ts */ +uint32_t initval) /* the previous hash, or an arbitrary value */ +{ + uint32_t a,b,c; + + /* Set up the internal state */ + a = b = c = 0xdeadbeef + (((uint32_t)length)<<2) + initval; + + /*------------------------------------------------- handle most of the key */ + while (length > 3) + { + a += k[0]; + b += k[1]; + c += k[2]; + mix(a,b,c); + length -= 3; + k += 3; + } + + /*------------------------------------------- handle the last 3 uint32_t's */ + switch(length) /* all the case statements fall through */ + { + case 3 : c+=k[2]; + case 2 : b+=k[1]; + case 1 : a+=k[0]; + final(a,b,c); + case 0: /* case 0: nothing left to add */ + break; + } + /*------------------------------------------------------ report the result */ + return c; +} + +/* +------------------------------------------------------------------------------- +hashlittle() -- hash a variable-length key into a 32-bit value + k : the key (the unaligned variable-length array of bytes) + length : the length of the key, counting by bytes + val2 : IN: can be any 4-byte value OUT: second 32 bit hash. +Returns a 32-bit value. Every bit of the key affects every bit of +the return value. Two keys differing by one or two bits will have +totally different hash values. Note that the return value is better +mixed than val2, so use that first. + +The best hash table sizes are powers of 2. There is no need to do +mod a prime (mod is sooo slow!). If you need less than 32 bits, +use a bitmask. For example, if you need only 10 bits, do + h = (h & hashmask(10)); +In which case, the hash table should have hashsize(10) elements. + +If you are hashing n strings (uint8_t **)k, do it like this: + for (i=0, h=0; i 12) + { + a += k[0]; + b += k[1]; + c += k[2]; + mix(a,b,c); + length -= 12; + k += 3; + } + + /*----------------------------- handle the last (probably partial) block */ + /* + * "k[2]&0xffffff" actually reads beyond the end of the string, but + * then masks off the part it's not allowed to read. Because the + * string is aligned, the masked-off tail is in the same word as the + * rest of the string. Every machine with memory protection I've seen + * does it on word boundaries, so is OK with this. But VALGRIND will + * still catch it and complain. The masking trick does make the hash + * noticeably faster for short strings (like English words). + * + * Not on my testing with gcc 4.5 on an intel i5 CPU, at least --RR. + */ +#if 0 + switch(length) + { + case 12: c+=k[2]; b+=k[1]; a+=k[0]; break; + case 11: c+=k[2]&0xffffff; b+=k[1]; a+=k[0]; break; + case 10: c+=k[2]&0xffff; b+=k[1]; a+=k[0]; break; + case 9 : c+=k[2]&0xff; b+=k[1]; a+=k[0]; break; + case 8 : b+=k[1]; a+=k[0]; break; + case 7 : b+=k[1]&0xffffff; a+=k[0]; break; + case 6 : b+=k[1]&0xffff; a+=k[0]; break; + case 5 : b+=k[1]&0xff; a+=k[0]; break; + case 4 : a+=k[0]; break; + case 3 : a+=k[0]&0xffffff; break; + case 2 : a+=k[0]&0xffff; break; + case 1 : a+=k[0]&0xff; break; + case 0 : return c; /* zero length strings require no mixing */ + } + +#else /* make valgrind happy */ + + k8 = (const uint8_t *)k; + switch(length) + { + case 12: c+=k[2]; b+=k[1]; a+=k[0]; break; + case 11: c+=((uint32_t)k8[10])<<16; /* fall through */ + case 10: c+=((uint32_t)k8[9])<<8; /* fall through */ + case 9 : c+=k8[8]; /* fall through */ + case 8 : b+=k[1]; a+=k[0]; break; + case 7 : b+=((uint32_t)k8[6])<<16; /* fall through */ + case 6 : b+=((uint32_t)k8[5])<<8; /* fall through */ + case 5 : b+=k8[4]; /* fall through */ + case 4 : a+=k[0]; break; + case 3 : a+=((uint32_t)k8[2])<<16; /* fall through */ + case 2 : a+=((uint32_t)k8[1])<<8; /* fall through */ + case 1 : a+=k8[0]; break; + case 0 : return c; + } + +#endif /* !valgrind */ + + } else if (HASH_LITTLE_ENDIAN && ((u.i & 0x1) == 0)) { + const uint16_t *k = (const uint16_t *)key; /* read 16-bit chunks */ + const uint8_t *k8; + + /*--------------- all but last block: aligned reads and different mixing */ + while (length > 12) + { + a += k[0] + (((uint32_t)k[1])<<16); + b += k[2] + (((uint32_t)k[3])<<16); + c += k[4] + (((uint32_t)k[5])<<16); + mix(a,b,c); + length -= 12; + k += 6; + } + + /*----------------------------- handle the last (probably partial) block */ + k8 = (const uint8_t *)k; + switch(length) + { + case 12: c+=k[4]+(((uint32_t)k[5])<<16); + b+=k[2]+(((uint32_t)k[3])<<16); + a+=k[0]+(((uint32_t)k[1])<<16); + break; + case 11: c+=((uint32_t)k8[10])<<16; /* fall through */ + case 10: c+=k[4]; + b+=k[2]+(((uint32_t)k[3])<<16); + a+=k[0]+(((uint32_t)k[1])<<16); + break; + case 9 : c+=k8[8]; /* fall through */ + case 8 : b+=k[2]+(((uint32_t)k[3])<<16); + a+=k[0]+(((uint32_t)k[1])<<16); + break; + case 7 : b+=((uint32_t)k8[6])<<16; /* fall through */ + case 6 : b+=k[2]; + a+=k[0]+(((uint32_t)k[1])<<16); + break; + case 5 : b+=k8[4]; /* fall through */ + case 4 : a+=k[0]+(((uint32_t)k[1])<<16); + break; + case 3 : a+=((uint32_t)k8[2])<<16; /* fall through */ + case 2 : a+=k[0]; + break; + case 1 : a+=k8[0]; + break; + case 0 : return c; /* zero length requires no mixing */ + } + + } else { /* need to read the key one byte at a time */ + const uint8_t *k = (const uint8_t *)key; + + /*--------------- all but the last block: affect some 32 bits of (a,b,c) */ + while (length > 12) + { + a += k[0]; + a += ((uint32_t)k[1])<<8; + a += ((uint32_t)k[2])<<16; + a += ((uint32_t)k[3])<<24; + b += k[4]; + b += ((uint32_t)k[5])<<8; + b += ((uint32_t)k[6])<<16; + b += ((uint32_t)k[7])<<24; + c += k[8]; + c += ((uint32_t)k[9])<<8; + c += ((uint32_t)k[10])<<16; + c += ((uint32_t)k[11])<<24; + mix(a,b,c); + length -= 12; + k += 12; + } + + /*-------------------------------- last block: affect all 32 bits of (c) */ + switch(length) /* all the case statements fall through */ + { + case 12: c+=((uint32_t)k[11])<<24; + case 11: c+=((uint32_t)k[10])<<16; + case 10: c+=((uint32_t)k[9])<<8; + case 9 : c+=k[8]; + case 8 : b+=((uint32_t)k[7])<<24; + case 7 : b+=((uint32_t)k[6])<<16; + case 6 : b+=((uint32_t)k[5])<<8; + case 5 : b+=k[4]; + case 4 : a+=((uint32_t)k[3])<<24; + case 3 : a+=((uint32_t)k[2])<<16; + case 2 : a+=((uint32_t)k[1])<<8; + case 1 : a+=k[0]; + break; + case 0 : return c; + } + } + + final(a,b,c); + *val2 = b; + return c; +} + +/* + * hashbig(): + * This is the same as hash_word() on big-endian machines. It is different + * from hashlittle() on all machines. hashbig() takes advantage of + * big-endian byte ordering. + */ +static uint32_t hashbig( const void *key, size_t length, uint32_t *val2) +{ + uint32_t a,b,c; + union { const void *ptr; size_t i; } u; /* to cast key to (size_t) happily */ + + /* Set up the internal state */ + a = b = c = 0xdeadbeef + ((uint32_t)length) + *val2; + + u.ptr = key; + if (HASH_BIG_ENDIAN && ((u.i & 0x3) == 0)) { + const uint32_t *k = (const uint32_t *)key; /* read 32-bit chunks */ + const uint8_t *k8; + + /*------ all but last block: aligned reads and affect 32 bits of (a,b,c) */ + while (length > 12) + { + a += k[0]; + b += k[1]; + c += k[2]; + mix(a,b,c); + length -= 12; + k += 3; + } + + /*----------------------------- handle the last (probably partial) block */ + /* + * "k[2]<<8" actually reads beyond the end of the string, but + * then shifts out the part it's not allowed to read. Because the + * string is aligned, the illegal read is in the same word as the + * rest of the string. Every machine with memory protection I've seen + * does it on word boundaries, so is OK with this. But VALGRIND will + * still catch it and complain. The masking trick does make the hash + * noticeably faster for short strings (like English words). + * + * Not on my testing with gcc 4.5 on an intel i5 CPU, at least --RR. + */ +#if 0 + switch(length) + { + case 12: c+=k[2]; b+=k[1]; a+=k[0]; break; + case 11: c+=k[2]&0xffffff00; b+=k[1]; a+=k[0]; break; + case 10: c+=k[2]&0xffff0000; b+=k[1]; a+=k[0]; break; + case 9 : c+=k[2]&0xff000000; b+=k[1]; a+=k[0]; break; + case 8 : b+=k[1]; a+=k[0]; break; + case 7 : b+=k[1]&0xffffff00; a+=k[0]; break; + case 6 : b+=k[1]&0xffff0000; a+=k[0]; break; + case 5 : b+=k[1]&0xff000000; a+=k[0]; break; + case 4 : a+=k[0]; break; + case 3 : a+=k[0]&0xffffff00; break; + case 2 : a+=k[0]&0xffff0000; break; + case 1 : a+=k[0]&0xff000000; break; + case 0 : return c; /* zero length strings require no mixing */ + } + +#else /* make valgrind happy */ + + k8 = (const uint8_t *)k; + switch(length) /* all the case statements fall through */ + { + case 12: c+=k[2]; b+=k[1]; a+=k[0]; break; + case 11: c+=((uint32_t)k8[10])<<8; /* fall through */ + case 10: c+=((uint32_t)k8[9])<<16; /* fall through */ + case 9 : c+=((uint32_t)k8[8])<<24; /* fall through */ + case 8 : b+=k[1]; a+=k[0]; break; + case 7 : b+=((uint32_t)k8[6])<<8; /* fall through */ + case 6 : b+=((uint32_t)k8[5])<<16; /* fall through */ + case 5 : b+=((uint32_t)k8[4])<<24; /* fall through */ + case 4 : a+=k[0]; break; + case 3 : a+=((uint32_t)k8[2])<<8; /* fall through */ + case 2 : a+=((uint32_t)k8[1])<<16; /* fall through */ + case 1 : a+=((uint32_t)k8[0])<<24; break; + case 0 : return c; + } + +#endif /* !VALGRIND */ + + } else { /* need to read the key one byte at a time */ + const uint8_t *k = (const uint8_t *)key; + + /*--------------- all but the last block: affect some 32 bits of (a,b,c) */ + while (length > 12) + { + a += ((uint32_t)k[0])<<24; + a += ((uint32_t)k[1])<<16; + a += ((uint32_t)k[2])<<8; + a += ((uint32_t)k[3]); + b += ((uint32_t)k[4])<<24; + b += ((uint32_t)k[5])<<16; + b += ((uint32_t)k[6])<<8; + b += ((uint32_t)k[7]); + c += ((uint32_t)k[8])<<24; + c += ((uint32_t)k[9])<<16; + c += ((uint32_t)k[10])<<8; + c += ((uint32_t)k[11]); + mix(a,b,c); + length -= 12; + k += 12; + } + + /*-------------------------------- last block: affect all 32 bits of (c) */ + switch(length) /* all the case statements fall through */ + { + case 12: c+=k[11]; + case 11: c+=((uint32_t)k[10])<<8; + case 10: c+=((uint32_t)k[9])<<16; + case 9 : c+=((uint32_t)k[8])<<24; + case 8 : b+=k[7]; + case 7 : b+=((uint32_t)k[6])<<8; + case 6 : b+=((uint32_t)k[5])<<16; + case 5 : b+=((uint32_t)k[4])<<24; + case 4 : a+=k[3]; + case 3 : a+=((uint32_t)k[2])<<8; + case 2 : a+=((uint32_t)k[1])<<16; + case 1 : a+=((uint32_t)k[0])<<24; + break; + case 0 : return c; + } + } + + final(a,b,c); + *val2 = b; + return c; +} + +/* I basically use hashlittle here, but use native endian within each + * element. This delivers least-surprise: hash such as "int arr[] = { + * 1, 2 }; hash_stable(arr, 2, 0);" will be the same on big and little + * endian machines, even though a bytewise hash wouldn't be. */ +uint64_t hash64_stable_64(const void *key, size_t n, uint64_t base) +{ + const uint64_t *k = key; + uint32_t a,b,c; + + /* Set up the internal state */ + a = b = c = 0xdeadbeef + ((uint32_t)n*8) + (base >> 32) + base; + + while (n > 3) { + a += (uint32_t)k[0]; + b += (uint32_t)(k[0] >> 32); + c += (uint32_t)k[1]; + mix(a,b,c); + a += (uint32_t)(k[1] >> 32); + b += (uint32_t)k[2]; + c += (uint32_t)(k[2] >> 32); + mix(a,b,c); + n -= 3; + k += 3; + } + switch (n) { + case 2: + a += (uint32_t)k[0]; + b += (uint32_t)(k[0] >> 32); + c += (uint32_t)k[1]; + mix(a,b,c); + a += (uint32_t)(k[1] >> 32); + break; + case 1: + a += (uint32_t)k[0]; + b += (uint32_t)(k[0] >> 32); + break; + case 0: + return c; + } + final(a,b,c); + return ((uint64_t)b << 32) | c; +} + +uint64_t hash64_stable_32(const void *key, size_t n, uint64_t base) +{ + const uint32_t *k = key; + uint32_t a,b,c; + + /* Set up the internal state */ + a = b = c = 0xdeadbeef + ((uint32_t)n*4) + (base >> 32) + base; + + while (n > 3) { + a += k[0]; + b += k[1]; + c += k[2]; + mix(a,b,c); + + n -= 3; + k += 3; + } + switch (n) { + case 2: + b += (uint32_t)k[1]; + case 1: + a += (uint32_t)k[0]; + break; + case 0: + return c; + } + final(a,b,c); + return ((uint64_t)b << 32) | c; +} + +uint64_t hash64_stable_16(const void *key, size_t n, uint64_t base) +{ + const uint16_t *k = key; + uint32_t a,b,c; + + /* Set up the internal state */ + a = b = c = 0xdeadbeef + ((uint32_t)n*2) + (base >> 32) + base; + + while (n > 6) { + a += (uint32_t)k[0] + ((uint32_t)k[1] << 16); + b += (uint32_t)k[2] + ((uint32_t)k[3] << 16); + c += (uint32_t)k[4] + ((uint32_t)k[5] << 16); + mix(a,b,c); + + n -= 6; + k += 6; + } + + switch (n) { + case 5: + c += (uint32_t)k[4]; + case 4: + b += ((uint32_t)k[3] << 16); + case 3: + b += (uint32_t)k[2]; + case 2: + a += ((uint32_t)k[1] << 16); + case 1: + a += (uint32_t)k[0]; + break; + case 0: + return c; + } + final(a,b,c); + return ((uint64_t)b << 32) | c; +} + +uint64_t hash64_stable_8(const void *key, size_t n, uint64_t base) +{ + uint32_t b32 = base + (base >> 32); + uint32_t lower = hashlittle(key, n, &b32); + + return ((uint64_t)b32 << 32) | lower; +} + +uint32_t hash_any(const void *key, size_t length, uint32_t base) +{ + if (HASH_BIG_ENDIAN) + return hashbig(key, length, &base); + else + return hashlittle(key, length, &base); +} + +uint32_t hash_stable_64(const void *key, size_t n, uint32_t base) +{ + return hash64_stable_64(key, n, base); +} + +uint32_t hash_stable_32(const void *key, size_t n, uint32_t base) +{ + return hash64_stable_32(key, n, base); +} + +uint32_t hash_stable_16(const void *key, size_t n, uint32_t base) +{ + return hash64_stable_16(key, n, base); +} + +uint32_t hash_stable_8(const void *key, size_t n, uint32_t base) +{ + return hashlittle(key, n, &base); +} + +/* Jenkins' lookup8 is a 64 bit hash, but he says it's obsolete. Use + * the plain one and recombine into 64 bits. */ +uint64_t hash64_any(const void *key, size_t length, uint64_t base) +{ + uint32_t b32 = base + (base >> 32); + uint32_t lower; + + if (HASH_BIG_ENDIAN) + lower = hashbig(key, length, &b32); + else + lower = hashlittle(key, length, &b32); + + return ((uint64_t)b32 << 32) | lower; +} + +#ifdef SELF_TEST + +/* used for timings */ +void driver1() +{ + uint8_t buf[256]; + uint32_t i; + uint32_t h=0; + time_t a,z; + + time(&a); + for (i=0; i<256; ++i) buf[i] = 'x'; + for (i=0; i<1; ++i) + { + h = hashlittle(&buf[0],1,h); + } + time(&z); + if (z-a > 0) printf("time %d %.8x\n", z-a, h); +} + +/* check that every input bit changes every output bit half the time */ +#define HASHSTATE 1 +#define HASHLEN 1 +#define MAXPAIR 60 +#define MAXLEN 70 +void driver2() +{ + uint8_t qa[MAXLEN+1], qb[MAXLEN+2], *a = &qa[0], *b = &qb[1]; + uint32_t c[HASHSTATE], d[HASHSTATE], i=0, j=0, k, l, m=0, z; + uint32_t e[HASHSTATE],f[HASHSTATE],g[HASHSTATE],h[HASHSTATE]; + uint32_t x[HASHSTATE],y[HASHSTATE]; + uint32_t hlen; + + printf("No more than %d trials should ever be needed \n",MAXPAIR/2); + for (hlen=0; hlen < MAXLEN; ++hlen) + { + z=0; + for (i=0; i>(8-j)); + c[0] = hashlittle(a, hlen, m); + b[i] ^= ((k+1)<>(8-j)); + d[0] = hashlittle(b, hlen, m); + /* check every bit is 1, 0, set, and not set at least once */ + for (l=0; lz) z=k; + if (k==MAXPAIR) + { + printf("Some bit didn't change: "); + printf("%.8x %.8x %.8x %.8x %.8x %.8x ", + e[0],f[0],g[0],h[0],x[0],y[0]); + printf("i %d j %d m %d len %d\n", i, j, m, hlen); + } + if (z==MAXPAIR) goto done; + } + } + } + done: + if (z < MAXPAIR) + { + printf("Mix success %2d bytes %2d initvals ",i,m); + printf("required %d trials\n", z/2); + } + } + printf("\n"); +} + +/* Check for reading beyond the end of the buffer and alignment problems */ +void driver3() +{ + uint8_t buf[MAXLEN+20], *b; + uint32_t len; + uint8_t q[] = "This is the time for all good men to come to the aid of their country..."; + uint32_t h; + uint8_t qq[] = "xThis is the time for all good men to come to the aid of their country..."; + uint32_t i; + uint8_t qqq[] = "xxThis is the time for all good men to come to the aid of their country..."; + uint32_t j; + uint8_t qqqq[] = "xxxThis is the time for all good men to come to the aid of their country..."; + uint32_t ref,x,y; + uint8_t *p; + + printf("Endianness. These lines should all be the same (for values filled in):\n"); + printf("%.8x %.8x %.8x\n", + hash_word((const uint32_t *)q, (sizeof(q)-1)/4, 13), + hash_word((const uint32_t *)q, (sizeof(q)-5)/4, 13), + hash_word((const uint32_t *)q, (sizeof(q)-9)/4, 13)); + p = q; + printf("%.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x\n", + hashlittle(p, sizeof(q)-1, 13), hashlittle(p, sizeof(q)-2, 13), + hashlittle(p, sizeof(q)-3, 13), hashlittle(p, sizeof(q)-4, 13), + hashlittle(p, sizeof(q)-5, 13), hashlittle(p, sizeof(q)-6, 13), + hashlittle(p, sizeof(q)-7, 13), hashlittle(p, sizeof(q)-8, 13), + hashlittle(p, sizeof(q)-9, 13), hashlittle(p, sizeof(q)-10, 13), + hashlittle(p, sizeof(q)-11, 13), hashlittle(p, sizeof(q)-12, 13)); + p = &qq[1]; + printf("%.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x\n", + hashlittle(p, sizeof(q)-1, 13), hashlittle(p, sizeof(q)-2, 13), + hashlittle(p, sizeof(q)-3, 13), hashlittle(p, sizeof(q)-4, 13), + hashlittle(p, sizeof(q)-5, 13), hashlittle(p, sizeof(q)-6, 13), + hashlittle(p, sizeof(q)-7, 13), hashlittle(p, sizeof(q)-8, 13), + hashlittle(p, sizeof(q)-9, 13), hashlittle(p, sizeof(q)-10, 13), + hashlittle(p, sizeof(q)-11, 13), hashlittle(p, sizeof(q)-12, 13)); + p = &qqq[2]; + printf("%.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x\n", + hashlittle(p, sizeof(q)-1, 13), hashlittle(p, sizeof(q)-2, 13), + hashlittle(p, sizeof(q)-3, 13), hashlittle(p, sizeof(q)-4, 13), + hashlittle(p, sizeof(q)-5, 13), hashlittle(p, sizeof(q)-6, 13), + hashlittle(p, sizeof(q)-7, 13), hashlittle(p, sizeof(q)-8, 13), + hashlittle(p, sizeof(q)-9, 13), hashlittle(p, sizeof(q)-10, 13), + hashlittle(p, sizeof(q)-11, 13), hashlittle(p, sizeof(q)-12, 13)); + p = &qqqq[3]; + printf("%.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x\n", + hashlittle(p, sizeof(q)-1, 13), hashlittle(p, sizeof(q)-2, 13), + hashlittle(p, sizeof(q)-3, 13), hashlittle(p, sizeof(q)-4, 13), + hashlittle(p, sizeof(q)-5, 13), hashlittle(p, sizeof(q)-6, 13), + hashlittle(p, sizeof(q)-7, 13), hashlittle(p, sizeof(q)-8, 13), + hashlittle(p, sizeof(q)-9, 13), hashlittle(p, sizeof(q)-10, 13), + hashlittle(p, sizeof(q)-11, 13), hashlittle(p, sizeof(q)-12, 13)); + printf("\n"); + + /* check that hashlittle2 and hashlittle produce the same results */ + i=47; j=0; + hashlittle2(q, sizeof(q), &i, &j); + if (hashlittle(q, sizeof(q), 47) != i) + printf("hashlittle2 and hashlittle mismatch\n"); + + /* check that hash_word2 and hash_word produce the same results */ + len = 0xdeadbeef; + i=47, j=0; + hash_word2(&len, 1, &i, &j); + if (hash_word(&len, 1, 47) != i) + printf("hash_word2 and hash_word mismatch %x %x\n", + i, hash_word(&len, 1, 47)); + + /* check hashlittle doesn't read before or after the ends of the string */ + for (h=0, b=buf+1; h<8; ++h, ++b) + { + for (i=0; i +#include +#include + +/* Stolen mostly from: lookup3.c, by Bob Jenkins, May 2006, Public Domain. + * + * http://burtleburtle.net/bob/c/lookup3.c + */ + +/** + * hash - fast hash of an array for internal use + * @p: the array or pointer to first element + * @num: the number of elements to hash + * @base: the base number to roll into the hash (usually 0) + * + * The memory region pointed to by p is combined with the base to form + * a 32-bit hash. + * + * This hash will have different results on different machines, so is + * only useful for internal hashes (ie. not hashes sent across the + * network or saved to disk). + * + * It may also change with future versions: it could even detect at runtime + * what the fastest hash to use is. + * + * See also: hash64, hash_stable. + * + * Example: + * #include + * #include + * #include + * #include + * + * // Simple demonstration: idential strings will have the same hash, but + * // two different strings will probably not. + * int main(int argc, char *argv[]) + * { + * uint32_t hash1, hash2; + * + * if (argc != 3) + * err(1, "Usage: %s ", argv[0]); + * + * hash1 = hash(argv[1], strlen(argv[1]), 0); + * hash2 = hash(argv[2], strlen(argv[2]), 0); + * printf("Hash is %s\n", hash1 == hash2 ? "same" : "different"); + * return 0; + * } + */ +#define hash(p, num, base) hash_any((p), (num)*sizeof(*(p)), (base)) + +/** + * hash_stable - hash of an array for external use + * @p: the array or pointer to first element + * @num: the number of elements to hash + * @base: the base number to roll into the hash (usually 0) + * + * The array of simple integer types pointed to by p is combined with + * the base to form a 32-bit hash. + * + * This hash will have the same results on different machines, so can + * be used for external hashes (ie. hashes sent across the network or + * saved to disk). The results will not change in future versions of + * this module. + * + * Note that it is only legal to hand an array of simple integer types + * to this hash (ie. char, uint16_t, int64_t, etc). In these cases, + * the same values will have the same hash result, even though the + * memory representations of integers depend on the machine + * endianness. + * + * See also: + * hash64_stable + * + * Example: + * #include + * #include + * #include + * #include + * + * int main(int argc, char *argv[]) + * { + * if (argc != 2) + * err(1, "Usage: %s ", argv[0]); + * + * printf("Hash stable result is %u\n", + * hash_stable(argv[1], strlen(argv[1]), 0)); + * return 0; + * } + */ +#define hash_stable(p, num, base) \ + (BUILD_ASSERT_OR_ZERO(sizeof(*(p)) == 8 || sizeof(*(p)) == 4 \ + || sizeof(*(p)) == 2 || sizeof(*(p)) == 1) + \ + sizeof(*(p)) == 8 ? hash_stable_64((p), (num), (base)) \ + : sizeof(*(p)) == 4 ? hash_stable_32((p), (num), (base)) \ + : sizeof(*(p)) == 2 ? hash_stable_16((p), (num), (base)) \ + : hash_stable_8((p), (num), (base))) + +/** + * hash_u32 - fast hash an array of 32-bit values for internal use + * @key: the array of uint32_t + * @num: the number of elements to hash + * @base: the base number to roll into the hash (usually 0) + * + * The array of uint32_t pointed to by @key is combined with the base + * to form a 32-bit hash. This is 2-3 times faster than hash() on small + * arrays, but the advantage vanishes over large hashes. + * + * This hash will have different results on different machines, so is + * only useful for internal hashes (ie. not hashes sent across the + * network or saved to disk). + */ +uint32_t hash_u32(const uint32_t *key, size_t num, uint32_t base); + +/** + * hash_string - very fast hash of an ascii string + * @str: the nul-terminated string + * + * The string is hashed, using a hash function optimized for ASCII and + * similar strings. It's weaker than the other hash functions. + * + * This hash may have different results on different machines, so is + * only useful for internal hashes (ie. not hashes sent across the + * network or saved to disk). The results will be different from the + * other hash functions in this module, too. + */ +static inline uint32_t hash_string(const char *string) +{ + /* This is Karl Nelson 's X31 hash. + * It's a little faster than the (much better) lookup3 hash(): 56ns vs + * 84ns on my 2GHz Intel Core Duo 2 laptop for a 10 char string. */ + uint32_t ret; + + for (ret = 0; *string; string++) + ret = (ret << 5) - ret + *string; + + return ret; +} + +/** + * hash64 - fast 64-bit hash of an array for internal use + * @p: the array or pointer to first element + * @num: the number of elements to hash + * @base: the 64-bit base number to roll into the hash (usually 0) + * + * The memory region pointed to by p is combined with the base to form + * a 64-bit hash. + * + * This hash will have different results on different machines, so is + * only useful for internal hashes (ie. not hashes sent across the + * network or saved to disk). + * + * It may also change with future versions: it could even detect at runtime + * what the fastest hash to use is. + * + * See also: hash. + * + * Example: + * #include + * #include + * #include + * #include + * + * // Simple demonstration: idential strings will have the same hash, but + * // two different strings will probably not. + * int main(int argc, char *argv[]) + * { + * uint64_t hash1, hash2; + * + * if (argc != 3) + * err(1, "Usage: %s ", argv[0]); + * + * hash1 = hash64(argv[1], strlen(argv[1]), 0); + * hash2 = hash64(argv[2], strlen(argv[2]), 0); + * printf("Hash is %s\n", hash1 == hash2 ? "same" : "different"); + * return 0; + * } + */ +#define hash64(p, num, base) hash64_any((p), (num)*sizeof(*(p)), (base)) + +/** + * hash64_stable - 64 bit hash of an array for external use + * @p: the array or pointer to first element + * @num: the number of elements to hash + * @base: the base number to roll into the hash (usually 0) + * + * The array of simple integer types pointed to by p is combined with + * the base to form a 64-bit hash. + * + * This hash will have the same results on different machines, so can + * be used for external hashes (ie. hashes sent across the network or + * saved to disk). The results will not change in future versions of + * this module. + * + * Note that it is only legal to hand an array of simple integer types + * to this hash (ie. char, uint16_t, int64_t, etc). In these cases, + * the same values will have the same hash result, even though the + * memory representations of integers depend on the machine + * endianness. + * + * See also: + * hash_stable + * + * Example: + * #include + * #include + * #include + * #include + * + * int main(int argc, char *argv[]) + * { + * if (argc != 2) + * err(1, "Usage: %s ", argv[0]); + * + * printf("Hash stable result is %llu\n", + * (long long)hash64_stable(argv[1], strlen(argv[1]), 0)); + * return 0; + * } + */ +#define hash64_stable(p, num, base) \ + (BUILD_ASSERT_OR_ZERO(sizeof(*(p)) == 8 || sizeof(*(p)) == 4 \ + || sizeof(*(p)) == 2 || sizeof(*(p)) == 1) + \ + sizeof(*(p)) == 8 ? hash64_stable_64((p), (num), (base)) \ + : sizeof(*(p)) == 4 ? hash64_stable_32((p), (num), (base)) \ + : sizeof(*(p)) == 2 ? hash64_stable_16((p), (num), (base)) \ + : hash64_stable_8((p), (num), (base))) + + +/** + * hashl - fast 32/64-bit hash of an array for internal use + * @p: the array or pointer to first element + * @num: the number of elements to hash + * @base: the base number to roll into the hash (usually 0) + * + * This is either hash() or hash64(), on 32/64 bit long machines. + */ +#define hashl(p, num, base) \ + (BUILD_ASSERT_OR_ZERO(sizeof(long) == sizeof(uint32_t) \ + || sizeof(long) == sizeof(uint64_t)) + \ + (sizeof(long) == sizeof(uint64_t) \ + ? hash64((p), (num), (base)) : hash((p), (num), (base)))) + +/* Our underlying operations. */ +uint32_t hash_any(const void *key, size_t length, uint32_t base); +uint32_t hash_stable_64(const void *key, size_t n, uint32_t base); +uint32_t hash_stable_32(const void *key, size_t n, uint32_t base); +uint32_t hash_stable_16(const void *key, size_t n, uint32_t base); +uint32_t hash_stable_8(const void *key, size_t n, uint32_t base); +uint64_t hash64_any(const void *key, size_t length, uint64_t base); +uint64_t hash64_stable_64(const void *key, size_t n, uint64_t base); +uint64_t hash64_stable_32(const void *key, size_t n, uint64_t base); +uint64_t hash64_stable_16(const void *key, size_t n, uint64_t base); +uint64_t hash64_stable_8(const void *key, size_t n, uint64_t base); + +/** + * hash_pointer - hash a pointer for internal use + * @p: the pointer value to hash + * @base: the base number to roll into the hash (usually 0) + * + * The pointer p (not what p points to!) is combined with the base to form + * a 32-bit hash. + * + * This hash will have different results on different machines, so is + * only useful for internal hashes (ie. not hashes sent across the + * network or saved to disk). + * + * Example: + * #include + * + * // Code to keep track of memory regions. + * struct region { + * struct region *chain; + * void *start; + * unsigned int size; + * }; + * // We keep a simple hash table. + * static struct region *region_hash[128]; + * + * static void add_region(struct region *r) + * { + * unsigned int h = hash_pointer(r->start, 0); + * + * r->chain = region_hash[h]; + * region_hash[h] = r->chain; + * } + * + * static struct region *find_region(const void *start) + * { + * struct region *r; + * + * for (r = region_hash[hash_pointer(start, 0)]; r; r = r->chain) + * if (r->start == start) + * return r; + * return NULL; + * } + */ +static inline uint32_t hash_pointer(const void *p, uint32_t base) +{ + if (sizeof(p) % sizeof(uint32_t) == 0) { + /* This convoluted union is the right way of aliasing. */ + union { + uint32_t a[sizeof(p) / sizeof(uint32_t)]; + const void *p; + } u; + u.p = p; + return hash_u32(u.a, sizeof(p) / sizeof(uint32_t), base); + } else + return hash(&p, 1, base); +} +#endif /* HASH_H */ diff --git a/ccan/ccan/htable/LICENSE b/ccan/ccan/htable/LICENSE new file mode 120000 index 0000000..dc314ec --- /dev/null +++ b/ccan/ccan/htable/LICENSE @@ -0,0 +1 @@ +../../licenses/LGPL-2.1 \ No newline at end of file diff --git a/ccan/ccan/htable/htable.c b/ccan/ccan/htable/htable.c new file mode 100644 index 0000000..f631ffe --- /dev/null +++ b/ccan/ccan/htable/htable.c @@ -0,0 +1,491 @@ +/* Licensed under LGPLv2+ - see LICENSE file for details */ +#include +#include +#include +#include +#include +#include +#include +#include + +/* We use 0x1 as deleted marker. */ +#define HTABLE_DELETED (0x1) + +/* perfect_bitnum 63 means there's no perfect bitnum */ +#define NO_PERFECT_BIT (sizeof(uintptr_t) * CHAR_BIT - 1) + +static void *htable_default_alloc(struct htable *ht, size_t len) +{ + return calloc(len, 1); +} + +static void htable_default_free(struct htable *ht, void *p) +{ + free(p); +} + +static void *(*htable_alloc)(struct htable *, size_t) = htable_default_alloc; +static void (*htable_free)(struct htable *, void *) = htable_default_free; + +void htable_set_allocator(void *(*alloc)(struct htable *, size_t len), + void (*free)(struct htable *, void *p)) +{ + if (!alloc) + alloc = htable_default_alloc; + if (!free) + free = htable_default_free; + htable_alloc = alloc; + htable_free = free; +} + +/* We clear out the bits which are always the same, and put metadata there. */ +static inline uintptr_t get_extra_ptr_bits(const struct htable *ht, + uintptr_t e) +{ + return e & ht->common_mask; +} + +static inline void *get_raw_ptr(const struct htable *ht, uintptr_t e) +{ + return (void *)((e & ~ht->common_mask) | ht->common_bits); +} + +static inline uintptr_t make_hval(const struct htable *ht, + const void *p, uintptr_t bits) +{ + return ((uintptr_t)p & ~ht->common_mask) | bits; +} + +static inline bool entry_is_valid(uintptr_t e) +{ + return e > HTABLE_DELETED; +} + +static inline uintptr_t ht_perfect_mask(const struct htable *ht) +{ + return (uintptr_t)2 << ht->perfect_bitnum; +} + +static inline uintptr_t get_hash_ptr_bits(const struct htable *ht, + size_t hash) +{ + /* Shuffling the extra bits (as specified in mask) down the + * end is quite expensive. But the lower bits are redundant, so + * we fold the value first. */ + return (hash ^ (hash >> ht->bits)) + & ht->common_mask & ~ht_perfect_mask(ht); +} + +void htable_init(struct htable *ht, + size_t (*rehash)(const void *elem, void *priv), void *priv) +{ + struct htable empty = HTABLE_INITIALIZER(empty, NULL, NULL); + *ht = empty; + ht->rehash = rehash; + ht->priv = priv; + ht->table = &ht->common_bits; +} + +/* Fill to 87.5% */ +static inline size_t ht_max(const struct htable *ht) +{ + return ((size_t)7 << ht->bits) / 8; +} + +/* Clean deleted if we're full, and more than 12.5% deleted */ +static inline size_t ht_max_deleted(const struct htable *ht) +{ + return ((size_t)1 << ht->bits) / 8; +} + +bool htable_init_sized(struct htable *ht, + size_t (*rehash)(const void *, void *), + void *priv, size_t expect) +{ + htable_init(ht, rehash, priv); + + /* Don't go insane with sizing. */ + for (ht->bits = 1; ht_max(ht) < expect; ht->bits++) { + if (ht->bits == 30) + break; + } + + ht->table = htable_alloc(ht, sizeof(size_t) << ht->bits); + if (!ht->table) { + ht->table = &ht->common_bits; + return false; + } + (void)htable_debug(ht, HTABLE_LOC); + return true; +} + +void htable_clear(struct htable *ht) +{ + if (ht->table != &ht->common_bits) + htable_free(ht, (void *)ht->table); + htable_init(ht, ht->rehash, ht->priv); +} + +bool htable_copy_(struct htable *dst, const struct htable *src) +{ + uintptr_t *htable = htable_alloc(dst, sizeof(size_t) << src->bits); + + if (!htable) + return false; + + *dst = *src; + dst->table = htable; + memcpy(dst->table, src->table, sizeof(size_t) << src->bits); + return true; +} + +static size_t hash_bucket(const struct htable *ht, size_t h) +{ + return h & ((1 << ht->bits)-1); +} + +static void *htable_val(const struct htable *ht, + struct htable_iter *i, size_t hash, uintptr_t perfect) +{ + uintptr_t h2 = get_hash_ptr_bits(ht, hash) | perfect; + + while (ht->table[i->off]) { + if (ht->table[i->off] != HTABLE_DELETED) { + if (get_extra_ptr_bits(ht, ht->table[i->off]) == h2) + return get_raw_ptr(ht, ht->table[i->off]); + } + i->off = (i->off + 1) & ((1 << ht->bits)-1); + h2 &= ~perfect; + } + return NULL; +} + +void *htable_firstval_(const struct htable *ht, + struct htable_iter *i, size_t hash) +{ + i->off = hash_bucket(ht, hash); + return htable_val(ht, i, hash, ht_perfect_mask(ht)); +} + +void *htable_nextval_(const struct htable *ht, + struct htable_iter *i, size_t hash) +{ + i->off = (i->off + 1) & ((1 << ht->bits)-1); + return htable_val(ht, i, hash, 0); +} + +void *htable_first_(const struct htable *ht, struct htable_iter *i) +{ + for (i->off = 0; i->off < (size_t)1 << ht->bits; i->off++) { + if (entry_is_valid(ht->table[i->off])) + return get_raw_ptr(ht, ht->table[i->off]); + } + return NULL; +} + +void *htable_next_(const struct htable *ht, struct htable_iter *i) +{ + for (i->off++; i->off < (size_t)1 << ht->bits; i->off++) { + if (entry_is_valid(ht->table[i->off])) + return get_raw_ptr(ht, ht->table[i->off]); + } + return NULL; +} + +void *htable_prev_(const struct htable *ht, struct htable_iter *i) +{ + for (;;) { + if (!i->off) + return NULL; + i->off--; + if (entry_is_valid(ht->table[i->off])) + return get_raw_ptr(ht, ht->table[i->off]); + } +} + +/* Another bit currently in mask needs to be exposed, so that a bucket with p in + * it won't appear invalid */ +static COLD void unset_another_common_bit(struct htable *ht, + uintptr_t *maskdiff, + const void *p) +{ + size_t i; + + for (i = sizeof(uintptr_t) * CHAR_BIT - 1; i > 0; i--) { + if (((uintptr_t)p & ((uintptr_t)1 << i)) + && ht->common_mask & ~*maskdiff & ((uintptr_t)1 << i)) + break; + } + /* There must have been one, right? */ + assert(i > 0); + + *maskdiff |= ((uintptr_t)1 << i); +} + +/* We want to change the common mask: this fixes up the table */ +static COLD void fixup_table_common(struct htable *ht, uintptr_t maskdiff) +{ + size_t i; + uintptr_t bitsdiff; + +again: + bitsdiff = ht->common_bits & maskdiff; + + for (i = 0; i < (size_t)1 << ht->bits; i++) { + uintptr_t e; + if (!entry_is_valid(e = ht->table[i])) + continue; + + /* Clear the bits no longer in the mask, set them as + * expected. */ + e &= ~maskdiff; + e |= bitsdiff; + /* If this made it invalid, restart with more exposed */ + if (!entry_is_valid(e)) { + unset_another_common_bit(ht, &maskdiff, get_raw_ptr(ht, e)); + goto again; + } + ht->table[i] = e; + } + + /* Take away those bits from our mask, bits and perfect bit. */ + ht->common_mask &= ~maskdiff; + ht->common_bits &= ~maskdiff; + if (ht_perfect_mask(ht) & maskdiff) + ht->perfect_bitnum = NO_PERFECT_BIT; +} + +/* Limited recursion */ +static void ht_add(struct htable *ht, const void *new, size_t h); + +/* We tried to add this entry, but it looked invalid! We need to + * let another pointer bit through mask */ +static COLD void update_common_fix_invalid(struct htable *ht, const void *p, size_t h) +{ + uintptr_t maskdiff; + + assert(ht->elems != 0); + + maskdiff = 0; + unset_another_common_bit(ht, &maskdiff, p); + fixup_table_common(ht, maskdiff); + + /* Now won't recurse */ + ht_add(ht, p, h); +} + +/* This does not expand the hash table, that's up to caller. */ +static void ht_add(struct htable *ht, const void *new, size_t h) +{ + size_t i; + uintptr_t perfect = ht_perfect_mask(ht); + + i = hash_bucket(ht, h); + + while (entry_is_valid(ht->table[i])) { + perfect = 0; + i = (i + 1) & ((1 << ht->bits)-1); + } + ht->table[i] = make_hval(ht, new, get_hash_ptr_bits(ht, h)|perfect); + if (!entry_is_valid(ht->table[i])) + update_common_fix_invalid(ht, new, h); +} + +static COLD bool double_table(struct htable *ht) +{ + unsigned int i; + size_t oldnum = (size_t)1 << ht->bits; + uintptr_t *oldtable, e; + + oldtable = ht->table; + ht->table = htable_alloc(ht, sizeof(size_t) << (ht->bits+1)); + if (!ht->table) { + ht->table = oldtable; + return false; + } + ht->bits++; + + /* If we lost our "perfect bit", get it back now. */ + if (ht->perfect_bitnum == NO_PERFECT_BIT && ht->common_mask) { + for (i = 0; i < sizeof(ht->common_mask) * CHAR_BIT; i++) { + if (ht->common_mask & ((size_t)2 << i)) { + ht->perfect_bitnum = i; + break; + } + } + } + + if (oldtable != &ht->common_bits) { + for (i = 0; i < oldnum; i++) { + if (entry_is_valid(e = oldtable[i])) { + void *p = get_raw_ptr(ht, e); + ht_add(ht, p, ht->rehash(p, ht->priv)); + } + } + htable_free(ht, oldtable); + } + ht->deleted = 0; + + (void)htable_debug(ht, HTABLE_LOC); + return true; +} + +static COLD void rehash_table(struct htable *ht) +{ + size_t start, i; + uintptr_t e, perfect = ht_perfect_mask(ht); + + /* Beware wrap cases: we need to start from first empty bucket. */ + for (start = 0; ht->table[start]; start++); + + for (i = 0; i < (size_t)1 << ht->bits; i++) { + size_t h = (i + start) & ((1 << ht->bits)-1); + e = ht->table[h]; + if (!e) + continue; + if (e == HTABLE_DELETED) + ht->table[h] = 0; + else if (!(e & perfect)) { + void *p = get_raw_ptr(ht, e); + ht->table[h] = 0; + ht_add(ht, p, ht->rehash(p, ht->priv)); + } + } + ht->deleted = 0; + (void)htable_debug(ht, HTABLE_LOC); +} + +/* We stole some bits, now we need to put them back... */ +static COLD void update_common(struct htable *ht, const void *p) +{ + uintptr_t maskdiff; + + if (ht->elems == 0) { + ht->common_mask = -1; + ht->common_bits = ((uintptr_t)p & ht->common_mask); + ht->perfect_bitnum = 0; + (void)htable_debug(ht, HTABLE_LOC); + return; + } + + /* Find bits which are unequal to old common set. */ + maskdiff = ht->common_bits ^ ((uintptr_t)p & ht->common_mask); + + fixup_table_common(ht, maskdiff); + (void)htable_debug(ht, HTABLE_LOC); +} + +bool htable_add_(struct htable *ht, size_t hash, const void *p) +{ + /* Cannot insert NULL, or (void *)1. */ + assert(p); + assert(entry_is_valid((uintptr_t)p)); + + /* Getting too full? */ + if (ht->elems+1 + ht->deleted > ht_max(ht)) { + /* If we're more than 1/8 deleted, clean those, + * otherwise double table size. */ + if (ht->deleted > ht_max_deleted(ht)) + rehash_table(ht); + else if (!double_table(ht)) + return false; + } + if (((uintptr_t)p & ht->common_mask) != ht->common_bits) + update_common(ht, p); + + ht_add(ht, p, hash); + ht->elems++; + return true; +} + +bool htable_del_(struct htable *ht, size_t h, const void *p) +{ + struct htable_iter i; + void *c; + + for (c = htable_firstval(ht,&i,h); c; c = htable_nextval(ht,&i,h)) { + if (c == p) { + htable_delval(ht, &i); + return true; + } + } + return false; +} + +void htable_delval_(struct htable *ht, struct htable_iter *i) +{ + assert(i->off < (size_t)1 << ht->bits); + assert(entry_is_valid(ht->table[i->off])); + + ht->elems--; + /* Cheap test: if the next bucket is empty, don't need delete marker */ + if (ht->table[hash_bucket(ht, i->off+1)] != 0) { + ht->table[i->off] = HTABLE_DELETED; + ht->deleted++; + } else + ht->table[i->off] = 0; +} + +void *htable_pick_(const struct htable *ht, size_t seed, struct htable_iter *i) +{ + void *e; + struct htable_iter unwanted; + + if (!i) + i = &unwanted; + i->off = seed % ((size_t)1 << ht->bits); + e = htable_next(ht, i); + if (!e) + e = htable_first(ht, i); + return e; +} + +struct htable *htable_check(const struct htable *ht, const char *abortstr) +{ + void *p; + struct htable_iter i; + size_t n = 0; + + /* Use non-DEBUG versions here, to avoid infinite recursion with + * CCAN_HTABLE_DEBUG! */ + for (p = htable_first_(ht, &i); p; p = htable_next_(ht, &i)) { + struct htable_iter i2; + void *c; + size_t h = ht->rehash(p, ht->priv); + bool found = false; + + n++; + + /* Open-code htable_get to avoid CCAN_HTABLE_DEBUG */ + for (c = htable_firstval_(ht, &i2, h); + c; + c = htable_nextval_(ht, &i2, h)) { + if (c == p) { + found = true; + break; + } + } + + if (!found) { + if (abortstr) { + fprintf(stderr, + "%s: element %p in position %zu" + " cannot find itself\n", + abortstr, p, i.off); + abort(); + } + return NULL; + } + } + if (n != ht->elems) { + if (abortstr) { + fprintf(stderr, + "%s: found %zu elems, expected %zu\n", + abortstr, n, ht->elems); + abort(); + } + return NULL; + } + + return (struct htable *)ht; +} diff --git a/ccan/ccan/htable/htable.h b/ccan/ccan/htable/htable.h new file mode 100644 index 0000000..faaf541 --- /dev/null +++ b/ccan/ccan/htable/htable.h @@ -0,0 +1,290 @@ +/* Licensed under LGPLv2+ - see LICENSE file for details */ +#ifndef CCAN_HTABLE_H +#define CCAN_HTABLE_H +#include "config.h" +#include +#include +#include +#include + +/* Define CCAN_HTABLE_DEBUG for expensive debugging checks on each call. */ +#define HTABLE_LOC __FILE__ ":" stringify(__LINE__) +#ifdef CCAN_HTABLE_DEBUG +#define htable_debug(h, loc) htable_check((h), loc) +#else +#define htable_debug(h, loc) ((void)loc, h) +#endif + +/** + * struct htable - private definition of a htable. + * + * It's exposed here so you can put it in your structures and so we can + * supply inline functions. + */ +struct htable { + size_t (*rehash)(const void *elem, void *priv); + void *priv; + unsigned int bits, perfect_bitnum; + size_t elems, deleted; + /* These are the bits which are the same in all pointers. */ + uintptr_t common_mask, common_bits; + uintptr_t *table; +}; + +/** + * HTABLE_INITIALIZER - static initialization for a hash table. + * @name: name of this htable. + * @rehash: hash function to use for rehashing. + * @priv: private argument to @rehash function. + * + * This is useful for setting up static and global hash tables. + * + * Example: + * // For simplicity's sake, say hash value is contents of elem. + * static size_t rehash(const void *elem, void *unused) + * { + * (void)unused; + * return *(size_t *)elem; + * } + * static struct htable ht = HTABLE_INITIALIZER(ht, rehash, NULL); + */ +#define HTABLE_INITIALIZER(name, rehash, priv) \ + { rehash, priv, 0, 0, 0, 0, -1, 0, &name.common_bits } + +/** + * htable_init - initialize an empty hash table. + * @ht: the hash table to initialize + * @rehash: hash function to use for rehashing. + * @priv: private argument to @rehash function. + */ +void htable_init(struct htable *ht, + size_t (*rehash)(const void *elem, void *priv), void *priv); + +/** + * htable_init_sized - initialize an empty hash table of given size. + * @ht: the hash table to initialize + * @rehash: hash function to use for rehashing. + * @priv: private argument to @rehash function. + * @size: the number of element. + * + * If this returns false, @ht is still usable, but may need to do reallocation + * upon an add. If this returns true, it will not need to reallocate within + * @size htable_adds. + */ +bool htable_init_sized(struct htable *ht, + size_t (*rehash)(const void *elem, void *priv), + void *priv, size_t size); + +/** + * htable_count - count number of entries in a hash table. + * @ht: the hash table + */ +static inline size_t htable_count(const struct htable *ht) +{ + return ht->elems; +} + +/** + * htable_clear - empty a hash table. + * @ht: the hash table to clear + * + * This doesn't do anything to any pointers left in it. + */ +void htable_clear(struct htable *ht); + + +/** + * htable_check - check hash table for consistency + * @ht: the htable + * @abortstr: the location to print on aborting, or NULL. + * + * Because hash tables have redundant information, consistency checking that + * each element is in the correct location can be done. This is useful as a + * debugging check. If @abortstr is non-NULL, that will be printed in a + * diagnostic if the htable is inconsistent, and the function will abort. + * + * Returns the htable if it is consistent, NULL if not (it can never return + * NULL if @abortstr is set). + */ +struct htable *htable_check(const struct htable *ht, const char *abortstr); + +/** + * htable_copy - duplicate a hash table. + * @dst: the hash table to overwrite + * @src: the hash table to copy + * + * Only fails on out-of-memory. + * + * Equivalent to (but faster than): + * if (!htable_init_sized(dst, src->rehash, src->priv, 1U << src->bits)) + * return false; + * v = htable_first(src, &i); + * while (v) { + * htable_add(dst, v); + * v = htable_next(src, i); + * } + * return true; + */ +#define htable_copy(dst, src) htable_copy_(dst, htable_debug(src, HTABLE_LOC)) +bool htable_copy_(struct htable *dst, const struct htable *src); + +/** + * htable_add - add a pointer into a hash table. + * @ht: the htable + * @hash: the hash value of the object + * @p: the non-NULL pointer (also cannot be (void *)1). + * + * Also note that this can only fail due to allocation failure. Otherwise, it + * returns true. + */ +#define htable_add(ht, hash, p) \ + htable_add_(htable_debug(ht, HTABLE_LOC), hash, p) +bool htable_add_(struct htable *ht, size_t hash, const void *p); + +/** + * htable_del - remove a pointer from a hash table + * @ht: the htable + * @hash: the hash value of the object + * @p: the pointer + * + * Returns true if the pointer was found (and deleted). + */ +#define htable_del(ht, hash, p) \ + htable_del_(htable_debug(ht, HTABLE_LOC), hash, p) +bool htable_del_(struct htable *ht, size_t hash, const void *p); + +/** + * struct htable_iter - iterator or htable_first or htable_firstval etc. + * + * This refers to a location inside the hashtable. + */ +struct htable_iter { + size_t off; +}; + +/** + * htable_firstval - find a candidate for a given hash value + * @htable: the hashtable + * @i: the struct htable_iter to initialize + * @hash: the hash value + * + * You'll need to check the value is what you want; returns NULL if none. + * See Also: + * htable_delval() + */ +#define htable_firstval(htable, i, hash) \ + htable_firstval_(htable_debug(htable, HTABLE_LOC), i, hash) + +void *htable_firstval_(const struct htable *htable, + struct htable_iter *i, size_t hash); + +/** + * htable_nextval - find another candidate for a given hash value + * @htable: the hashtable + * @i: the struct htable_iter to initialize + * @hash: the hash value + * + * You'll need to check the value is what you want; returns NULL if no more. + */ +#define htable_nextval(htable, i, hash) \ + htable_nextval_(htable_debug(htable, HTABLE_LOC), i, hash) +void *htable_nextval_(const struct htable *htable, + struct htable_iter *i, size_t hash); + +/** + * htable_get - find an entry in the hash table + * @ht: the hashtable + * @h: the hash value of the entry + * @cmp: the comparison function + * @ptr: the pointer to hand to the comparison function. + * + * Convenient inline wrapper for htable_firstval/htable_nextval loop. + */ +static inline void *htable_get(const struct htable *ht, + size_t h, + bool (*cmp)(const void *candidate, void *ptr), + const void *ptr) +{ + struct htable_iter i; + void *c; + + for (c = htable_firstval(ht,&i,h); c; c = htable_nextval(ht,&i,h)) { + if (cmp(c, (void *)ptr)) + return c; + } + return NULL; +} + +/** + * htable_first - find an entry in the hash table + * @ht: the hashtable + * @i: the struct htable_iter to initialize + * + * Get an entry in the hashtable; NULL if empty. + */ +#define htable_first(htable, i) \ + htable_first_(htable_debug(htable, HTABLE_LOC), i) +void *htable_first_(const struct htable *htable, struct htable_iter *i); + +/** + * htable_next - find another entry in the hash table + * @ht: the hashtable + * @i: the struct htable_iter to use + * + * Get another entry in the hashtable; NULL if all done. + * This is usually used after htable_first or prior non-NULL htable_next. + */ +#define htable_next(htable, i) \ + htable_next_(htable_debug(htable, HTABLE_LOC), i) +void *htable_next_(const struct htable *htable, struct htable_iter *i); + +/** + * htable_prev - find the previous entry in the hash table + * @ht: the hashtable + * @i: the struct htable_iter to use + * + * Get previous entry in the hashtable; NULL if all done. + * + * "previous" here only means the item that would have been returned by + * htable_next() before the item it returned most recently. + * + * This is usually used in the middle of (or after) a htable_next iteration and + * to "unwind" actions taken. + */ +#define htable_prev(htable, i) \ + htable_prev_(htable_debug(htable, HTABLE_LOC), i) +void *htable_prev_(const struct htable *htable, struct htable_iter *i); + +/** + * htable_delval - remove an iterated pointer from a hash table + * @ht: the htable + * @i: the htable_iter + * + * Usually used to delete a hash entry after it has been found with + * htable_firstval etc. + */ +#define htable_delval(htable, i) \ + htable_delval_(htable_debug(htable, HTABLE_LOC), i) +void htable_delval_(struct htable *ht, struct htable_iter *i); + +/** + * htable_pick - set iterator to a random valid entry. + * @ht: the htable + * @seed: a random number to use. + * @i: the htable_iter which is output (or NULL). + * + * Usually used with htable_delval to delete a random entry. Returns + * NULL iff the table is empty, otherwise a random entry. + */ +#define htable_pick(htable, seed, i) \ + htable_pick_(htable_debug(htable, HTABLE_LOC), seed, i) +void *htable_pick_(const struct htable *ht, size_t seed, struct htable_iter *i); + +/** + * htable_set_allocator - set calloc/free functions. + * @alloc: allocator to use, must zero memory! + * @free: unallocator to use (@p is NULL or a return from @alloc) + */ +void htable_set_allocator(void *(*alloc)(struct htable *, size_t len), + void (*free)(struct htable *, void *p)); +#endif /* CCAN_HTABLE_H */ diff --git a/ccan/ccan/htable/htable_type.h b/ccan/ccan/htable/htable_type.h new file mode 100644 index 0000000..0aacb7f --- /dev/null +++ b/ccan/ccan/htable/htable_type.h @@ -0,0 +1,188 @@ +/* Licensed under LGPLv2+ - see LICENSE file for details */ +#ifndef CCAN_HTABLE_TYPE_H +#define CCAN_HTABLE_TYPE_H +#include +#include +#include "config.h" + +/** + * HTABLE_DEFINE_TYPE - create a set of htable ops for a type + * @type: a type whose pointers will be values in the hash. + * @keyof: a function/macro to extract a key: @keyof(const type *elem) + * @hashfn: a hash function for a @key: size_t @hashfn(const *) + * @eqfn: an equality function keys: bool @eqfn(const type *, const *) + * @prefix: a prefix for all the functions to define (of form _*) + * + * NULL values may not be placed into the hash table. + * + * This defines the type hashtable type and an iterator type: + * struct ; + * struct _iter; + * + * It also defines initialization and freeing functions: + * void _init(struct *); + * bool _init_sized(struct *, size_t); + * void _clear(struct *); + * bool _copy(struct *dst, const struct *src); + * + * Count entries: + * size_t _count(const struct *ht); + * + * Add function only fails if we run out of memory: + * bool _add(struct *ht, const *e); + * + * Delete and delete-by key return true if it was in the set: + * bool _del(struct *ht, const *e); + * bool _delkey(struct *ht, const *k); + * + * Delete by iterator: + * bool _delval(struct *ht, struct _iter *i); + * + * Find and return the (first) matching element, or NULL: + * type *_get(const struct @name *ht, const *k); + * + * Find and return all matching elements, or NULL: + * type *_getfirst(const struct @name *ht, const *k, + * struct _iter *i); + * type *_getnext(const struct @name *ht, const *k, + * struct _iter *i); + * + * Iteration over hashtable is also supported: + * type *_first(const struct *ht, struct _iter *i); + * type *_next(const struct *ht, struct _iter *i); + * type *_prev(const struct *ht, struct _iter *i); + * type *_pick(const struct *ht, size_t seed, + * struct _iter *i); + * It's currently safe to iterate over a changing hashtable, but you might + * miss an element. Iteration isn't very efficient, either. + * + * You can use HTABLE_INITIALIZER like so: + * struct ht = { HTABLE_INITIALIZER(ht.raw, _hash, NULL) }; + */ +#define HTABLE_DEFINE_TYPE(type, keyof, hashfn, eqfn, name) \ + struct name { struct htable raw; }; \ + struct name##_iter { struct htable_iter i; }; \ + static inline size_t name##_hash(const void *elem, void *priv) \ + { \ + (void)priv; \ + return hashfn(keyof((const type *)elem)); \ + } \ + static inline UNNEEDED void name##_init(struct name *ht) \ + { \ + htable_init(&ht->raw, name##_hash, NULL); \ + } \ + static inline UNNEEDED bool name##_init_sized(struct name *ht, \ + size_t s) \ + { \ + return htable_init_sized(&ht->raw, name##_hash, NULL, s); \ + } \ + static inline UNNEEDED size_t name##_count(const struct name *ht) \ + { \ + return htable_count(&ht->raw); \ + } \ + static inline UNNEEDED void name##_clear(struct name *ht) \ + { \ + htable_clear(&ht->raw); \ + } \ + static inline UNNEEDED bool name##_copy(struct name *dst, \ + const struct name *src) \ + { \ + return htable_copy(&dst->raw, &src->raw); \ + } \ + static inline bool name##_add(struct name *ht, const type *elem) \ + { \ + return htable_add(&ht->raw, hashfn(keyof(elem)), elem); \ + } \ + static inline UNNEEDED bool name##_del(struct name *ht, \ + const type *elem) \ + { \ + return htable_del(&ht->raw, hashfn(keyof(elem)), elem); \ + } \ + static inline UNNEEDED type *name##_get(const struct name *ht, \ + const HTABLE_KTYPE(keyof, type) k) \ + { \ + struct htable_iter i; \ + size_t h = hashfn(k); \ + void *c; \ + \ + for (c = htable_firstval(&ht->raw,&i,h); \ + c; \ + c = htable_nextval(&ht->raw,&i,h)) { \ + if (eqfn(c, k)) \ + return c; \ + } \ + return NULL; \ + } \ + static inline UNNEEDED type *name##_getmatch_(const struct name *ht, \ + const HTABLE_KTYPE(keyof, type) k, \ + size_t h, \ + type *v, \ + struct name##_iter *iter) \ + { \ + while (v) { \ + if (eqfn(v, k)) \ + break; \ + v = htable_nextval(&ht->raw, &iter->i, h); \ + } \ + return v; \ + } \ + static inline UNNEEDED type *name##_getfirst(const struct name *ht, \ + const HTABLE_KTYPE(keyof, type) k, \ + struct name##_iter *iter) \ + { \ + size_t h = hashfn(k); \ + type *v = htable_firstval(&ht->raw, &iter->i, h); \ + return name##_getmatch_(ht, k, h, v, iter); \ + } \ + static inline UNNEEDED type *name##_getnext(const struct name *ht, \ + const HTABLE_KTYPE(keyof, type) k, \ + struct name##_iter *iter) \ + { \ + size_t h = hashfn(k); \ + type *v = htable_nextval(&ht->raw, &iter->i, h); \ + return name##_getmatch_(ht, k, h, v, iter); \ + } \ + static inline UNNEEDED bool name##_delkey(struct name *ht, \ + const HTABLE_KTYPE(keyof, type) k) \ + { \ + type *elem = name##_get(ht, k); \ + if (elem) \ + return name##_del(ht, elem); \ + return false; \ + } \ + static inline UNNEEDED void name##_delval(struct name *ht, \ + struct name##_iter *iter) \ + { \ + htable_delval(&ht->raw, &iter->i); \ + } \ + static inline UNNEEDED type *name##_pick(const struct name *ht, \ + size_t seed, \ + struct name##_iter *iter) \ + { \ + return htable_pick(&ht->raw, seed, iter ? &iter->i : NULL); \ + } \ + static inline UNNEEDED type *name##_first(const struct name *ht, \ + struct name##_iter *iter) \ + { \ + return htable_first(&ht->raw, &iter->i); \ + } \ + static inline UNNEEDED type *name##_next(const struct name *ht, \ + struct name##_iter *iter) \ + { \ + return htable_next(&ht->raw, &iter->i); \ + } \ + static inline UNNEEDED type *name##_prev(const struct name *ht, \ + struct name##_iter *iter) \ + { \ + return htable_prev(&ht->raw, &iter->i); \ + } + +#if HAVE_TYPEOF +#define HTABLE_KTYPE(keyof, type) typeof(keyof((const type *)NULL)) +#else +/* Assumes keys are a pointer: if not, override. */ +#ifndef HTABLE_KTYPE +#define HTABLE_KTYPE(keyof, type) void * +#endif +#endif +#endif /* CCAN_HTABLE_TYPE_H */ diff --git a/ccan/ccan/ilog/LICENSE b/ccan/ccan/ilog/LICENSE new file mode 120000 index 0000000..b7951da --- /dev/null +++ b/ccan/ccan/ilog/LICENSE @@ -0,0 +1 @@ +../../licenses/CC0 \ No newline at end of file diff --git a/ccan/ccan/ilog/ilog.c b/ccan/ccan/ilog/ilog.c new file mode 100644 index 0000000..5f5122d --- /dev/null +++ b/ccan/ccan/ilog/ilog.c @@ -0,0 +1,141 @@ +/*(C) Timothy B. Terriberry (tterribe@xiph.org) 2001-2009 CC0 (Public domain). + * See LICENSE file for details. */ +#include "ilog.h" +#include + +/*The fastest fallback strategy for platforms with fast multiplication appears + to be based on de Bruijn sequences~\cite{LP98}. + Tests confirmed this to be true even on an ARM11, where it is actually faster + than using the native clz instruction. + Define ILOG_NODEBRUIJN to use a simpler fallback on platforms where + multiplication or table lookups are too expensive. + + @UNPUBLISHED{LP98, + author="Charles E. Leiserson and Harald Prokop", + title="Using de {Bruijn} Sequences to Index a 1 in a Computer Word", + month=Jun, + year=1998, + note="\url{http://supertech.csail.mit.edu/papers/debruijn.pdf}" + }*/ +static UNNEEDED const unsigned char DEBRUIJN_IDX32[32]={ + 0, 1,28, 2,29,14,24, 3,30,22,20,15,25,17, 4, 8, + 31,27,13,23,21,19,16, 7,26,12,18, 6,11, 5,10, 9 +}; + +/* We always compile these in, in case someone takes address of function. */ +#undef ilog32_nz +#undef ilog32 +#undef ilog64_nz +#undef ilog64 + +int ilog32(uint32_t _v){ +/*On a Pentium M, this branchless version tested as the fastest version without + multiplications on 1,000,000,000 random 32-bit integers, edging out a + similar version with branches, and a 256-entry LUT version.*/ +# if defined(ILOG_NODEBRUIJN) + int ret; + int m; + ret=_v>0; + m=(_v>0xFFFFU)<<4; + _v>>=m; + ret|=m; + m=(_v>0xFFU)<<3; + _v>>=m; + ret|=m; + m=(_v>0xFU)<<2; + _v>>=m; + ret|=m; + m=(_v>3)<<1; + _v>>=m; + ret|=m; + ret+=_v>1; + return ret; +/*This de Bruijn sequence version is faster if you have a fast multiplier.*/ +# else + int ret; + ret=_v>0; + _v|=_v>>1; + _v|=_v>>2; + _v|=_v>>4; + _v|=_v>>8; + _v|=_v>>16; + _v=(_v>>1)+1; + ret+=DEBRUIJN_IDX32[_v*0x77CB531U>>27&0x1F]; + return ret; +# endif +} + +int ilog32_nz(uint32_t _v) +{ + return ilog32(_v); +} + +int ilog64(uint64_t _v){ +# if defined(ILOG_NODEBRUIJN) + uint32_t v; + int ret; + int m; + ret=_v>0; + m=(_v>0xFFFFFFFFU)<<5; + v=(uint32_t)(_v>>m); + ret|=m; + m=(v>0xFFFFU)<<4; + v>>=m; + ret|=m; + m=(v>0xFFU)<<3; + v>>=m; + ret|=m; + m=(v>0xFU)<<2; + v>>=m; + ret|=m; + m=(v>3)<<1; + v>>=m; + ret|=m; + ret+=v>1; + return ret; +# else +/*If we don't have a 64-bit word, split it into two 32-bit halves.*/ +# if LONG_MAX<9223372036854775807LL + uint32_t v; + int ret; + int m; + ret=_v>0; + m=(_v>0xFFFFFFFFU)<<5; + v=(uint32_t)(_v>>m); + ret|=m; + v|=v>>1; + v|=v>>2; + v|=v>>4; + v|=v>>8; + v|=v>>16; + v=(v>>1)+1; + ret+=DEBRUIJN_IDX32[v*0x77CB531U>>27&0x1F]; + return ret; +/*Otherwise do it in one 64-bit operation.*/ +# else + static const unsigned char DEBRUIJN_IDX64[64]={ + 0, 1, 2, 7, 3,13, 8,19, 4,25,14,28, 9,34,20,40, + 5,17,26,38,15,46,29,48,10,31,35,54,21,50,41,57, + 63, 6,12,18,24,27,33,39,16,37,45,47,30,53,49,56, + 62,11,23,32,36,44,52,55,61,22,43,51,60,42,59,58 + }; + int ret; + ret=_v>0; + _v|=_v>>1; + _v|=_v>>2; + _v|=_v>>4; + _v|=_v>>8; + _v|=_v>>16; + _v|=_v>>32; + _v=(_v>>1)+1; + ret+=DEBRUIJN_IDX64[_v*0x218A392CD3D5DBF>>58&0x3F]; + return ret; +# endif +# endif +} + +int ilog64_nz(uint64_t _v) +{ + return ilog64(_v); +} + diff --git a/ccan/ccan/ilog/ilog.h b/ccan/ccan/ilog/ilog.h new file mode 100644 index 0000000..32702b1 --- /dev/null +++ b/ccan/ccan/ilog/ilog.h @@ -0,0 +1,154 @@ +/* CC0 (Public domain) - see LICENSE file for details */ +#if !defined(_ilog_H) +# define _ilog_H (1) +# include "config.h" +# include +# include +# include + +/** + * ilog32 - Integer binary logarithm of a 32-bit value. + * @_v: A 32-bit value. + * Returns floor(log2(_v))+1, or 0 if _v==0. + * This is the number of bits that would be required to represent _v in two's + * complement notation with all of the leading zeros stripped. + * Note that many uses will resolve to the fast macro version instead. + * + * See Also: + * ilog32_nz(), ilog64() + * + * Example: + * // Rounds up to next power of 2 (if not a power of 2). + * static uint32_t round_up32(uint32_t i) + * { + * assert(i != 0); + * return 1U << ilog32(i-1); + * } + */ +int ilog32(uint32_t _v) CONST_FUNCTION; + +/** + * ilog32_nz - Integer binary logarithm of a non-zero 32-bit value. + * @_v: A 32-bit value. + * Returns floor(log2(_v))+1, or undefined if _v==0. + * This is the number of bits that would be required to represent _v in two's + * complement notation with all of the leading zeros stripped. + * Note that many uses will resolve to the fast macro version instead. + * See Also: + * ilog32(), ilog64_nz() + * Example: + * // Find Last Set (ie. highest bit set, 0 to 31). + * static uint32_t fls32(uint32_t i) + * { + * assert(i != 0); + * return ilog32_nz(i) - 1; + * } + */ +int ilog32_nz(uint32_t _v) CONST_FUNCTION; + +/** + * ilog64 - Integer binary logarithm of a 64-bit value. + * @_v: A 64-bit value. + * Returns floor(log2(_v))+1, or 0 if _v==0. + * This is the number of bits that would be required to represent _v in two's + * complement notation with all of the leading zeros stripped. + * Note that many uses will resolve to the fast macro version instead. + * See Also: + * ilog64_nz(), ilog32() + */ +int ilog64(uint64_t _v) CONST_FUNCTION; + +/** + * ilog64_nz - Integer binary logarithm of a non-zero 64-bit value. + * @_v: A 64-bit value. + * Returns floor(log2(_v))+1, or undefined if _v==0. + * This is the number of bits that would be required to represent _v in two's + * complement notation with all of the leading zeros stripped. + * Note that many uses will resolve to the fast macro version instead. + * See Also: + * ilog64(), ilog32_nz() + */ +int ilog64_nz(uint64_t _v) CONST_FUNCTION; + +/** + * STATIC_ILOG_32 - The integer logarithm of an (unsigned, 32-bit) constant. + * @_v: A non-negative 32-bit constant. + * Returns floor(log2(_v))+1, or 0 if _v==0. + * This is the number of bits that would be required to represent _v in two's + * complement notation with all of the leading zeros stripped. + * This macro should only be used when you need a compile-time constant, + * otherwise ilog32 or ilog32_nz are just as fast and more flexible. + * + * Example: + * #define MY_PAGE_SIZE 4096 + * #define MY_PAGE_BITS (STATIC_ILOG_32(PAGE_SIZE) - 1) + */ +#define STATIC_ILOG_32(_v) (STATIC_ILOG5((uint32_t)(_v))) + +/** + * STATIC_ILOG_64 - The integer logarithm of an (unsigned, 64-bit) constant. + * @_v: A non-negative 64-bit constant. + * Returns floor(log2(_v))+1, or 0 if _v==0. + * This is the number of bits that would be required to represent _v in two's + * complement notation with all of the leading zeros stripped. + * This macro should only be used when you need a compile-time constant, + * otherwise ilog64 or ilog64_nz are just as fast and more flexible. + */ +#define STATIC_ILOG_64(_v) (STATIC_ILOG6((uint64_t)(_v))) + +/* Private implementation details */ + +/*Note the casts to (int) below: this prevents "upgrading" + the type of an entire expression to an (unsigned) size_t.*/ +#if INT_MAX>=2147483647 && HAVE_BUILTIN_CLZ +#define builtin_ilog32_nz(v) \ + (((int)sizeof(unsigned)*CHAR_BIT) - __builtin_clz(v)) +#elif LONG_MAX>=2147483647L && HAVE_BUILTIN_CLZL +#define builtin_ilog32_nz(v) \ + (((int)sizeof(unsigned)*CHAR_BIT) - __builtin_clzl(v)) +#endif + +#if INT_MAX>=9223372036854775807LL && HAVE_BUILTIN_CLZ +#define builtin_ilog64_nz(v) \ + (((int)sizeof(unsigned)*CHAR_BIT) - __builtin_clz(v)) +#elif LONG_MAX>=9223372036854775807LL && HAVE_BUILTIN_CLZL +#define builtin_ilog64_nz(v) \ + (((int)sizeof(unsigned long)*CHAR_BIT) - __builtin_clzl(v)) +#elif HAVE_BUILTIN_CLZLL +#define builtin_ilog64_nz(v) \ + (((int)sizeof(unsigned long long)*CHAR_BIT) - __builtin_clzll(v)) +#endif + +#ifdef builtin_ilog32_nz +/* This used to be builtin_ilog32_nz(_v)&-!!(_v), which means it zeroes out + * the undefined builtin_ilog32_nz(0) return. But clang UndefinedBehaviorSantizer + * complains, so do the branch: */ +#define ilog32(_v) ((_v) ? builtin_ilog32_nz(_v) : 0) +#define ilog32_nz(_v) builtin_ilog32_nz(_v) +#else +#define ilog32_nz(_v) ilog32(_v) +#define ilog32(_v) (IS_COMPILE_CONSTANT(_v) ? STATIC_ILOG_32(_v) : ilog32(_v)) +#endif /* builtin_ilog32_nz */ + +#ifdef builtin_ilog64_nz +#define ilog32(_v) ((_v) ? builtin_ilog32_nz(_v) : 0) +#define ilog64_nz(_v) builtin_ilog64_nz(_v) +#else +#define ilog64_nz(_v) ilog64(_v) +#define ilog64(_v) (IS_COMPILE_CONSTANT(_v) ? STATIC_ILOG_64(_v) : ilog64(_v)) +#endif /* builtin_ilog64_nz */ + +/* Macros for evaluating compile-time constant ilog. */ +# define STATIC_ILOG0(_v) (!!(_v)) +# define STATIC_ILOG1(_v) (((_v)&0x2)?2:STATIC_ILOG0(_v)) +# define STATIC_ILOG2(_v) (((_v)&0xC)?2+STATIC_ILOG1((_v)>>2):STATIC_ILOG1(_v)) +# define STATIC_ILOG3(_v) \ + (((_v)&0xF0)?4+STATIC_ILOG2((_v)>>4):STATIC_ILOG2(_v)) +# define STATIC_ILOG4(_v) \ + (((_v)&0xFF00)?8+STATIC_ILOG3((_v)>>8):STATIC_ILOG3(_v)) +# define STATIC_ILOG5(_v) \ + (((_v)&0xFFFF0000)?16+STATIC_ILOG4((_v)>>16):STATIC_ILOG4(_v)) +# define STATIC_ILOG6(_v) \ + (((_v)&0xFFFFFFFF00000000ULL)?32+STATIC_ILOG5((_v)>>32):STATIC_ILOG5(_v)) + +#endif /* _ilog_H */ diff --git a/ccan/ccan/likely/LICENSE b/ccan/ccan/likely/LICENSE new file mode 120000 index 0000000..b7951da --- /dev/null +++ b/ccan/ccan/likely/LICENSE @@ -0,0 +1 @@ +../../licenses/CC0 \ No newline at end of file diff --git a/ccan/ccan/likely/likely.c b/ccan/ccan/likely/likely.c new file mode 100644 index 0000000..83e8d6f --- /dev/null +++ b/ccan/ccan/likely/likely.c @@ -0,0 +1,136 @@ +/* CC0 (Public domain) - see LICENSE file for details. */ +#ifdef CCAN_LIKELY_DEBUG +#include +#include +#include +#include +#include +struct trace { + const char *condstr; + const char *file; + unsigned int line; + bool expect; + unsigned long count, right; +}; + +static size_t hash_trace(const struct trace *trace) +{ + return hash(trace->condstr, strlen(trace->condstr), + hash(trace->file, strlen(trace->file), + trace->line + trace->expect)); +} + +static bool trace_eq(const struct trace *t1, const struct trace *t2) +{ + return t1->condstr == t2->condstr + && t1->file == t2->file + && t1->line == t2->line + && t1->expect == t2->expect; +} + +/* struct thash */ +HTABLE_DEFINE_TYPE(struct trace, (const struct trace *), hash_trace, trace_eq, + thash); + +static struct thash htable += { HTABLE_INITIALIZER(htable.raw, thash_hash, NULL) }; + +static void init_trace(struct trace *trace, + const char *condstr, const char *file, unsigned int line, + bool expect) +{ + trace->condstr = condstr; + trace->file = file; + trace->line = line; + trace->expect = expect; + trace->count = trace->right = 0; +} + +static struct trace *add_trace(const struct trace *t) +{ + struct trace *trace = malloc(sizeof(*trace)); + *trace = *t; + thash_add(&htable, trace); + return trace; +} + +long _likely_trace(bool cond, bool expect, + const char *condstr, + const char *file, unsigned int line) +{ + struct trace *p, trace; + + init_trace(&trace, condstr, file, line, expect); + p = thash_get(&htable, &trace); + if (!p) + p = add_trace(&trace); + + p->count++; + if (cond == expect) + p->right++; + + return cond; +} + +static double right_ratio(const struct trace *t) +{ + return (double)t->right / t->count; +} + +char *likely_stats(unsigned int min_hits, unsigned int percent) +{ + struct trace *worst; + double worst_ratio; + struct thash_iter i; + char *ret; + struct trace *t; + + worst = NULL; + worst_ratio = 2; + + /* This is O(n), but it's not likely called that often. */ + for (t = thash_first(&htable, &i); t; t = thash_next(&htable, &i)) { + if (t->count >= min_hits) { + if (right_ratio(t) < worst_ratio) { + worst = t; + worst_ratio = right_ratio(t); + } + } + } + + if (worst_ratio * 100 > percent) + return NULL; + + ret = malloc(strlen(worst->condstr) + + strlen(worst->file) + + sizeof(long int) * 8 + + sizeof("%s:%u:%slikely(%s) correct %u%% (%lu/%lu)")); + sprintf(ret, "%s:%u:%slikely(%s) correct %u%% (%lu/%lu)", + worst->file, worst->line, + worst->expect ? "" : "un", worst->condstr, + (unsigned)(worst_ratio * 100), + worst->right, worst->count); + + thash_del(&htable, worst); + free(worst); + + return ret; +} + +void likely_stats_reset(void) +{ + struct thash_iter i; + struct trace *t; + + /* This is a bit better than O(n^2), but we have to loop since + * first/next during delete is unreliable. */ + while ((t = thash_first(&htable, &i)) != NULL) { + for (; t; t = thash_next(&htable, &i)) { + thash_del(&htable, t); + free(t); + } + } + + thash_clear(&htable); +} +#endif /*CCAN_LIKELY_DEBUG*/ diff --git a/ccan/ccan/likely/likely.h b/ccan/ccan/likely/likely.h new file mode 100644 index 0000000..a8f003d --- /dev/null +++ b/ccan/ccan/likely/likely.h @@ -0,0 +1,111 @@ +/* CC0 (Public domain) - see LICENSE file for details */ +#ifndef CCAN_LIKELY_H +#define CCAN_LIKELY_H +#include "config.h" +#include + +#ifndef CCAN_LIKELY_DEBUG +#if HAVE_BUILTIN_EXPECT +/** + * likely - indicate that a condition is likely to be true. + * @cond: the condition + * + * This uses a compiler extension where available to indicate a likely + * code path and optimize appropriately; it's also useful for readers + * to quickly identify exceptional paths through functions. The + * threshold for "likely" is usually considered to be between 90 and + * 99%; marginal cases should not be marked either way. + * + * See Also: + * unlikely(), likely_stats() + * + * Example: + * // Returns false if we overflow. + * static inline bool inc_int(unsigned int *val) + * { + * (*val)++; + * if (likely(*val)) + * return true; + * return false; + * } + */ +#define likely(cond) __builtin_expect(!!(cond), 1) + +/** + * unlikely - indicate that a condition is unlikely to be true. + * @cond: the condition + * + * This uses a compiler extension where available to indicate an unlikely + * code path and optimize appropriately; see likely() above. + * + * See Also: + * likely(), likely_stats(), COLD (compiler.h) + * + * Example: + * // Prints a warning if we overflow. + * static inline void inc_int(unsigned int *val) + * { + * (*val)++; + * if (unlikely(*val == 0)) + * fprintf(stderr, "Overflow!"); + * } + */ +#define unlikely(cond) __builtin_expect(!!(cond), 0) +#else +#define likely(cond) (!!(cond)) +#define unlikely(cond) (!!(cond)) +#endif +#else /* CCAN_LIKELY_DEBUG versions */ +#include + +#define likely(cond) \ + (_likely_trace(!!(cond), 1, stringify(cond), __FILE__, __LINE__)) +#define unlikely(cond) \ + (_likely_trace(!!(cond), 0, stringify(cond), __FILE__, __LINE__)) + +long _likely_trace(bool cond, bool expect, + const char *condstr, + const char *file, unsigned int line); +/** + * likely_stats - return description of abused likely()/unlikely() + * @min_hits: minimum number of hits + * @percent: maximum percentage correct + * + * When CCAN_LIKELY_DEBUG is defined, likely() and unlikely() trace their + * results: this causes a significant slowdown, but allows analysis of + * whether the branches are labelled correctly. + * + * This function returns a malloc'ed description of the least-correct + * usage of likely() or unlikely(). It ignores places which have been + * called less than @min_hits times, and those which were predicted + * correctly more than @percent of the time. It returns NULL when + * nothing meets those criteria. + * + * Note that this call is destructive; the returned offender is + * removed from the trace so that the next call to likely_stats() will + * return the next-worst likely()/unlikely() usage. + * + * Example: + * // Print every place hit more than twice which was wrong > 5%. + * static void report_stats(void) + * { + * #ifdef CCAN_LIKELY_DEBUG + * const char *bad; + * + * while ((bad = likely_stats(2, 95)) != NULL) { + * printf("Suspicious likely: %s", bad); + * free(bad); + * } + * #endif + * } + */ +char *likely_stats(unsigned int min_hits, unsigned int percent); + +/** + * likely_stats_reset - free up memory of likely()/unlikely() branches. + * + * This can also plug memory leaks. + */ +void likely_stats_reset(void); +#endif /* CCAN_LIKELY_DEBUG */ +#endif /* CCAN_LIKELY_H */ diff --git a/ccan/ccan/list/_info b/ccan/ccan/list/_info deleted file mode 100644 index c4f3e2a..0000000 --- a/ccan/ccan/list/_info +++ /dev/null @@ -1,72 +0,0 @@ -#include "config.h" -#include -#include - -/** - * list - double linked list routines - * - * The list header contains routines for manipulating double linked lists. - * It defines two types: struct list_head used for anchoring lists, and - * struct list_node which is usually embedded in the structure which is placed - * in the list. - * - * Example: - * #include - * #include - * #include - * #include - * - * struct parent { - * const char *name; - * struct list_head children; - * unsigned int num_children; - * }; - * - * struct child { - * const char *name; - * struct list_node list; - * }; - * - * int main(int argc, char *argv[]) - * { - * struct parent p; - * struct child *c; - * int i; - * - * if (argc < 2) - * errx(1, "Usage: %s parent children...", argv[0]); - * - * p.name = argv[1]; - * list_head_init(&p.children); - * p.num_children = 0; - * for (i = 2; i < argc; i++) { - * c = malloc(sizeof(*c)); - * c->name = argv[i]; - * list_add(&p.children, &c->list); - * p.num_children++; - * } - * - * printf("%s has %u children:", p.name, p.num_children); - * list_for_each(&p.children, c, list) - * printf("%s ", c->name); - * printf("\n"); - * return 0; - * } - * - * License: BSD-MIT - * Author: Rusty Russell - */ -int main(int argc, char *argv[]) -{ - if (argc != 2) - return 1; - - if (strcmp(argv[1], "depends") == 0) { - printf("ccan/str\n"); - printf("ccan/container_of\n"); - printf("ccan/check_type\n"); - return 0; - } - - return 1; -} diff --git a/ccan/ccan/short_types/LICENSE b/ccan/ccan/short_types/LICENSE new file mode 120000 index 0000000..b7951da --- /dev/null +++ b/ccan/ccan/short_types/LICENSE @@ -0,0 +1 @@ +../../licenses/CC0 \ No newline at end of file diff --git a/ccan/ccan/short_types/short_types.h b/ccan/ccan/short_types/short_types.h new file mode 100644 index 0000000..175377e --- /dev/null +++ b/ccan/ccan/short_types/short_types.h @@ -0,0 +1,35 @@ +/* CC0 (Public domain) - see LICENSE file for details */ +#ifndef CCAN_SHORT_TYPES_H +#define CCAN_SHORT_TYPES_H +#include + +/** + * u64/s64/u32/s32/u16/s16/u8/s8 - short names for explicitly-sized types. + */ +typedef uint64_t u64; +typedef int64_t s64; +typedef uint32_t u32; +typedef int32_t s32; +typedef uint16_t u16; +typedef int16_t s16; +typedef uint8_t u8; +typedef int8_t s8; + +/* Whichever they include first, they get these definitions. */ +#ifdef CCAN_ENDIAN_H +/** + * be64/be32/be16 - 64/32/16 bit big-endian representation. + */ +typedef beint64_t be64; +typedef beint32_t be32; +typedef beint16_t be16; + +/** + * le64/le32/le16 - 64/32/16 bit little-endian representation. + */ +typedef leint64_t le64; +typedef leint32_t le32; +typedef leint16_t le16; +#endif + +#endif /* CCAN_SHORT_TYPES_H */ diff --git a/ccan/ccan/str/_info b/ccan/ccan/str/_info deleted file mode 100644 index b579525..0000000 --- a/ccan/ccan/str/_info +++ /dev/null @@ -1,52 +0,0 @@ -#include "config.h" -#include -#include - -/** - * str - string helper routines - * - * This is a grab bag of functions for string operations, designed to enhance - * the standard string.h. - * - * Note that if you define CCAN_STR_DEBUG, you will get extra compile - * checks on common misuses of the following functions (they will now - * be out-of-line, so there is a runtime penalty!). - * - * strstr, strchr, strrchr: - * Return const char * if first argument is const (gcc only). - * - * isalnum, isalpha, isascii, isblank, iscntrl, isdigit, isgraph, - * islower, isprint, ispunct, isspace, isupper, isxdigit: - * Static and runtime check that input is EOF or an *unsigned* - * char, as per C standard (really!). - * - * Example: - * #include - * #include - * - * int main(int argc, char *argv[]) - * { - * if (argc > 1 && streq(argv[1], "--verbose")) - * printf("verbose set\n"); - * if (argc > 1 && strstarts(argv[1], "--")) - * printf("Some option set\n"); - * if (argc > 1 && strends(argv[1], "cow-powers")) - * printf("Magic option set\n"); - * return 0; - * } - * - * License: CC0 (Public domain) - * Author: Rusty Russell - */ -int main(int argc, char *argv[]) -{ - if (argc != 2) - return 1; - - if (strcmp(argv[1], "depends") == 0) { - printf("ccan/build_assert\n"); - return 0; - } - - return 1; -} diff --git a/ccan/ccan/strset/strset.c b/ccan/ccan/strset/strset.c new file mode 100644 index 0000000..06b0d7a --- /dev/null +++ b/ccan/ccan/strset/strset.c @@ -0,0 +1,309 @@ +/* This code is based on the public domain code at + * http://github.com/agl/critbit writtem by Adam Langley + * . + * + * Here are the main implementation differences: + * (1) We don't strdup the string on insert; we use the pointer we're given. + * (2) We use a straight bit number rather than a mask; it's simpler. + * (3) We don't use the bottom bit of the pointer, but instead use a leading + * zero to distinguish nodes from strings. + * (4) The empty string (which would look like a node) is handled + * using a special "empty node". + * (5) Delete returns the string, so you can free it if you want to. + * (6) Unions instead of void *, bool instead of int. + */ +#include +#include +#include +#include +#include +#include +#include +#include + +struct node { + /* To differentiate us from strings. */ + char nul_byte; + /* The bit where these children differ. */ + u8 bit_num; + /* The byte number where first bit differs (-1 == empty string node). */ + size_t byte_num; + /* These point to strings or nodes. */ + struct strset child[2]; +}; + +/* Closest member to this in a non-empty set. */ +static const char *closest(struct strset n, const char *member) +{ + size_t len = strlen(member); + const u8 *bytes = (const u8 *)member; + + /* Anything with first byte 0 is a node. */ + while (!n.u.s[0]) { + u8 direction = 0; + + /* Special node which represents the empty string. */ + if (unlikely(n.u.n->byte_num == (size_t)-1)) { + n = n.u.n->child[0]; + break; + } + + if (n.u.n->byte_num < len) { + u8 c = bytes[n.u.n->byte_num]; + direction = (c >> n.u.n->bit_num) & 1; + } + n = n.u.n->child[direction]; + } + return n.u.s; +} + +char *strset_get(const struct strset *set, const char *member) +{ + const char *str; + + /* Non-empty set? */ + if (set->u.n) { + str = closest(*set, member); + if (streq(member, str)) + return (char *)str; + } + errno = ENOENT; + return NULL; +} + +static bool set_string(struct strset *set, + struct strset *n, const char *member) +{ + /* Substitute magic empty node if this is the empty string */ + if (unlikely(!member[0])) { + n->u.n = malloc(sizeof(*n->u.n)); + if (unlikely(!n->u.n)) { + errno = ENOMEM; + return false; + } + n->u.n->nul_byte = '\0'; + n->u.n->byte_num = (size_t)-1; + /* Attach the string to child[0] */ + n = &n->u.n->child[0]; + } + n->u.s = member; + return true; +} + +bool strset_add(struct strset *set, const char *member) +{ + size_t len = strlen(member); + const u8 *bytes = (const u8 *)member; + struct strset *np; + const char *str; + struct node *newn; + size_t byte_num; + u8 bit_num, new_dir; + + /* Empty set? */ + if (!set->u.n) { + return set_string(set, set, member); + } + + /* Find closest existing member. */ + str = closest(*set, member); + + /* Find where they differ. */ + for (byte_num = 0; str[byte_num] == member[byte_num]; byte_num++) { + if (member[byte_num] == '\0') { + /* All identical! */ + errno = EEXIST; + return false; + } + } + + /* Find which bit differs (if we had ilog8, we'd use it) */ + bit_num = ilog32_nz((u8)str[byte_num] ^ bytes[byte_num]) - 1; + assert(bit_num < CHAR_BIT); + + /* Which direction do we go at this bit? */ + new_dir = ((bytes[byte_num]) >> bit_num) & 1; + + /* Allocate new node. */ + newn = malloc(sizeof(*newn)); + if (!newn) { + errno = ENOMEM; + return false; + } + newn->nul_byte = '\0'; + newn->byte_num = byte_num; + newn->bit_num = bit_num; + if (unlikely(!set_string(set, &newn->child[new_dir], member))) { + free(newn); + return false; + } + + /* Find where to insert: not closest, but first which differs! */ + np = set; + while (!np->u.s[0]) { + u8 direction = 0; + + /* Special node which represents the empty string will + * break here too! */ + if (np->u.n->byte_num > byte_num) + break; + /* Subtle: bit numbers are "backwards" for comparison */ + if (np->u.n->byte_num == byte_num && np->u.n->bit_num < bit_num) + break; + + if (np->u.n->byte_num < len) { + u8 c = bytes[np->u.n->byte_num]; + direction = (c >> np->u.n->bit_num) & 1; + } + np = &np->u.n->child[direction]; + } + + newn->child[!new_dir]= *np; + np->u.n = newn; + return true; +} + +char *strset_del(struct strset *set, const char *member) +{ + size_t len = strlen(member); + const u8 *bytes = (const u8 *)member; + struct strset *parent = NULL, *n; + const char *ret = NULL; + u8 direction = 0; /* prevent bogus gcc warning. */ + + /* Empty set? */ + if (!set->u.n) { + errno = ENOENT; + return NULL; + } + + /* Find closest, but keep track of parent. */ + n = set; + /* Anything with first byte 0 is a node. */ + while (!n->u.s[0]) { + u8 c = 0; + + /* Special node which represents the empty string. */ + if (unlikely(n->u.n->byte_num == (size_t)-1)) { + const char *empty_str = n->u.n->child[0].u.s; + + if (member[0]) { + errno = ENOENT; + return NULL; + } + + /* Sew empty string back so remaining logic works */ + free(n->u.n); + n->u.s = empty_str; + break; + } + + parent = n; + if (n->u.n->byte_num < len) { + c = bytes[n->u.n->byte_num]; + direction = (c >> n->u.n->bit_num) & 1; + } else + direction = 0; + n = &n->u.n->child[direction]; + } + + /* Did we find it? */ + if (!streq(member, n->u.s)) { + errno = ENOENT; + return NULL; + } + + ret = n->u.s; + + if (!parent) { + /* We deleted last node. */ + set->u.n = NULL; + } else { + struct node *old = parent->u.n; + /* Raise other node to parent. */ + *parent = old->child[!direction]; + free(old); + } + + return (char *)ret; +} + +static bool iterate(struct strset n, + bool (*handle)(const char *, void *), const void *data) +{ + if (n.u.s[0]) + return handle(n.u.s, (void *)data); + if (unlikely(n.u.n->byte_num == (size_t)-1)) + return handle(n.u.n->child[0].u.s, (void *)data); + + return iterate(n.u.n->child[0], handle, data) + && iterate(n.u.n->child[1], handle, data); +} + +void strset_iterate_(const struct strset *set, + bool (*handle)(const char *, void *), const void *data) +{ + /* Empty set? */ + if (!set->u.n) + return; + + iterate(*set, handle, data); +} + +const struct strset *strset_prefix(const struct strset *set, const char *prefix) +{ + const struct strset *n, *top; + size_t len = strlen(prefix); + const u8 *bytes = (const u8 *)prefix; + + /* Empty set -> return empty set. */ + if (!set->u.n) + return set; + + top = n = set; + + /* We walk to find the top, but keep going to check prefix matches. */ + while (!n->u.s[0]) { + u8 c = 0, direction; + + /* Special node which represents the empty string. */ + if (unlikely(n->u.n->byte_num == (size_t)-1)) { + n = &n->u.n->child[0]; + break; + } + + if (n->u.n->byte_num < len) + c = bytes[n->u.n->byte_num]; + + direction = (c >> n->u.n->bit_num) & 1; + n = &n->u.n->child[direction]; + if (c) + top = n; + } + + if (!strstarts(n->u.s, prefix)) { + /* Convenient return for prefixes which do not appear in set. */ + static const struct strset empty_set; + return &empty_set; + } + + return top; +} + +static void clear(struct strset n) +{ + if (!n.u.s[0]) { + if (likely(n.u.n->byte_num != (size_t)-1)) { + clear(n.u.n->child[0]); + clear(n.u.n->child[1]); + } + free(n.u.n); + } +} + +void strset_clear(struct strset *set) +{ + if (set->u.n) + clear(*set); + set->u.n = NULL; +} diff --git a/ccan/ccan/strset/strset.h b/ccan/ccan/strset/strset.h new file mode 100644 index 0000000..9d6f1ae --- /dev/null +++ b/ccan/ccan/strset/strset.h @@ -0,0 +1,167 @@ +#ifndef CCAN_STRSET_H +#define CCAN_STRSET_H +#include "config.h" +#include +#include +#include + +/** + * struct strset - representation of a string set + * + * It's exposed here to allow you to embed it and so we can inline the + * trivial functions. + */ +struct strset { + union { + struct node *n; + const char *s; + } u; +}; + +/** + * strset_init - initialize a string set (empty) + * + * For completeness; if you've arranged for it to be NULL already you don't + * need this. + * + * Example: + * struct strset set; + * + * strset_init(&set); + */ +static inline void strset_init(struct strset *set) +{ + set->u.n = NULL; +} + +/** + * strset_empty - is this string set empty? + * @set: the set. + * + * Example: + * if (!strset_empty(&set)) + * abort(); + */ +static inline bool strset_empty(const struct strset *set) +{ + return set->u.n == NULL; +} + +/** + * strset_get - is this a member of this string set? + * @set: the set. + * @member: the string to search for. + * + * Returns the member, or NULL if it isn't in the set (and sets errno + * = ENOENT). + * + * Example: + * if (strset_get(&set, "hello")) + * printf("hello is in the set\n"); + */ +char *strset_get(const struct strset *set, const char *member); + +/** + * strset_add - place a member in the string set. + * @set: the set. + * @member: the string to place in the set. + * + * This returns false if we run out of memory (errno = ENOMEM), or + * (more normally) if that string already appears in the set (EEXIST). + * + * Note that the pointer is placed in the set, the string is not copied. If + * you want a copy in the set, use strdup(). + * + * Example: + * if (!strset_add(&set, "goodbye")) + * printf("goodbye was already in the set\n"); + */ +bool strset_add(struct strset *set, const char *member); + +/** + * strset_del - remove a member from the string set. + * @set: the set. + * @member: the string to remove from the set. + * + * This returns the string which was passed to strset_add(), or NULL if + * the string was not in the map (in which case it sets errno = ENOENT). + * + * This means that if you allocated a string (eg. using strdup()), you can + * free it here. + * + * Example: + * if (!strset_del(&set, "goodbye")) + * printf("goodbye was not in the set?\n"); + */ +char *strset_del(struct strset *set, const char *member); + +/** + * strset_clear - remove every member from the set. + * @set: the set. + * + * The set will be empty after this. + * + * Example: + * strset_clear(&set); + */ +void strset_clear(struct strset *set); + +/** + * strset_iterate - ordered iteration over a set + * @set: the set. + * @handle: the function to call. + * @arg: the argument for the function (types should match). + * + * You should not alter the set within the @handle function! If it returns + * false, the iteration will stop. + * + * Example: + * static bool dump_some(const char *member, int *num) + * { + * // Only dump out num nodes. + * if (*(num--) == 0) + * return false; + * printf("%s\n", member); + * return true; + * } + * + * static void dump_set(const struct strset *set) + * { + * int max = 100; + * strset_iterate(set, dump_some, &max); + * if (max < 0) + * printf("... (truncated to 100 entries)\n"); + * } + */ +#define strset_iterate(set, handle, arg) \ + strset_iterate_((set), typesafe_cb_preargs(bool, void *, \ + (handle), (arg), \ + const char *), \ + (arg)) +void strset_iterate_(const struct strset *set, + bool (*handle)(const char *, void *), const void *data); + + +/** + * strset_prefix - return a subset matching a prefix + * @set: the set. + * @prefix: the prefix. + * + * This returns a pointer into @set, so don't alter @set while using + * the return value. You can use strset_iterate(), strset_test() or + * strset_empty() on the returned pointer. + * + * Example: + * static void dump_prefix(const struct strset *set, const char *prefix) + * { + * int max = 100; + * printf("Nodes with prefix %s:\n", prefix); + * strset_iterate(strset_prefix(set, prefix), dump_some, &max); + * if (max < 0) + * printf("... (truncated to 100 entries)\n"); + * } + */ +const struct strset *strset_prefix(const struct strset *set, + const char *prefix); + +#endif /* CCAN_STRSET_H */ diff --git a/ccan/ccan/typesafe_cb/LICENSE b/ccan/ccan/typesafe_cb/LICENSE new file mode 120000 index 0000000..b7951da --- /dev/null +++ b/ccan/ccan/typesafe_cb/LICENSE @@ -0,0 +1 @@ +../../licenses/CC0 \ No newline at end of file diff --git a/ccan/ccan/typesafe_cb/typesafe_cb.h b/ccan/ccan/typesafe_cb/typesafe_cb.h new file mode 100644 index 0000000..126d325 --- /dev/null +++ b/ccan/ccan/typesafe_cb/typesafe_cb.h @@ -0,0 +1,134 @@ +/* CC0 (Public domain) - see LICENSE file for details */ +#ifndef CCAN_TYPESAFE_CB_H +#define CCAN_TYPESAFE_CB_H +#include "config.h" + +#if HAVE_TYPEOF && HAVE_BUILTIN_CHOOSE_EXPR && HAVE_BUILTIN_TYPES_COMPATIBLE_P +/** + * typesafe_cb_cast - only cast an expression if it matches a given type + * @desttype: the type to cast to + * @oktype: the type we allow + * @expr: the expression to cast + * + * This macro is used to create functions which allow multiple types. + * The result of this macro is used somewhere that a @desttype type is + * expected: if @expr is exactly of type @oktype, then it will be + * cast to @desttype type, otherwise left alone. + * + * This macro can be used in static initializers. + * + * This is merely useful for warnings: if the compiler does not + * support the primitives required for typesafe_cb_cast(), it becomes an + * unconditional cast, and the @oktype argument is not used. In + * particular, this means that @oktype can be a type which uses the + * "typeof": it will not be evaluated if typeof is not supported. + * + * Example: + * // We can take either an unsigned long or a void *. + * void _set_some_value(void *val); + * #define set_some_value(e) \ + * _set_some_value(typesafe_cb_cast(void *, unsigned long, (e))) + */ +#define typesafe_cb_cast(desttype, oktype, expr) \ + __builtin_choose_expr( \ + __builtin_types_compatible_p(__typeof__(0?(expr):(expr)), \ + oktype), \ + (desttype)(expr), (expr)) +#else +#define typesafe_cb_cast(desttype, oktype, expr) ((desttype)(expr)) +#endif + +/** + * typesafe_cb_cast3 - only cast an expression if it matches given types + * @desttype: the type to cast to + * @ok1: the first type we allow + * @ok2: the second type we allow + * @ok3: the third type we allow + * @expr: the expression to cast + * + * This is a convenient wrapper for multiple typesafe_cb_cast() calls. + * You can chain them inside each other (ie. use typesafe_cb_cast() + * for expr) if you need more than 3 arguments. + * + * Example: + * // We can take either a long, unsigned long, void * or a const void *. + * void _set_some_value(void *val); + * #define set_some_value(expr) \ + * _set_some_value(typesafe_cb_cast3(void *,, \ + * long, unsigned long, const void *,\ + * (expr))) + */ +#define typesafe_cb_cast3(desttype, ok1, ok2, ok3, expr) \ + typesafe_cb_cast(desttype, ok1, \ + typesafe_cb_cast(desttype, ok2, \ + typesafe_cb_cast(desttype, ok3, \ + (expr)))) + +/** + * typesafe_cb - cast a callback function if it matches the arg + * @rtype: the return type of the callback function + * @atype: the (pointer) type which the callback function expects. + * @fn: the callback function to cast + * @arg: the (pointer) argument to hand to the callback function. + * + * If a callback function takes a single argument, this macro does + * appropriate casts to a function which takes a single atype argument if the + * callback provided matches the @arg. + * + * It is assumed that @arg is of pointer type: usually @arg is passed + * or assigned to a void * elsewhere anyway. + * + * Example: + * void _register_callback(void (*fn)(void *arg), void *arg); + * #define register_callback(fn, arg) \ + * _register_callback(typesafe_cb(void, (fn), void*, (arg)), (arg)) + */ +#define typesafe_cb(rtype, atype, fn, arg) \ + typesafe_cb_cast(rtype (*)(atype), \ + rtype (*)(__typeof__(arg)), \ + (fn)) + +/** + * typesafe_cb_preargs - cast a callback function if it matches the arg + * @rtype: the return type of the callback function + * @atype: the (pointer) type which the callback function expects. + * @fn: the callback function to cast + * @arg: the (pointer) argument to hand to the callback function. + * + * This is a version of typesafe_cb() for callbacks that take other arguments + * before the @arg. + * + * Example: + * void _register_callback(void (*fn)(int, void *arg), void *arg); + * #define register_callback(fn, arg) \ + * _register_callback(typesafe_cb_preargs(void, void *, \ + * (fn), (arg), int), \ + * (arg)) + */ +#define typesafe_cb_preargs(rtype, atype, fn, arg, ...) \ + typesafe_cb_cast(rtype (*)(__VA_ARGS__, atype), \ + rtype (*)(__VA_ARGS__, __typeof__(arg)), \ + (fn)) + +/** + * typesafe_cb_postargs - cast a callback function if it matches the arg + * @rtype: the return type of the callback function + * @atype: the (pointer) type which the callback function expects. + * @fn: the callback function to cast + * @arg: the (pointer) argument to hand to the callback function. + * + * This is a version of typesafe_cb() for callbacks that take other arguments + * after the @arg. + * + * Example: + * void _register_callback(void (*fn)(void *arg, int), void *arg); + * #define register_callback(fn, arg) \ + * _register_callback(typesafe_cb_postargs(void, (fn), void *, \ + * (arg), int), \ + * (arg)) + */ +#define typesafe_cb_postargs(rtype, atype, fn, arg, ...) \ + typesafe_cb_cast(rtype (*)(atype, __VA_ARGS__), \ + rtype (*)(__typeof__(arg), __VA_ARGS__), \ + (fn)) +#endif /* CCAN_CAST_IF_TYPE_H */ diff --git a/ccan/licenses/LGPL-2.1 b/ccan/licenses/LGPL-2.1 new file mode 100644 index 0000000..2d2d780 --- /dev/null +++ b/ccan/licenses/LGPL-2.1 @@ -0,0 +1,510 @@ + + GNU LESSER GENERAL PUBLIC LICENSE + Version 2.1, February 1999 + + Copyright (C) 1991, 1999 Free Software Foundation, Inc. + 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + +[This is the first released version of the Lesser GPL. It also counts + as the successor of the GNU Library Public License, version 2, hence + the version number 2.1.] + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +Licenses are intended to guarantee your freedom to share and change +free software--to make sure the software is free for all its users. + + This license, the Lesser General Public License, applies to some +specially designated software packages--typically libraries--of the +Free Software Foundation and other authors who decide to use it. You +can use it too, but we suggest you first think carefully about whether +this license or the ordinary General Public License is the better +strategy to use in any particular case, based on the explanations +below. + + When we speak of free software, we are referring to freedom of use, +not price. Our General Public Licenses are designed to make sure that +you have the freedom to distribute copies of free software (and charge +for this service if you wish); that you receive source code or can get +it if you want it; that you can change the software and use pieces of +it in new free programs; and that you are informed that you can do +these things. + + To protect your rights, we need to make restrictions that forbid +distributors to deny you these rights or to ask you to surrender these +rights. These restrictions translate to certain responsibilities for +you if you distribute copies of the library or if you modify it. + + For example, if you distribute copies of the library, whether gratis +or for a fee, you must give the recipients all the rights that we gave +you. You must make sure that they, too, receive or can get the source +code. If you link other code with the library, you must provide +complete object files to the recipients, so that they can relink them +with the library after making changes to the library and recompiling +it. And you must show them these terms so they know their rights. + + We protect your rights with a two-step method: (1) we copyright the +library, and (2) we offer you this license, which gives you legal +permission to copy, distribute and/or modify the library. + + To protect each distributor, we want to make it very clear that +there is no warranty for the free library. Also, if the library is +modified by someone else and passed on, the recipients should know +that what they have is not the original version, so that the original +author's reputation will not be affected by problems that might be +introduced by others. + + Finally, software patents pose a constant threat to the existence of +any free program. We wish to make sure that a company cannot +effectively restrict the users of a free program by obtaining a +restrictive license from a patent holder. Therefore, we insist that +any patent license obtained for a version of the library must be +consistent with the full freedom of use specified in this license. + + Most GNU software, including some libraries, is covered by the +ordinary GNU General Public License. This license, the GNU Lesser +General Public License, applies to certain designated libraries, and +is quite different from the ordinary General Public License. We use +this license for certain libraries in order to permit linking those +libraries into non-free programs. + + When a program is linked with a library, whether statically or using +a shared library, the combination of the two is legally speaking a +combined work, a derivative of the original library. The ordinary +General Public License therefore permits such linking only if the +entire combination fits its criteria of freedom. The Lesser General +Public License permits more lax criteria for linking other code with +the library. + + We call this license the "Lesser" General Public License because it +does Less to protect the user's freedom than the ordinary General +Public License. It also provides other free software developers Less +of an advantage over competing non-free programs. These disadvantages +are the reason we use the ordinary General Public License for many +libraries. However, the Lesser license provides advantages in certain +special circumstances. + + For example, on rare occasions, there may be a special need to +encourage the widest possible use of a certain library, so that it +becomes a de-facto standard. To achieve this, non-free programs must +be allowed to use the library. A more frequent case is that a free +library does the same job as widely used non-free libraries. In this +case, there is little to gain by limiting the free library to free +software only, so we use the Lesser General Public License. + + In other cases, permission to use a particular library in non-free +programs enables a greater number of people to use a large body of +free software. For example, permission to use the GNU C Library in +non-free programs enables many more people to use the whole GNU +operating system, as well as its variant, the GNU/Linux operating +system. + + Although the Lesser General Public License is Less protective of the +users' freedom, it does ensure that the user of a program that is +linked with the Library has the freedom and the wherewithal to run +that program using a modified version of the Library. + + The precise terms and conditions for copying, distribution and +modification follow. Pay close attention to the difference between a +"work based on the library" and a "work that uses the library". The +former contains code derived from the library, whereas the latter must +be combined with the library in order to run. + + GNU LESSER GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License Agreement applies to any software library or other +program which contains a notice placed by the copyright holder or +other authorized party saying it may be distributed under the terms of +this Lesser General Public License (also called "this License"). +Each licensee is addressed as "you". + + A "library" means a collection of software functions and/or data +prepared so as to be conveniently linked with application programs +(which use some of those functions and data) to form executables. + + The "Library", below, refers to any such software library or work +which has been distributed under these terms. A "work based on the +Library" means either the Library or any derivative work under +copyright law: that is to say, a work containing the Library or a +portion of it, either verbatim or with modifications and/or translated +straightforwardly into another language. (Hereinafter, translation is +included without limitation in the term "modification".) + + "Source code" for a work means the preferred form of the work for +making modifications to it. For a library, complete source code means +all the source code for all modules it contains, plus any associated +interface definition files, plus the scripts used to control +compilation and installation of the library. + + Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running a program using the Library is not restricted, and output from +such a program is covered only if its contents constitute a work based +on the Library (independent of the use of the Library in a tool for +writing it). Whether that is true depends on what the Library does +and what the program that uses the Library does. + + 1. You may copy and distribute verbatim copies of the Library's +complete source code as you receive it, in any medium, provided that +you conspicuously and appropriately publish on each copy an +appropriate copyright notice and disclaimer of warranty; keep intact +all the notices that refer to this License and to the absence of any +warranty; and distribute a copy of this License along with the +Library. + + You may charge a fee for the physical act of transferring a copy, +and you may at your option offer warranty protection in exchange for a +fee. + + 2. You may modify your copy or copies of the Library or any portion +of it, thus forming a work based on the Library, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) The modified work must itself be a software library. + + b) You must cause the files modified to carry prominent notices + stating that you changed the files and the date of any change. + + c) You must cause the whole of the work to be licensed at no + charge to all third parties under the terms of this License. + + d) If a facility in the modified Library refers to a function or a + table of data to be supplied by an application program that uses + the facility, other than as an argument passed when the facility + is invoked, then you must make a good faith effort to ensure that, + in the event an application does not supply such function or + table, the facility still operates, and performs whatever part of + its purpose remains meaningful. + + (For example, a function in a library to compute square roots has + a purpose that is entirely well-defined independent of the + application. Therefore, Subsection 2d requires that any + application-supplied function or table used by this function must + be optional: if the application does not supply it, the square + root function must still compute square roots.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Library, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Library, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote +it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Library. + +In addition, mere aggregation of another work not based on the Library +with the Library (or with a work based on the Library) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may opt to apply the terms of the ordinary GNU General Public +License instead of this License to a given copy of the Library. To do +this, you must alter all the notices that refer to this License, so +that they refer to the ordinary GNU General Public License, version 2, +instead of to this License. (If a newer version than version 2 of the +ordinary GNU General Public License has appeared, then you can specify +that version instead if you wish.) Do not make any other change in +these notices. + + Once this change is made in a given copy, it is irreversible for +that copy, so the ordinary GNU General Public License applies to all +subsequent copies and derivative works made from that copy. + + This option is useful when you wish to copy part of the code of +the Library into a program that is not a library. + + 4. You may copy and distribute the Library (or a portion or +derivative of it, under Section 2) in object code or executable form +under the terms of Sections 1 and 2 above provided that you accompany +it with the complete corresponding machine-readable source code, which +must be distributed under the terms of Sections 1 and 2 above on a +medium customarily used for software interchange. + + If distribution of object code is made by offering access to copy +from a designated place, then offering equivalent access to copy the +source code from the same place satisfies the requirement to +distribute the source code, even though third parties are not +compelled to copy the source along with the object code. + + 5. A program that contains no derivative of any portion of the +Library, but is designed to work with the Library by being compiled or +linked with it, is called a "work that uses the Library". Such a +work, in isolation, is not a derivative work of the Library, and +therefore falls outside the scope of this License. + + However, linking a "work that uses the Library" with the Library +creates an executable that is a derivative of the Library (because it +contains portions of the Library), rather than a "work that uses the +library". The executable is therefore covered by this License. +Section 6 states terms for distribution of such executables. + + When a "work that uses the Library" uses material from a header file +that is part of the Library, the object code for the work may be a +derivative work of the Library even though the source code is not. +Whether this is true is especially significant if the work can be +linked without the Library, or if the work is itself a library. The +threshold for this to be true is not precisely defined by law. + + If such an object file uses only numerical parameters, data +structure layouts and accessors, and small macros and small inline +functions (ten lines or less in length), then the use of the object +file is unrestricted, regardless of whether it is legally a derivative +work. (Executables containing this object code plus portions of the +Library will still fall under Section 6.) + + Otherwise, if the work is a derivative of the Library, you may +distribute the object code for the work under the terms of Section 6. +Any executables containing that work also fall under Section 6, +whether or not they are linked directly with the Library itself. + + 6. As an exception to the Sections above, you may also combine or +link a "work that uses the Library" with the Library to produce a +work containing portions of the Library, and distribute that work +under terms of your choice, provided that the terms permit +modification of the work for the customer's own use and reverse +engineering for debugging such modifications. + + You must give prominent notice with each copy of the work that the +Library is used in it and that the Library and its use are covered by +this License. You must supply a copy of this License. If the work +during execution displays copyright notices, you must include the +copyright notice for the Library among them, as well as a reference +directing the user to the copy of this License. Also, you must do one +of these things: + + a) Accompany the work with the complete corresponding + machine-readable source code for the Library including whatever + changes were used in the work (which must be distributed under + Sections 1 and 2 above); and, if the work is an executable linked + with the Library, with the complete machine-readable "work that + uses the Library", as object code and/or source code, so that the + user can modify the Library and then relink to produce a modified + executable containing the modified Library. (It is understood + that the user who changes the contents of definitions files in the + Library will not necessarily be able to recompile the application + to use the modified definitions.) + + b) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (1) uses at run time a + copy of the library already present on the user's computer system, + rather than copying library functions into the executable, and (2) + will operate properly with a modified version of the library, if + the user installs one, as long as the modified version is + interface-compatible with the version that the work was made with. + + c) Accompany the work with a written offer, valid for at least + three years, to give the same user the materials specified in + Subsection 6a, above, for a charge no more than the cost of + performing this distribution. + + d) If distribution of the work is made by offering access to copy + from a designated place, offer equivalent access to copy the above + specified materials from the same place. + + e) Verify that the user has already received a copy of these + materials or that you have already sent this user a copy. + + For an executable, the required form of the "work that uses the +Library" must include any data and utility programs needed for +reproducing the executable from it. However, as a special exception, +the materials to be distributed need not include anything that is +normally distributed (in either source or binary form) with the major +components (compiler, kernel, and so on) of the operating system on +which the executable runs, unless that component itself accompanies +the executable. + + It may happen that this requirement contradicts the license +restrictions of other proprietary libraries that do not normally +accompany the operating system. Such a contradiction means you cannot +use both them and the Library together in an executable that you +distribute. + + 7. You may place library facilities that are a work based on the +Library side-by-side in a single library together with other library +facilities not covered by this License, and distribute such a combined +library, provided that the separate distribution of the work based on +the Library and of the other library facilities is otherwise +permitted, and provided that you do these two things: + + a) Accompany the combined library with a copy of the same work + based on the Library, uncombined with any other library + facilities. This must be distributed under the terms of the + Sections above. + + b) Give prominent notice with the combined library of the fact + that part of it is a work based on the Library, and explaining + where to find the accompanying uncombined form of the same work. + + 8. You may not copy, modify, sublicense, link with, or distribute +the Library except as expressly provided under this License. Any +attempt otherwise to copy, modify, sublicense, link with, or +distribute the Library is void, and will automatically terminate your +rights under this License. However, parties who have received copies, +or rights, from you under this License will not have their licenses +terminated so long as such parties remain in full compliance. + + 9. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Library or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Library (or any work based on the +Library), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Library or works based on it. + + 10. Each time you redistribute the Library (or any work based on the +Library), the recipient automatically receives a license from the +original licensor to copy, distribute, link with or modify the Library +subject to these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties with +this License. + + 11. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Library at all. For example, if a patent +license would not permit royalty-free redistribution of the Library by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Library. + +If any portion of this section is held invalid or unenforceable under +any particular circumstance, the balance of the section is intended to +apply, and the section as a whole is intended to apply in other +circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 12. If the distribution and/or use of the Library is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Library under this License +may add an explicit geographical distribution limitation excluding those +countries, so that distribution is permitted only in or among +countries not thus excluded. In such case, this License incorporates +the limitation as if written in the body of this License. + + 13. The Free Software Foundation may publish revised and/or new +versions of the Lesser General Public License from time to time. +Such new versions will be similar in spirit to the present version, +but may differ in detail to address new problems or concerns. + +Each version is given a distinguishing version number. If the Library +specifies a version number of this License which applies to it and +"any later version", you have the option of following the terms and +conditions either of that version or of any later version published by +the Free Software Foundation. If the Library does not specify a +license version number, you may choose any version ever published by +the Free Software Foundation. + + 14. If you wish to incorporate parts of the Library into other free +programs whose distribution conditions are incompatible with these, +write to the author to ask for permission. For software which is +copyrighted by the Free Software Foundation, write to the Free +Software Foundation; we sometimes make exceptions for this. Our +decision will be guided by the two goals of preserving the free status +of all derivatives of our free software and of promoting the sharing +and reuse of software generally. + + NO WARRANTY + + 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO +WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. +EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR +OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY +KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE +LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME +THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN +WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY +AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU +FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR +CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE +LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING +RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A +FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF +SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH +DAMAGES. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Libraries + + If you develop a new library, and you want it to be of the greatest +possible use to the public, we recommend making it free software that +everyone can redistribute and change. You can do so by permitting +redistribution under these terms (or, alternatively, under the terms +of the ordinary General Public License). + + To apply these terms, attach the following notices to the library. +It is safest to attach them to the start of each source file to most +effectively convey the exclusion of warranty; and each file should +have at least the "copyright" line and a pointer to where the full +notice is found. + + + + Copyright (C) + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +Also add information on how to contact you by electronic and paper mail. + +You should also get your employer (if you work as a programmer) or +your school, if any, to sign a "copyright disclaimer" for the library, +if necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the + library `Frob' (a library for tweaking knobs) written by James + Random Hacker. + + , 1 April 1990 + Ty Coon, President of Vice + +That's all there is to it! + + diff --git a/ccan/meson.build b/ccan/meson.build index 4ba3b5f..35d2b88 100644 --- a/ccan/meson.build +++ b/ccan/meson.build @@ -1,9 +1,14 @@ # SPDX-License-Identifier: GPL-2.0-or-later sources += files([ + 'ccan/hash/hash.c', + 'ccan/htable/htable.c', + 'ccan/ilog/ilog.c', + 'ccan/likely/likely.c', 'ccan/list/list.c', 'ccan/str/debug.c', 'ccan/str/str.c', + 'ccan/strset/strset.c', ]) if get_option('buildtype') == 'debug' diff --git a/completions/_nvme b/completions/_nvme index 49736f5..5d07bc5 100644 --- a/completions/_nvme +++ b/completions/_nvme @@ -217,15 +217,15 @@ _nvme () { _arguments '*:: :->subcmds' _describe -t commands "nvme ocp clear-pcie-correctable-error-counters options" _clear_pcie_correctable_error_counters ;; - (vs-fw-activate-history) - local _vs_fw_activate_history - _vs_fw_activate_history=( + (fw-activate-history) + local _fw_activate_history + _fw_activate_history=( /dev/nvme':supply a device to use (required)' --output-format=':Output format: normal|json' -o':alias for --output-format' ) _arguments '*:: :->subcmds' - _describe -t commands "nvme ocp vs-fw-activate-history options" _vs_fw_activate_history + _describe -t commands "nvme ocp fw-activate-history options" _fw_activate_history ;; (device-capability-log) local _device_capability_log @@ -237,6 +237,20 @@ _nvme () { _arguments '*:: :->subcmds' _describe -t commands "nvme ocp device-capability-log options" _device_capability_log ;; + (set-dssd-power-state-feature) + local _set_dssd_power_state_feature + _set_dssd_power_state_feature=( + /dev/nvme':supply a device to use (required)' + --power-state=':DSSD Power State to set in watts' + -p':alias for --power-state' + --save':Specifies that the controller shall save the attribute' + -s':alias for --save' + --no-uuid':Skip UUID index search' + -n':alias for --no-uuid' + ) + _arguments '*:: :->subcmds' + _describe -t commands "nvme ocp set-dssd-power-state-feature options" _set_dssd_power_state_feature + ;; (*) _files ;; @@ -525,6 +539,8 @@ _nvme () { -a':alias of --anagrp-id' --nvmset-id=':NVM Set Identifier' -i':alias of --nvmset-id' + --endg-id=':Endurance Group Identifier' + -e':alias of --endg-id' --block-size=':target block size' -b':alias of --block-size' --timeout=':value for timeout' @@ -1991,6 +2007,7 @@ _nvme () { clear-pcie-correctable-error-counters':Clear PCIe correctable error counters' vs-fw-activate-history':Get firmware activation history log' device-capability-log':Get Device capability log' + set-dssd-power-state-feature':Set DSSD Power State' ) _arguments '*:: :->subcmds' _describe -t commands "nvme ocp options" _ocp diff --git a/completions/bash-nvme-completion.sh b/completions/bash-nvme-completion.sh index 8f451ff..fc3b49e 100644 --- a/completions/bash-nvme-completion.sh +++ b/completions/bash-nvme-completion.sh @@ -114,7 +114,7 @@ nvme_list_opts () { --dps= -d --nmic= -m --anagrp-id= -a --nvmset-id= -i \ --block-size= -b --timeout= -t --csi= -y --lbstm= -l \ --nphndls= -n --nsze-si= -S --ncap-si= -C --azr -z --rar= -r \ - --ror= -o --rnumzrwa= -u --phndls= -p" + --ror= -o --rnumzrwa= -u --phndls= -p --endg-id= -e" ;; "delete-ns") opts+=" -namespace-id= -n --timeout= -t" @@ -1342,12 +1342,15 @@ plugin_ocp_opts () { "clear-pcie-correctable-error-counters") opts+=" --no-uuid -n" ;; - "vs-fw-activate-history") + "fw-activate-history") opts+=" --output-format= -o" ;; "device-capability-log") opts+=" --output-format= -o" ;; + "set-dssd-power-state-feature") + opts+=" --power-state= -p --no-uuid -n --save -s" + ;; "help") opts+=$NO_OPTS ;; @@ -1416,7 +1419,8 @@ _nvme_subcmds () { set-latency-monitor-feature internal-log \ clear-fw-activate-history eol-plp-failure-mode \ clear-pcie-correctable-error-counters \ - vs-fw-activate-history device-capability-log" + vs-fw-activate-history device-capability-log \ + set-dssd-power-state-feature" ) # Associative array mapping plugins to coresponding option completions @@ -1473,7 +1477,7 @@ _nvme_subcmds () { _cmds+=" $plugin" done - cmds+=" version help" + _cmds+=" version help" if [[ ${#words[*]} -lt 3 ]]; then COMPREPLY+=( $(compgen -W "$_cmds" -- $cur ) ) diff --git a/fabrics.c b/fabrics.c index ac240ca..57ca927 100644 --- a/fabrics.c +++ b/fabrics.c @@ -88,7 +88,7 @@ static const char *nvmf_config_file = "Use specified JSON configuration file or static const char *nvmf_context = "execution context identification string"; #define NVMF_ARGS(n, c, ...) \ - struct argconfig_commandline_options opts[] = { \ + struct argconfig_commandline_options n[] = { \ OPT_STRING("transport", 't', "STR", &transport, nvmf_tport), \ OPT_STRING("nqn", 'n', "STR", &subsysnqn, nvmf_nqn), \ OPT_STRING("traddr", 'a', "STR", &traddr, nvmf_traddr), \ @@ -117,30 +117,6 @@ static const char *nvmf_context = "execution context identification string"; OPT_END() \ } -/* - * Compare two C strings and handle NULL pointers gracefully. - * If either of the two strings is NULL, return 0 - * to let caller ignore the compare. - */ -static inline int strcmp0(const char *s1, const char *s2) -{ - if (!s1 || !s2) - return 0; - return strcmp(s1, s2); -} - -/* - * Compare two C strings and handle NULL pointers gracefully. - * If either of the two strings is NULL, return 0 - * to let caller ignore the compare. - */ -static inline int strcasecmp0(const char *s1, const char *s2) -{ - if (!s1 || !s2) - return 0; - return strcasecmp(s1, s2); -} - static bool is_persistent_discovery_ctrl(nvme_host_t h, nvme_ctrl_t c) { if (nvme_host_is_pdc_enabled(h, DEFAULT_PDC_ENABLED)) @@ -149,62 +125,26 @@ static bool is_persistent_discovery_ctrl(nvme_host_t h, nvme_ctrl_t c) return false; } -static bool disc_ctrl_config_match(nvme_ctrl_t c, struct tr_config *trcfg) -{ - if (nvme_ctrl_is_discovery_ctrl(c) && - !strcmp0(nvme_ctrl_get_transport(c), trcfg->transport) && - !strcasecmp0(nvme_ctrl_get_traddr(c), trcfg->traddr) && - !strcmp0(nvme_ctrl_get_trsvcid(c), trcfg->trsvcid) && - !strcmp0(nvme_ctrl_get_host_traddr(c), trcfg->host_traddr) && - !strcmp0(nvme_ctrl_get_host_iface(c), trcfg->host_iface)) - return true; - - return false; -} - -static bool ctrl_config_match(nvme_ctrl_t c, struct tr_config *trcfg) +nvme_ctrl_t lookup_ctrl(nvme_host_t h, struct tr_config *trcfg) { - if (!strcmp0(nvme_ctrl_get_subsysnqn(c), trcfg->subsysnqn) && - !strcmp0(nvme_ctrl_get_transport(c), trcfg->transport) && - !strcasecmp0(nvme_ctrl_get_traddr(c), trcfg->traddr) && - !strcmp0(nvme_ctrl_get_trsvcid(c), trcfg->trsvcid) && - !strcmp0(nvme_ctrl_get_host_traddr(c), trcfg->host_traddr) && - !strcmp0(nvme_ctrl_get_host_iface(c), trcfg->host_iface)) - return true; - - return false; -} - -static nvme_ctrl_t __lookup_ctrl(nvme_root_t r, struct tr_config *trcfg, - bool (*filter)(nvme_ctrl_t, struct tr_config *)) -{ - nvme_host_t h; nvme_subsystem_t s; nvme_ctrl_t c; - nvme_for_each_host(r, h) { - nvme_for_each_subsystem(h, s) { - nvme_subsystem_for_each_ctrl(s, c) { - if (!(filter(c, trcfg))) - continue; - return c; - } - } + nvme_for_each_subsystem(h, s) { + c = nvme_ctrl_find(s, + trcfg->transport, + trcfg->traddr, + trcfg->trsvcid, + trcfg->subsysnqn, + trcfg->host_traddr, + trcfg->host_iface); + if (c) + return c; } return NULL; } -static nvme_ctrl_t lookup_discovery_ctrl(nvme_root_t r, struct tr_config *trcfg) -{ - return __lookup_ctrl(r, trcfg, disc_ctrl_config_match); -} - -nvme_ctrl_t lookup_ctrl(nvme_root_t r, struct tr_config *trcfg) -{ - return __lookup_ctrl(r, trcfg, ctrl_config_match); -} - static int set_discovery_kato(struct nvme_fabrics_config *cfg) { int tmo = cfg->keep_alive_tmo; @@ -317,7 +257,6 @@ static int __discover(nvme_ctrl_t c, struct nvme_fabrics_config *defcfg, struct nvmf_discovery_log *log = NULL; nvme_subsystem_t s = nvme_ctrl_get_subsystem(c); nvme_host_t h = nvme_subsystem_get_host(s); - nvme_root_t r = nvme_host_get_root(h); uint64_t numrec; struct nvme_get_discovery_args args = { @@ -362,7 +301,7 @@ static int __discover(nvme_ctrl_t c, struct nvme_fabrics_config *defcfg, }; /* Already connected ? */ - cl = lookup_ctrl(r, &trcfg); + cl = lookup_ctrl(h, &trcfg); if (cl && nvme_ctrl_get_name(cl)) continue; @@ -527,7 +466,7 @@ static int discover_from_conf_file(nvme_root_t r, nvme_host_t h, }; if (!force) { - c = lookup_discovery_ctrl(r, &trcfg); + c = lookup_ctrl(h, &trcfg); if (c) { __discover(c, &cfg, raw, connect, true, flags); @@ -621,7 +560,7 @@ static int discover_from_json_config_file(nvme_root_t r, nvme_host_t h, }; if (!force) { - cn = lookup_discovery_ctrl(r, &trcfg); + cn = lookup_ctrl(h, &trcfg); if (cn) { __discover(cn, &cfg, raw, connect, true, flags); @@ -677,6 +616,43 @@ static int nvme_read_volatile_config(nvme_root_t r) return ret; } +char *nvmf_hostid_from_hostnqn(const char *hostnqn) +{ + const char *uuid; + + if (!hostnqn) + return NULL; + + uuid = strstr(hostnqn, "uuid:"); + if (!uuid) + return NULL; + + return strdup(uuid + strlen("uuid:")); +} + +void nvmf_check_hostid_and_hostnqn(const char *hostid, const char *hostnqn) +{ + char *hostid_from_file, *hostid_from_hostnqn; + + if (!hostid) + return; + + hostid_from_file = nvmf_hostid_from_file(); + if (hostid_from_file && strcmp(hostid_from_file, hostid)) { + fprintf(stderr, "warning: use generated hostid instead of hostid file\n"); + free(hostid_from_file); + } + + if (!hostnqn) + return; + + hostid_from_hostnqn = nvmf_hostid_from_hostnqn(hostnqn); + if (hostid_from_hostnqn && strcmp(hostid_from_hostnqn, hostid)) { + fprintf(stderr, "warning: use hostid which does not match uuid in hostnqn\n"); + free(hostid_from_hostnqn); + } +} + int nvmf_discover(const char *desc, int argc, char **argv, bool connect) { char *subsysnqn = NVME_DISC_SUBSYS_NAME; @@ -753,10 +729,13 @@ int nvmf_discover(const char *desc, int argc, char **argv, bool connect) hostid_arg = hostid; if (!hostnqn) hostnqn = hnqn = nvmf_hostnqn_from_file(); - if (!hostnqn) + if (!hostnqn) { hostnqn = hnqn = nvmf_hostnqn_generate(); + hostid = hid = nvmf_hostid_from_hostnqn(hostnqn); + } if (!hostid) hostid = hid = nvmf_hostid_from_file(); + nvmf_check_hostid_and_hostnqn(hostid, hostnqn); h = nvme_lookup_host(r, hostnqn, hostid); if (!h) { ret = ENOMEM; @@ -807,7 +786,8 @@ int nvmf_discover(const char *desc, int argc, char **argv, bool connect) c = nvme_scan_ctrl(r, device); if (c) { /* Check if device matches command-line options */ - if (!ctrl_config_match(c, &trcfg)) { + if (!nvme_ctrl_config_match(c, transport, traddr, trsvcid, subsysnqn, + cfg.host_traddr, cfg.host_iface)) { fprintf(stderr, "ctrl device %s found, ignoring non matching command-line options\n", device); @@ -855,7 +835,7 @@ int nvmf_discover(const char *desc, int argc, char **argv, bool connect) } } if (!c && !force) { - c = lookup_discovery_ctrl(r, &trcfg); + c = lookup_ctrl(h, &trcfg); if (c) persistent = true; } @@ -966,10 +946,13 @@ int nvmf_connect(const char *desc, int argc, char **argv) if (!hostnqn) hostnqn = hnqn = nvmf_hostnqn_from_file(); - if (!hostnqn) + if (!hostnqn) { hostnqn = hnqn = nvmf_hostnqn_generate(); + hostid = hid = nvmf_hostid_from_hostnqn(hostnqn); + } if (!hostid) hostid = hid = nvmf_hostid_from_file(); + nvmf_check_hostid_and_hostnqn(hostid, hostnqn); h = nvme_lookup_host(r, hostnqn, hostid); if (!h) { errno = ENOMEM; @@ -989,7 +972,7 @@ int nvmf_connect(const char *desc, int argc, char **argv) .trsvcid = trsvcid, }; - c = lookup_ctrl(r, &trcfg); + c = lookup_ctrl(h, &trcfg); if (c && nvme_ctrl_get_name(c)) { fprintf(stderr, "already connected\n"); errno = EALREADY; diff --git a/fabrics.h b/fabrics.h index 02cebf5..c16df60 100644 --- a/fabrics.h +++ b/fabrics.h @@ -11,7 +11,7 @@ struct tr_config { const char *trsvcid; }; -extern nvme_ctrl_t lookup_ctrl(nvme_root_t r, struct tr_config *trcfg); +extern nvme_ctrl_t lookup_ctrl(nvme_host_t h, struct tr_config *trcfg); extern int nvmf_discover(const char *desc, int argc, char **argv, bool connect); extern int nvmf_connect(const char *desc, int argc, char **argv); extern int nvmf_disconnect(const char *desc, int argc, char **argv); diff --git a/meson.build b/meson.build index af79bd4..3d3fb08 100644 --- a/meson.build +++ b/meson.build @@ -4,7 +4,7 @@ project( 'nvme-cli', ['c'], meson_version: '>= 0.50.0', license: 'GPL-2.0-only', - version: '2.5', + version: '2.6', default_options: [ 'c_std=gnu99', 'buildtype=debug', @@ -48,7 +48,7 @@ conf.set('SYSCONFDIR', '"@0@"'.format(sysconfdir)) conf.set('RUNDIR', '"@0@"'.format(rundir)) # Check for libnvme availability -libnvme_dep = dependency('libnvme', version: '>=1.5', required: true, +libnvme_dep = dependency('libnvme', version: '>=1.6', required: true, fallback : ['libnvme', 'libnvme_dep']) libnvme_mi_dep = dependency('libnvme-mi', required: true, fallback : ['libnvme', 'libnvme_mi_dep']) @@ -157,6 +157,11 @@ conf.set10( ), description: 'Is sys/random.h(getrandom) include-able?' ) +conf.set10( + 'HAVE_ATTRIBUTE_UNUSED', + cc.get_id() == 'clang', + description: 'Is compiler warning about unused static line function?' +) if cc.has_function_attribute('fallthrough') conf.set('fallthrough', '__attribute__((__fallthrough__))') diff --git a/nbft.c b/nbft.c index c73413f..e46e9d3 100644 --- a/nbft.c +++ b/nbft.c @@ -144,7 +144,7 @@ int discover_from_nbft(nvme_root_t r, char *hostnqn_arg, char *hostid_arg, }; /* Already connected ? */ - cl = lookup_ctrl(r, &trcfg); + cl = lookup_ctrl(h, &trcfg); if (cl && nvme_ctrl_get_name(cl)) continue; @@ -170,7 +170,7 @@ int discover_from_nbft(nvme_root_t r, char *hostnqn_arg, char *hostid_arg, nvme_free_ctrl(c); trcfg.host_traddr = NULL; - cl = lookup_ctrl(r, &trcfg); + cl = lookup_ctrl(h, &trcfg); if (cl && nvme_ctrl_get_name(cl)) continue; diff --git a/nvme-builtin.h b/nvme-builtin.h index 784e19d..9b9a145 100644 --- a/nvme-builtin.h +++ b/nvme-builtin.h @@ -49,6 +49,7 @@ COMMAND_LIST( ENTRY("lba-status-log", "Retrieve LBA Status Information Log, show it", get_lba_status_log) ENTRY("resv-notif-log", "Retrieve Reservation Notification Log, show it", get_resv_notif_log) ENTRY("boot-part-log", "Retrieve Boot Partition Log, show it", get_boot_part_log) + ENTRY("phy-rx-eom-log", "Retrieve Physical Interface Receiver Eye Opening Measurement, show it", get_phy_rx_eom_log) ENTRY("get-feature", "Get feature and show the resulting value", get_feature) ENTRY("device-self-test", "Perform the necessary tests to observe the performance", device_self_test) ENTRY("self-test-log", "Retrieve the SELF-TEST Log, show it", self_test_log) diff --git a/nvme-print-binary.c b/nvme-print-binary.c index 616d731..45d86d3 100644 --- a/nvme-print-binary.c +++ b/nvme-print-binary.c @@ -63,6 +63,18 @@ static void binary_boot_part_log(void *bp_log, const char *devname, d_raw((unsigned char *)bp_log, size); } +static void binary_phy_rx_eom_log(struct nvme_phy_rx_eom_log *log, + __u16 controller) +{ + size_t len; + if (log->eomip == NVME_PHY_RX_EOM_COMPLETED) + len = log->hsize + log->dsize * log->nd; + else + len = log->hsize; + + d_raw((unsigned char *)log, len); +} + static void binary_media_unit_stat_log(struct nvme_media_unit_stat_log *mus_log) { d_raw((unsigned char *)mus_log, sizeof(*mus_log)); @@ -234,8 +246,8 @@ static void binary_supported_log(struct nvme_supported_log_pages *support_log, d_raw((unsigned char *)support_log, sizeof(*support_log)); } -static void binary_endurance_log(struct nvme_endurance_group_log *endurance_log, - __u16 group_id, const char *devname) +static void binary_endurance_log(struct nvme_endurance_group_log *endurance_log, __u16 group_id, + const char *devname) { return d_raw((unsigned char *)endurance_log, sizeof(*endurance_log)); } @@ -288,6 +300,7 @@ static void binary_discovery_log(struct nvmf_discovery_log *log, int numrec) static struct print_ops binary_print_ops = { .ana_log = binary_ana_log, .boot_part_log = binary_boot_part_log, + .phy_rx_eom_log = binary_phy_rx_eom_log, .ctrl_list = binary_list_ctrl, .ctrl_registers = binary_ctrl_registers, .directive = binary_directive, diff --git a/nvme-print-json.c b/nvme-print-json.c index 923b28e..485c013 100644 --- a/nvme-print-json.c +++ b/nvme-print-json.c @@ -210,6 +210,7 @@ static void json_nvme_id_ns(struct nvme_id_ns *ns, unsigned int nsid, json_object_add_value_uint(root, "mnan", le32_to_cpu(ctrl->mnan)); json_object_add_value_uint128(root, "maxdna", maxdna); json_object_add_value_uint(root, "maxcna", le32_to_cpu(ctrl->maxcna)); + json_object_add_value_uint(root, "oaqd", le32_to_cpu(ctrl->oaqd)); if (strlen(subnqn)) json_object_add_value_string(root, "subnqn", subnqn); @@ -229,7 +230,10 @@ static void json_nvme_id_ns(struct nvme_id_ns *ns, unsigned int nsid, json_object_add_value_int(psd, "max_power", le16_to_cpu(ctrl->psd[i].mp)); - json_object_add_value_int(psd, "flags", ctrl->psd[i].flags); + json_object_add_value_int(psd, "max_power_scale", + ctrl->psd[i].flags & 0x1); + json_object_add_value_int(psd, "non-operational_state", + (ctrl->psd[i].flags & 0x2) >> 1); json_object_add_value_uint(psd, "entry_lat", le32_to_cpu(ctrl->psd[i].enlat)); json_object_add_value_uint(psd, "exit_lat", @@ -451,51 +455,43 @@ void json_changed_ns_list_log(struct nvme_ns_list *log, json_free_object(root); } -static void json_endurance_log(struct nvme_endurance_group_log *endurance_group, - __u16 group_id, const char *devname) +static void json_endurance_log(struct nvme_endurance_group_log *endurance_group, __u16 group_id, + const char *devname) { struct json_object *root; - - nvme_uint128_t endurance_estimate = - le128_to_cpu(endurance_group->endurance_estimate); - nvme_uint128_t data_units_read = - le128_to_cpu(endurance_group->data_units_read); - nvme_uint128_t data_units_written = - le128_to_cpu(endurance_group->data_units_written); - nvme_uint128_t media_units_written = - le128_to_cpu(endurance_group->media_units_written); - nvme_uint128_t host_read_cmds = - le128_to_cpu(endurance_group->host_read_cmds); - nvme_uint128_t host_write_cmds = - le128_to_cpu(endurance_group->host_write_cmds); + nvme_uint128_t endurance_estimate = le128_to_cpu(endurance_group->endurance_estimate); + nvme_uint128_t data_units_read = le128_to_cpu(endurance_group->data_units_read); + nvme_uint128_t data_units_written = le128_to_cpu(endurance_group->data_units_written); + nvme_uint128_t media_units_written = le128_to_cpu(endurance_group->media_units_written); + nvme_uint128_t host_read_cmds = le128_to_cpu(endurance_group->host_read_cmds); + nvme_uint128_t host_write_cmds = le128_to_cpu(endurance_group->host_write_cmds); nvme_uint128_t media_data_integrity_err = - le128_to_cpu(endurance_group->media_data_integrity_err); + le128_to_cpu(endurance_group->media_data_integrity_err); nvme_uint128_t num_err_info_log_entries = - le128_to_cpu(endurance_group->num_err_info_log_entries); + le128_to_cpu(endurance_group->num_err_info_log_entries); + nvme_uint128_t total_end_grp_cap = le128_to_cpu(endurance_group->total_end_grp_cap); + nvme_uint128_t unalloc_end_grp_cap = le128_to_cpu(endurance_group->unalloc_end_grp_cap); root = json_create_object(); - json_object_add_value_int(root, "critical_warning", - endurance_group->critical_warning); - json_object_add_value_int(root, "avl_spare", - endurance_group->avl_spare); + json_object_add_value_int(root, "critical_warning", endurance_group->critical_warning); + json_object_add_value_int(root, "endurance_group_features", + endurance_group->endurance_group_features); + json_object_add_value_int(root, "avl_spare", endurance_group->avl_spare); json_object_add_value_int(root, "avl_spare_threshold", - endurance_group->avl_spare_threshold); - json_object_add_value_int(root, "percent_used", - endurance_group->percent_used); - json_object_add_value_uint128(root, "endurance_estimate", - endurance_estimate); + endurance_group->avl_spare_threshold); + json_object_add_value_int(root, "percent_used", endurance_group->percent_used); + json_object_add_value_int(root, "domain_identifier", endurance_group->domain_identifier); + json_object_add_value_uint128(root, "endurance_estimate", endurance_estimate); json_object_add_value_uint128(root, "data_units_read", data_units_read); - json_object_add_value_uint128(root, "data_units_written", - data_units_written); - json_object_add_value_uint128(root, "media_units_written", - media_units_written); + json_object_add_value_uint128(root, "data_units_written", data_units_written); + json_object_add_value_uint128(root, "media_units_written", media_units_written); json_object_add_value_uint128(root, "host_read_cmds", host_read_cmds); json_object_add_value_uint128(root, "host_write_cmds", host_write_cmds); - json_object_add_value_uint128(root, "media_data_integrity_err", - media_data_integrity_err); - json_object_add_value_uint128(root, "num_err_info_log_entries", - num_err_info_log_entries); + json_object_add_value_uint128(root, "media_data_integrity_err", media_data_integrity_err); + json_object_add_value_uint128(root, "num_err_info_log_entries", num_err_info_log_entries); + json_object_add_value_uint128(root, "total_end_grp_cap", total_end_grp_cap); + json_object_add_value_uint128(root, "unalloc_end_grp_cap", unalloc_end_grp_cap); json_print_object(root, NULL); printf("\n"); @@ -1413,6 +1409,116 @@ static void json_boot_part_log(void *bp_log, const char *devname, json_free_object(root); } +/* Printable Eye string is allocated and returned, caller must free */ +static char *json_eom_printable_eye(struct nvme_eom_lane_desc *lane, + struct json_object *root) +{ + char *eye = (char *)lane->eye_desc; + + char *printable = malloc(lane->nrows * lane->ncols + lane->ncols); + char *printable_start = printable; + if (!printable) + goto exit; + + int i, j; + for (i = 0; i < lane->nrows; i++) { + for (j = 0; j < lane->ncols; j++, printable++) + sprintf(printable, "%c", eye[i * lane->ncols + j]); + sprintf(printable++, "\n"); + } + + json_object_add_value_string(root, "printable_eye", printable_start); + +exit: + return printable_start; +} + + +static void json_phy_rx_eom_descs(struct nvme_phy_rx_eom_log *log, + struct json_object *root, char **allocated_eyes) +{ + void *p = log->descs; + uint16_t num_descs = le16_to_cpu(log->nd); + int i; + struct json_object *descs; + + descs = json_create_array(); + json_object_add_value_array(root, "descs", descs); + + for (i = 0; i < num_descs; i++) { + struct nvme_eom_lane_desc *desc = p; + struct json_object *jdesc = json_create_object(); + + json_object_add_value_uint(jdesc, "lid", desc->mstatus); + json_object_add_value_uint(jdesc, "lane", desc->lane); + json_object_add_value_uint(jdesc, "eye", desc->eye); + json_object_add_value_uint(jdesc, "top", le16_to_cpu(desc->top)); + json_object_add_value_uint(jdesc, "bottom", le16_to_cpu(desc->bottom)); + json_object_add_value_uint(jdesc, "left", le16_to_cpu(desc->left)); + json_object_add_value_uint(jdesc, "right", le16_to_cpu(desc->right)); + json_object_add_value_uint(jdesc, "nrows", le16_to_cpu(desc->nrows)); + json_object_add_value_uint(jdesc, "ncols", le16_to_cpu(desc->ncols)); + json_object_add_value_uint(jdesc, "edlen", le16_to_cpu(desc->edlen)); + + if (log->odp & NVME_EOM_PRINTABLE_EYE_PRESENT) + allocated_eyes[i] = json_eom_printable_eye(desc, root); + + /* Eye Data field is vendor specific, doesn't map to JSON */ + + json_array_add_value_object(descs, jdesc); + + p += log->dsize; + } +} + +static void json_phy_rx_eom_log(struct nvme_phy_rx_eom_log *log, __u16 controller) +{ + char **allocated_eyes = NULL; + int i; + + struct json_object *root; + root = json_create_object(); + + json_object_add_value_uint(root, "lid", log->lid); + json_object_add_value_uint(root, "eomip", log->eomip); + json_object_add_value_uint(root, "hsize", le16_to_cpu(log->hsize)); + json_object_add_value_uint(root, "rsize", le32_to_cpu(log->rsize)); + json_object_add_value_uint(root, "eomdgn", log->eomdgn); + json_object_add_value_uint(root, "lr", log->lr); + json_object_add_value_uint(root, "lanes", log->lanes); + json_object_add_value_uint(root, "epl", log->epl); + json_object_add_value_uint(root, "lspfc", log->lspfc); + json_object_add_value_uint(root, "li", log->li); + json_object_add_value_uint(root, "lsic", le16_to_cpu(log->lsic)); + json_object_add_value_uint(root, "dsize", le32_to_cpu(log->dsize)); + json_object_add_value_uint(root, "nd", le16_to_cpu(log->nd)); + json_object_add_value_uint(root, "maxtb", le16_to_cpu(log->maxtb)); + json_object_add_value_uint(root, "maxlr", le16_to_cpu(log->maxlr)); + json_object_add_value_uint(root, "etgood", le16_to_cpu(log->etgood)); + json_object_add_value_uint(root, "etbetter", le16_to_cpu(log->etbetter)); + json_object_add_value_uint(root, "etbest", le16_to_cpu(log->etbest)); + + if (log->eomip == NVME_PHY_RX_EOM_COMPLETED) { + /* Save Printable Eye strings allocated to free later */ + allocated_eyes = malloc(log->nd * sizeof(char *)); + if (allocated_eyes) + json_phy_rx_eom_descs(log, root, allocated_eyes); + } + + json_print_object(root, NULL); + printf("\n"); + + if (allocated_eyes) { + for (i = 0; i < log->nd; i++) { + /* Free any Printable Eye strings allocated */ + if (allocated_eyes[i]) + free(allocated_eyes[i]); + } + free(allocated_eyes); + } + json_free_object(root); +} + static void json_media_unit_stat_log(struct nvme_media_unit_stat_log *mus) { @@ -1797,6 +1903,8 @@ static void json_print_nvme_subsystem_list(nvme_root_t r, bool show_ana) nvme_subsystem_get_name(s)); json_object_add_value_string(subsystem_attrs, "NQN", nvme_subsystem_get_nqn(s)); + json_object_add_value_string(subsystem_attrs, "IOPolicy", + nvme_subsystem_get_iopolicy(s)); json_array_add_value_object(subsystems, subsystem_attrs); paths = json_create_array(); @@ -2378,8 +2486,8 @@ static void json_nvme_id_uuid_list(const struct nvme_id_uuid_list *uuid_list) root = json_create_object(); entries = json_create_array(); - /* The 0th entry is reserved */ - for (i = 1; i < NVME_ID_UUID_LIST_MAX; i++) { + + for (i = 0; i < NVME_ID_UUID_LIST_MAX; i++) { __u8 uuid[NVME_UUID_LEN]; struct json_object *entry = json_create_object(); @@ -2757,6 +2865,8 @@ static void json_simple_topology(nvme_root_t r) nvme_subsystem_get_name(s)); json_object_add_value_string(subsystem_attrs, "NQN", nvme_subsystem_get_nqn(s)); + json_object_add_value_string(subsystem_attrs, "IOPolicy", + nvme_subsystem_get_iopolicy(s)); json_array_add_value_object(subsystems, subsystem_attrs); namespaces = json_create_array(); @@ -2921,6 +3031,7 @@ static void json_output_perror(const char *msg) static struct print_ops json_print_ops = { .ana_log = json_ana_log, .boot_part_log = json_boot_part_log, + .phy_rx_eom_log = json_phy_rx_eom_log, .ctrl_list = json_nvme_list_ctrl, .ctrl_registers = json_ctrl_registers, .discovery_log = json_discovery_log, diff --git a/nvme-print-stdout.c b/nvme-print-stdout.c index 877ba75..90cd8dd 100644 --- a/nvme-print-stdout.c +++ b/nvme-print-stdout.c @@ -7,6 +7,11 @@ #include #include +#include +#include +#include +#include + #include "nvme.h" #include "libnvme.h" #include "nvme-print.h" @@ -23,13 +28,158 @@ static struct print_ops stdout_print_ops; struct nvme_bar_cap { __u16 mqes; - __u8 ams_cqr; + __u8 cqr:1; + __u8 ams:2; + __u8 rsvd19:5; __u8 to; - __u16 bps_css_nssrs_dstrd; - __u8 mpsmax_mpsmin; - __u8 rsvd_crms_nsss_cmbs_pmrs; + __u16 dstrd:4; + __u16 nssrs:1; + __u16 css:8; + __u16 bps:1; + __u8 cps:2; + __u8 mpsmin:4; + __u8 mpsmax:4; + __u8 pmrs:1; + __u8 cmbs:1; + __u8 nsss:1; + __u8 crwms:1; + __u8 crims:1; + __u8 rsvd61:3; +}; + +static const char *subsys_key(const struct nvme_subsystem *s) +{ + return nvme_subsystem_get_name((nvme_subsystem_t)s); +} + +static const char *ctrl_key(const struct nvme_ctrl *c) +{ + return nvme_ctrl_get_name((nvme_ctrl_t)c); +} + +static const char *ns_key(const struct nvme_ns *n) +{ + return nvme_ns_get_name((nvme_ns_t)n); +} + +static bool subsys_cmp(const struct nvme_subsystem *s, const char *name) +{ + return !strcmp(nvme_subsystem_get_name((nvme_subsystem_t)s), name); +} + +static bool ctrl_cmp(const struct nvme_ctrl *c, const char *name) +{ + return !strcmp(nvme_ctrl_get_name((nvme_ctrl_t)c), name); +} + +static bool ns_cmp(const struct nvme_ns *n, const char *name) +{ + return !strcmp(nvme_ns_get_name((nvme_ns_t)n), name); +} + +HTABLE_DEFINE_TYPE(struct nvme_subsystem, subsys_key, hash_string, + subsys_cmp, htable_subsys); +HTABLE_DEFINE_TYPE(struct nvme_ctrl, ctrl_key, hash_string, + ctrl_cmp, htable_ctrl); +HTABLE_DEFINE_TYPE(struct nvme_ns, ns_key, hash_string, + ns_cmp, htable_ns); + +static void htable_ctrl_add_unique(struct htable_ctrl *ht, nvme_ctrl_t c) +{ + if (htable_ctrl_get(ht, nvme_ctrl_get_name(c))) + return; + + htable_ctrl_add(ht, c); +} + +static void htable_ns_add_unique(struct htable_ns *ht, nvme_ns_t n) +{ + struct htable_ns_iter it; + nvme_ns_t _n; + + /* + * Test if namespace pointer is already in the hash, and thus avoid + * inserting severaltimes the same pointer. + */ + for (_n = htable_ns_getfirst(ht, nvme_ns_get_name(n), &it); + _n; + _n = htable_ns_getnext(ht, nvme_ns_get_name(n), &it)) { + if (_n == n) + return; + } + htable_ns_add(ht, n); +} + +struct nvme_resources { + nvme_root_t r; + + struct htable_subsys ht_s; + struct htable_ctrl ht_c; + struct htable_ns ht_n; + struct strset subsystems; + struct strset ctrls; + struct strset namespaces; }; +static int nvme_resources_init(nvme_root_t r, struct nvme_resources *res) +{ + nvme_host_t h; + nvme_subsystem_t s; + nvme_ctrl_t c; + nvme_ns_t n; + nvme_path_t p; + + res->r = r; + htable_subsys_init(&res->ht_s); + htable_ctrl_init(&res->ht_c); + htable_ns_init(&res->ht_n); + strset_init(&res->subsystems); + strset_init(&res->ctrls); + strset_init(&res->namespaces); + + nvme_for_each_host(r, h) { + nvme_for_each_subsystem(h, s) { + htable_subsys_add(&res->ht_s, s); + strset_add(&res->subsystems, nvme_subsystem_get_name(s)); + + nvme_subsystem_for_each_ctrl(s, c) { + htable_ctrl_add_unique(&res->ht_c, c); + strset_add(&res->ctrls, nvme_ctrl_get_name(c)); + + nvme_ctrl_for_each_ns(c, n) { + htable_ns_add_unique(&res->ht_n, n); + strset_add(&res->namespaces, nvme_ns_get_name(n)); + } + + nvme_ctrl_for_each_path(c, p) { + n = nvme_path_get_ns(p); + if (n) { + htable_ns_add_unique(&res->ht_n, n); + strset_add(&res->namespaces, nvme_ns_get_name(n)); + } + } + } + + nvme_subsystem_for_each_ns(s, n) { + htable_ns_add_unique(&res->ht_n, n); + strset_add(&res->namespaces, nvme_ns_get_name(n)); + } + } + } + + return 0; +} + +static void nvme_resources_free(struct nvme_resources *res) +{ + strset_clear(&res->namespaces); + strset_clear(&res->ctrls); + strset_clear(&res->subsystems); + htable_ns_clear(&res->ht_n); + htable_ctrl_clear(&res->ht_c); + htable_subsys_clear(&res->ht_s); +} + static void stdout_feature_show_fields(enum nvme_features_id fid, unsigned int result, unsigned char *buf); @@ -567,6 +717,111 @@ static void stdout_boot_part_log(void *bp_log, const char *devname, printf("Active BPID: %u\n", (le32_to_cpu(hdr->bpinfo) >> 31) & 0x1); } +static const char *eomip_to_string(__u8 eomip) +{ + const char *string; + switch (eomip) { + case NVME_PHY_RX_EOM_NOT_STARTED: + string = "Not Started"; + break; + case NVME_PHY_RX_EOM_IN_PROGRESS: + string = "In Progress"; + break; + case NVME_PHY_RX_EOM_COMPLETED: + string = "Completed"; + break; + default: + string = "Unknown"; + } + return string; +} + +static void stdout_phy_rx_eom_odp(uint8_t odp) +{ + __u8 rsvd = (odp >> 2) & 0x3F; + __u8 edfp = (odp >> 1) & 0x1; + __u8 pefp = odp & 0x1; + + if (rsvd) + printf(" [7:2] : %#x\tReserved\n", rsvd); + printf(" [1:1] : %#x\tEye Data Field %sPresent\n", + edfp, edfp ? "" : "Not "); + printf(" [0:0] : %#x\tPrintable Eye Field %sPresent\n", + pefp, pefp ? "" : "Not "); +} + +static void stdout_eom_printable_eye(struct nvme_eom_lane_desc *lane) +{ + char *eye = (char *)lane->eye_desc; + int i, j; + for (i = 0; i < lane->nrows; i++) { + for (j = 0; j < lane->ncols; j++) + printf("%c", eye[i * lane->ncols + j]); + printf("\n"); + } +} + +static void stdout_phy_rx_eom_descs(struct nvme_phy_rx_eom_log *log) +{ + void *p = log->descs; + int i; + + for (i = 0; i < log->nd; i++) { + struct nvme_eom_lane_desc *desc = p; + + printf("Measurement Status: %s\n", + desc->mstatus ? "Successful" : "Not Successful"); + printf("Lane: %u\n", desc->lane); + printf("Eye: %u\n", desc->eye); + printf("Top: %u\n", le16_to_cpu(desc->top)); + printf("Bottom: %u\n", le16_to_cpu(desc->bottom)); + printf("Left: %u\n", le16_to_cpu(desc->left)); + printf("Right: %u\n", le16_to_cpu(desc->right)); + printf("Number of Rows: %u\n", le16_to_cpu(desc->nrows)); + printf("Number of Columns: %u\n", le16_to_cpu(desc->ncols)); + printf("Eye Data Length: %u\n", le16_to_cpu(desc->edlen)); + + if (log->odp & NVME_EOM_PRINTABLE_EYE_PRESENT) + stdout_eom_printable_eye(desc); + + /* Eye Data field is vendor specific */ + + p += log->dsize; + } +} + +static void stdout_phy_rx_eom_log(struct nvme_phy_rx_eom_log *log, __u16 controller) +{ + int human = stdout_print_ops.flags & VERBOSE; + + printf("Physical Interface Receiver Eye Opening Measurement Log for controller ID: %u\n", controller); + printf("Log ID: %u\n", log->lid); + printf("EOM In Progress: %s\n", eomip_to_string(log->eomip)); + printf("Header Size: %u\n", le16_to_cpu(log->hsize)); + printf("Result Size: %u\n", le32_to_cpu(log->rsize)); + printf("EOM Data Generation Number: %u\n", log->eomdgn); + printf("Log Revision: %u\n", log->lr); + printf("Optional Data Present: %u\n", log->odp); + if (human) + stdout_phy_rx_eom_odp(log->odp); + printf("Lanes: %u\n", log->lanes); + printf("Eyes Per Lane: %u\n", log->epl); + printf("Log Specific Parameter Field Copy: %u\n", log->lspfc); + printf("Link Information: %u\n", log->li); + printf("Log Specific Identifier Copy: %u\n", le16_to_cpu(log->lsic)); + printf("Descriptor Size: %u\n", le32_to_cpu(log->dsize)); + printf("Number of Descriptors: %u\n", le16_to_cpu(log->nd)); + printf("Maximum Top Bottom: %u\n", le16_to_cpu(log->maxtb)); + printf("Maximum Left Right: %u\n", le16_to_cpu(log->maxlr)); + printf("Estimated Time for Good Quality: %u\n", le16_to_cpu(log->etgood)); + printf("Estimated Time for Better Quality: %u\n", le16_to_cpu(log->etbetter)); + printf("Estimated Time for Best Quality: %u\n", le16_to_cpu(log->etbest)); + + if (log->eomip == NVME_PHY_RX_EOM_COMPLETED) { + stdout_phy_rx_eom_descs(log); + } +} + static void stdout_media_unit_stat_log(struct nvme_media_unit_stat_log *mus_log) { int i; @@ -597,7 +852,7 @@ static void stdout_media_unit_stat_log(struct nvme_media_unit_stat_log *mus_log) static void stdout_fdp_config_fdpa(uint8_t fdpa) { __u8 valid = (fdpa >> 7) & 0x1; - __u8 rsvd = (fdpa >> 5) >> 0x3; + __u8 rsvd = (fdpa >> 5) & 0x3; __u8 fdpvwc = (fdpa >> 4) & 0x1; __u8 rgif = fdpa & 0xf; @@ -826,13 +1081,24 @@ static void stdout_subsystem_ctrls(nvme_subsystem_t s) static void stdout_subsystem(nvme_root_t r, bool show_ana) { nvme_host_t h; + bool first = true; nvme_for_each_host(r, h) { nvme_subsystem_t s; nvme_for_each_subsystem(h, s) { + int len = strlen(nvme_subsystem_get_name(s)); + + if (!first) + printf("\n"); + first = false; + printf("%s - NQN=%s\n", nvme_subsystem_get_name(s), nvme_subsystem_get_nqn(s)); + printf("%*s hostnqn=%s\n", len, " ", + nvme_host_get_hostnqn(nvme_subsystem_get_host(s))); + printf("%*s iopolicy=%s\n", len, " ", + nvme_subsystem_get_iopolicy(s)); printf("\\\n"); if (!show_ana || !stdout_subsystem_multipath(s)) @@ -849,39 +1115,33 @@ static void stdout_subsystem_list(nvme_root_t r, bool show_ana) static void stdout_registers_cap(struct nvme_bar_cap *cap) { printf("\tController Ready With Media Support (CRWMS): %s\n", - ((cap->rsvd_crms_nsss_cmbs_pmrs & 0x08) >> 3) ? "Supported" : "Not Supported"); + cap->crwms ? "Supported" : "Not Supported"); printf("\tController Ready Independent of Media Support (CRIMS): %s\n", - ((cap->rsvd_crms_nsss_cmbs_pmrs & 0x10) >> 4) ? "Supported" : "Not Supported"); + cap->crims ? "Supported" : "Not Supported"); + printf("\tNVM Subsystem Shutdown Supported (NSSS): %s\n", cap->nsss ? "Supported" : "Not Supported"); printf("\tController Memory Buffer Supported (CMBS): The Controller Memory Buffer is %s\n", - ((cap->rsvd_crms_nsss_cmbs_pmrs & 0x02) >> 1) ? "Supported" : - "Not Supported"); + cap->cmbs ? "Supported" : "Not Supported"); printf("\tPersistent Memory Region Supported (PMRS): The Persistent Memory Region is %s\n", - (cap->rsvd_crms_nsss_cmbs_pmrs & 0x01) ? "Supported" : "Not Supported"); - printf("\tMemory Page Size Maximum (MPSMAX): %u bytes\n", - 1 << (12 + ((cap->mpsmax_mpsmin & 0xf0) >> 4))); - printf("\tMemory Page Size Minimum (MPSMIN): %u bytes\n", - 1 << (12 + (cap->mpsmax_mpsmin & 0x0f))); - printf("\tBoot Partition Support (BPS): %s\n", - (cap->bps_css_nssrs_dstrd & 0x2000) ? "Yes":"No"); + cap->pmrs ? "Supported" : "Not Supported"); + printf("\tMemory Page Size Maximum (MPSMAX): %u bytes\n", 1 << (12 + cap->mpsmax)); + printf("\tMemory Page Size Minimum (MPSMIN): %u bytes\n", 1 << (12 + cap->mpsmin)); + printf("\tController Power Scope (CPS): %s\n", + !cap->cps ? "Not Reported" : cap->cps == 1 ? "Controller scope" : + cap->cps == 2 ? "Domain scope" : "NVM subsystem scope"); + printf("\tBoot Partition Support (BPS): %s\n", cap->bps ? "Yes" : "No"); printf("\tCommand Sets Supported (CSS): NVM command set is %s\n", - (cap->bps_css_nssrs_dstrd & 0x0020) ? "Supported" : "Not Supported"); + cap->css & 0x01 ? "Supported" : "Not Supported"); printf("\t One or more I/O Command Sets are %s\n", - (cap->bps_css_nssrs_dstrd & 0x0800) ? "Supported" : "Not Supported"); + cap->css & 0x40 ? "Supported" : "Not Supported"); printf("\t %s\n", - (cap->bps_css_nssrs_dstrd & 0x1000) ? "Only Admin Command Set Supported" : - "I/O Command Set is Supported"); - printf("\tNVM Subsystem Reset Supported (NSSRS): %s\n", - (cap->bps_css_nssrs_dstrd & 0x0010) ? "Yes":"No"); - printf("\tDoorbell Stride (DSTRD): %u bytes\n", - 1 << (2 + (cap->bps_css_nssrs_dstrd & 0x000f))); - printf("\tTimeout (TO): %u ms\n", - cap->to * 500); + cap->css & 0x80 ? "Only Admin Command Set Supported" : "I/O Command Set is Supported"); + printf("\tNVM Subsystem Reset Supported (NSSRS): %s\n", cap->nssrs ? "Yes" : "No"); + printf("\tDoorbell Stride (DSTRD): %u bytes\n", 1 << (2 + cap->dstrd)); + printf("\tTimeout (TO): %u ms\n", cap->to * 500); printf("\tArbitration Mechanism Supported (AMS): Weighted Round Robin with Urgent Priority Class is %s\n", - (cap->ams_cqr & 0x02) ? "supported":"not supported"); - printf("\tContiguous Queues Required (CQR): %s\n", - (cap->ams_cqr & 0x01) ? "Yes":"No"); - printf("\tMaximum Queue Entries Supported (MQES): %u\n\n", - cap->mqes + 1); + cap->ams & 0x02 ? "Supported" : "Not supported"); + printf("\tContiguous Queues Required (CQR): %s\n", cap->cqr ? "Yes" : "No"); + printf("\tMaximum Queue Entries Supported (MQES): %u\n\n", cap->mqes + 1); } static void stdout_registers_version(__u32 vs) @@ -2184,9 +2444,9 @@ static void stdout_id_ns_dpc(__u8 dpc) __u8 pit1 = dpc & 0x1; if (rsvd) printf(" [7:5] : %#x\tReserved\n", rsvd); - printf(" [4:4] : %#x\tProtection Information Transferred as Last 8 Bytes of Metadata %sSupported\n", + printf(" [4:4] : %#x\tProtection Information Transferred as Last Bytes of Metadata %sSupported\n", pil8, pil8 ? "" : "Not "); - printf(" [3:3] : %#x\tProtection Information Transferred as First 8 Bytes of Metadata %sSupported\n", + printf(" [3:3] : %#x\tProtection Information Transferred as First Bytes of Metadata %sSupported\n", pif8, pif8 ? "" : "Not "); printf(" [2:2] : %#x\tProtection Information Type 3 %sSupported\n", pit3, pit3 ? "" : "Not "); @@ -2204,7 +2464,7 @@ static void stdout_id_ns_dps(__u8 dps) __u8 pit = dps & 0x7; if (rsvd) printf(" [7:4] : %#x\tReserved\n", rsvd); - printf(" [3:3] : %#x\tProtection Information is Transferred as %s 8 Bytes of Metadata\n", + printf(" [3:3] : %#x\tProtection Information is Transferred as %s Bytes of Metadata\n", pif8, pif8 ? "First" : "Last"); printf(" [2:0] : %#x\tProtection Information %s\n", pit, pit == 3 ? "Type 3 Enabled" : @@ -2765,6 +3025,7 @@ static void stdout_id_ctrl(struct nvme_id_ctrl *ctrl, printf("maxdna : %s\n", uint128_t_to_l10n_string(le128_to_cpu(ctrl->maxdna))); printf("maxcna : %u\n", le32_to_cpu(ctrl->maxcna)); + printf("oaqd : %u\n", le32_to_cpu(ctrl->oaqd)); printf("subnqn : %-.*s\n", (int)sizeof(ctrl->subnqn), ctrl->subnqn); printf("ioccsz : %u\n", le32_to_cpu(ctrl->ioccsz)); printf("iorcsz : %u\n", le32_to_cpu(ctrl->iorcsz)); @@ -2846,7 +3107,7 @@ static void stdout_nvm_id_ns(struct nvme_nvm_id_ns *nvm_ns, unsigned int nsid, pif == 1 ? "32b Guard" : "16b Guard", pif, sts, i == (ns->flbas & 0xf) ? in_use : ""); else - printf("elbaf %2d : pif:%d lbads:%-2d %s\n", i, + printf("elbaf %2d : pif:%d sts:%-2d %s\n", i, pif, sts, i == (ns->flbas & 0xf) ? in_use : ""); } } @@ -3207,8 +3468,8 @@ static void stdout_id_uuid_list(const struct nvme_id_uuid_list *uuid_list) { int i, human = stdout_print_ops.flags & VERBOSE; - /* The 0th entry is reserved */ printf("NVME Identify UUID:\n"); + for (i = 0; i < NVME_ID_UUID_LIST_MAX; i++) { __u8 uuid[NVME_UUID_LEN]; char *association = ""; @@ -3560,41 +3821,36 @@ static void stdout_supported_log(struct nvme_supported_log_pages *support_log, } } -static void stdout_endurance_log(struct nvme_endurance_group_log *endurance_log, - __u16 group_id, const char *devname) +static void stdout_endurance_log(struct nvme_endurance_group_log *endurance_log, __u16 group_id, + const char *devname) { - printf("Endurance Group Log for NVME device:%s Group ID:%x\n", devname, - group_id); - printf("critical warning : %u\n", - endurance_log->critical_warning); + printf("Endurance Group Log for NVME device:%s Group ID:%x\n", devname, group_id); + printf("critical_warning : %u\n", endurance_log->critical_warning); + printf("endurance_group_features: %u\n", endurance_log->endurance_group_features); printf("avl_spare : %u\n", endurance_log->avl_spare); - printf("avl_spare_threshold : %u\n", - endurance_log->avl_spare_threshold); + printf("avl_spare_threshold : %u\n", endurance_log->avl_spare_threshold); printf("percent_used : %u%%\n", endurance_log->percent_used); + printf("domain_identifier : %u\n", endurance_log->domain_identifier); printf("endurance_estimate : %s\n", - uint128_t_to_l10n_string( - le128_to_cpu(endurance_log->endurance_estimate))); + uint128_t_to_l10n_string(le128_to_cpu(endurance_log->endurance_estimate))); printf("data_units_read : %s\n", - uint128_t_to_l10n_string( - le128_to_cpu(endurance_log->data_units_read))); + uint128_t_to_l10n_string(le128_to_cpu(endurance_log->data_units_read))); printf("data_units_written : %s\n", - uint128_t_to_l10n_string( - le128_to_cpu(endurance_log->data_units_written))); + uint128_t_to_l10n_string(le128_to_cpu(endurance_log->data_units_written))); printf("media_units_written : %s\n", - uint128_t_to_l10n_string( - le128_to_cpu(endurance_log->media_units_written))); + uint128_t_to_l10n_string(le128_to_cpu(endurance_log->media_units_written))); printf("host_read_cmds : %s\n", - uint128_t_to_l10n_string( - le128_to_cpu(endurance_log->host_read_cmds))); + uint128_t_to_l10n_string(le128_to_cpu(endurance_log->host_read_cmds))); printf("host_write_cmds : %s\n", - uint128_t_to_l10n_string( - le128_to_cpu(endurance_log->host_write_cmds))); + uint128_t_to_l10n_string(le128_to_cpu(endurance_log->host_write_cmds))); printf("media_data_integrity_err: %s\n", - uint128_t_to_l10n_string( - le128_to_cpu(endurance_log->media_data_integrity_err))); + uint128_t_to_l10n_string(le128_to_cpu(endurance_log->media_data_integrity_err))); printf("num_err_info_log_entries: %s\n", - uint128_t_to_l10n_string( - le128_to_cpu(endurance_log->num_err_info_log_entries))); + uint128_t_to_l10n_string(le128_to_cpu(endurance_log->num_err_info_log_entries))); + printf("total_end_grp_cap : %s\n", + uint128_t_to_l10n_string(le128_to_cpu(endurance_log->total_end_grp_cap))); + printf("unalloc_end_grp_cap : %s\n", + uint128_t_to_l10n_string(le128_to_cpu(endurance_log->unalloc_end_grp_cap))); } static void stdout_smart_log(struct nvme_smart_log *smart, unsigned int nsid, @@ -4387,28 +4643,30 @@ static void stdout_list_item(nvme_ns_t n) nvme_ns_get_firmware(n)); } -static void stdout_simple_list(nvme_root_t r) +static bool stdout_simple_ns(const char *name, void *arg) { - nvme_host_t h; - nvme_subsystem_t s; - nvme_ctrl_t c; + struct nvme_resources *res = arg; nvme_ns_t n; + n = htable_ns_get(&res->ht_n, name); + stdout_list_item(n); + + return true; +} + +static void stdout_simple_list(nvme_root_t r) +{ + struct nvme_resources res; + + nvme_resources_init(r, &res); + printf("%-21s %-21s %-20s %-40s %-10s %-26s %-16s %-8s\n", "Node", "Generic", "SN", "Model", "Namespace", "Usage", "Format", "FW Rev"); printf("%-.21s %-.21s %-.20s %-.40s %-.10s %-.26s %-.16s %-.8s\n", dash, dash, dash, dash, dash, dash, dash, dash); + strset_iterate(&res.namespaces, stdout_simple_ns, &res); - nvme_for_each_host(r, h) { - nvme_for_each_subsystem(h, s) { - nvme_subsystem_for_each_ns(s, n) - stdout_list_item(n); - - nvme_subsystem_for_each_ctrl(s, c) - nvme_ctrl_for_each_ns(c, n) - stdout_list_item(n); - } - } + nvme_resources_free(&res); } static void stdout_ns_details(nvme_ns_t n) @@ -4435,100 +4693,155 @@ static void stdout_ns_details(nvme_ns_t n) genname, nvme_ns_get_nsid(n), usage, format); } -static void stdout_detailed_list(nvme_root_t r) +static bool stdout_detailed_name(const char *name, void *arg) { - nvme_host_t h; + bool *first = arg; + + printf("%s%s", *first ? "" : ", ", name); + *first = false; + + return true; +} + +static bool stdout_detailed_subsys(const char *name, void *arg) +{ + struct nvme_resources *res = arg; + struct htable_subsys_iter it; + struct strset ctrls; nvme_subsystem_t s; nvme_ctrl_t c; - nvme_path_t p; - nvme_ns_t n; - - printf("%-16s %-96s %-.16s\n", "Subsystem", "Subsystem-NQN", "Controllers"); - printf("%-.16s %-.96s %-.16s\n", dash, dash, dash); + bool first; - nvme_for_each_host(r, h) { - nvme_for_each_subsystem(h, s) { - bool first = true; - printf("%-16s %-96s ", nvme_subsystem_get_name(s), - nvme_subsystem_get_nqn(s)); + strset_init(&ctrls); + first = true; + for (s = htable_subsys_getfirst(&res->ht_s, name, &it); + s; + s = htable_subsys_getnext(&res->ht_s, name, &it)) { - nvme_subsystem_for_each_ctrl(s, c) { - printf("%s%s", first ? "": ", ", - nvme_ctrl_get_name(c)); - first = false; - } - printf("\n"); + if (first) { + printf("%-16s %-96s ", name, nvme_subsystem_get_nqn(s)); + first = false; } + + nvme_subsystem_for_each_ctrl(s, c) + strset_add(&ctrls, nvme_ctrl_get_name(c)); } + + first = true; + strset_iterate(&ctrls, stdout_detailed_name, &first); + strset_clear(&ctrls); printf("\n"); - printf("%-8s %-20s %-40s %-8s %-6s %-14s %-6s %-12s %-16s\n", "Device", - "SN", "MN", "FR", "TxPort", "Address", "Slot", "Subsystem", "Namespaces"); - printf("%-.8s %-.20s %-.40s %-.8s %-.6s %-.14s %-.6s %-.12s %-.16s\n", dash, - dash, dash, dash, dash, dash, dash, dash, dash); + return true; +} - nvme_for_each_host(r, h) { - nvme_for_each_subsystem(h, s) { - nvme_subsystem_for_each_ctrl(s, c) { - bool first = true; +static bool stdout_detailed_ctrl(const char *name, void *arg) +{ + struct nvme_resources *res = arg; + struct strset namespaces; + nvme_ctrl_t c; + nvme_path_t p; + nvme_ns_t n; + bool first; + + c = htable_ctrl_get(&res->ht_c, name); + assert(c); + + printf("%-8s %-20s %-40s %-8s %-6s %-14s %-6s %-12s ", + nvme_ctrl_get_name(c), + nvme_ctrl_get_serial(c), + nvme_ctrl_get_model(c), + nvme_ctrl_get_firmware(c), + nvme_ctrl_get_transport(c), + nvme_ctrl_get_address(c), + nvme_ctrl_get_phy_slot(c), + nvme_subsystem_get_name(nvme_ctrl_get_subsystem(c))); + + strset_init(&namespaces); + + nvme_ctrl_for_each_ns(c, n) + strset_add(&namespaces, nvme_ns_get_name(n)); + nvme_ctrl_for_each_path(c, p) { + n = nvme_path_get_ns(p); + if (!n) + continue; + strset_add(&namespaces, nvme_ns_get_name(n)); + } - printf("%-8s %-20s %-40s %-8s %-6s %-14s %-6s %-12s ", - nvme_ctrl_get_name(c), - nvme_ctrl_get_serial(c), - nvme_ctrl_get_model(c), - nvme_ctrl_get_firmware(c), - nvme_ctrl_get_transport(c), - nvme_ctrl_get_address(c), - nvme_ctrl_get_phy_slot(c), - nvme_subsystem_get_name(s)); + first = true; + strset_iterate(&namespaces, stdout_detailed_name, &first); + strset_clear(&namespaces); - nvme_ctrl_for_each_ns(c, n) { - printf("%s%s", first ? "": ", ", - nvme_ns_get_name(n)); - first = false; - } + printf("\n"); - nvme_ctrl_for_each_path(c, p) { - n = nvme_path_get_ns(p); - if (!n) - continue; - printf("%s%s", first ? "": ", ", - nvme_ns_get_name(n)); - first = false; - } - printf("\n"); - } + return true; +} + +static bool stdout_detailed_ns(const char *name, void *arg) +{ + struct nvme_resources *res = arg; + struct htable_ns_iter it; + struct strset ctrls; + nvme_ctrl_t c; + nvme_path_t p; + nvme_ns_t n; + bool first; + + strset_init(&ctrls); + first = true; + for (n = htable_ns_getfirst(&res->ht_n, name, &it); + n; + n = htable_ns_getnext(&res->ht_n, name, &it)) { + + if (first) { + stdout_ns_details(n); + first = false; + } + + if (nvme_ns_get_ctrl(n)) { + printf("%s\n", nvme_ctrl_get_name(nvme_ns_get_ctrl(n))); + return true; + } + + nvme_namespace_for_each_path(n, p) { + c = nvme_path_get_ctrl(p); + strset_add(&ctrls, nvme_ctrl_get_name(c)); } } + + first = true; + strset_iterate(&ctrls, stdout_detailed_name, &first); + strset_clear(&ctrls); + + printf("\n"); + return true; +} + +static void stdout_detailed_list(nvme_root_t r) +{ + struct nvme_resources res; + + nvme_resources_init(r, &res); + + printf("%-16s %-96s %-.16s\n", "Subsystem", "Subsystem-NQN", "Controllers"); + printf("%-.16s %-.96s %-.16s\n", dash, dash, dash); + strset_iterate(&res.subsystems, stdout_detailed_subsys, &res); + printf("\n"); + + printf("%-8s %-20s %-40s %-8s %-6s %-14s %-6s %-12s %-16s\n", "Device", + "SN", "MN", "FR", "TxPort", "Asdress", "Slot", "Subsystem", "Namespaces"); + printf("%-.8s %-.20s %-.40s %-.8s %-.6s %-.14s %-.6s %-.12s %-.16s\n", dash, + dash, dash, dash, dash, dash, dash, dash, dash); + strset_iterate(&res.ctrls, stdout_detailed_ctrl, &res); printf("\n"); printf("%-12s %-12s %-10s %-26s %-16s %-16s\n", "Device", "Generic", "NSID", "Usage", "Format", "Controllers"); printf("%-.12s %-.12s %-.10s %-.26s %-.16s %-.16s\n", dash, dash, dash, dash, dash, dash); + strset_iterate(&res.namespaces, stdout_detailed_ns, &res); - nvme_for_each_host(r, h) { - nvme_for_each_subsystem(h, s) { - nvme_subsystem_for_each_ctrl(s, c) { - nvme_ctrl_for_each_ns(c, n) { - stdout_ns_details(n); - printf("%s\n", nvme_ctrl_get_name(c)); - } - } - - nvme_subsystem_for_each_ns(s, n) { - bool first = true; - - stdout_ns_details(n); - nvme_subsystem_for_each_ctrl(s, c) { - printf("%s%s", first ? "" : ", ", - nvme_ctrl_get_name(c)); - first = false; - } - printf("\n"); - } - } - } + nvme_resources_free(&res); } static void stdout_list_items(nvme_root_t r) @@ -4560,6 +4873,9 @@ static void stdout_subsystem_topology_multipath(nvme_subsystem_t s, if (ranking == NVME_CLI_TOPO_NAMESPACE) { nvme_subsystem_for_each_ns(s, n) { + if (!nvme_namespace_first_path(n)) + continue; + printf(" +- ns %d\n", nvme_ns_get_nsid(n)); printf(" \\\n"); @@ -4638,12 +4954,22 @@ static void stdout_simple_topology(nvme_root_t r, { nvme_host_t h; nvme_subsystem_t s; + bool first = true; nvme_for_each_host(r, h) { nvme_for_each_subsystem(h, s) { + int len = strlen(nvme_subsystem_get_name(s)); + + if (!first) + printf("\n"); + first = false; printf("%s - NQN=%s\n", nvme_subsystem_get_name(s), nvme_subsystem_get_nqn(s)); + printf("%*s hostnqn=%s\n", len, " ", + nvme_host_get_hostnqn(nvme_subsystem_get_host(s))); + printf("%*s iopolicy=%s\n", len, " ", + nvme_subsystem_get_iopolicy(s)); printf("\\\n"); if (nvme_is_multipath(s)) @@ -4727,6 +5053,7 @@ static void stdout_connect_msg(nvme_ctrl_t c) static struct print_ops stdout_print_ops = { .ana_log = stdout_ana_log, .boot_part_log = stdout_boot_part_log, + .phy_rx_eom_log = stdout_phy_rx_eom_log, .ctrl_list = stdout_list_ctrl, .ctrl_registers = stdout_ctrl_registers, .directive = stdout_directive_show, diff --git a/nvme-print.c b/nvme-print.c index 4b977c1..ea5bf76 100644 --- a/nvme-print.c +++ b/nvme-print.c @@ -258,6 +258,12 @@ void nvme_show_boot_part_log(void *bp_log, const char *devname, nvme_print(boot_part_log, flags, bp_log, devname, size); } +void nvme_show_phy_rx_eom_log(struct nvme_phy_rx_eom_log *log, __u16 controller, + enum nvme_print_flags flags) +{ + nvme_print(phy_rx_eom_log, flags, log, controller); +} + void nvme_show_media_unit_stat_log(struct nvme_media_unit_stat_log *mus_log, enum nvme_print_flags flags) { @@ -420,7 +426,7 @@ void nvme_show_status(int status) { struct print_ops *ops; - if (argconfig_output_format_json(false)) + if (nvme_is_output_format_json()) ops = nvme_print_ops(JSON); else ops =nvme_print_ops(0); @@ -1016,7 +1022,11 @@ void nvme_show_topology(nvme_root_t r, enum nvme_cli_topo_ranking ranking, enum nvme_print_flags flags) { - nvme_print(topology_namespace, flags, r); + if (ranking == NVME_CLI_TOPO_NAMESPACE) { + nvme_print(topology_namespace, flags, r); + } else { + nvme_print(topology_ctrl, flags, r); + } } void nvme_show_message(bool error, const char *msg, ...) @@ -1025,7 +1035,7 @@ void nvme_show_message(bool error, const char *msg, ...) va_list ap; va_start(ap, msg); - if (argconfig_output_format_json(false)) + if (nvme_is_output_format_json()) ops = nvme_print_ops(JSON); else ops = nvme_print_ops(0); @@ -1044,7 +1054,7 @@ void nvme_show_perror(const char *msg) { struct print_ops *ops; - if (argconfig_output_format_json(false)) + if (nvme_is_output_format_json()) ops = nvme_print_ops(JSON); else ops = nvme_print_ops(0); diff --git a/nvme-print.h b/nvme-print.h index 638a2b6..1c7e5dc 100644 --- a/nvme-print.h +++ b/nvme-print.h @@ -8,8 +8,8 @@ #include typedef struct nvme_effects_log_node { + struct nvme_cmd_effects_log effects; /* needs to be first member because of alignment requirement. */ enum nvme_csi csi; - struct nvme_cmd_effects_log effects; struct list_node node; } nvme_effects_log_node_t; @@ -23,6 +23,7 @@ struct print_ops { /* libnvme types.h print functions */ void (*ana_log)(struct nvme_ana_log *ana_log, const char *devname, size_t len); void (*boot_part_log)(void *bp_log, const char *devname, __u32 size); + void (*phy_rx_eom_log)(struct nvme_phy_rx_eom_log *log, __u16 controller); void (*ctrl_list)(struct nvme_ctrl_list *ctrl_list); void (*ctrl_registers)(void *bar, bool fabrics); void (*directive)(__u8 type, __u8 oper, __u16 spec, __u32 nsid, __u32 result, void *buf, __u32 len); @@ -116,6 +117,7 @@ void nvme_show_relatives(const char *name); void nvme_show_id_iocs(struct nvme_id_iocs *iocs, enum nvme_print_flags flags); void nvme_show_id_ctrl(struct nvme_id_ctrl *ctrl, enum nvme_print_flags flags, void (*vendor_show)(__u8 *vs, struct json_object *root)); +void nvme_show_id_ctrl_rpmbs(__le32 ctrl_rpmbs, enum nvme_print_flags flags); void nvme_show_id_ns(struct nvme_id_ns *ns, unsigned int nsid, unsigned int lba_index, bool cap_only, enum nvme_print_flags flags); void nvme_show_cmd_set_independent_id_ns( @@ -165,6 +167,8 @@ void nvme_show_resv_notif_log(struct nvme_resv_notification_log *resv, const char *devname, enum nvme_print_flags flags); void nvme_show_boot_part_log(void *bp_log, const char *devname, __u32 size, enum nvme_print_flags flags); +void nvme_show_phy_rx_eom_log(struct nvme_phy_rx_eom_log *log, + __u16 controller, enum nvme_print_flags flags); void nvme_show_fid_support_effects_log(struct nvme_fid_supported_effects_log *fid_log, const char *devname, enum nvme_print_flags flags); void nvme_show_mi_cmd_support_effects_log(struct nvme_mi_cmd_supported_effects_log *mi_cmd_log, diff --git a/nvme-rpmb.c b/nvme-rpmb.c index 40ddb5b..3dbbbd3 100644 --- a/nvme-rpmb.c +++ b/nvme-rpmb.c @@ -32,6 +32,7 @@ #include "common.h" #include "nvme.h" #include "libnvme.h" +#include "nvme-print.h" #define CREATE_CMD @@ -48,7 +49,6 @@ #define HMAC_SHA256_HASH_SIZE 32 #define MD5_HASH_HASH_SIZE 16 -extern int nvme_show_id_ctrl_rpmbs(unsigned int); /* * Utility function to create hash value of given data (with given key) using * given hash algorithm; this function uses kernel crypto services @@ -903,7 +903,7 @@ int rpmb_cmd_option(int argc, char **argv, struct command *cmd, struct plugin *p /* parse and validate options; default print rpmb support info */ if (cfg.cmd == 0 || strcmp(cfg.cmd, "info") == 0) { - nvme_show_id_ctrl_rpmbs(regs.rpmbs); + nvme_show_id_ctrl_rpmbs(regs.rpmbs, 0); goto out; } diff --git a/nvme-wrap.c b/nvme-wrap.c index faea690..a61b489 100644 --- a/nvme-wrap.c +++ b/nvme-wrap.c @@ -106,11 +106,11 @@ int nvme_cli_identify_primary_ctrl(struct nvme_dev *dev, __u32 nsid, return do_admin_op(identify_primary_ctrl, dev, nsid, cap); } -int nvme_cli_identify_secondary_ctrl_list(struct nvme_dev *dev, __u32 nsid, +int nvme_cli_identify_secondary_ctrl_list(struct nvme_dev *dev, __u16 ctrl_id, struct nvme_secondary_ctrl_list *sc_list) { - return do_admin_op(identify_secondary_ctrl_list, dev, nsid, ctrl_id, + return do_admin_op(identify_secondary_ctrl_list, dev, ctrl_id, sc_list); } @@ -302,6 +302,12 @@ int nvme_cli_get_log_boot_partition(struct nvme_dev *dev, bool rae, __u8 lsp, return do_admin_op(get_log_boot_partition, dev, rae, lsp, len, part); } +int nvme_cli_get_log_phy_rx_eom(struct nvme_dev *dev, __u8 lsp, __u16 controller, + __u32 len, struct nvme_phy_rx_eom_log *part) +{ + return do_admin_op(get_log_phy_rx_eom, dev, lsp, controller, len, part); +} + int nvme_cli_get_log_discovery(struct nvme_dev *dev, bool rae, __u32 offset, __u32 len, void *log) { @@ -423,3 +429,8 @@ int nvme_cli_security_receive(struct nvme_dev *dev, return -ENODEV; } +void nvme_cli_set_debug(struct nvme_dev *dev, bool set) +{ + if (dev->type == NVME_DEV_DIRECT) + nvme_set_debug(set); +} diff --git a/nvme-wrap.h b/nvme-wrap.h index f4bec98..c50df14 100644 --- a/nvme-wrap.h +++ b/nvme-wrap.h @@ -28,7 +28,7 @@ int nvme_cli_identify_allocated_ns_list(struct nvme_dev *dev, __u32 nsid, struct nvme_ns_list *list); int nvme_cli_identify_primary_ctrl(struct nvme_dev *dev, __u32 nsid, struct nvme_primary_ctrl_cap *cap); -int nvme_cli_identify_secondary_ctrl_list(struct nvme_dev *dev, __u32 nsid, +int nvme_cli_identify_secondary_ctrl_list(struct nvme_dev *dev, __u16 ctrl_id, struct nvme_secondary_ctrl_list *sc_list); int nvme_cli_ns_mgmt_delete(struct nvme_dev *dev, __u32 nsid); @@ -105,6 +105,8 @@ int nvme_cli_get_log_mi_cmd_supported_effects(struct nvme_dev *dev, bool rae, int nvme_cli_get_log_boot_partition(struct nvme_dev *dev, bool rae, __u8 lsp, __u32 len, struct nvme_boot_partition *part); +int nvme_cli_get_log_phy_rx_eom(struct nvme_dev *dev, __u8 lsp, __u16 controller, + __u32 len, struct nvme_phy_rx_eom_log *part); int nvme_cli_get_log_discovery(struct nvme_dev *dev, bool rae, __u32 offset, __u32 len, void *log); int nvme_cli_get_log_media_unit_stat(struct nvme_dev *dev, __u16 domid, @@ -145,4 +147,5 @@ int nvme_cli_security_send(struct nvme_dev *dev, int nvme_cli_security_receive(struct nvme_dev *dev, struct nvme_security_receive_args* args); +void nvme_cli_set_debug(struct nvme_dev *dev, bool set); #endif /* _NVME_WRAP_H */ diff --git a/nvme.c b/nvme.c index b5b0585..968214a 100644 --- a/nvme.c +++ b/nvme.c @@ -27,6 +27,7 @@ #include "config.h" #include "nvme/tree.h" #include "nvme/types.h" +#include "util/cleanup.h" #include #include #include @@ -68,6 +69,7 @@ #include "fabrics.h" #define CREATE_CMD #include "nvme-builtin.h" +#include "malloc.h" struct feat_cfg { enum nvme_features_id feature_id; @@ -108,6 +110,14 @@ struct passthru_config { bool latency; }; +#define NVME_ARGS(n, c, ...) \ + struct argconfig_commandline_options n[] = { \ + OPT_FLAG("verbose", 'v', NULL, verbose), \ + OPT_FMT("output-format", 'o', &output_format_val, output_format), \ + ##__VA_ARGS__, \ + OPT_END() \ + } + static const char nvme_version_string[] = NVME_VERSION; static struct plugin builtin = { @@ -130,8 +140,6 @@ static struct program nvme = { }; const char *output_format = "Output format: normal|json|binary"; -static const char *output_format_no_binary = "Output format: normal|json"; - static const char *app_tag = "app tag for end-to-end PI"; static const char *app_tag_mask = "app tag mask for end-to-end PI"; static const char *block_count = "number of blocks (zeroes based) on device to access"; @@ -181,9 +189,39 @@ static const char *verbose = "Increase output verbosity"; static const char dash[51] = {[0 ... 49] = '=', '\0'}; static const char space[51] = {[0 ... 49] = ' ', '\0'}; +static char *output_format_val = "normal"; + static void *mmap_registers(nvme_root_t r, struct nvme_dev *dev); -static void *__nvme_alloc(size_t len, bool *huge) +#define ROUND_UP(N, S) ((((N) + (S) - 1) / (S)) * (S)) + +static void *nvme_alloc(size_t len) +{ + size_t _len = ROUND_UP(len, 0x1000); + void *p; + + if (posix_memalign((void *)&p, getpagesize(), _len)) + return NULL; + + memset(p, 0, _len); + return p; +} + +void *nvme_realloc(void *p, size_t len) +{ + size_t old_len = malloc_usable_size(p); + + void *result = nvme_alloc(len); + + if (p) { + memcpy(result, p, min(old_len, len)); + free(p); + } + + return result; +} + +static void *__nvme_alloc_huge(size_t len, bool *huge) { void *p; @@ -198,7 +236,7 @@ static void *__nvme_alloc(size_t len, bool *huge) #define HUGE_MIN 0x80000 #ifdef CONFIG_LIBHUGETLBFS -void nvme_free(void *p, bool huge) +void nvme_free_huge(void *p, bool huge) { if (huge) { if (p) @@ -208,32 +246,47 @@ void nvme_free(void *p, bool huge) } } -void *nvme_alloc(size_t len, bool *huge) +void *nvme_alloc_huge(size_t len, bool *huge) { void *p; if (len < HUGE_MIN) - return __nvme_alloc(len, huge); + return __nvme_alloc_huge(len, huge); p = get_hugepage_region(len, GHR_DEFAULT); if (!p) - return __nvme_alloc(len, huge); + return __nvme_alloc_huge(len, huge); *huge = true; return p; } #else -void nvme_free(void *p, bool huge) +void nvme_free_huge(void *p, bool huge) { free(p); } -void *nvme_alloc(size_t len, bool *huge) +void *nvme_alloc_huge(size_t len, bool *huge) { - return __nvme_alloc(len, huge); + return __nvme_alloc_huge(len, huge); } #endif +void *nvme_realloc_huge(void *p, size_t len, bool *huge) +{ + size_t old_len = malloc_usable_size(p); + bool was_huge = *huge; + + void *result = nvme_alloc_huge(len, huge); + + if (p) { + memcpy(result, p, min(old_len, len)); + nvme_free_huge(p, was_huge); + } + + return result; +} + const char *nvme_strerror(int errnum) { if (errnum >= ENVME_CONNECT_RESOLVE) @@ -429,7 +482,7 @@ static int get_dev(struct nvme_dev **dev, int argc, char **argv, int flags) else ret = open_dev_direct(dev, devname, flags); - return ret; + return ret != 0 ? -errno : 0; } int parse_and_open(struct nvme_dev **dev, int argc, char **argv, @@ -445,6 +498,8 @@ int parse_and_open(struct nvme_dev **dev, int argc, char **argv, ret = get_dev(dev, argc, argv, O_RDONLY); if (ret < 0) argconfig_print_help(desc, opts); + else if (argconfig_parse_seen(opts, "verbose")) + nvme_cli_set_debug(*dev, true); return ret; } @@ -477,6 +532,11 @@ enum nvme_print_flags validate_output_format(const char *format) return -EINVAL; } +bool nvme_is_output_format_json(void) +{ + return validate_output_format(output_format_val) == JSON; +} + void dev_close(struct nvme_dev *dev) { switch (dev->type) { @@ -493,46 +553,41 @@ void dev_close(struct nvme_dev *dev) static int get_smart_log(int argc, char **argv, struct command *cmd, struct plugin *plugin) { - struct nvme_smart_log smart_log; const char *desc = "Retrieve SMART log for the given device\n" "(or optionally a namespace) in either decoded format\n" "(default) or binary."; + + _cleanup_free_ struct nvme_smart_log *smart_log = NULL; + _cleanup_nvme_dev_ struct nvme_dev *dev = NULL; const char *namespace = "(optional) desired namespace"; enum nvme_print_flags flags; - struct nvme_dev *dev; int err = -1; struct config { __u32 namespace_id; - char *output_format; bool raw_binary; bool human_readable; }; struct config cfg = { .namespace_id = NVME_NSID_ALL, - .output_format = "normal", .raw_binary = false, .human_readable = false, }; - - OPT_ARGS(opts) = { - OPT_UINT("namespace-id", 'n', &cfg.namespace_id, namespace), - OPT_FMT("output-format", 'o', &cfg.output_format, output_format), - OPT_FLAG("raw-binary", 'b', &cfg.raw_binary, raw_output), - OPT_FLAG("human-readable", 'H', &cfg.human_readable, human_readable_info), - OPT_END() - }; + NVME_ARGS(opts, cfg, + OPT_UINT("namespace-id", 'n', &cfg.namespace_id, namespace), + OPT_FLAG("raw-binary", 'b', &cfg.raw_binary, raw_output), + OPT_FLAG("human-readable", 'H', &cfg.human_readable, human_readable_info)); err = parse_and_open(&dev, argc, argv, desc, opts); if (err) - goto ret; + return err; - err = flags = validate_output_format(cfg.output_format); + err = flags = validate_output_format(output_format_val); if (err < 0) { nvme_show_error("Invalid output format"); - goto close_fd; + return err; } if (cfg.raw_binary) @@ -541,18 +596,20 @@ static int get_smart_log(int argc, char **argv, struct command *cmd, struct plug if (cfg.human_readable) flags |= VERBOSE; + smart_log = nvme_alloc(sizeof(*smart_log)); + if (!smart_log) + return -ENOMEM; + err = nvme_cli_get_log_smart(dev, cfg.namespace_id, false, - &smart_log); + smart_log); if (!err) - nvme_show_smart_log(&smart_log, cfg.namespace_id, + nvme_show_smart_log(smart_log, cfg.namespace_id, dev->name, flags); else if (err > 0) nvme_show_status(err); else nvme_show_error("smart log: %s", nvme_strerror(errno)); -close_fd: - dev_close(dev); -ret: + return err; } @@ -562,72 +619,67 @@ static int get_ana_log(int argc, char **argv, struct command *cmd, const char *desc = "Retrieve ANA log for the given device in\n" "decoded format (default), json or binary."; const char *groups = "Return ANA groups only."; - void *ana_log; + + _cleanup_nvme_dev_ struct nvme_dev *dev= NULL; + _cleanup_free_ struct nvme_id_ctrl *ctrl = NULL; + _cleanup_free_ void *ana_log = NULL; size_t ana_log_len; - struct nvme_id_ctrl ctrl; enum nvme_print_flags flags; enum nvme_log_ana_lsp lsp; - struct nvme_dev *dev; int err = -1; struct config { bool groups; - char *output_format; }; struct config cfg = { .groups = false, - .output_format = "normal", }; - OPT_ARGS(opts) = { - OPT_FLAG("groups", 'g', &cfg.groups, groups), - OPT_FMT("output-format", 'o', &cfg.output_format, output_format), - OPT_END() - }; + NVME_ARGS(opts, cfg, + OPT_FLAG("groups", 'g', &cfg.groups, groups)); err = parse_and_open(&dev, argc, argv, desc, opts); if (err) - goto ret; + return err; - err = flags = validate_output_format(cfg.output_format); + err = flags = validate_output_format(output_format_val); if (err < 0) { nvme_show_error("Invalid output format"); - goto close_fd; + return err; } - err = nvme_cli_identify_ctrl(dev, &ctrl); + ctrl = nvme_alloc(sizeof(*ctrl)); + if (!ctrl) + return -ENOMEM; + + err = nvme_cli_identify_ctrl(dev, ctrl); if (err) { nvme_show_error("ERROR : nvme_identify_ctrl() failed: %s", nvme_strerror(errno)); - goto close_fd; + return err; } ana_log_len = sizeof(struct nvme_ana_log) + - le32_to_cpu(ctrl.nanagrpid) * sizeof(struct nvme_ana_group_desc); - if (!(ctrl.anacap & (1 << 6))) - ana_log_len += le32_to_cpu(ctrl.mnan) * sizeof(__le32); + le32_to_cpu(ctrl->nanagrpid) * sizeof(struct nvme_ana_group_desc); + if (!(ctrl->anacap & (1 << 6))) + ana_log_len += le32_to_cpu(ctrl->mnan) * sizeof(__le32); - ana_log = malloc(ana_log_len); - if (!ana_log) { - err = -ENOMEM; - goto close_fd; - } + ana_log = nvme_alloc(ana_log_len); + if (!ana_log) + return -ENOMEM; lsp = cfg.groups ? NVME_LOG_ANA_LSP_RGO_GROUPS_ONLY : NVME_LOG_ANA_LSP_RGO_NAMESPACES; err = nvme_cli_get_log_ana(dev, lsp, true, 0, ana_log_len, ana_log); if (!err) - nvme_show_ana_log(ana_log, dev->name, flags, ana_log_len); + nvme_show_ana_log(ana_log, dev->name, ana_log_len, flags); else if (err > 0) nvme_show_status(err); else nvme_show_error("ana-log: %s", nvme_strerror(errno)); - free(ana_log); -close_fd: - dev_close(dev); -ret: + return err; } @@ -637,25 +689,32 @@ static int parse_telemetry_da(struct nvme_dev *dev, size_t *size) { - struct nvme_id_ctrl id_ctrl; + _cleanup_free_ struct nvme_id_ctrl *id_ctrl = NULL; + size_t dalb = 0; + + id_ctrl = nvme_alloc(sizeof(*id_ctrl)); + if (!id_ctrl) + return -ENOMEM; switch (da) { case NVME_TELEMETRY_DA_1: + dalb = le16_to_cpu(telem->dalb1); + break; case NVME_TELEMETRY_DA_2: + dalb = le16_to_cpu(telem->dalb2); + break; case NVME_TELEMETRY_DA_3: /* dalb3 >= dalb2 >= dalb1 */ - *size = (le16_to_cpu(telem->dalb3) + 1) * - NVME_LOG_TELEM_BLOCK_SIZE; + dalb = le16_to_cpu(telem->dalb3); break; case NVME_TELEMETRY_DA_4: - if (nvme_cli_identify_ctrl(dev, &id_ctrl)) { + if (nvme_cli_identify_ctrl(dev, id_ctrl)) { perror("identify-ctrl"); return -errno; } - if (id_ctrl.lpa & 0x40) { - *size = (le32_to_cpu(telem->dalb4) + 1) * - NVME_LOG_TELEM_BLOCK_SIZE; + if (id_ctrl->lpa & 0x40) { + dalb = le32_to_cpu(telem->dalb4); } else { nvme_show_error( "Data area 4 unsupported, bit 6 of Log Page Attributes not set"); @@ -667,10 +726,11 @@ static int parse_telemetry_da(struct nvme_dev *dev, return -EINVAL; } - if (*size == NVME_LOG_TELEM_BLOCK_SIZE) { + if (dalb == 0) { nvme_show_error("ERROR: No telemetry data block"); return -ENOENT; } + *size = (dalb + 1) * NVME_LOG_TELEM_BLOCK_SIZE; return 0; } @@ -680,7 +740,7 @@ static int get_log_telemetry_ctrl(struct nvme_dev *dev, bool rae, size_t size, struct nvme_telemetry_log *log; int err; - log = calloc(1, size); + log = nvme_alloc(size); if (!log) return -errno; @@ -700,7 +760,7 @@ static int get_log_telemetry_host(struct nvme_dev *dev, size_t size, struct nvme_telemetry_log *log; int err; - log = calloc(1, size); + log = nvme_alloc(size); if (!log) return -errno; @@ -719,10 +779,14 @@ static int __create_telemetry_log_host(struct nvme_dev *dev, size_t *size, struct nvme_telemetry_log **buf) { - struct nvme_telemetry_log log = { 0 }; + _cleanup_free_ struct nvme_telemetry_log *log = NULL; int err; - err = nvme_cli_get_log_create_telemetry_host(dev, &log); + log = nvme_alloc(sizeof(*log)); + if (!log) + return -ENOMEM; + + err = nvme_cli_get_log_create_telemetry_host(dev, log); if (err) return -errno; @@ -739,7 +803,7 @@ static int __get_telemetry_log_ctrl(struct nvme_dev *dev, struct nvme_telemetry_log *log; int err; - log = calloc(1, NVME_LOG_TELEM_BLOCK_SIZE); + log = nvme_alloc(NVME_LOG_TELEM_BLOCK_SIZE); if (!log) return -errno; @@ -784,16 +848,16 @@ static int __get_telemetry_log_host(struct nvme_dev *dev, size_t *size, struct nvme_telemetry_log **buf) { - struct nvme_telemetry_log log = { 0 }; + _cleanup_free_ struct nvme_telemetry_log *log = NULL; int err; err = nvme_cli_get_log_telemetry_host(dev, 0, NVME_LOG_TELEM_BLOCK_SIZE, - &log); + log); if (err) return err; - err = parse_telemetry_da(dev, da, &log, size); + err = parse_telemetry_da(dev, da, log, size); if (err) return err; @@ -808,12 +872,14 @@ static int get_telemetry_log(int argc, char **argv, struct command *cmd, const char *hgen = "Have the host tell the controller to generate the report"; const char *cgen = "Gather report generated by the controller."; const char *dgen = "Pick which telemetry data area to report. Default is 3 to fetch areas 1-3. Valid options are 1, 2, 3, 4."; - struct nvme_telemetry_log *log = NULL; - int err = 0, output; + + _cleanup_free_ struct nvme_telemetry_log *log = NULL; + _cleanup_nvme_dev_ struct nvme_dev *dev = NULL; + _cleanup_file_ int output = -1; + int err = 0; size_t total_size; __u8 *data_ptr = NULL; int data_written = 0, data_remaining = 0; - struct nvme_dev *dev; struct config { char *file_name; @@ -830,23 +896,20 @@ static int get_telemetry_log(int argc, char **argv, struct command *cmd, .rae = true, }; - OPT_ARGS(opts) = { - OPT_FILE("output-file", 'o', &cfg.file_name, fname), - OPT_UINT("host-generate", 'g', &cfg.host_gen, hgen), - OPT_FLAG("controller-init", 'c', &cfg.ctrl_init, cgen), - OPT_UINT("data-area", 'd', &cfg.data_area, dgen), - OPT_FLAG("rae", 'r', &cfg.rae, rae), - OPT_END() - }; + NVME_ARGS(opts, cfg, + OPT_FILE("output-file", 'o', &cfg.file_name, fname), + OPT_UINT("host-generate", 'g', &cfg.host_gen, hgen), + OPT_FLAG("controller-init", 'c', &cfg.ctrl_init, cgen), + OPT_UINT("data-area", 'd', &cfg.data_area, dgen), + OPT_FLAG("rae", 'r', &cfg.rae, rae)); err = parse_and_open(&dev, argc, argv, desc, opts); if (err) - goto ret; + return err; if (!cfg.file_name) { nvme_show_error("Please provide an output file!"); - err = -EINVAL; - goto close_dev; + return -EINVAL; } cfg.host_gen = !!cfg.host_gen; @@ -854,10 +917,13 @@ static int get_telemetry_log(int argc, char **argv, struct command *cmd, if (output < 0) { nvme_show_error("Failed to open output file %s: %s!", cfg.file_name, strerror(errno)); - err = output; - goto close_dev; + return output; } + log = nvme_alloc(sizeof(*log)); + if (!log) + return -ENOMEM; + if (cfg.ctrl_init) err = __get_telemetry_log_ctrl(dev, cfg.rae, cfg.data_area, &total_size, &log); @@ -870,11 +936,11 @@ static int get_telemetry_log(int argc, char **argv, struct command *cmd, if (err < 0) { nvme_show_error("get-telemetry-log: %s", nvme_strerror(errno)); - goto close_output; + return err; } else if (err > 0) { nvme_show_status(err); fprintf(stderr, "Failed to acquire telemetry log %d!\n", err); - goto close_output; + return err; } data_written = 0; @@ -902,63 +968,54 @@ static int get_telemetry_log(int argc, char **argv, struct command *cmd, return -1; } - free(log); - -close_output: - close(output); -close_dev: - dev_close(dev); -ret: return err; } static int get_endurance_log(int argc, char **argv, struct command *cmd, struct plugin *plugin) { - struct nvme_endurance_group_log endurance_log; const char *desc = "Retrieves endurance groups log page and prints the log."; const char *group_id = "The endurance group identifier"; + + _cleanup_free_ struct nvme_endurance_group_log *endurance_log = NULL; + _cleanup_nvme_dev_ struct nvme_dev *dev = NULL; enum nvme_print_flags flags; - struct nvme_dev *dev; int err; struct config { - char *output_format; __u16 group_id; }; struct config cfg = { - .output_format = "normal", .group_id = 0, }; - OPT_ARGS(opts) = { - OPT_FMT("output-format", 'o', &cfg.output_format, output_format), - OPT_SHRT("group-id", 'g', &cfg.group_id, group_id), - OPT_END() - }; + NVME_ARGS(opts, cfg, + OPT_SHRT("group-id", 'g', &cfg.group_id, group_id)); err = parse_and_open(&dev, argc, argv, desc, opts); if (err) - goto ret; + return err; - err = flags = validate_output_format(cfg.output_format); + err = flags = validate_output_format(output_format_val); if (err < 0) { nvme_show_error("Invalid output format"); - goto close_dev; + return err; } + endurance_log = nvme_alloc(sizeof(*endurance_log)); + if (!endurance_log) + return -ENOMEM; + err = nvme_cli_get_log_endurance_group(dev, cfg.group_id, - &endurance_log); + endurance_log); if (!err) - nvme_show_endurance_log(&endurance_log, cfg.group_id, + nvme_show_endurance_log(endurance_log, cfg.group_id, dev->name, flags); else if (err > 0) nvme_show_status(err); else nvme_show_error("endurance log: %s", nvme_strerror(errno)); -close_dev: - dev_close(dev); -ret: + return err; } @@ -968,7 +1025,7 @@ static int collect_effects_log(struct nvme_dev *dev, enum nvme_csi csi, nvme_effects_log_node_t *node; int err; - node = malloc(sizeof(nvme_effects_log_node_t)); + node = nvme_alloc(sizeof(*node)); if (!node) return -ENOMEM; @@ -986,9 +1043,10 @@ static int collect_effects_log(struct nvme_dev *dev, enum nvme_csi csi, static int get_effects_log(int argc, char **argv, struct command *cmd, struct plugin *plugin) { const char *desc = "Retrieve command effects log page and print the table."; + + _cleanup_nvme_dev_ struct nvme_dev *dev = NULL; struct list_head log_pages; nvme_effects_log_node_t *node; - struct nvme_dev *dev; void *bar = NULL; @@ -996,35 +1054,30 @@ static int get_effects_log(int argc, char **argv, struct command *cmd, struct pl enum nvme_print_flags flags; struct config { - char *output_format; bool human_readable; bool raw_binary; int csi; }; struct config cfg = { - .output_format = "normal", .human_readable = false, .raw_binary = false, .csi = -1, }; - OPT_ARGS(opts) = { - OPT_FMT("output-format", 'o', &cfg.output_format, output_format), - OPT_FLAG("human-readable", 'H', &cfg.human_readable, human_readable_log), - OPT_FLAG("raw-binary", 'b', &cfg.raw_binary, raw_log), - OPT_INT("csi", 'c', &cfg.csi, csi), - OPT_END() - }; + NVME_ARGS(opts, cfg, + OPT_FLAG("human-readable", 'H', &cfg.human_readable, human_readable_log), + OPT_FLAG("raw-binary", 'b', &cfg.raw_binary, raw_log), + OPT_INT("csi", 'c', &cfg.csi, csi)); err = parse_and_open(&dev, argc, argv, desc, opts); if (err) - goto ret; + return err; - err = flags = validate_output_format(cfg.output_format); + err = flags = validate_output_format(output_format_val); if (err < 0) { nvme_show_error("Invalid output format"); - goto close_dev; + return err; } if (cfg.raw_binary) @@ -1056,7 +1109,7 @@ static int get_effects_log(int argc, char **argv, struct command *cmd, struct pl }; err = nvme_get_property(&args); if (err) - goto close_dev; + goto cleanup_list; } if (NVME_CAP_CSS(cap) & NVME_CAP_CSS_NVM) @@ -1077,12 +1130,10 @@ static int get_effects_log(int argc, char **argv, struct command *cmd, struct pl else nvme_show_perror("effects log page"); -close_dev: +cleanup_list: while ((node = list_pop(&log_pages, nvme_effects_log_node_t, node))) free(node); - dev_close(dev); -ret: return err; } @@ -1090,51 +1141,39 @@ static int get_supported_log_pages(int argc, char **argv, struct command *cmd, struct plugin *plugin) { const char *desc = "Retrieve supported logs and print the table."; - struct nvme_supported_log_pages supports; + + _cleanup_free_ struct nvme_supported_log_pages *supports = NULL; + _cleanup_nvme_dev_ struct nvme_dev *dev = NULL; enum nvme_print_flags flags; - struct nvme_dev *dev; int err = -1; - struct config { - char *output_format; - bool verbose; - }; - - struct config cfg = { - .output_format = "normal", - .verbose = false - }; - - OPT_ARGS(opts) = { - OPT_FMT("output-format", 'o', &cfg.output_format, output_format), - OPT_FLAG("verbose", 'v', &cfg.verbose, verbose), - OPT_END() - }; + NVME_ARGS(opts, cfg); err = parse_and_open(&dev, argc, argv, desc, opts); if (err) - goto ret; + return err; - err = flags = validate_output_format(cfg.output_format); + err = flags = validate_output_format(output_format_val); if (err < 0) { nvme_show_error("Invalid output format"); - goto close_dev; + return err; } - if (cfg.verbose) + if (argconfig_parse_seen(opts, "verbose")) flags |= VERBOSE; - err = nvme_cli_get_log_supported_log_pages(dev, false, &supports); + supports = nvme_alloc(sizeof(*supports)); + if (!supports) + return -ENOMEM; + + err = nvme_cli_get_log_supported_log_pages(dev, false, supports); if (!err) - nvme_show_supported_log(&supports, dev->name, flags); + nvme_show_supported_log(supports, dev->name, flags); else if (err > 0) nvme_show_status(err); else nvme_show_error("supported log pages: %s", nvme_strerror(errno)); -close_dev: - dev_close(dev); -ret: return err; } @@ -1145,39 +1184,35 @@ static int get_error_log(int argc, char **argv, struct command *cmd, struct plug "in either decoded format (default) or binary."; const char *log_entries = "number of entries to retrieve"; const char *raw = "dump in binary format"; - struct nvme_error_log_page *err_log; + + _cleanup_free_ struct nvme_error_log_page *err_log = NULL; + _cleanup_nvme_dev_ struct nvme_dev *dev = NULL; struct nvme_id_ctrl ctrl; enum nvme_print_flags flags; - struct nvme_dev *dev; int err = -1; struct config { __u32 log_entries; - char *output_format; bool raw_binary; }; struct config cfg = { .log_entries = 64, - .output_format = "normal", .raw_binary = false, }; - OPT_ARGS(opts) = { - OPT_UINT("log-entries", 'e', &cfg.log_entries, log_entries), - OPT_FMT("output-format", 'o', &cfg.output_format, output_format), - OPT_FLAG("raw-binary", 'b', &cfg.raw_binary, raw), - OPT_END() - }; + NVME_ARGS(opts, cfg, + OPT_UINT("log-entries", 'e', &cfg.log_entries, log_entries), + OPT_FLAG("raw-binary", 'b', &cfg.raw_binary, raw)); err = parse_and_open(&dev, argc, argv, desc, opts); if (err) - goto ret; + return err; - err = flags = validate_output_format(cfg.output_format); + err = flags = validate_output_format(output_format_val); if (err < 0) { nvme_show_error("Invalid output format"); - goto close_dev; + return err; } if (cfg.raw_binary) @@ -1185,26 +1220,22 @@ static int get_error_log(int argc, char **argv, struct command *cmd, struct plug if (!cfg.log_entries) { nvme_show_error("non-zero log-entries is required param"); - err = -1; - goto close_dev; + return -1; } err = nvme_cli_identify_ctrl(dev, &ctrl); if (err < 0) { nvme_show_perror("identify controller"); - goto close_dev; + return err; } else if (err) { nvme_show_error("could not identify controller"); - err = -1; - goto close_dev; + return err; } cfg.log_entries = min(cfg.log_entries, ctrl.elpe + 1); - err_log = calloc(cfg.log_entries, sizeof(struct nvme_error_log_page)); - if (!err_log) { - err = -1; - goto close_dev; - } + err_log = nvme_alloc(cfg.log_entries * sizeof(struct nvme_error_log_page)); + if (!err_log) + return -ENOMEM; err = nvme_cli_get_log_error(dev, cfg.log_entries, false, err_log); if (!err) @@ -1215,11 +1246,6 @@ static int get_error_log(int argc, char **argv, struct command *cmd, struct plug else nvme_show_perror("error log"); - free(err_log); - -close_dev: - dev_close(dev); -ret: return err; } @@ -1227,50 +1253,48 @@ static int get_fw_log(int argc, char **argv, struct command *cmd, struct plugin { const char *desc = "Retrieve the firmware log for the\n" "specified device in either decoded format (default) or binary."; - struct nvme_firmware_slot fw_log; + + _cleanup_free_ struct nvme_firmware_slot *fw_log = NULL;; + _cleanup_nvme_dev_ struct nvme_dev *dev = NULL; enum nvme_print_flags flags; - struct nvme_dev *dev; int err; struct config { - char *output_format; bool raw_binary; }; struct config cfg = { - .output_format = "normal", .raw_binary = false, }; - OPT_ARGS(opts) = { - OPT_FMT("output-format", 'o', &cfg.output_format, output_format), - OPT_FLAG("raw-binary", 'b', &cfg.raw_binary, raw_use), - OPT_END() - }; + NVME_ARGS(opts, cfg, + OPT_FLAG("raw-binary", 'b', &cfg.raw_binary, raw_use)); err = parse_and_open(&dev, argc, argv, desc, opts); if (err) - goto ret; + return err; - err = flags = validate_output_format(cfg.output_format); + err = flags = validate_output_format(output_format_val); if (err < 0) { nvme_show_error("Invalid output format"); - goto close_dev; + return err; } if (cfg.raw_binary) flags = BINARY; - err = nvme_cli_get_log_fw_slot(dev, false, &fw_log); + fw_log = nvme_alloc(sizeof(*fw_log)); + if (!fw_log) + return -ENOMEM; + + err = nvme_cli_get_log_fw_slot(dev, false, fw_log); if (!err) - nvme_show_fw_log(&fw_log, dev->name, flags); + nvme_show_fw_log(fw_log, dev->name, flags); else if (err > 0) nvme_show_status(err); else nvme_show_error("fw log: %s", nvme_strerror(errno)); -close_dev: - dev_close(dev); -ret: + return err; } @@ -1278,52 +1302,50 @@ static int get_changed_ns_list_log(int argc, char **argv, struct command *cmd, s { const char *desc = "Retrieve Changed Namespaces log for the given device\n" "in either decoded format (default) or binary."; - struct nvme_ns_list changed_ns_list_log; + + _cleanup_free_ struct nvme_ns_list *changed_ns_list_log = NULL; + _cleanup_nvme_dev_ struct nvme_dev *dev = NULL; enum nvme_print_flags flags; - struct nvme_dev *dev; int err; struct config { - char *output_format; bool raw_binary; }; struct config cfg = { - .output_format = "normal", .raw_binary = false, }; - OPT_ARGS(opts) = { - OPT_FMT("output-format", 'o', &cfg.output_format, output_format), - OPT_FLAG("raw-binary", 'b', &cfg.raw_binary, raw_output), - OPT_END() - }; + NVME_ARGS(opts, cfg, + OPT_FLAG("raw-binary", 'b', &cfg.raw_binary, raw_output)); err = parse_and_open(&dev, argc, argv, desc, opts); if (err) - goto ret; + return err; - err = flags = validate_output_format(cfg.output_format); + err = flags = validate_output_format(output_format_val); if (err < 0) { nvme_show_error("Invalid output format"); - goto close_dev; + return err; } if (cfg.raw_binary) flags = BINARY; + changed_ns_list_log = nvme_alloc(sizeof(*changed_ns_list_log)); + if (!changed_ns_list_log) + return -ENOMEM; + err = nvme_cli_get_log_changed_ns_list(dev, true, - &changed_ns_list_log); + changed_ns_list_log); if (!err) - nvme_show_changed_ns_list_log(&changed_ns_list_log, + nvme_show_changed_ns_list_log(changed_ns_list_log, dev->name, flags); else if (err > 0) nvme_show_status(err); else nvme_show_error("changed ns list log: %s", nvme_strerror(errno)); -close_dev: - dev_close(dev); -ret: + return err; } @@ -1334,56 +1356,53 @@ static int get_pred_lat_per_nvmset_log(int argc, char **argv, "page and prints it for the given device in either decoded\n" "format(default),json or binary."; const char *nvmset_id = "NVM Set Identifier"; - struct nvme_nvmset_predictable_lat_log plpns_log; + + _cleanup_free_ struct nvme_nvmset_predictable_lat_log *plpns_log = NULL; + _cleanup_nvme_dev_ struct nvme_dev *dev = NULL; enum nvme_print_flags flags; - struct nvme_dev *dev; int err; struct config { __u16 nvmset_id; - char *output_format; bool raw_binary; }; struct config cfg = { .nvmset_id = 1, - .output_format = "normal", .raw_binary = false, }; - OPT_ARGS(opts) = { - OPT_SHRT("nvmset-id", 'i', &cfg.nvmset_id, nvmset_id), - OPT_FMT("output-format", 'o', &cfg.output_format, output_format), - OPT_FLAG("raw-binary", 'b', &cfg.raw_binary, raw_use), - OPT_END() - }; + NVME_ARGS(opts, cfg, + OPT_SHRT("nvmset-id", 'i', &cfg.nvmset_id, nvmset_id), + OPT_FLAG("raw-binary", 'b', &cfg.raw_binary, raw_use)); err = parse_and_open(&dev, argc, argv, desc, opts); if (err) - goto ret; + return err; - err = flags = validate_output_format(cfg.output_format); + err = flags = validate_output_format(output_format_val); if (err < 0) { nvme_show_error("Invalid output format"); - goto close_dev; + return err; } if (cfg.raw_binary) flags = BINARY; + plpns_log = nvme_alloc(sizeof(*plpns_log)); + if (!plpns_log) + return -ENOMEM; + err = nvme_cli_get_log_predictable_lat_nvmset(dev, cfg.nvmset_id, - &plpns_log); + plpns_log); if (!err) - nvme_show_predictable_latency_per_nvmset(&plpns_log, cfg.nvmset_id, dev->name, + nvme_show_predictable_latency_per_nvmset(plpns_log, cfg.nvmset_id, dev->name, flags); else if (err > 0) nvme_show_status(err); else nvme_show_error("predictable latency per nvm set: %s", nvme_strerror(errno)); -close_dev: - dev_close(dev); -ret: return err; } @@ -1394,43 +1413,39 @@ static int get_pred_lat_event_agg_log(int argc, char **argv, "Aggregate Log page and prints it, for the given\n" "device in either decoded format(default), json or binary."; const char *log_entries = "Number of pending NVM Set log Entries list"; + + _cleanup_nvme_dev_ struct nvme_dev *dev = NULL; + _cleanup_free_ struct nvme_id_ctrl *ctrl = NULL; + _cleanup_free_ void *pea_log = NULL; enum nvme_print_flags flags; - struct nvme_id_ctrl ctrl; - struct nvme_dev *dev; __u32 log_size; - void *pea_log; int err; struct config { __u64 log_entries; bool rae; - char *output_format; bool raw_binary; }; struct config cfg = { .log_entries = 2044, .rae = false, - .output_format = "normal", .raw_binary = false, }; - OPT_ARGS(opts) = { - OPT_UINT("log-entries", 'e', &cfg.log_entries, log_entries), - OPT_FLAG("rae", 'r', &cfg.rae, rae), - OPT_FMT("output-format", 'o', &cfg.output_format, output_format), - OPT_FLAG("raw-binary", 'b', &cfg.raw_binary, raw_use), - OPT_END() - }; + NVME_ARGS(opts, cfg, + OPT_UINT("log-entries", 'e', &cfg.log_entries, log_entries), + OPT_FLAG("rae", 'r', &cfg.rae, rae), + OPT_FLAG("raw-binary", 'b', &cfg.raw_binary, raw_use)); err = parse_and_open(&dev, argc, argv, desc, opts); if (err) - goto ret; + return err; - err = flags = validate_output_format(cfg.output_format); + err = flags = validate_output_format(output_format_val); if (err < 0) { nvme_show_error("Invalid output format"); - goto close_dev; + return err; } if (cfg.raw_binary) @@ -1438,26 +1453,28 @@ static int get_pred_lat_event_agg_log(int argc, char **argv, if (!cfg.log_entries) { nvme_show_error("non-zero log-entries is required param"); - err = -EINVAL; - goto close_dev; + return -EINVAL; } - err = nvme_cli_identify_ctrl(dev, &ctrl); + ctrl = nvme_alloc(sizeof(*ctrl)); + if (!ctrl) + return -ENOMEM; + + err = nvme_cli_identify_ctrl(dev, ctrl); if (err < 0) { nvme_show_error("identify controller: %s", nvme_strerror(errno)); - goto close_dev; + return err; } else if (err) { nvme_show_status(err); - goto close_dev; + return err; } - cfg.log_entries = min(cfg.log_entries, le32_to_cpu(ctrl.nsetidmax)); + cfg.log_entries = min(cfg.log_entries, le32_to_cpu(ctrl->nsetidmax)); log_size = sizeof(__u64) + cfg.log_entries * sizeof(__u16); - pea_log = calloc(log_size, 1); - if (!pea_log) { - err = -ENOMEM; - goto close_dev; - } + + pea_log = nvme_alloc(log_size); + if (!pea_log) + return -ENOMEM; err = nvme_cli_get_log_predictable_lat_event(dev, cfg.rae, 0, log_size, pea_log); @@ -1469,11 +1486,7 @@ static int get_pred_lat_event_agg_log(int argc, char **argv, else nvme_show_error("predictable latency event aggregate log page: %s", nvme_strerror(errno)); - free(pea_log); -close_dev: - dev_close(dev); -ret: return err; } @@ -1485,74 +1498,69 @@ static int get_persistent_event_log(int argc, char **argv, const char *action = "action the controller shall take during\n" "processing this persistent log page command."; const char *log_len = "number of bytes to retrieve"; - struct nvme_persistent_event_log *pevent, *pevent_collected; + + _cleanup_free_ struct nvme_persistent_event_log *pevent_collected = NULL; + _cleanup_free_ struct nvme_persistent_event_log *pevent = NULL; + _cleanup_nvme_dev_ struct nvme_dev *dev = NULL; enum nvme_print_flags flags; void *pevent_log_info; - struct nvme_dev *dev; bool huge; int err; struct config { __u8 action; __u32 log_len; - char *output_format; bool raw_binary; }; struct config cfg = { .action = 0xff, .log_len = 0, - .output_format = "normal", .raw_binary = false, }; - OPT_ARGS(opts) = { - OPT_BYTE("action", 'a', &cfg.action, action), - OPT_UINT("log_len", 'l', &cfg.log_len, log_len), - OPT_FMT("output-format", 'o', &cfg.output_format, output_format), - OPT_FLAG("raw-binary", 'b', &cfg.raw_binary, raw_use), - OPT_END() - }; + NVME_ARGS(opts, cfg, + OPT_BYTE("action", 'a', &cfg.action, action), + OPT_UINT("log_len", 'l', &cfg.log_len, log_len), + OPT_FLAG("raw-binary", 'b', &cfg.raw_binary, raw_use)); err = parse_and_open(&dev, argc, argv, desc, opts); if (err) - goto ret; + return err; - err = flags = validate_output_format(cfg.output_format); + err = flags = validate_output_format(output_format_val); if (err < 0) { nvme_show_error("Invalid output format"); - goto close_dev; + return err; } if (cfg.raw_binary) flags = BINARY; - pevent = calloc(sizeof(*pevent), 1); - if (!pevent) { - err = -ENOMEM; - goto close_dev; - } + pevent = nvme_alloc(sizeof(*pevent)); + if (!pevent) + return -ENOMEM; err = nvme_cli_get_log_persistent_event(dev, cfg.action, sizeof(*pevent), pevent); if (err < 0) { nvme_show_error("persistent event log: %s", nvme_strerror(errno)); - goto free_pevent; + return err; } else if (err) { nvme_show_status(err); - goto free_pevent; + return err; } if (cfg.action == NVME_PEVENT_LOG_RELEASE_CTX) { printf("Releasing Persistent Event Log Context\n"); - goto free_pevent; + return 0; } if (!cfg.log_len && cfg.action != NVME_PEVENT_LOG_EST_CTX_AND_READ) { cfg.log_len = le64_to_cpu(pevent->tll); } else if (!cfg.log_len && cfg.action == NVME_PEVENT_LOG_EST_CTX_AND_READ) { printf("Establishing Persistent Event Log Context\n"); - goto free_pevent; + return 0; } /* @@ -1565,11 +1573,9 @@ static int get_persistent_event_log(int argc, char **argv, if (cfg.action == NVME_PEVENT_LOG_EST_CTX_AND_READ) cfg.action = NVME_PEVENT_LOG_READ; - pevent_log_info = nvme_alloc(cfg.log_len, &huge); - if (!pevent_log_info) { - err = -ENOMEM; - goto free_pevent; - } + pevent_log_info = nvme_alloc_huge(cfg.log_len, &huge); + if (!pevent_log_info) + return -ENOMEM; err = nvme_cli_get_log_persistent_event(dev, cfg.action, cfg.log_len, pevent_log_info); if (!err) { @@ -1599,12 +1605,7 @@ static int get_persistent_event_log(int argc, char **argv, } free: - nvme_free(pevent_log_info, huge); -free_pevent: - free(pevent); -close_dev: - dev_close(dev); -ret: + nvme_free_huge(pevent_log_info, huge); return err; } @@ -1615,43 +1616,39 @@ static int get_endurance_event_agg_log(int argc, char **argv, "Event Aggregate page and prints it, for the given\n" "device in either decoded format(default), json or binary."; const char *log_entries = "Number of pending Endurance Group Event log Entries list"; - void *endurance_log; - struct nvme_id_ctrl ctrl; + + _cleanup_nvme_dev_ struct nvme_dev *dev = NULL; + _cleanup_free_ struct nvme_id_ctrl *ctrl = NULL; + _cleanup_free_ void *endurance_log = NULL; enum nvme_print_flags flags; - struct nvme_dev *dev; __u32 log_size; int err; struct config { __u64 log_entries; bool rae; - char *output_format; bool raw_binary; }; struct config cfg = { .log_entries = 2044, .rae = false, - .output_format = "normal", .raw_binary = false, }; - OPT_ARGS(opts) = { - OPT_UINT("log-entries", 'e', &cfg.log_entries, log_entries), - OPT_FLAG("rae", 'r', &cfg.rae, rae), - OPT_FMT("output-format", 'o', &cfg.output_format, output_format), - OPT_FLAG("raw-binary", 'b', &cfg.raw_binary, raw_use), - OPT_END() - }; + NVME_ARGS(opts, cfg, + OPT_UINT("log-entries", 'e', &cfg.log_entries, log_entries), + OPT_FLAG("rae", 'r', &cfg.rae, rae), + OPT_FLAG("raw-binary", 'b', &cfg.raw_binary, raw_use)); err = parse_and_open(&dev, argc, argv, desc, opts); if (err) - goto ret; + return err; - err = flags = validate_output_format(cfg.output_format); + err = flags = validate_output_format(output_format_val); if (err < 0) { nvme_show_error("Invalid output format"); - goto close_dev; + return err; } if (cfg.raw_binary) @@ -1659,27 +1656,28 @@ static int get_endurance_event_agg_log(int argc, char **argv, if (!cfg.log_entries) { nvme_show_error("non-zero log-entries is required param"); - err = -EINVAL; - goto close_dev; + return -EINVAL; } - err = nvme_cli_identify_ctrl(dev, &ctrl); + ctrl = nvme_alloc(sizeof(*ctrl)); + if (!ctrl) + return -ENOMEM; + + err = nvme_cli_identify_ctrl(dev, ctrl); if (err < 0) { nvme_show_error("identify controller: %s", nvme_strerror(errno)); - goto close_dev; + return err; } else if (err) { nvme_show_error("could not identify controller"); - err = -ENODEV; - goto close_dev; + return -ENODEV; } - cfg.log_entries = min(cfg.log_entries, le16_to_cpu(ctrl.endgidmax)); + cfg.log_entries = min(cfg.log_entries, le16_to_cpu(ctrl->endgidmax)); log_size = sizeof(__u64) + cfg.log_entries * sizeof(__u16); - endurance_log = calloc(log_size, 1); - if (!endurance_log) { - err = -ENOMEM; - goto close_dev; - } + + endurance_log = nvme_alloc(log_size); + if (!endurance_log) + return -ENOMEM; err = nvme_cli_get_log_endurance_grp_evt(dev, cfg.rae, 0, log_size, endurance_log); @@ -1691,11 +1689,7 @@ static int get_endurance_event_agg_log(int argc, char **argv, else nvme_show_error("endurance group event aggregate log page: %s", nvme_strerror(errno)); - free(endurance_log); -close_dev: - dev_close(dev); -ret: return err; } @@ -1704,66 +1698,56 @@ static int get_lba_status_log(int argc, char **argv, { const char *desc = "Retrieve Get LBA Status Info Log and prints it,\n" "for the given device in either decoded format(default),json or binary."; - void *lab_status; + + _cleanup_nvme_dev_ struct nvme_dev *dev = NULL; + _cleanup_free_ void *lba_status = NULL; enum nvme_print_flags flags; - struct nvme_dev *dev; __u32 lslplen; int err; struct config { bool rae; - char *output_format; }; struct config cfg = { .rae = false, - .output_format = "normal", }; - OPT_ARGS(opts) = { - OPT_FLAG("rae", 'r', &cfg.rae, rae), - OPT_FMT("output-format", 'o', &cfg.output_format, output_format), - OPT_END() - }; + NVME_ARGS(opts, cfg, + OPT_FLAG("rae", 'r', &cfg.rae, rae)); err = parse_and_open(&dev, argc, argv, desc, opts); if (err) - goto ret; + return err; - err = flags = validate_output_format(cfg.output_format); + err = flags = validate_output_format(output_format_val); if (err < 0) { nvme_show_error("Invalid output format"); - goto close_dev; + return err; } err = nvme_cli_get_log_lba_status(dev, true, 0, sizeof(__u32), &lslplen); if (err < 0) { nvme_show_error("lba status log page: %s", nvme_strerror(errno)); - goto close_dev; + return err; } else if (err) { nvme_show_status(err); - goto close_dev; + return err; } - lab_status = calloc(lslplen, 1); - if (!lab_status) { - err = -ENOMEM; - goto close_dev; - } + lba_status = nvme_alloc(lslplen); + if (!lba_status) + return -ENOMEM; - err = nvme_cli_get_log_lba_status(dev, cfg.rae, 0, lslplen, lab_status); + err = nvme_cli_get_log_lba_status(dev, cfg.rae, 0, lslplen, lba_status); if (!err) - nvme_show_lba_status_log(lab_status, lslplen, dev->name, flags); + nvme_show_lba_status_log(lba_status, lslplen, dev->name, flags); else if (err > 0) nvme_show_status(err); else nvme_show_error("lba status log page: %s", nvme_strerror(errno)); - free(lab_status); -close_dev: - dev_close(dev); -ret: return err; } @@ -1774,45 +1758,36 @@ static int get_resv_notif_log(int argc, char **argv, const char *desc = "Retrieve Reservation Notification\n" "log page and prints it, for the given\n" "device in either decoded format(default), json or binary."; - struct nvme_resv_notification_log resv; + + _cleanup_free_ struct nvme_resv_notification_log *resv = NULL; + _cleanup_nvme_dev_ struct nvme_dev *dev = NULL; enum nvme_print_flags flags; - struct nvme_dev *dev; int err; - struct config { - char *output_format; - }; - - struct config cfg = { - .output_format = "normal", - }; - - OPT_ARGS(opts) = { - OPT_FMT("output-format", 'o', &cfg.output_format, output_format), - OPT_END() - }; + NVME_ARGS(opts, cfg); err = parse_and_open(&dev, argc, argv, desc, opts); if (err) - goto ret; + return err; - err = flags = validate_output_format(cfg.output_format); + err = flags = validate_output_format(output_format_val); if (err < 0) { nvme_show_error("Invalid output format"); - goto close_dev; + return err; } - err = nvme_cli_get_log_reservation(dev, false, &resv); + resv = nvme_alloc(sizeof(*resv)); + if (!resv) + return -ENOMEM; + + err = nvme_cli_get_log_reservation(dev, false, resv); if (!err) - nvme_show_resv_notif_log(&resv, dev->name, flags); + nvme_show_resv_notif_log(resv, dev->name, flags); else if (err > 0) nvme_show_status(err); else nvme_show_error("resv notifi log: %s", nvme_strerror(errno)); -close_dev: - dev_close(dev); -ret: return err; } @@ -1823,103 +1798,190 @@ static int get_boot_part_log(int argc, char **argv, struct command *cmd, struct "log page and prints it, for the given\n" "device in either decoded format(default), json or binary."; const char *fname = "boot partition data output file name"; - struct nvme_boot_partition boot; - __u8 *bp_log; + + _cleanup_nvme_dev_ struct nvme_dev *dev = NULL; + _cleanup_free_ struct nvme_boot_partition *boot = NULL; + _cleanup_free_ __u8 *bp_log = NULL; enum nvme_print_flags flags; - int err = -1, output = 0; - struct nvme_dev *dev; + int err = -1; + _cleanup_file_ int output = -1; __u32 bpsz = 0; struct config { __u8 lsp; char *file_name; - char *output_format; }; struct config cfg = { .lsp = 0, - .output_format = "normal", .file_name = NULL, }; - OPT_ARGS(opts) = { - OPT_BYTE("lsp", 's', &cfg.lsp, lsp), - OPT_FILE("output-file", 'f', &cfg.file_name, fname), - OPT_FMT("output-format", 'o', &cfg.output_format, output_format), - OPT_END() - }; + NVME_ARGS(opts, cfg, + OPT_BYTE("lsp", 's', &cfg.lsp, lsp), + OPT_FILE("output-file", 'f', &cfg.file_name, fname)); err = parse_and_open(&dev, argc, argv, desc, opts); if (err) - goto ret; + return err; - err = flags = validate_output_format(cfg.output_format); + err = flags = validate_output_format(output_format_val); if (err < 0) { nvme_show_error("Invalid output format"); - goto close_dev; + return err; } if (!cfg.file_name) { nvme_show_error("Please provide an output file!"); - err = -1; - goto close_dev; + return -1; } if (cfg.lsp > 127) { nvme_show_error("invalid lsp param: %u", cfg.lsp); - err = -1; - goto close_dev; + return -1; } output = open(cfg.file_name, O_WRONLY | O_CREAT | O_TRUNC, 0666); if (output < 0) { nvme_show_error("Failed to open output file %s: %s!", cfg.file_name, strerror(errno)); - err = output; - goto close_dev; + return output; } + boot = nvme_alloc(sizeof(*boot)); + if (!boot) + return -ENOMEM; + err = nvme_cli_get_log_boot_partition(dev, false, cfg.lsp, - sizeof(boot), &boot); + sizeof(*boot), boot); if (err < 0) { nvme_show_error("boot partition log: %s", nvme_strerror(errno)); - goto close_output; + return err; } else if (err) { nvme_show_status(err); - goto close_output; + return err; } - bpsz = (boot.bpinfo & 0x7fff) * 128 * 1024; - bp_log = calloc(sizeof(boot) + bpsz, 1); - if (!bp_log) { - err = -1; - goto close_output; - } + bpsz = (boot->bpinfo & 0x7fff) * 128 * 1024; + bp_log = nvme_alloc(sizeof(*boot) + bpsz); + if (!bp_log) + return -ENOMEM; err = nvme_cli_get_log_boot_partition(dev, false, cfg.lsp, - sizeof(boot) + bpsz, + sizeof(*boot) + bpsz, (struct nvme_boot_partition *)bp_log); if (!err) - nvme_show_boot_part_log(&bp_log, dev->name, flags, sizeof(boot) + bpsz); + nvme_show_boot_part_log(&bp_log, dev->name, sizeof(*boot) + bpsz, flags); else if (err > 0) nvme_show_status(err); else nvme_show_error("boot partition log: %s", nvme_strerror(errno)); - err = write(output, (void *) bp_log + sizeof(boot), bpsz); + err = write(output, (void *) bp_log + sizeof(*boot), bpsz); if (err != bpsz) fprintf(stderr, "Failed to flush all data to file!\n"); else printf("Data flushed into file %s\n", cfg.file_name); err = 0; - free(bp_log); + return err; +} + +static int get_phy_rx_eom_log(int argc, char **argv, struct command *cmd, + struct plugin *plugin) +{ + const char *desc = "Retrieve Physical Interface Receiver Eye Opening\n" + "Measurement log for the given device in decoded format\n" + "(default), json or binary."; + const char *controller = "Target Controller ID."; + _cleanup_free_ struct nvme_phy_rx_eom_log *phy_rx_eom_log = NULL; + size_t phy_rx_eom_log_len; + enum nvme_print_flags flags; + _cleanup_nvme_dev_ struct nvme_dev *dev = NULL; + int err = -1; + __u8 lsp_tmp; + + struct config { + __u8 lsp; + __u16 controller; + char *output_format; + }; + + struct config cfg = { + .lsp = 0, + .controller = NVME_LOG_LSI_NONE, + .output_format = "normal", + }; + + OPT_ARGS(opts) = { + OPT_BYTE("lsp", 's', &cfg.lsp, lsp), + OPT_SHRT("controller", 'c', &cfg.controller, controller), + OPT_FMT("output-format", 'o', &cfg.output_format, output_format), + OPT_END() + }; + + err = parse_and_open(&dev, argc, argv, desc, opts); + if (err) + return err; + + err = flags = validate_output_format(cfg.output_format); + if (err < 0) { + nvme_show_error("Invalid output format"); + return err; + } + + if (cfg.lsp > 127) { + nvme_show_error("invalid lsp param: %u", cfg.lsp); + return -1; + } else if ((cfg.lsp & 3) == 3) { + nvme_show_error("invalid measurement quality: %u", cfg.lsp & 3); + return -1; + } else if ((cfg.lsp & 12) == 12) { + nvme_show_error("invalid action: %u", cfg.lsp & 12); + return -1; + } + + /* Fetching header to calculate total log length */ + phy_rx_eom_log_len = sizeof(struct nvme_phy_rx_eom_log); + phy_rx_eom_log = nvme_alloc(phy_rx_eom_log_len); + if (!phy_rx_eom_log) + return -ENOMEM; + + /* Just read measurement, take given action when fetching full log */ + lsp_tmp = cfg.lsp & 0xf3; + + err = nvme_cli_get_log_phy_rx_eom(dev, lsp_tmp, cfg.controller, + phy_rx_eom_log_len, phy_rx_eom_log); + if (err) { + if (err > 0) + nvme_show_status(err); + else + nvme_show_error("phy-rx-eom-log: %s", nvme_strerror(errno)); + + return err; + } + + if (phy_rx_eom_log->eomip == NVME_PHY_RX_EOM_COMPLETED) { + phy_rx_eom_log_len = le16_to_cpu(phy_rx_eom_log->hsize) + + le32_to_cpu(phy_rx_eom_log->dsize) * + le16_to_cpu(phy_rx_eom_log->nd); + } else { + phy_rx_eom_log_len = le16_to_cpu(phy_rx_eom_log->hsize); + } + + phy_rx_eom_log = nvme_realloc(phy_rx_eom_log, phy_rx_eom_log_len); + if (!phy_rx_eom_log) + return -ENOMEM; + + err = nvme_cli_get_log_phy_rx_eom(dev, cfg.lsp, cfg.controller, + phy_rx_eom_log_len, phy_rx_eom_log); + if (!err) + nvme_show_phy_rx_eom_log(phy_rx_eom_log, cfg.controller, flags); + else if (err > 0) + nvme_show_status(err); + else + nvme_show_error("phy-rx-eom-log: %s", nvme_strerror(errno)); -close_output: - close(output); -close_dev: - dev_close(dev); -ret: return err; } @@ -1927,54 +1989,51 @@ static int get_media_unit_stat_log(int argc, char **argv, struct command *cmd, struct plugin *plugin) { const char *desc = "Retrieve the configuration and wear of media units and print it"; - struct nvme_media_unit_stat_log mus; + + _cleanup_free_ struct nvme_media_unit_stat_log *mus = NULL; + _cleanup_nvme_dev_ struct nvme_dev *dev = NULL; enum nvme_print_flags flags; - struct nvme_dev *dev; int err = -1; struct config { __u16 domainid; - char *output_format; bool raw_binary; }; struct config cfg = { .domainid = 0, - .output_format = "normal", .raw_binary = false, }; - OPT_ARGS(opts) = { - OPT_UINT("domain-id", 'd', &cfg.domainid, domainid), - OPT_FMT("output-format", 'o', &cfg.output_format, output_format), - OPT_FLAG("raw-binary", 'b', &cfg.raw_binary, raw_use), - OPT_END() - }; + NVME_ARGS(opts, cfg, + OPT_UINT("domain-id", 'd', &cfg.domainid, domainid), + OPT_FLAG("raw-binary", 'b', &cfg.raw_binary, raw_use)); err = parse_and_open(&dev, argc, argv, desc, opts); if (err) - goto ret; + return err; - err = flags = validate_output_format(cfg.output_format); + err = flags = validate_output_format(output_format_val); if (err < 0) { nvme_show_error("Invalid output format"); - goto close_dev; + return err; } if (cfg.raw_binary) flags = BINARY; - err = nvme_cli_get_log_media_unit_stat(dev, cfg.domainid, &mus); + mus = nvme_alloc(sizeof(*mus)); + if (!mus) + return -ENOMEM; + + err = nvme_cli_get_log_media_unit_stat(dev, cfg.domainid, mus); if (!err) - nvme_show_media_unit_stat_log(&mus, flags); + nvme_show_media_unit_stat_log(mus, flags); else if (err > 0) nvme_show_status(err); else nvme_show_error("media unit status log: %s", nvme_strerror(errno)); -close_dev: - dev_close(dev); -ret: return err; } @@ -1982,55 +2041,52 @@ static int get_supp_cap_config_log(int argc, char **argv, struct command *cmd, struct plugin *plugin) { const char *desc = "Retrieve the list of Supported Capacity Configuration Descriptors"; - struct nvme_supported_cap_config_list_log cap_log; + + _cleanup_free_ struct nvme_supported_cap_config_list_log *cap_log = NULL; + _cleanup_nvme_dev_ struct nvme_dev *dev = NULL; enum nvme_print_flags flags; - struct nvme_dev *dev; int err = -1; struct config { __u16 domainid; - char *output_format; bool raw_binary; }; struct config cfg = { .domainid = 0, - .output_format = "normal", .raw_binary = false, }; - OPT_ARGS(opts) = { - OPT_UINT("domain-id", 'd', &cfg.domainid, domainid), - OPT_FMT("output-format", 'o', &cfg.output_format, output_format), - OPT_FLAG("raw-binary", 'b', &cfg.raw_binary, raw_use), - OPT_END() - }; + NVME_ARGS(opts, cfg, + OPT_UINT("domain-id", 'd', &cfg.domainid, domainid), + OPT_FLAG("raw-binary", 'b', &cfg.raw_binary, raw_use)); err = parse_and_open(&dev, argc, argv, desc, opts); if (err) - goto ret; + return err; - err = flags = validate_output_format(cfg.output_format); + err = flags = validate_output_format(output_format_val); if (err < 0) { nvme_show_error("Invalid output format"); - goto close_dev; + return err; } if (cfg.raw_binary) flags = BINARY; + cap_log = nvme_alloc(sizeof(*cap_log)); + if (!cap_log) + return -ENOMEM; + err = nvme_cli_get_log_support_cap_config_list(dev, cfg.domainid, - &cap_log); + cap_log); if (!err) - nvme_show_supported_cap_config_log(&cap_log, flags); + nvme_show_supported_cap_config_log(cap_log, flags); else if (err > 0) nvme_show_status(err); else nvme_show_perror("supported capacity configuration list log"); -close_dev: - dev_close(dev); -ret: return err; } @@ -2039,8 +2095,8 @@ static int io_mgmt_send(int argc, char **argv, struct command *cmd, struct plugi const char *desc = "I/O Management Send"; const char *data = "optional file for data (default stdin)"; - struct nvme_dev *dev; - void *buf = NULL; + _cleanup_nvme_dev_ struct nvme_dev *dev = NULL; + _cleanup_free_ void *buf = NULL; int err = -1; int dfd = STDIN_FILENO; @@ -2056,41 +2112,36 @@ static int io_mgmt_send(int argc, char **argv, struct command *cmd, struct plugi .mos = 0, }; - OPT_ARGS(opts) = { - OPT_UINT("namespace-id", 'n', &cfg.namespace_id, namespace_id_desired), - OPT_SHRT("mos", 's', &cfg.mos, mos), - OPT_BYTE("mo", 'm', &cfg.mo, mo), - OPT_FILE("data", 'd', &cfg.file, data), - OPT_UINT("data-len", 'l', &cfg.data_len, buf_len), - OPT_END() - }; + NVME_ARGS(opts, cfg, + OPT_UINT("namespace-id", 'n', &cfg.namespace_id, namespace_id_desired), + OPT_SHRT("mos", 's', &cfg.mos, mos), + OPT_BYTE("mo", 'm', &cfg.mo, mo), + OPT_FILE("data", 'd', &cfg.file, data), + OPT_UINT("data-len", 'l', &cfg.data_len, buf_len)); err = parse_and_open(&dev, argc, argv, desc, opts); if (err) - return errno; + return err; if (!cfg.namespace_id) { err = nvme_get_nsid(dev_fd(dev), &cfg.namespace_id); if (err < 0) { nvme_show_perror("get-namespace-id"); - goto close_dev; + return err; } } if (cfg.data_len) { - buf = calloc(1, cfg.data_len); - if (!buf) { - nvme_show_perror("could not alloc memory for io mgmt receive data"); - err = -ENOMEM; - goto close_dev; - } + buf = nvme_alloc(cfg.data_len); + if (!buf) + return -ENOMEM; } if (cfg.file) { dfd = open(cfg.file, O_RDONLY); if (dfd < 0) { nvme_show_perror(cfg.file); - goto free; + return -errno; } } @@ -2123,10 +2174,6 @@ static int io_mgmt_send(int argc, char **argv, struct command *cmd, struct plugi close_fd: if (cfg.file) close(dfd); -free: - free(buf); -close_dev: - dev_close(dev); return err; } @@ -2135,10 +2182,10 @@ static int io_mgmt_recv(int argc, char **argv, struct command *cmd, struct plugi const char *desc = "I/O Management Receive"; const char *data = "optional file for data (default stdout)"; - struct nvme_dev *dev; - void *buf = NULL; + _cleanup_nvme_dev_ struct nvme_dev *dev = NULL; + _cleanup_free_ void *buf = NULL; int err = -1; - int dfd = STDOUT_FILENO; + _cleanup_file_ int dfd = -1; struct config { __u16 mos; @@ -2152,34 +2199,29 @@ static int io_mgmt_recv(int argc, char **argv, struct command *cmd, struct plugi .mos = 0, }; - OPT_ARGS(opts) = { - OPT_UINT("namespace-id", 'n', &cfg.namespace_id, namespace_id_desired), - OPT_SHRT("mos", 's', &cfg.mos, mos), - OPT_BYTE("mo", 'm', &cfg.mo, mo), - OPT_FILE("data", 'd', &cfg.file, data), - OPT_UINT("data-len", 'l', &cfg.data_len, buf_len), - OPT_END() - }; + NVME_ARGS(opts, cfg, + OPT_UINT("namespace-id", 'n', &cfg.namespace_id, namespace_id_desired), + OPT_SHRT("mos", 's', &cfg.mos, mos), + OPT_BYTE("mo", 'm', &cfg.mo, mo), + OPT_FILE("data", 'd', &cfg.file, data), + OPT_UINT("data-len", 'l', &cfg.data_len, buf_len)); err = parse_and_open(&dev, argc, argv, desc, opts); if (err) - return errno; + return err; if (!cfg.namespace_id) { err = nvme_get_nsid(dev_fd(dev), &cfg.namespace_id); if (err < 0) { nvme_show_perror("get-namespace-id"); - goto close_dev; + return err; } } if (cfg.data_len) { - buf = calloc(1, cfg.data_len); - if (!buf) { - nvme_show_perror("could not alloc memory for io mgmt receive data"); - err = -ENOMEM; - goto close_dev; - } + buf = nvme_alloc(cfg.data_len); + if (!buf) + return -ENOMEM; } struct nvme_io_mgmt_recv_args args = { @@ -2202,13 +2244,13 @@ static int io_mgmt_recv(int argc, char **argv, struct command *cmd, struct plugi dfd = open(cfg.file, O_WRONLY | O_CREAT, 0644); if (dfd < 0) { nvme_show_perror(cfg.file); - goto free; + return -errno; } err = write(dfd, buf, cfg.data_len); if (err < 0) { nvme_show_perror("write"); - goto close_fd; + return -errno; } } else { d((unsigned char *)buf, cfg.data_len, 16, 1); @@ -2219,14 +2261,6 @@ static int io_mgmt_recv(int argc, char **argv, struct command *cmd, struct plugi nvme_show_perror("io-mgmt-recv"); } -close_fd: - if (cfg.file) - close(dfd); -free: - free(buf); -close_dev: - dev_close(dev); - return err; } @@ -2243,8 +2277,9 @@ static int get_log(int argc, char **argv, struct command *cmd, struct plugin *pl const char *raw = "output in raw format"; const char *offset_type = "offset type"; const char *xfer_len = "read chunk size (default 4k)"; - struct nvme_dev *dev; - unsigned char *log; + + _cleanup_nvme_dev_ struct nvme_dev *dev = NULL; + _cleanup_free_ unsigned char *log = NULL; int err; struct config { @@ -2279,26 +2314,24 @@ static int get_log(int argc, char **argv, struct command *cmd, struct plugin *pl .xfer_len = 4096, }; - OPT_ARGS(opts) = { - OPT_UINT("namespace-id", 'n', &cfg.namespace_id, namespace_desired), - OPT_BYTE("log-id", 'i', &cfg.log_id, log_id), - OPT_UINT("log-len", 'l', &cfg.log_len, log_len), - OPT_UINT("aen", 'a', &cfg.aen, aen), - OPT_SUFFIX("lpo", 'o', &cfg.lpo, lpo), - OPT_BYTE("lsp", 's', &cfg.lsp, lsp), - OPT_SHRT("lsi", 'S', &cfg.lsi, lsi), - OPT_FLAG("rae", 'r', &cfg.rae, rae), - OPT_BYTE("uuid-index", 'U', &cfg.uuid_index, uuid_index), - OPT_FLAG("raw-binary", 'b', &cfg.raw_binary, raw), - OPT_BYTE("csi", 'y', &cfg.csi, csi), - OPT_FLAG("ot", 'O', &cfg.ot, offset_type), - OPT_UINT("xfer-len", 'x', &cfg.xfer_len, xfer_len), - OPT_END() - }; + NVME_ARGS(opts, cfg, + OPT_UINT("namespace-id", 'n', &cfg.namespace_id, namespace_desired), + OPT_BYTE("log-id", 'i', &cfg.log_id, log_id), + OPT_UINT("log-len", 'l', &cfg.log_len, log_len), + OPT_UINT("aen", 'a', &cfg.aen, aen), + OPT_SUFFIX("lpo", 'o', &cfg.lpo, lpo), + OPT_BYTE("lsp", 's', &cfg.lsp, lsp), + OPT_SHRT("lsi", 'S', &cfg.lsi, lsi), + OPT_FLAG("rae", 'r', &cfg.rae, rae), + OPT_BYTE("uuid-index", 'U', &cfg.uuid_index, uuid_index), + OPT_FLAG("raw-binary", 'b', &cfg.raw_binary, raw), + OPT_BYTE("csi", 'y', &cfg.csi, csi), + OPT_FLAG("ot", 'O', &cfg.ot, offset_type), + OPT_UINT("xfer-len", 'x', &cfg.xfer_len, xfer_len)); err = parse_and_open(&dev, argc, argv, desc, opts); if (err) - goto ret; + return err; if (cfg.aen) { cfg.log_len = 4096; @@ -2307,34 +2340,27 @@ static int get_log(int argc, char **argv, struct command *cmd, struct plugin *pl if (!cfg.log_len || cfg.log_len & 0x3) { nvme_show_error("non-zero or non-dw alignment log-len is required param"); - err = -EINVAL; - goto close_dev; + return -EINVAL; } if (cfg.lsp > 127) { nvme_show_error("invalid lsp param"); - err = -EINVAL; - goto close_dev; + return -EINVAL; } if (cfg.uuid_index > 127) { nvme_show_error("invalid uuid index param"); - err = -EINVAL; - goto close_dev; + return -EINVAL; } if (cfg.xfer_len == 0 || cfg.xfer_len % 4096) { nvme_show_error("xfer-len argument invalid. It needs to be multiple of 4k"); - err = -EINVAL; - goto close_dev; + return -EINVAL; } - log = malloc(cfg.log_len); - if (!log) { - nvme_show_perror("could not alloc buffer for log\n"); - err = -ENOMEM; - goto close_dev; - } + log = nvme_alloc(cfg.log_len); + if (!log) + return -ENOMEM; struct nvme_get_log_args args = { .args_size = sizeof(args), @@ -2365,52 +2391,44 @@ static int get_log(int argc, char **argv, struct command *cmd, struct plugin *pl } else { nvme_show_error("log page: %s", nvme_strerror(errno)); } - free(log); -close_dev: - dev_close(dev); -ret: return err; } static int sanitize_log(int argc, char **argv, struct command *command, struct plugin *plugin) { const char *desc = "Retrieve sanitize log and show it."; - struct nvme_sanitize_log_page sanitize_log; + + _cleanup_free_ struct nvme_sanitize_log_page *sanitize_log = NULL; + _cleanup_nvme_dev_ struct nvme_dev *dev = NULL; enum nvme_print_flags flags; - struct nvme_dev *dev; int err; struct config { bool rae; - char *output_format; bool human_readable; bool raw_binary; }; struct config cfg = { .rae = false, - .output_format = "normal", .human_readable = false, .raw_binary = false, }; - OPT_ARGS(opts) = { - OPT_FLAG("rae", 'r', &cfg.rae, rae), - OPT_FMT("output-format", 'o', &cfg.output_format, output_format), - OPT_FLAG("human-readable", 'H', &cfg.human_readable, human_readable_log), - OPT_FLAG("raw-binary", 'b', &cfg.raw_binary, raw_log), - OPT_END() - }; + NVME_ARGS(opts, cfg, + OPT_FLAG("rae", 'r', &cfg.rae, rae), + OPT_FLAG("human-readable", 'H', &cfg.human_readable, human_readable_log), + OPT_FLAG("raw-binary", 'b', &cfg.raw_binary, raw_log)); err = parse_and_open(&dev, argc, argv, desc, opts); if (err) - goto ret; + return err; - err = flags = validate_output_format(cfg.output_format); + err = flags = validate_output_format(output_format_val); if (err < 0) { nvme_show_error("Invalid output format"); - goto close_dev; + return err; } if (cfg.raw_binary) @@ -2419,16 +2437,18 @@ static int sanitize_log(int argc, char **argv, struct command *command, struct p if (cfg.human_readable) flags |= VERBOSE; - err = nvme_cli_get_log_sanitize(dev, cfg.rae, &sanitize_log); + sanitize_log = nvme_alloc(sizeof(*sanitize_log)); + if (!sanitize_log) + return -ENOMEM; + + err = nvme_cli_get_log_sanitize(dev, cfg.rae, sanitize_log); if (!err) - nvme_show_sanitize_log(&sanitize_log, dev->name, flags); + nvme_show_sanitize_log(sanitize_log, dev->name, flags); else if (err > 0) nvme_show_status(err); else nvme_show_error("sanitize status log: %s", nvme_strerror(errno)); -close_dev: - dev_close(dev); -ret: + return err; } @@ -2436,50 +2456,48 @@ static int get_fid_support_effects_log(int argc, char **argv, struct command *cm struct plugin *plugin) { const char *desc = "Retrieve FID Support and Effects log and show it."; - struct nvme_fid_supported_effects_log fid_support_log; + + _cleanup_free_ struct nvme_fid_supported_effects_log *fid_support_log = NULL; + _cleanup_nvme_dev_ struct nvme_dev *dev = NULL; enum nvme_print_flags flags; - struct nvme_dev *dev; int err = -1; struct config { - char *output_format; bool human_readable; }; struct config cfg = { - .output_format = "normal", .human_readable = false, }; - OPT_ARGS(opts) = { - OPT_FMT("output-format", 'o', &cfg.output_format, output_format), - OPT_FLAG("human-readable", 'H', &cfg.human_readable, human_readable_log), - OPT_END() - }; + NVME_ARGS(opts, cfg, + OPT_FLAG("human-readable", 'H', &cfg.human_readable, human_readable_log)); err = parse_and_open(&dev, argc, argv, desc, opts); if (err) - goto ret; + return err; - err = flags = validate_output_format(cfg.output_format); + err = flags = validate_output_format(output_format_val); if (err < 0) { nvme_show_error("Invalid output format"); - goto close_dev; + return err; } if (cfg.human_readable) flags |= VERBOSE; - err = nvme_cli_get_log_fid_supported_effects(dev, false, &fid_support_log); + fid_support_log = nvme_alloc(sizeof(*fid_support_log)); + if (!fid_support_log) + return -ENOMEM; + + err = nvme_cli_get_log_fid_supported_effects(dev, false, fid_support_log); if (!err) - nvme_show_fid_support_effects_log(&fid_support_log, dev->name, flags); + nvme_show_fid_support_effects_log(fid_support_log, dev->name, flags); else if (err > 0) nvme_show_status(err); else nvme_show_error("fid support effects log: %s", nvme_strerror(errno)); -close_dev: - dev_close(dev); -ret: + return err; } @@ -2487,50 +2505,48 @@ static int get_mi_cmd_support_effects_log(int argc, char **argv, struct command struct plugin *plugin) { const char *desc = "Retrieve NVMe-MI Command Support and Effects log and show it."; - struct nvme_mi_cmd_supported_effects_log mi_cmd_support_log; + + _cleanup_free_ struct nvme_mi_cmd_supported_effects_log *mi_cmd_support_log = NULL; + _cleanup_nvme_dev_ struct nvme_dev *dev = NULL; enum nvme_print_flags flags; - struct nvme_dev *dev; int err = -1; struct config { - char *output_format; bool human_readable; }; struct config cfg = { - .output_format = "normal", .human_readable = false, }; - OPT_ARGS(opts) = { - OPT_FMT("output-format", 'o', &cfg.output_format, output_format), - OPT_FLAG("human-readable", 'H', &cfg.human_readable, human_readable_log), - OPT_END() - }; + NVME_ARGS(opts, cfg, + OPT_FLAG("human-readable", 'H', &cfg.human_readable, human_readable_log)); err = parse_and_open(&dev, argc, argv, desc, opts); if (err) - goto ret; + return err; - err = flags = validate_output_format(cfg.output_format); + err = flags = validate_output_format(output_format_val); if (err < 0) { nvme_show_error("Invalid output format"); - goto close_dev; + return err; } if (cfg.human_readable) flags |= VERBOSE; - err = nvme_cli_get_log_mi_cmd_supported_effects(dev, false, &mi_cmd_support_log); + mi_cmd_support_log = nvme_alloc(sizeof(*mi_cmd_support_log)); + if (!mi_cmd_support_log) + return -ENOMEM; + + err = nvme_cli_get_log_mi_cmd_supported_effects(dev, false, mi_cmd_support_log); if (!err) - nvme_show_mi_cmd_support_effects_log(&mi_cmd_support_log, dev->name, flags); + nvme_show_mi_cmd_support_effects_log(mi_cmd_support_log, dev->name, flags); else if (err > 0) nvme_show_status(err); else nvme_show_error("mi command support effects log: %s", nvme_strerror(errno)); -close_dev: - dev_close(dev); -ret: + return err; } @@ -2539,45 +2555,39 @@ static int list_ctrl(int argc, char **argv, struct command *cmd, struct plugin * const char *desc = "Show controller list information for the subsystem the\n" "given device is part of, or optionally controllers attached to a specific namespace."; const char *controller = "controller to display"; - struct nvme_ctrl_list *cntlist; + + _cleanup_free_ struct nvme_ctrl_list *cntlist = NULL; + _cleanup_nvme_dev_ struct nvme_dev *dev = NULL; enum nvme_print_flags flags; - struct nvme_dev *dev; int err; struct config { __u16 cntid; __u32 namespace_id; - char *output_format; }; struct config cfg = { .cntid = 0, .namespace_id = NVME_NSID_NONE, - .output_format = "normal", }; - OPT_ARGS(opts) = { - OPT_SHRT("cntid", 'c', &cfg.cntid, controller), - OPT_UINT("namespace-id", 'n', &cfg.namespace_id, namespace_id_optional), - OPT_FMT("output-format", 'o', &cfg.output_format, output_format), - OPT_END() - }; + NVME_ARGS(opts, cfg, + OPT_SHRT("cntid", 'c', &cfg.cntid, controller), + OPT_UINT("namespace-id", 'n', &cfg.namespace_id, namespace_id_optional)); err = parse_and_open(&dev, argc, argv, desc, opts); if (err) - goto ret; + return err; - err = flags = validate_output_format(cfg.output_format); + err = flags = validate_output_format(output_format_val); if (err < 0) { nvme_show_error("Invalid output format"); - goto close_dev; + return err; } - if (posix_memalign((void *)&cntlist, getpagesize(), 0x1000)) { - nvme_show_error("can not allocate controller list payload"); - err = -ENOMEM; - goto close_dev; - } + cntlist = nvme_alloc(sizeof(*cntlist)); + if (!cntlist) + return -ENOMEM; if (cfg.namespace_id == NVME_NSID_NONE) err = nvme_cli_identify_ctrl_list(dev, cfg.cntid, cntlist); @@ -2591,10 +2601,6 @@ static int list_ctrl(int argc, char **argv, struct command *cmd, struct plugin * else nvme_show_error("id controller list: %s", nvme_strerror(errno)); - free(cntlist); -close_dev: - dev_close(dev); -ret: return err; } @@ -2605,53 +2611,52 @@ static int list_ns(int argc, char **argv, struct command *cmd, struct plugin *pl const char *namespace_id = "first nsid returned list should start from"; const char *csi = "I/O command set identifier"; const char *all = "show all namespaces in the subsystem, whether attached or inactive"; - struct nvme_ns_list ns_list; + + _cleanup_free_ struct nvme_ns_list *ns_list = NULL; + _cleanup_nvme_dev_ struct nvme_dev *dev = NULL; enum nvme_print_flags flags; - struct nvme_dev *dev; int err; struct config { __u32 namespace_id; int csi; bool all; - char *output_format; }; struct config cfg = { .namespace_id = 1, .csi = -1, .all = false, - .output_format = "normal", }; - OPT_ARGS(opts) = { - OPT_UINT("namespace-id", 'n', &cfg.namespace_id, namespace_id), - OPT_INT("csi", 'y', &cfg.csi, csi), - OPT_FLAG("all", 'a', &cfg.all, all), - OPT_FMT("output-format", 'o', &cfg.output_format, output_format_no_binary), - OPT_END() - }; + NVME_ARGS(opts, cfg, + OPT_UINT("namespace-id", 'n', &cfg.namespace_id, namespace_id), + OPT_INT("csi", 'y', &cfg.csi, csi), + OPT_FLAG("all", 'a', &cfg.all, all)); err = parse_and_open(&dev, argc, argv, desc, opts); if (err) - goto ret; + return err; - err = flags = validate_output_format(cfg.output_format); + err = flags = validate_output_format(output_format_val); if (flags != JSON && flags != NORMAL) { nvme_show_error("Invalid output format"); - goto close_dev; + return err; } if (!cfg.namespace_id) { - err = -EINVAL; nvme_show_error("invalid nsid parameter"); - goto close_dev; + return -EINVAL; } + ns_list = nvme_alloc(sizeof(*ns_list)); + if (!ns_list) + return -ENOMEM; + struct nvme_identify_args args = { .args_size = sizeof(args), .timeout = NVME_DEFAULT_IOCTL_TIMEOUT, - .data = &ns_list, + .data = ns_list, .nsid = cfg.namespace_id - 1. }; if (cfg.csi < 0) { @@ -2664,16 +2669,13 @@ static int list_ns(int argc, char **argv, struct command *cmd, struct plugin *pl } err = nvme_cli_identify(dev, &args); - if (!err) - nvme_show_list_ns(&ns_list, flags); + nvme_show_list_ns(ns_list, flags); else if (err > 0) nvme_show_status(err); else nvme_show_error("id namespace list: %s", nvme_strerror(errno)); -close_dev: - dev_close(dev); -ret: + return err; } @@ -2682,60 +2684,54 @@ static int id_ns_lba_format(int argc, char **argv, struct command *cmd, struct p const char *desc = "Send an Identify Namespace command to the given\n" "device, returns capability field properties of the specified\n" "LBA Format index in various formats."; + + _cleanup_nvme_dev_ struct nvme_dev *dev = NULL; + _cleanup_free_ struct nvme_id_ns *ns = NULL; enum nvme_print_flags flags; - struct nvme_id_ns ns; - struct nvme_dev *dev; int err = -1; struct config { __u16 lba_format_index; __u8 uuid_index; - bool verbose; - char *output_format; }; struct config cfg = { .lba_format_index = 0, .uuid_index = NVME_UUID_NONE, - .verbose = false, - .output_format = "normal", }; - OPT_ARGS(opts) = { - OPT_UINT("lba-format-index", 'i', &cfg.lba_format_index, lba_format_index), - OPT_BYTE("uuid-index", 'U', &cfg.uuid_index, uuid_index), - OPT_FLAG("verbose", 'v', &cfg.verbose, verbose), - OPT_FMT("output-format", 'o', &cfg.output_format, output_format), - OPT_END() - }; + NVME_ARGS(opts, cfg, + OPT_UINT("lba-format-index", 'i', &cfg.lba_format_index, lba_format_index), + OPT_BYTE("uuid-index", 'U', &cfg.uuid_index, uuid_index)); err = parse_and_open(&dev, argc, argv, desc, opts); if (err) - goto ret; + return err; - err = flags = validate_output_format(cfg.output_format); + err = flags = validate_output_format(output_format_val); if (err < 0) { nvme_show_error("Invalid output format"); - goto close_dev; + return err; } - if (cfg.verbose) + if (argconfig_parse_seen(opts, "verbose")) flags |= VERBOSE; + ns = nvme_alloc(sizeof(*ns)); + if (!ns) + return -ENOMEM; + err = nvme_identify_ns_csi_user_data_format(dev_fd(dev), - cfg.lba_format_index, - cfg.uuid_index, NVME_CSI_NVM, &ns); + cfg.lba_format_index, + cfg.uuid_index, NVME_CSI_NVM, ns); if (!err) - nvme_show_id_ns(&ns, 0, cfg.lba_format_index, true, flags); + nvme_show_id_ns(ns, 0, cfg.lba_format_index, true, flags); else if (err > 0) nvme_show_status(err); else nvme_show_perror("identify namespace for specific LBA format"); -close_dev: - dev_close(dev); -ret: - return nvme_status_to_errno(err, false); + return err; } static int id_endurance_grp_list(int argc, char **argv, struct command *cmd, @@ -2743,41 +2739,36 @@ static int id_endurance_grp_list(int argc, char **argv, struct command *cmd, { const char *desc = "Show endurance group list information for the given endurance group id"; const char *endurance_grp_id = "Endurance Group ID"; - struct nvme_id_endurance_group_list *endgrp_list; + + _cleanup_free_ struct nvme_id_endurance_group_list *endgrp_list = NULL; + _cleanup_nvme_dev_ struct nvme_dev *dev = NULL; enum nvme_print_flags flags; - struct nvme_dev *dev; int err = -1; struct config { __u16 endgrp_id; - char *output_format; }; struct config cfg = { .endgrp_id = 0, - .output_format = "normal", }; - OPT_ARGS(opts) = { - OPT_SHRT("endgrp-id", 'i', &cfg.endgrp_id, endurance_grp_id), - OPT_FMT("output-format", 'o', &cfg.output_format, output_format), - OPT_END() - }; + NVME_ARGS(opts, cfg, + OPT_SHRT("endgrp-id", 'i', &cfg.endgrp_id, endurance_grp_id)); err = parse_and_open(&dev, argc, argv, desc, opts); if (err) - goto ret; + return err; - err = flags = validate_output_format(cfg.output_format); + err = flags = validate_output_format(output_format_val); if (flags != JSON && flags != NORMAL) { nvme_show_error("invalid output format"); - goto close_dev; + return err; } - if (posix_memalign((void *)&endgrp_list, getpagesize(), 0x1000)) { - err = -1; - goto close_dev; - } + endgrp_list = nvme_alloc(sizeof(*endgrp_list)); + if (!endgrp_list) + return -ENOMEM; err = nvme_identify_endurance_group_list(dev_fd(dev), cfg.endgrp_id, endgrp_list); if (!err) @@ -2787,10 +2778,6 @@ static int id_endurance_grp_list(int argc, char **argv, struct command *cmd, else nvme_show_error("Id endurance group list: %s", nvme_strerror(errno)); - free(endgrp_list); -close_dev: - dev_close(dev); -ret: return err; } @@ -2803,7 +2790,8 @@ static int delete_ns(int argc, char **argv, struct command *cmd, struct plugin * "becomes inactive when that namespace is detached or, if\n" "the namespace is not already inactive, once deleted."; const char *namespace_id = "namespace to delete"; - struct nvme_dev *dev; + + _cleanup_nvme_dev_ struct nvme_dev *dev = NULL; int err; struct config { @@ -2816,21 +2804,19 @@ static int delete_ns(int argc, char **argv, struct command *cmd, struct plugin * .timeout = 120000, }; - OPT_ARGS(opts) = { - OPT_UINT("namespace-id", 'n', &cfg.namespace_id, namespace_id), - OPT_UINT("timeout", 't', &cfg.timeout, timeout), - OPT_END() - }; + NVME_ARGS(opts, cfg, + OPT_UINT("namespace-id", 'n', &cfg.namespace_id, namespace_id), + OPT_UINT("timeout", 't', &cfg.timeout, timeout)); err = parse_and_open(&dev, argc, argv, desc, opts); if (err) - goto ret; + return err; if (!cfg.namespace_id) { err = nvme_get_nsid(dev_fd(dev), &cfg.namespace_id); if (err < 0) { nvme_show_error("get-namespace-id: %s", nvme_strerror(errno)); - goto close_dev; + return err; } } @@ -2842,18 +2828,15 @@ static int delete_ns(int argc, char **argv, struct command *cmd, struct plugin * else nvme_show_error("delete namespace: %s", nvme_strerror(errno)); -close_dev: - dev_close(dev); -ret: return err; } static int nvme_attach_ns(int argc, char **argv, int attach, const char *desc, struct command *cmd) { - struct nvme_ctrl_list cntlist; + _cleanup_free_ struct nvme_ctrl_list *cntlist = NULL; + _cleanup_free_ __u16 *ctrlist = NULL; + _cleanup_nvme_dev_ struct nvme_dev *dev = NULL; int err, num, i, list[2048]; - struct nvme_dev *dev; - __u16 ctrlist[2048]; const char *namespace_id = "namespace to attach"; const char *cont = "optional comma-sep controller id list"; @@ -2868,20 +2851,17 @@ static int nvme_attach_ns(int argc, char **argv, int attach, const char *desc, s .cntlist = "", }; - OPT_ARGS(opts) = { - OPT_UINT("namespace-id", 'n', &cfg.namespace_id, namespace_id), - OPT_LIST("controllers", 'c', &cfg.cntlist, cont), - OPT_END() - }; + NVME_ARGS(opts, cfg, + OPT_UINT("namespace-id", 'n', &cfg.namespace_id, namespace_id), + OPT_LIST("controllers", 'c', &cfg.cntlist, cont)); err = parse_and_open(&dev, argc, argv, desc, opts); if (err) - goto ret; + return err; if (!cfg.namespace_id) { nvme_show_error("%s: namespace-id parameter required", cmd->name); - err = -EINVAL; - goto close_dev; + return -EINVAL; } num = argconfig_parse_comma_sep_array(cfg.cntlist, list, 2047); @@ -2890,21 +2870,28 @@ static int nvme_attach_ns(int argc, char **argv, int attach, const char *desc, s if (num == -1) { nvme_show_error("%s: controller id list is malformed", cmd->name); - err = -EINVAL; - goto close_dev; + return -EINVAL; } + cntlist = nvme_alloc(sizeof(*cntlist)); + if (!cntlist) + return -ENOMEM; + + ctrlist = nvme_alloc(sizeof(*ctrlist) * 2048); + if (!ctrlist) + return -ENOMEM; + for (i = 0; i < num; i++) ctrlist[i] = (__u16)list[i]; - nvme_init_ctrl_list(&cntlist, num, ctrlist); + nvme_init_ctrl_list(cntlist, num, ctrlist); if (attach) err = nvme_cli_ns_attach_ctrls(dev, cfg.namespace_id, - &cntlist); + cntlist); else err = nvme_cli_ns_detach_ctrls(dev, cfg.namespace_id, - &cntlist); + cntlist); if (!err) printf("%s: Success, nsid:%d\n", cmd->name, cfg.namespace_id); @@ -2913,9 +2900,6 @@ static int nvme_attach_ns(int argc, char **argv, int attach, const char *desc, s else nvme_show_perror(attach ? "attach namespace" : "detach namespace"); -close_dev: - dev_close(dev); -ret: return err; } @@ -2943,18 +2927,18 @@ static int detach_ns(int argc, char **argv, struct command *cmd, struct plugin * static int parse_lba_num_si(struct nvme_dev *dev, const char *opt, const char *val, __u8 flbas, __u64 *num) { - struct nvme_id_ctrl ctrl; + _cleanup_free_ struct nvme_ns_list *ns_list = NULL; + _cleanup_free_ struct nvme_id_ctrl *ctrl = NULL; + _cleanup_free_ struct nvme_id_ns *ns = NULL; __u32 nsid = 1; - struct nvme_id_ns ns; char *endptr; int err = -EINVAL; int i; int lbas; - struct nvme_ns_list ns_list; + struct nvme_identify_args args = { .args_size = sizeof(args), .timeout = NVME_DEFAULT_IOCTL_TIMEOUT, - .data = &ns_list, .cns = NVME_IDENTIFY_CNS_NS_ACTIVE_LIST, .nsid = nsid - 1. }; @@ -2969,7 +2953,11 @@ static int parse_lba_num_si(struct nvme_dev *dev, const char *opt, return err; } - err = nvme_cli_identify_ctrl(dev, &ctrl); + ctrl = nvme_alloc(sizeof(*ctrl)); + if (!ctrl) + return -ENOMEM; + + err = nvme_cli_identify_ctrl(dev, ctrl); if (err) { if (err < 0) nvme_show_error("identify controller: %s", nvme_strerror(errno)); @@ -2978,7 +2966,12 @@ static int parse_lba_num_si(struct nvme_dev *dev, const char *opt, return err; } - if ((ctrl.oacs & 0x8) >> 3) + ns_list = nvme_alloc(sizeof(*ns_list)); + if (!ns_list) + return -ENOMEM; + args.data = ns_list; + + if ((ctrl->oacs & 0x8) >> 3) nsid = NVME_NSID_ALL; else { err = nvme_cli_identify(dev, &args); @@ -2990,10 +2983,14 @@ static int parse_lba_num_si(struct nvme_dev *dev, const char *opt, nvme_show_status(err); return err; } - nsid = le32_to_cpu(ns_list.ns[0]); + nsid = le32_to_cpu(ns_list->ns[0]); } - err = nvme_cli_identify_ns(dev, nsid, &ns); + ns = nvme_alloc(sizeof(*ns)); + if (!ns) + return -ENOMEM; + + err = nvme_cli_identify_ns(dev, nsid, ns); if (err) { if (err < 0) nvme_show_error("identify namespace: %s", nvme_strerror(errno)); @@ -3003,7 +3000,7 @@ static int parse_lba_num_si(struct nvme_dev *dev, const char *opt, } i = flbas & NVME_NS_FLBAS_LOWER_MASK; - lbas = (1 << ns.lbaf[i].ds) + ns.lbaf[i].ms; + lbas = (1 << ns->lbaf[i].ds) + ns->lbaf[i].ms; if (suffix_si_parse(val, &endptr, (uint64_t *)num)) { nvme_show_error("Expected long suffixed integer argument for '%s-si' but got '%s'!", @@ -3032,6 +3029,7 @@ static int create_ns(int argc, char **argv, struct command *cmd, struct plugin * const char *nmic = "multipath and sharing capabilities (NMIC)"; const char *anagrpid = "ANA Group Identifier (ANAGRPID)"; const char *nvmsetid = "NVM Set Identifier (NVMSETID)"; + const char *endgid = "Endurance Group Identifier (ENDGID)"; const char *csi = "command set identifier (CSI)"; const char *lbstm = "logical block storage tag mask (LBSTM)"; const char *nphndls = "Number of Placement Handles (NPHNDLS)"; @@ -3045,8 +3043,9 @@ static int create_ns(int argc, char **argv, struct command *cmd, struct plugin * "Requested Number of ZRWA Resources (RNUMZRWA) for Zoned Namespace Command Set"; const char *phndls = "Comma separated list of Placement Handle Associated RUH"; - struct nvme_id_ns ns; - struct nvme_dev *dev; + _cleanup_free_ struct nvme_ns_mgmt_host_sw_specified *data = NULL; + _cleanup_free_ struct nvme_id_ns *ns = NULL; + _cleanup_nvme_dev_ struct nvme_dev *dev = NULL; int err = 0, i; __u32 nsid; uint16_t num_phandle; @@ -3060,6 +3059,7 @@ static int create_ns(int argc, char **argv, struct command *cmd, struct plugin * __u8 nmic; __u32 anagrpid; __u16 nvmsetid; + __u16 endgid; __u64 bs; __u32 timeout; __u8 csi; @@ -3082,7 +3082,8 @@ static int create_ns(int argc, char **argv, struct command *cmd, struct plugin * .nmic = 0, .anagrpid = 0, .nvmsetid = 0, - .bs = 0x00, + .endgid = 0, + .bs = 0x00, .timeout = 120000, .csi = 0, .lbstm = 0, @@ -3096,48 +3097,51 @@ static int create_ns(int argc, char **argv, struct command *cmd, struct plugin * .phndls = "", }; - OPT_ARGS(opts) = { - OPT_SUFFIX("nsze", 's', &cfg.nsze, nsze), - OPT_SUFFIX("ncap", 'c', &cfg.ncap, ncap), - OPT_BYTE("flbas", 'f', &cfg.flbas, flbas), - OPT_BYTE("dps", 'd', &cfg.dps, dps), - OPT_BYTE("nmic", 'm', &cfg.nmic, nmic), - OPT_UINT("anagrp-id", 'a', &cfg.anagrpid, anagrpid), - OPT_UINT("nvmset-id", 'i', &cfg.nvmsetid, nvmsetid), - OPT_SUFFIX("block-size", 'b', &cfg.bs, bs), - OPT_UINT("timeout", 't', &cfg.timeout, timeout), - OPT_BYTE("csi", 'y', &cfg.csi, csi), - OPT_SUFFIX("lbstm", 'l', &cfg.lbstm, lbstm), - OPT_SHRT("nphndls", 'n', &cfg.nphndls, nphndls), - OPT_STR("nsze-si", 'S', &cfg.nsze_si, nsze_si), - OPT_STR("ncap-si", 'C', &cfg.ncap_si, ncap_si), - OPT_FLAG("azr", 'z', &cfg.azr, azr), - OPT_UINT("rar", 'r', &cfg.rar, rar), - OPT_UINT("ror", 'o', &cfg.ror, ror), - OPT_UINT("rnumzrwa", 'u', &cfg.rnumzrwa, rnumzrwa), - OPT_LIST("phndls", 'p', &cfg.phndls, phndls), - OPT_END() - }; + NVME_ARGS(opts, cfg, + OPT_SUFFIX("nsze", 's', &cfg.nsze, nsze), + OPT_SUFFIX("ncap", 'c', &cfg.ncap, ncap), + OPT_BYTE("flbas", 'f', &cfg.flbas, flbas), + OPT_BYTE("dps", 'd', &cfg.dps, dps), + OPT_BYTE("nmic", 'm', &cfg.nmic, nmic), + OPT_UINT("anagrp-id", 'a', &cfg.anagrpid, anagrpid), + OPT_UINT("nvmset-id", 'i', &cfg.nvmsetid, nvmsetid), + OPT_UINT("endg-id", 'e', &cfg.endgid, endgid), + OPT_SUFFIX("block-size", 'b', &cfg.bs, bs), + OPT_UINT("timeout", 't', &cfg.timeout, timeout), + OPT_BYTE("csi", 'y', &cfg.csi, csi), + OPT_SUFFIX("lbstm", 'l', &cfg.lbstm, lbstm), + OPT_SHRT("nphndls", 'n', &cfg.nphndls, nphndls), + OPT_STR("nsze-si", 'S', &cfg.nsze_si, nsze_si), + OPT_STR("ncap-si", 'C', &cfg.ncap_si, ncap_si), + OPT_FLAG("azr", 'z', &cfg.azr, azr), + OPT_UINT("rar", 'r', &cfg.rar, rar), + OPT_UINT("ror", 'o', &cfg.ror, ror), + OPT_UINT("rnumzrwa", 'u', &cfg.rnumzrwa, rnumzrwa), + OPT_LIST("phndls", 'p', &cfg.phndls, phndls)); err = parse_and_open(&dev, argc, argv, desc, opts); if (err) - goto ret; + return err; if (cfg.flbas != 0xff && cfg.bs != 0x00) { nvme_show_error( "Invalid specification of both FLBAS and Block Size, please specify only one"); - err = -EINVAL; - goto close_dev; + return -EINVAL; } if (cfg.bs) { if ((cfg.bs & (~cfg.bs + 1)) != cfg.bs) { nvme_show_error( "Invalid value for block size (%"PRIu64"). Block size must be a power of two", (uint64_t)cfg.bs); - err = -EINVAL; - goto close_dev; + return -EINVAL; } - err = nvme_cli_identify_ns(dev, NVME_NSID_ALL, &ns); + + + ns = nvme_alloc(sizeof(*ns)); + if (!ns) + return -ENOMEM; + + err = nvme_cli_identify_ns(dev, NVME_NSID_ALL, ns); if (err) { if (err < 0) { nvme_show_error("identify-namespace: %s", nvme_strerror(errno)); @@ -3145,10 +3149,10 @@ static int create_ns(int argc, char **argv, struct command *cmd, struct plugin * fprintf(stderr, "identify failed\n"); nvme_show_status(err); } - goto close_dev; + return err; } - for (i = 0; i <= ns.nlbaf; ++i) { - if ((1 << ns.lbaf[i].ds) == cfg.bs && ns.lbaf[i].ms == 0) { + for (i = 0; i <= ns->nlbaf; ++i) { + if ((1 << ns->lbaf[i].ds) == cfg.bs && ns->lbaf[i].ms == 0) { cfg.flbas = i; break; } @@ -3160,51 +3164,51 @@ static int create_ns(int argc, char **argv, struct command *cmd, struct plugin * (uint64_t)cfg.bs); fprintf(stderr, "Please correct block size, or specify FLBAS directly\n"); - err = -EINVAL; - goto close_dev; + return -EINVAL; } err = parse_lba_num_si(dev, "nsze", cfg.nsze_si, cfg.flbas, &cfg.nsze); if (err) - goto close_dev; + return err; err = parse_lba_num_si(dev, "ncap", cfg.ncap_si, cfg.flbas, &cfg.ncap); if (err) - goto close_dev; + return err; if (cfg.csi != NVME_CSI_ZNS && (cfg.azr || cfg.rar || cfg.ror || cfg.rnumzrwa)) { nvme_show_error("Invalid ZNS argument is given (CSI:%#x)", cfg.csi); - err = -EINVAL; - goto close_dev; + return -EINVAL; } - struct nvme_ns_mgmt_host_sw_specified data = { - .nsze = cpu_to_le64(cfg.nsze), - .ncap = cpu_to_le64(cfg.ncap), - .flbas = cfg.flbas, - .dps = cfg.dps, - .nmic = cfg.nmic, - .anagrpid = cpu_to_le32(cfg.anagrpid), - .nvmsetid = cpu_to_le16(cfg.nvmsetid), - .lbstm = cpu_to_le64(cfg.lbstm), - .zns.znsco = cfg.azr, - .zns.rar = cpu_to_le32(cfg.rar), - .zns.ror = cpu_to_le32(cfg.ror), - .zns.rnumzrwa = cpu_to_le32(cfg.rnumzrwa), - .nphndls = cpu_to_le16(cfg.nphndls), - }; + data = nvme_alloc(sizeof(*data)); + if (!data) + return -ENOMEM; + + data->nsze = cpu_to_le64(cfg.nsze); + data->ncap = cpu_to_le64(cfg.ncap); + data->flbas = cfg.flbas; + data->dps = cfg.dps; + data->nmic = cfg.nmic; + data->anagrpid = cpu_to_le32(cfg.anagrpid); + data->nvmsetid = cpu_to_le16(cfg.nvmsetid); + data->endgid = cpu_to_le16(cfg.endgid); + data->lbstm = cpu_to_le64(cfg.lbstm); + data->zns.znsco = cfg.azr; + data->zns.rar = cpu_to_le32(cfg.rar); + data->zns.ror = cpu_to_le32(cfg.ror); + data->zns.rnumzrwa = cpu_to_le32(cfg.rnumzrwa); + data->nphndls = cpu_to_le16(cfg.nphndls); num_phandle = argconfig_parse_comma_sep_array_short(cfg.phndls, phndl, ARRAY_SIZE(phndl)); if (cfg.nphndls != num_phandle) { nvme_show_error("Invalid Placement handle list"); - err = -EINVAL; - goto close_dev; + return -EINVAL; } for (i = 0; i < num_phandle; i++) - data.phndl[i] = cpu_to_le16(phndl[i]); + data->phndl[i] = cpu_to_le16(phndl[i]); - err = nvme_cli_ns_mgmt_create(dev, &data, &nsid, cfg.timeout, cfg.csi); + err = nvme_cli_ns_mgmt_create(dev, data, &nsid, cfg.timeout, cfg.csi); if (!err) printf("%s: Success, created nsid:%d\n", cmd->name, nsid); else if (err > 0) @@ -3212,9 +3216,6 @@ static int create_ns(int argc, char **argv, struct command *cmd, struct plugin * else nvme_show_error("create namespace: %s", nvme_strerror(errno)); -close_dev: - dev_close(dev); -ret: return err; } @@ -3264,21 +3265,7 @@ static int list_subsys(int argc, char **argv, struct command *cmd, int err; int nsid = NVME_NSID_ALL; - struct config { - char *output_format; - int verbose; - }; - - struct config cfg = { - .output_format = "normal", - .verbose = 0, - }; - - OPT_ARGS(opts) = { - OPT_FMT("output-format", 'o', &cfg.output_format, output_format_no_binary), - OPT_INCR("verbose", 'v', &cfg.verbose, verbose), - OPT_END() - }; + NVME_ARGS(opts, cfg); err = argconfig_parse(argc, argv, desc, opts); if (err < 0) @@ -3288,16 +3275,16 @@ static int list_subsys(int argc, char **argv, struct command *cmd, if (optind < argc) devname = basename(argv[optind++]); - err = flags = validate_output_format(cfg.output_format); + err = flags = validate_output_format(output_format_val); if (flags != JSON && flags != NORMAL) { nvme_show_error("Invalid output format"); goto ret; } - if (cfg.verbose) + if (argconfig_parse_seen(opts, "verbose")) flags |= VERBOSE; - r = nvme_create_root(stderr, map_log_level(cfg.verbose, false)); + r = nvme_create_root(stderr, map_log_level(!!(flags & VERBOSE), false)); if (!r) { if (devname) nvme_show_error("Failed to scan nvme subsystem for %s", devname); @@ -3339,36 +3326,22 @@ static int list(int argc, char **argv, struct command *cmd, struct plugin *plugi nvme_root_t r; int err = 0; - struct config { - char *output_format; - bool verbose; - }; - - struct config cfg = { - .output_format = "normal", - .verbose = false, - }; - - OPT_ARGS(opts) = { - OPT_FMT("output-format", 'o', &cfg.output_format, output_format_no_binary), - OPT_FLAG("verbose", 'v', &cfg.verbose, verbose), - OPT_END() - }; + NVME_ARGS(opts, cfg); err = argconfig_parse(argc, argv, desc, opts); if (err < 0) return err; - flags = validate_output_format(cfg.output_format); + flags = validate_output_format(output_format_val); if (flags != JSON && flags != NORMAL) { nvme_show_error("Invalid output format"); return -EINVAL; } - if (cfg.verbose) + if (argconfig_parse_seen(opts, "verbose")) flags |= VERBOSE; - r = nvme_create_root(stderr, map_log_level(cfg.verbose, false)); + r = nvme_create_root(stderr, map_log_level(!!(flags & VERBOSE), false)); if (!r) { nvme_show_error("Failed to create topology root: %s", nvme_strerror(errno)); return -errno; @@ -3395,41 +3368,37 @@ int __id_ctrl(int argc, char **argv, struct command *cmd, struct plugin *plugin, "binary format. May also return vendor-specific\n" "controller attributes in hex-dump if requested."; const char *vendor_specific = "dump binary vendor field"; + + _cleanup_free_ struct nvme_id_ctrl *ctrl = NULL; + _cleanup_nvme_dev_ struct nvme_dev *dev = NULL; enum nvme_print_flags flags; - struct nvme_id_ctrl ctrl; - struct nvme_dev *dev; int err; struct config { bool vendor_specific; - char *output_format; bool raw_binary; bool human_readable; }; struct config cfg = { .vendor_specific = false, - .output_format = "normal", .raw_binary = false, .human_readable = false, }; - OPT_ARGS(opts) = { - OPT_FLAG("vendor-specific", 'v', &cfg.vendor_specific, vendor_specific), - OPT_FMT("output-format", 'o', &cfg.output_format, output_format), - OPT_FLAG("raw-binary", 'b', &cfg.raw_binary, raw_identify), - OPT_FLAG("human-readable", 'H', &cfg.human_readable, human_readable_identify), - OPT_END() - }; + NVME_ARGS(opts, cfg, + OPT_FLAG("vendor-specific", 'v', &cfg.vendor_specific, vendor_specific), + OPT_FLAG("raw-binary", 'b', &cfg.raw_binary, raw_identify), + OPT_FLAG("human-readable", 'H', &cfg.human_readable, human_readable_identify)); err = parse_and_open(&dev, argc, argv, desc, opts); if (err) - goto ret; + return err; - err = flags = validate_output_format(cfg.output_format); + err = flags = validate_output_format(output_format_val); if (err < 0) { nvme_show_error("Invalid output format"); - goto close_dev; + return err; } if (cfg.raw_binary) @@ -3441,16 +3410,18 @@ int __id_ctrl(int argc, char **argv, struct command *cmd, struct plugin *plugin, if (cfg.human_readable) flags |= VERBOSE; - err = nvme_cli_identify_ctrl(dev, &ctrl); + ctrl = nvme_alloc(sizeof(*ctrl)); + if (!ctrl) + return -ENOMEM; + + err = nvme_cli_identify_ctrl(dev, ctrl); if (!err) - nvme_show_id_ctrl(&ctrl, flags, vs); + nvme_show_id_ctrl(ctrl, flags, vs); else if (err > 0) nvme_show_status(err); else nvme_show_error("identify controller: %s", nvme_strerror(errno)); -close_dev: - dev_close(dev); -ret: + return err; } @@ -3465,44 +3436,36 @@ static int nvm_id_ctrl(int argc, char **argv, struct command *cmd, const char *desc = "Send an Identify Controller NVM Command Set\n" "command to the given device and report information about\n" "the specified controller in various formats."; + + _cleanup_free_ struct nvme_id_ctrl_nvm *ctrl_nvm = NULL; + _cleanup_nvme_dev_ struct nvme_dev *dev = NULL; enum nvme_print_flags flags; - struct nvme_id_ctrl_nvm ctrl_nvm; - struct nvme_dev *dev; int err = -1; - struct config { - char *output_format; - }; - - struct config cfg = { - .output_format = "normal", - }; - - OPT_ARGS(opts) = { - OPT_FMT("output-format", 'o', &cfg.output_format, output_format), - OPT_END() - }; + NVME_ARGS(opts, cfg); err = parse_and_open(&dev, argc, argv, desc, opts); if (err) - goto ret; + return err; - err = flags = validate_output_format(cfg.output_format); + err = flags = validate_output_format(output_format_val); if (err < 0) { nvme_show_error("Invalid output format"); - goto close_dev; + return err; } - err = nvme_nvm_identify_ctrl(dev_fd(dev), &ctrl_nvm); + ctrl_nvm = nvme_alloc(sizeof(*ctrl_nvm)); + if (!ctrl_nvm) + return -ENOMEM; + + err = nvme_nvm_identify_ctrl(dev_fd(dev), ctrl_nvm); if (!err) - nvme_show_id_ctrl_nvm(&ctrl_nvm, flags); + nvme_show_id_ctrl_nvm(ctrl_nvm, flags); else if (err > 0) nvme_show_status(err); else nvme_show_error("nvm identify controller: %s", nvme_strerror(errno)); -close_dev: - dev_close(dev); -ret: + return err; } @@ -3512,75 +3475,73 @@ static int nvm_id_ns(int argc, char **argv, struct command *cmd, const char *desc = "Send an Identify Namespace NVM Command Set\n" "command to the given device and report information about\n" "the specified namespace in various formats."; + + _cleanup_free_ struct nvme_nvm_id_ns *id_ns = NULL; + _cleanup_free_ struct nvme_id_ns *ns = NULL; + _cleanup_nvme_dev_ struct nvme_dev *dev = NULL; enum nvme_print_flags flags; - struct nvme_nvm_id_ns id_ns; - struct nvme_id_ns ns; - struct nvme_dev *dev; int err = -1; struct config { __u32 namespace_id; __u8 uuid_index; - char *output_format; - bool verbose; }; struct config cfg = { .namespace_id = 0, .uuid_index = NVME_UUID_NONE, - .output_format = "normal", - .verbose = false, }; - OPT_ARGS(opts) = { - OPT_UINT("namespace-id", 'n', &cfg.namespace_id, namespace_id_desired), - OPT_BYTE("uuid-index", 'U', &cfg.uuid_index, uuid_index), - OPT_FMT("output-format", 'o', &cfg.output_format, output_format), - OPT_FLAG("verbose", 'v', &cfg.verbose, verbose), - OPT_END() - }; + NVME_ARGS(opts, cfg, + OPT_UINT("namespace-id", 'n', &cfg.namespace_id, namespace_id_desired), + OPT_BYTE("uuid-index", 'U', &cfg.uuid_index, uuid_index)); err = parse_and_open(&dev, argc, argv, desc, opts); if (err) - goto ret; + return err; - err = flags = validate_output_format(cfg.output_format); + err = flags = validate_output_format(output_format_val); if (err < 0) { nvme_show_error("Invalid output format"); - goto close_dev; + return err; } - if (cfg.verbose) + if (argconfig_parse_seen(opts, "verbose")) flags |= VERBOSE; if (!cfg.namespace_id) { err = nvme_get_nsid(dev_fd(dev), &cfg.namespace_id); if (err < 0) { nvme_show_perror("get-namespace-id"); - goto close_dev; + return err; } } - err = nvme_cli_identify_ns(dev, cfg.namespace_id, &ns); + ns = nvme_alloc(sizeof(*ns)); + if (!ns) + return -ENOMEM; + + err = nvme_cli_identify_ns(dev, cfg.namespace_id, ns); if (err) { nvme_show_status(err); - goto close_dev; + return err; } + id_ns = nvme_alloc(sizeof(*id_ns)); + if (!id_ns) + return -ENOMEM; + err = nvme_identify_ns_csi(dev_fd(dev), cfg.namespace_id, - cfg.uuid_index, - NVME_CSI_NVM, &id_ns); + cfg.uuid_index, + NVME_CSI_NVM, id_ns); if (!err) - nvme_show_nvm_id_ns(&id_ns, cfg.namespace_id, &ns, 0, false, flags); + nvme_show_nvm_id_ns(id_ns, cfg.namespace_id, ns, 0, false, flags); else if (err > 0) nvme_show_status(err); else nvme_show_perror("nvm identify namespace"); -close_dev: - dev_close(dev); -ret: - return nvme_status_to_errno(err, false); + return err; } static int nvm_id_ns_lba_format(int argc, char **argv, struct command *cmd, struct plugin *plugin) @@ -3588,66 +3549,64 @@ static int nvm_id_ns_lba_format(int argc, char **argv, struct command *cmd, stru const char *desc = "Send an NVM Command Set specific Identify Namespace\n" "command to the given device, returns capability field properties of\n" "the specified LBA Format index in the specified namespace in various formats."; + + _cleanup_free_ struct nvme_nvm_id_ns *nvm_ns = NULL; + _cleanup_free_ struct nvme_id_ns *ns = NULL; + _cleanup_nvme_dev_ struct nvme_dev *dev = NULL; enum nvme_print_flags flags; - struct nvme_id_ns ns; - struct nvme_nvm_id_ns nvm_ns; - struct nvme_dev *dev; int err = -1; struct config { __u16 lba_format_index; __u8 uuid_index; - bool verbose; - char *output_format; }; struct config cfg = { .lba_format_index = 0, .uuid_index = NVME_UUID_NONE, - .verbose = false, - .output_format = "normal", }; - OPT_ARGS(opts) = { - OPT_UINT("lba-format-index", 'i', &cfg.lba_format_index, lba_format_index), - OPT_BYTE("uuid-index", 'U', &cfg.uuid_index, uuid_index), - OPT_FLAG("verbose", 'v', &cfg.verbose, verbose), - OPT_FMT("output-format", 'o', &cfg.output_format, output_format), - OPT_END() - }; + NVME_ARGS(opts, cfg, + OPT_UINT("lba-format-index", 'i', &cfg.lba_format_index, lba_format_index), + OPT_BYTE("uuid-index", 'U', &cfg.uuid_index, uuid_index)); err = parse_and_open(&dev, argc, argv, desc, opts); if (err) - goto ret; + return err; - err = flags = validate_output_format(cfg.output_format); + err = flags = validate_output_format(output_format_val); if (err < 0) { nvme_show_error("Invalid output format"); - goto close_dev; + return err; } - if (cfg.verbose) + if (argconfig_parse_seen(opts, "verbose")) flags |= VERBOSE; - err = nvme_cli_identify_ns(dev, NVME_NSID_ALL, &ns); + ns = nvme_alloc(sizeof(*ns)); + if (!ns) + return -ENOMEM; + + err = nvme_cli_identify_ns(dev, NVME_NSID_ALL, ns); if (err) { - ns.nlbaf = NVME_FEAT_LBA_RANGE_MAX - 1; - ns.nulbaf = 0; + ns->nlbaf = NVME_FEAT_LBA_RANGE_MAX - 1; + ns->nulbaf = 0; } + nvm_ns = nvme_alloc(sizeof(*nvm_ns)); + if (!nvm_ns) + return -ENOMEM; + err = nvme_identify_iocs_ns_csi_user_data_format(dev_fd(dev), cfg.lba_format_index, - cfg.uuid_index, NVME_CSI_NVM, &nvm_ns); + cfg.uuid_index, NVME_CSI_NVM, nvm_ns); if (!err) - nvme_show_nvm_id_ns(&nvm_ns, 0, &ns, cfg.lba_format_index, true, flags); + nvme_show_nvm_id_ns(nvm_ns, 0, ns, cfg.lba_format_index, true, flags); else if (err > 0) nvme_show_status(err); else nvme_show_perror("NVM identify namespace for specific LBA format"); -close_dev: - dev_close(dev); -ret: - return nvme_status_to_errno(err, false); + return err; } static int ns_descs(int argc, char **argv, struct command *cmd, struct plugin *plugin) @@ -3656,38 +3615,34 @@ static int ns_descs(int argc, char **argv, struct command *cmd, struct plugin *p "given device, returns the namespace identification descriptors\n" "of the specific namespace in either human-readable or binary format."; const char *raw = "show descriptors in binary format"; + + _cleanup_nvme_dev_ struct nvme_dev *dev = NULL; + _cleanup_free_ void *nsdescs = NULL;; enum nvme_print_flags flags; - struct nvme_dev *dev; - void *nsdescs; int err; struct config { __u32 namespace_id; - char *output_format; bool raw_binary; }; struct config cfg = { .namespace_id = 0, - .output_format = "normal", .raw_binary = false, }; - OPT_ARGS(opts) = { - OPT_UINT("namespace-id", 'n', &cfg.namespace_id, namespace_id_desired), - OPT_FMT("output-format", 'o', &cfg.output_format, output_format), - OPT_FLAG("raw-binary", 'b', &cfg.raw_binary, raw), - OPT_END() - }; + NVME_ARGS(opts, cfg, + OPT_UINT("namespace-id", 'n', &cfg.namespace_id, namespace_id_desired), + OPT_FLAG("raw-binary", 'b', &cfg.raw_binary, raw)); err = parse_and_open(&dev, argc, argv, desc, opts); if (err) - goto ret; + return err; - err = flags = validate_output_format(cfg.output_format); + err = flags = validate_output_format(output_format_val); if (err < 0) { nvme_show_error("Invalid output format"); - goto close_dev; + return err; } if (cfg.raw_binary) @@ -3697,14 +3652,13 @@ static int ns_descs(int argc, char **argv, struct command *cmd, struct plugin *p err = nvme_get_nsid(dev_fd(dev), &cfg.namespace_id); if (err < 0) { nvme_show_error("get-namespace-id: %s", nvme_strerror(errno)); - goto close_dev; + return err; } } - if (posix_memalign(&nsdescs, getpagesize(), 0x1000)) { - err = -ENOMEM; - goto close_dev; - } + nsdescs = nvme_alloc(sizeof(*nsdescs)); + if (!nsdescs) + return -ENOMEM; err = nvme_cli_identify_ns_descs(dev, cfg.namespace_id, nsdescs); if (!err) @@ -3713,10 +3667,7 @@ static int ns_descs(int argc, char **argv, struct command *cmd, struct plugin *p nvme_show_status(err); else nvme_show_error("identify namespace: %s", nvme_strerror(errno)); - free(nsdescs); -close_dev: - dev_close(dev); -ret: + return err; } @@ -3729,9 +3680,9 @@ static int id_ns(int argc, char **argv, struct command *cmd, struct plugin *plug const char *force = "Return this namespace, even if not attached (1.2 devices only)"; const char *vendor_specific = "dump binary vendor fields"; + _cleanup_nvme_dev_ struct nvme_dev *dev = NULL; + _cleanup_free_ struct nvme_id_ns *ns = NULL; enum nvme_print_flags flags; - struct nvme_id_ns ns; - struct nvme_dev *dev; int err; struct config { @@ -3739,7 +3690,6 @@ static int id_ns(int argc, char **argv, struct command *cmd, struct plugin *plug bool force; bool vendor_specific; bool raw_binary; - char *output_format; bool human_readable; }; @@ -3748,28 +3698,24 @@ static int id_ns(int argc, char **argv, struct command *cmd, struct plugin *plug .force = false, .vendor_specific = false, .raw_binary = false, - .output_format = "normal", .human_readable = false, }; - OPT_ARGS(opts) = { - OPT_UINT("namespace-id", 'n', &cfg.namespace_id, namespace_id_desired), - OPT_FLAG("force", 0, &cfg.force, force), - OPT_FLAG("vendor-specific", 'v', &cfg.vendor_specific, vendor_specific), - OPT_FLAG("raw-binary", 'b', &cfg.raw_binary, raw_identify), - OPT_FMT("output-format", 'o', &cfg.output_format, output_format), - OPT_FLAG("human-readable", 'H', &cfg.human_readable, human_readable_identify), - OPT_END() - }; + NVME_ARGS(opts, cfg, + OPT_UINT("namespace-id", 'n', &cfg.namespace_id, namespace_id_desired), + OPT_FLAG("force", 0, &cfg.force, force), + OPT_FLAG("vendor-specific", 'v', &cfg.vendor_specific, vendor_specific), + OPT_FLAG("raw-binary", 'b', &cfg.raw_binary, raw_identify), + OPT_FLAG("human-readable", 'H', &cfg.human_readable, human_readable_identify)); err = parse_and_open(&dev, argc, argv, desc, opts); if (err) - goto ret; + return err; - err = flags = validate_output_format(cfg.output_format); + err = flags = validate_output_format(output_format_val); if (err < 0) { nvme_show_error("Invalid output format"); - goto close_dev; + return err; } if (cfg.raw_binary) @@ -3785,24 +3731,26 @@ static int id_ns(int argc, char **argv, struct command *cmd, struct plugin *plug err = nvme_get_nsid(dev_fd(dev), &cfg.namespace_id); if (err < 0) { nvme_show_error("get-namespace-id: %s", nvme_strerror(errno)); - goto close_dev; + return err; } } + ns = nvme_alloc(sizeof(*ns)); + if (!ns) + return -ENOMEM; + if (cfg.force) - err = nvme_cli_identify_allocated_ns(dev, cfg.namespace_id, &ns); + err = nvme_cli_identify_allocated_ns(dev, cfg.namespace_id, ns); else - err = nvme_cli_identify_ns(dev, cfg.namespace_id, &ns); + err = nvme_cli_identify_ns(dev, cfg.namespace_id, ns); if (!err) - nvme_show_id_ns(&ns, cfg.namespace_id, 0, false, flags); + nvme_show_id_ns(ns, cfg.namespace_id, 0, false, flags); else if (err > 0) nvme_show_status(err); else nvme_show_error("identify namespace: %s", nvme_strerror(errno)); -close_dev: - dev_close(dev); -ret: + return err; } @@ -3812,41 +3760,37 @@ static int cmd_set_independent_id_ns(int argc, char **argv, struct command *cmd, const char *desc = "Send an I/O Command Set Independent Identify\n" "Namespace command to the given device, returns properties of the\n" "specified namespace in human-readable or binary or json format."; + + _cleanup_free_ struct nvme_id_independent_id_ns *ns = NULL; + _cleanup_nvme_dev_ struct nvme_dev *dev = NULL; enum nvme_print_flags flags; - struct nvme_id_independent_id_ns ns; - struct nvme_dev *dev; int err = -1; struct config { __u32 namespace_id; bool raw_binary; - char *output_format; bool human_readable; }; struct config cfg = { .namespace_id = 0, .raw_binary = false, - .output_format = "normal", .human_readable = false, }; - OPT_ARGS(opts) = { - OPT_UINT("namespace-id", 'n', &cfg.namespace_id, namespace_id_desired), - OPT_FLAG("raw-binary", 'b', &cfg.raw_binary, raw_identify), - OPT_FMT("output-format", 'o', &cfg.output_format, output_format), - OPT_FLAG("human-readable", 'H', &cfg.human_readable, human_readable_identify), - OPT_END() - }; + NVME_ARGS(opts, cfg, + OPT_UINT("namespace-id", 'n', &cfg.namespace_id, namespace_id_desired), + OPT_FLAG("raw-binary", 'b', &cfg.raw_binary, raw_identify), + OPT_FLAG("human-readable", 'H', &cfg.human_readable, human_readable_identify)); err = parse_and_open(&dev, argc, argv, desc, opts); if (err) - goto ret; + return err; - err = flags = validate_output_format(cfg.output_format); + err = flags = validate_output_format(output_format_val); if (err < 0) { nvme_show_error("Invalid output format"); - goto close_dev; + return err; } if (cfg.raw_binary) @@ -3859,21 +3803,23 @@ static int cmd_set_independent_id_ns(int argc, char **argv, struct command *cmd, err = cfg.namespace_id = nvme_get_nsid(dev_fd(dev), &cfg.namespace_id); if (err < 0) { nvme_show_perror("get-namespace-id"); - goto close_dev; + return err; } } - err = nvme_identify_independent_identify_ns(dev_fd(dev), cfg.namespace_id, &ns); + ns = nvme_alloc(sizeof(*ns)); + if (!ns) + return -ENOMEM; + + err = nvme_identify_independent_identify_ns(dev_fd(dev), cfg.namespace_id, ns); if (!err) - nvme_show_cmd_set_independent_id_ns(&ns, cfg.namespace_id, flags); + nvme_show_cmd_set_independent_id_ns(ns, cfg.namespace_id, flags); else if (err > 0) nvme_show_status(err); else nvme_show_error("I/O command set independent identify namespace: %s", nvme_strerror(errno)); -close_dev: - dev_close(dev); -ret: + return err; } @@ -3882,39 +3828,27 @@ static int id_ns_granularity(int argc, char **argv, struct command *cmd, struct const char *desc = "Send an Identify Namespace Granularity List command to the\n" "given device, returns namespace granularity list\n" "in either human-readable or binary format."; - struct nvme_id_ns_granularity_list *granularity_list; + + _cleanup_free_ struct nvme_id_ns_granularity_list *granularity_list = NULL; + _cleanup_nvme_dev_ struct nvme_dev *dev = NULL; enum nvme_print_flags flags; - struct nvme_dev *dev; int err; - struct config { - char *output_format; - }; - - struct config cfg = { - .output_format = "normal", - }; - - OPT_ARGS(opts) = { - OPT_FMT("output-format", 'o', &cfg.output_format, output_format), - OPT_END() - }; + NVME_ARGS(opts, cfg); err = parse_and_open(&dev, argc, argv, desc, opts); if (err) - goto ret; + return err; - err = flags = validate_output_format(cfg.output_format); + err = flags = validate_output_format(output_format_val); if (err < 0) { nvme_show_error("Invalid output format"); - goto close_dev; + return err; } - if (posix_memalign((void *)&granularity_list, getpagesize(), NVME_IDENTIFY_DATA_SIZE)) { - nvme_show_error("can not allocate granularity list payload"); - err = -ENOMEM; - goto close_dev; - } + granularity_list = nvme_alloc(NVME_IDENTIFY_DATA_SIZE); + if (!granularity_list) + return -ENOMEM; err = nvme_identify_ns_granularity(dev_fd(dev), granularity_list); if (!err) @@ -3923,10 +3857,7 @@ static int id_ns_granularity(int argc, char **argv, struct command *cmd, struct nvme_show_status(err); else nvme_show_error("identify namespace granularity: %s", nvme_strerror(errno)); - free(granularity_list); -close_dev: - dev_close(dev); -ret: + return err; } @@ -3937,48 +3868,45 @@ static int id_nvmset(int argc, char **argv, struct command *cmd, struct plugin * "than or equal to the value specified CDW11.NVMSETID\n" "in either binary format or json format"; const char *nvmset_id = "NVM Set Identify value"; - struct nvme_id_nvmset_list nvmset; + + _cleanup_free_ struct nvme_id_nvmset_list *nvmset = NULL; + _cleanup_nvme_dev_ struct nvme_dev *dev = NULL; enum nvme_print_flags flags; - struct nvme_dev *dev; int err; struct config { __u16 nvmset_id; - char *output_format; }; struct config cfg = { .nvmset_id = 0, - .output_format = "normal", }; - OPT_ARGS(opts) = { - OPT_SHRT("nvmset_id", 'i', &cfg.nvmset_id, nvmset_id), - OPT_FMT("output-format", 'o', &cfg.output_format, output_format), - OPT_END() - }; + NVME_ARGS(opts, cfg, + OPT_SHRT("nvmset_id", 'i', &cfg.nvmset_id, nvmset_id)); err = parse_and_open(&dev, argc, argv, desc, opts); if (err) - goto ret; + return err; - err = flags = validate_output_format(cfg.output_format); + err = flags = validate_output_format(output_format_val); if (err < 0) { nvme_show_error("Invalid output format"); - goto close_dev; + return err; } - err = nvme_identify_nvmset_list(dev_fd(dev), cfg.nvmset_id, &nvmset); + nvmset = nvme_alloc(sizeof(*nvmset)); + if (!nvmset) + return -ENOMEM; + + err = nvme_identify_nvmset_list(dev_fd(dev), cfg.nvmset_id, nvmset); if (!err) - nvme_show_id_nvmset(&nvmset, cfg.nvmset_id, flags); + nvme_show_id_nvmset(nvmset, cfg.nvmset_id, flags); else if (err > 0) nvme_show_status(err); else nvme_show_error("identify nvm set list: %s", nvme_strerror(errno)); -close_dev: - dev_close(dev); -ret: return err; } @@ -3989,38 +3917,34 @@ static int id_uuid(int argc, char **argv, struct command *cmd, struct plugin *pl "in either human-readable or binary format."; const char *raw = "show uuid in binary format"; const char *human_readable = "show uuid in readable format"; - struct nvme_id_uuid_list uuid_list; + + _cleanup_free_ struct nvme_id_uuid_list *uuid_list = NULL; + _cleanup_nvme_dev_ struct nvme_dev *dev = NULL; enum nvme_print_flags flags; - struct nvme_dev *dev; int err; struct config { - char *output_format; bool raw_binary; bool human_readable; }; struct config cfg = { - .output_format = "normal", .raw_binary = false, .human_readable = false, }; - OPT_ARGS(opts) = { - OPT_FMT("output-format", 'o', &cfg.output_format, output_format), - OPT_FLAG("raw-binary", 'b', &cfg.raw_binary, raw), - OPT_FLAG("human-readable", 'H', &cfg.human_readable, human_readable), - OPT_END() - }; + NVME_ARGS(opts, cfg, + OPT_FLAG("raw-binary", 'b', &cfg.raw_binary, raw), + OPT_FLAG("human-readable", 'H', &cfg.human_readable, human_readable)); err = parse_and_open(&dev, argc, argv, desc, opts); if (err) - goto ret; + return err; - err = flags = validate_output_format(cfg.output_format); + err = flags = validate_output_format(output_format_val); if (err < 0) { nvme_show_error("Invalid output format"); - goto close_dev; + return err; } if (cfg.raw_binary) @@ -4029,16 +3953,18 @@ static int id_uuid(int argc, char **argv, struct command *cmd, struct plugin *pl if (cfg.human_readable) flags |= VERBOSE; - err = nvme_identify_uuid(dev_fd(dev), &uuid_list); + uuid_list = nvme_alloc(sizeof(*uuid_list)); + if (!uuid_list) + return -ENOMEM; + + err = nvme_identify_uuid(dev_fd(dev), uuid_list); if (!err) - nvme_show_id_uuid_list(&uuid_list, flags); + nvme_show_id_uuid_list(uuid_list, flags); else if (err > 0) nvme_show_status(err); else nvme_show_error("identify UUID list: %s", nvme_strerror(errno)); -close_dev: - dev_close(dev); -ret: + return err; } @@ -4048,8 +3974,9 @@ static int id_iocs(int argc, char **argv, struct command *cmd, struct plugin *pl "the given device, returns properties of the specified controller\n" "in either human-readable or binary format."; const char *controller_id = "identifier of desired controller"; - struct nvme_id_iocs iocs; - struct nvme_dev *dev; + + _cleanup_free_ struct nvme_id_iocs *iocs = NULL; + _cleanup_nvme_dev_ struct nvme_dev *dev = NULL; int err; struct config { @@ -4060,27 +3987,27 @@ static int id_iocs(int argc, char **argv, struct command *cmd, struct plugin *pl .cntid = 0xffff, }; - OPT_ARGS(opts) = { - OPT_SHRT("controller-id", 'c', &cfg.cntid, controller_id), - OPT_END() - }; + NVME_ARGS(opts, cfg, + OPT_SHRT("controller-id", 'c', &cfg.cntid, controller_id)); err = parse_and_open(&dev, argc, argv, desc, opts); if (err) - goto ret; + return err; - err = nvme_identify_iocs(dev_fd(dev), cfg.cntid, &iocs); + iocs = nvme_alloc(sizeof(*iocs)); + if (!iocs) + return -ENOMEM; + + err = nvme_identify_iocs(dev_fd(dev), cfg.cntid, iocs); if (!err) { printf("NVMe Identify I/O Command Set:\n"); - nvme_show_id_iocs(&iocs, 0); + nvme_show_id_iocs(iocs, 0); } else if (err > 0) { nvme_show_status(err); } else { nvme_show_error("NVMe Identify I/O Command Set: %s", nvme_strerror(errno)); } - dev_close(dev); -ret: return err; } @@ -4090,82 +4017,74 @@ static int id_domain(int argc, char **argv, struct command *cmd, struct plugin * "given device, returns properties of the specified domain\n" "in either normal|json|binary format."; const char *domain_id = "identifier of desired domain"; - struct nvme_id_domain_list id_domain; + + _cleanup_free_ struct nvme_id_domain_list *id_domain = NULL; + _cleanup_nvme_dev_ struct nvme_dev *dev = NULL; enum nvme_print_flags flags; - struct nvme_dev *dev; int err; struct config { __u16 dom_id; - char *output_format; }; struct config cfg = { .dom_id = 0xffff, - .output_format = "normal", }; - OPT_ARGS(opts) = { - OPT_SHRT("dom-id", 'd', &cfg.dom_id, domain_id), - OPT_FMT("output-format", 'o', &cfg.output_format, output_format), - OPT_END() - }; + NVME_ARGS(opts, cfg, + OPT_SHRT("dom-id", 'd', &cfg.dom_id, domain_id)); err = parse_and_open(&dev, argc, argv, desc, opts); if (err) - goto ret; + return err; - err = flags = validate_output_format(cfg.output_format); + err = flags = validate_output_format(output_format_val); if (err < 0) { nvme_show_error("Invalid output format"); - goto close_dev; + return err; } - err = nvme_identify_domain_list(dev_fd(dev), cfg.dom_id, &id_domain); + id_domain = nvme_alloc(sizeof(*id_domain)); + if (!id_domain) + return -ENOMEM; + + err = nvme_identify_domain_list(dev_fd(dev), cfg.dom_id, id_domain); if (!err) { printf("NVMe Identify command for Domain List is successful:\n"); printf("NVMe Identify Domain List:\n"); - nvme_show_id_domain_list(&id_domain, flags); + nvme_show_id_domain_list(id_domain, flags); } else if (err > 0) { nvme_show_status(err); } else { nvme_show_error("NVMe Identify Domain List: %s", nvme_strerror(errno)); } -close_dev: - dev_close(dev); -ret: return err; } static int get_ns_id(int argc, char **argv, struct command *cmd, struct plugin *plugin) { const char *desc = "Get namespace ID of a the block device."; - struct nvme_dev *dev; + + _cleanup_nvme_dev_ struct nvme_dev *dev = NULL; unsigned int nsid; - int err = 0; + int err; - OPT_ARGS(opts) = { - OPT_END() - }; + NVME_ARGS(opts, cfg); err = parse_and_open(&dev, argc, argv, desc, opts); if (err) - goto ret; + return err; err = nvme_get_nsid(dev_fd(dev), &nsid); if (err < 0) { nvme_show_error("get namespace ID: %s", nvme_strerror(errno)); - err = errno; - goto close_fd; + return -errno; } - err = 0; + printf("%s: namespace-id:%d\n", dev->name, nsid); -close_fd: - dev_close(dev); -ret: - return err; + return 0; } static int virtual_mgmt(int argc, char **argv, struct command *cmd, struct plugin *plugin) @@ -4185,7 +4104,8 @@ static int virtual_mgmt(int argc, char **argv, struct command *cmd, struct plugi "8h: Secondary Assign\n" "9h: Secondary Online"; const char *nr = "Number of Controller Resources(NR)"; - struct nvme_dev *dev; + + _cleanup_nvme_dev_ struct nvme_dev *dev = NULL; __u32 result; int err; @@ -4203,17 +4123,15 @@ static int virtual_mgmt(int argc, char **argv, struct command *cmd, struct plugi .nr = 0, }; - OPT_ARGS(opts) = { - OPT_UINT("cntlid", 'c', &cfg.cntlid, cntlid), - OPT_BYTE("rt", 'r', &cfg.rt, rt), - OPT_BYTE("act", 'a', &cfg.act, act), - OPT_SHRT("nr", 'n', &cfg.nr, nr), - OPT_END() - }; + NVME_ARGS(opts, cfg, + OPT_UINT("cntlid", 'c', &cfg.cntlid, cntlid), + OPT_BYTE("rt", 'r', &cfg.rt, rt), + OPT_BYTE("act", 'a', &cfg.act, act), + OPT_SHRT("nr", 'n', &cfg.nr, nr)); err = parse_and_open(&dev, argc, argv, desc, opts); if (err) - goto ret; + return err; struct nvme_virtual_mgmt_args args = { .args_size = sizeof(args), @@ -4233,8 +4151,6 @@ static int virtual_mgmt(int argc, char **argv, struct command *cmd, struct plugi else nvme_show_error("virt-mgmt: %s", nvme_strerror(errno)); - dev_close(dev); -ret: return err; } @@ -4244,54 +4160,52 @@ static int primary_ctrl_caps(int argc, char **argv, struct command *cmd, struct const char *desc = "Send an Identify Primary Controller Capabilities\n" "command to the given device and report the information in a\n" "decoded format (default), json or binary."; - struct nvme_primary_ctrl_cap caps; + + _cleanup_free_ struct nvme_primary_ctrl_cap *caps = NULL; + _cleanup_nvme_dev_ struct nvme_dev *dev = NULL; enum nvme_print_flags flags; - struct nvme_dev *dev; int err; struct config { __u16 cntlid; - char *output_format; bool human_readable; }; struct config cfg = { .cntlid = 0, - .output_format = "normal", .human_readable = false, }; - OPT_ARGS(opts) = { - OPT_UINT("cntlid", 'c', &cfg.cntlid, cntlid), - OPT_FMT("output-format", 'o', &cfg.output_format, output_format), - OPT_FLAG("human-readable", 'H', &cfg.human_readable, human_readable_info), - OPT_END() - }; + NVME_ARGS(opts, cfg, + OPT_UINT("cntlid", 'c', &cfg.cntlid, cntlid), + OPT_FLAG("human-readable", 'H', &cfg.human_readable, human_readable_info)); err = parse_and_open(&dev, argc, argv, desc, opts); if (err) - goto ret; + return err; - err = flags = validate_output_format(cfg.output_format); + err = flags = validate_output_format(output_format_val); if (err < 0) { nvme_show_error("Invalid output format"); - goto close_dev; + return err; } if (cfg.human_readable) flags |= VERBOSE; - err = nvme_cli_identify_primary_ctrl(dev, cfg.cntlid, &caps); + caps = nvme_alloc(sizeof(*caps)); + if (!caps) + return -ENOMEM; + + err = nvme_cli_identify_primary_ctrl(dev, cfg.cntlid, caps); if (!err) - nvme_show_primary_ctrl_cap(&caps, flags); + nvme_show_primary_ctrl_cap(caps, flags); else if (err > 0) nvme_show_status(err); else nvme_show_error("identify primary controller capabilities: %s", nvme_strerror(errno)); -close_dev: - dev_close(dev); -ret: + return err; } @@ -4302,56 +4216,45 @@ static int list_secondary_ctrl(int argc, char **argv, struct command *cmd, struc const char *controller = "lowest controller identifier to display"; const char *num_entries = "number of entries to retrieve"; - struct nvme_secondary_ctrl_list *sc_list; + _cleanup_free_ struct nvme_secondary_ctrl_list *sc_list = NULL; + _cleanup_nvme_dev_ struct nvme_dev *dev = NULL; enum nvme_print_flags flags; - struct nvme_dev *dev; int err; struct config { __u16 cntid; - __u32 namespace_id; __u32 num_entries; - char *output_format; }; struct config cfg = { .cntid = 0, - .namespace_id = 0, .num_entries = ARRAY_SIZE(sc_list->sc_entry), - .output_format = "normal", }; - OPT_ARGS(opts) = { - OPT_SHRT("cntid", 'c', &cfg.cntid, controller), - OPT_UINT("namespace-id", 'n', &cfg.namespace_id, namespace_id_optional), - OPT_UINT("num-entries", 'e', &cfg.num_entries, num_entries), - OPT_FMT("output-format", 'o', &cfg.output_format, output_format), - OPT_END() - }; + NVME_ARGS(opts, cfg, + OPT_SHRT("cntid", 'c', &cfg.cntid, controller), + OPT_UINT("num-entries", 'e', &cfg.num_entries, num_entries)); err = parse_and_open(&dev, argc, argv, desc, opts); if (err) - goto ret; + return err; - err = flags = validate_output_format(cfg.output_format); + err = flags = validate_output_format(output_format_val); if (err < 0) { nvme_show_error("Invalid output format"); - goto close_err; + return err; } if (!cfg.num_entries) { nvme_show_error("non-zero num-entries is required param"); - err = -EINVAL; - goto close_err; + return -EINVAL; } - if (posix_memalign((void *)&sc_list, getpagesize(), sizeof(*sc_list))) { - nvme_show_error("can not allocate controller list payload"); - err = -ENOMEM; - goto close_err; - } + sc_list = nvme_alloc(sizeof(*sc_list)); + if (!sc_list) + return -ENOMEM; - err = nvme_cli_identify_secondary_ctrl_list(dev, cfg.namespace_id, cfg.cntid, sc_list); + err = nvme_cli_identify_secondary_ctrl_list(dev, cfg.cntid, sc_list); if (!err) nvme_show_list_secondary_ctrl(sc_list, cfg.num_entries, flags); else if (err > 0) @@ -4359,11 +4262,6 @@ static int list_secondary_ctrl(int argc, char **argv, struct command *cmd, struc else nvme_show_error("id secondary controller list: %s", nvme_strerror(errno)); - free(sc_list); - -close_err: - dev_close(dev); -ret: return err; } @@ -4389,20 +4287,28 @@ static int sleep_self_test(unsigned int seconds) static int wait_self_test(struct nvme_dev *dev) { static const char spin[] = {'-', '\\', '|', '/' }; - struct nvme_self_test_log log; - struct nvme_id_ctrl ctrl; + _cleanup_free_ struct nvme_self_test_log *log = NULL; + _cleanup_free_ struct nvme_id_ctrl *ctrl = NULL; int err, i = 0, p = 0, cnt = 0; int wthr; signal(SIGINT, intr_self_test); - err = nvme_cli_identify_ctrl(dev, &ctrl); + ctrl = nvme_alloc(sizeof(*ctrl)); + if (!ctrl) + return -ENOMEM; + + log = nvme_alloc(sizeof(*log)); + if (!log) + return -ENOMEM; + + err = nvme_cli_identify_ctrl(dev, ctrl); if (err) { nvme_show_error("identify-ctrl: %s", nvme_strerror(errno)); return err; } - wthr = le16_to_cpu(ctrl.edstt) * 60 / 100 + 60; + wthr = le16_to_cpu(ctrl->edstt) * 60 / 100 + 60; printf("Waiting for self test completion...\n"); while (true) { @@ -4412,7 +4318,7 @@ static int wait_self_test(struct nvme_dev *dev) if (err) return err; - err = nvme_cli_get_log_device_self_test(dev, &log); + err = nvme_cli_get_log_device_self_test(dev, log); if (err) { printf("\n"); if (err < 0) @@ -4427,17 +4333,17 @@ static int wait_self_test(struct nvme_dev *dev) return -EIO; } - if (log.completion == 0 && p > 0) { + if (log->completion == 0 && p > 0) { printf("\r[%.*s] %3d%%\n", 50, dash, 100); break; } - if (log.completion < p) { + if (log->completion < p) { printf("\n"); - nvme_show_error("progress broken"); - return -EIO; - } else if (log.completion != p) { - p = log.completion; + nvme_show_error("progress broken"); + return -EIO; + } else if (log->completion != p) { + p = log->completion; cnt = 0; } @@ -4476,7 +4382,8 @@ static int device_self_test(int argc, char **argv, struct command *cmd, struct p "eh Start a vendor specific device self-test operation\n" "fh Abort the device self-test operation"; const char *wait = "Wait for the test to finish"; - struct nvme_dev *dev; + + _cleanup_nvme_dev_ struct nvme_dev *dev = NULL; int err; struct config { @@ -4491,21 +4398,23 @@ static int device_self_test(int argc, char **argv, struct command *cmd, struct p .wait = false, }; - OPT_ARGS(opts) = { - OPT_UINT("namespace-id", 'n', &cfg.namespace_id, namespace_id), - OPT_BYTE("self-test-code", 's', &cfg.stc, self_test_code), - OPT_FLAG("wait", 'w', &cfg.wait, wait), - OPT_END() - }; + NVME_ARGS(opts, cfg, + OPT_UINT("namespace-id", 'n', &cfg.namespace_id, namespace_id), + OPT_BYTE("self-test-code", 's', &cfg.stc, self_test_code), + OPT_FLAG("wait", 'w', &cfg.wait, wait)); err = parse_and_open(&dev, argc, argv, desc, opts); if (err) - goto ret; + return err; if (cfg.stc == NVME_ST_CODE_RESERVED) { - struct nvme_self_test_log log; + _cleanup_free_ struct nvme_self_test_log *log = NULL; + + log = nvme_alloc(sizeof(*log)); + if (!log) + return -ENOMEM; - err = nvme_cli_get_log_device_self_test(dev, &log); + err = nvme_cli_get_log_device_self_test(dev, log); if (err) { printf("\n"); if (err < 0) @@ -4514,15 +4423,16 @@ static int device_self_test(int argc, char **argv, struct command *cmd, struct p nvme_show_status(err); } - if (log.completion == 0) { + if (log->completion == 0) { printf("no self test running\n"); } else { if (cfg.wait) err = wait_self_test(dev); else - printf("progress %d%%\n", log.completion); + printf("progress %d%%\n", log->completion); } - goto close_dev; + + goto check_abort; } struct nvme_dev_self_test_args args = { @@ -4550,12 +4460,10 @@ static int device_self_test(int argc, char **argv, struct command *cmd, struct p nvme_show_error("Device self-test: %s", nvme_strerror(errno)); } -close_dev: +check_abort: if (err == -EINTR) abort_self_test(&args); - dev_close(dev); -ret: return err; } @@ -4565,61 +4473,54 @@ static int self_test_log(int argc, char **argv, struct command *cmd, struct plug "(or optionally a namespace) in either decoded format (default) or binary."; const char *dst_entries = "Indicate how many DST log entries to be retrieved,\n" "by default all the 20 entries will be retrieved"; - struct nvme_self_test_log log; + + _cleanup_free_ struct nvme_self_test_log *log = NULL; + _cleanup_nvme_dev_ struct nvme_dev *dev = NULL; enum nvme_print_flags flags; - struct nvme_dev *dev; int err; struct config { __u8 dst_entries; - char *output_format; - bool verbose; }; struct config cfg = { .dst_entries = NVME_LOG_ST_MAX_RESULTS, - .output_format = "normal", - .verbose = false, }; - OPT_ARGS(opts) = { - OPT_BYTE("dst-entries", 'e', &cfg.dst_entries, dst_entries), - OPT_FMT("output-format", 'o', &cfg.output_format, output_format), - OPT_FLAG("verbose", 'v', &cfg.verbose, verbose), - OPT_END() - }; + NVME_ARGS(opts, cfg, + OPT_BYTE("dst-entries", 'e', &cfg.dst_entries, dst_entries)); err = parse_and_open(&dev, argc, argv, desc, opts); if (err) - goto ret; + return err; - err = flags = validate_output_format(cfg.output_format); + err = flags = validate_output_format(output_format_val); if (err < 0) { nvme_show_error("Invalid output format"); - goto close_dev; + return err; } - if (cfg.verbose) + if (argconfig_parse_seen(opts, "verbose")) flags |= VERBOSE; - err = nvme_cli_get_log_device_self_test(dev, &log); + log = nvme_alloc(sizeof(*log)); + if (!log) + return -ENOMEM; + + err = nvme_cli_get_log_device_self_test(dev, log); if (!err) - nvme_show_self_test_log(&log, cfg.dst_entries, 0, dev->name, flags); + nvme_show_self_test_log(log, cfg.dst_entries, 0, dev->name, flags); else if (err > 0) nvme_show_status(err); else nvme_show_error("self test log: %s", nvme_strerror(errno)); -close_dev: - dev_close(dev); -ret: + return err; } static int get_feature_id(struct nvme_dev *dev, struct feat_cfg *cfg, void **buf, __u32 *result) { - size_t size; - if (!cfg->data_len) nvme_get_feature_length(cfg->feature_id, cfg->cdw11, &cfg->data_len); @@ -4637,11 +4538,9 @@ static int get_feature_id(struct nvme_dev *dev, struct feat_cfg *cfg, cfg->data_len = 0; if (cfg->data_len) { - /* rounding up size to page size */ - size = ((cfg->data_len - 1) / getpagesize() + 1) * getpagesize(); - if (posix_memalign(buf, getpagesize(), size)) + *buf = nvme_alloc(cfg->data_len - 1); + if (!*buf) return -1; - memset(*buf, 0, size); } struct nvme_get_features_args args = { @@ -4786,7 +4685,8 @@ static int get_feature(int argc, char **argv, struct command *cmd, const char *sel = "[0-3,8]: current/default/saved/supported/changed"; const char *cdw11 = "feature specific dword 11"; const char *human_readable = "show feature in readable format"; - struct nvme_dev *dev; + + _cleanup_nvme_dev_ struct nvme_dev *dev = NULL; int err; struct feat_cfg cfg = { @@ -4800,28 +4700,26 @@ static int get_feature(int argc, char **argv, struct command *cmd, .human_readable = false, }; - OPT_ARGS(opts) = { - OPT_BYTE("feature-id", 'f', &cfg.feature_id, feature_id), - OPT_UINT("namespace-id", 'n', &cfg.namespace_id, namespace_id_desired), - OPT_BYTE("sel", 's', &cfg.sel, sel), - OPT_UINT("data-len", 'l', &cfg.data_len, buf_len), - OPT_FLAG("raw-binary", 'b', &cfg.raw_binary, raw), - OPT_UINT("cdw11", 'c', &cfg.cdw11, cdw11), - OPT_BYTE("uuid-index", 'U', &cfg.uuid_index, uuid_index_specify), - OPT_FLAG("human-readable", 'H', &cfg.human_readable, human_readable), - OPT_END() - }; + NVME_ARGS(opts, cfg, + OPT_BYTE("feature-id", 'f', &cfg.feature_id, feature_id), + OPT_UINT("namespace-id", 'n', &cfg.namespace_id, namespace_id_desired), + OPT_BYTE("sel", 's', &cfg.sel, sel), + OPT_UINT("data-len", 'l', &cfg.data_len, buf_len), + OPT_FLAG("raw-binary", 'b', &cfg.raw_binary, raw), + OPT_UINT("cdw11", 'c', &cfg.cdw11, cdw11), + OPT_BYTE("uuid-index", 'U', &cfg.uuid_index, uuid_index_specify), + OPT_FLAG("human-readable", 'H', &cfg.human_readable, human_readable)); err = parse_and_open(&dev, argc, argv, desc, opts); if (err) - goto ret; + return err; if (!argconfig_parse_seen(opts, "namespace-id")) { err = nvme_get_nsid(dev_fd(dev), &cfg.namespace_id); if (err < 0) { if (errno != ENOTTY) { nvme_show_error("get-namespace-id: %s", nvme_strerror(errno)); - goto close_dev; + return err; } cfg.namespace_id = NVME_NSID_ALL; } @@ -4829,22 +4727,16 @@ static int get_feature(int argc, char **argv, struct command *cmd, if (cfg.sel > 8) { nvme_show_error("invalid 'select' param:%d", cfg.sel); - err = -EINVAL; - goto close_dev; + return -EINVAL; } if (cfg.uuid_index > 127) { nvme_show_error("invalid uuid index param: %u", cfg.uuid_index); - err = -1; - goto close_dev; + return -1; } err = get_feature_ids(dev, cfg); -close_dev: - dev_close(dev); - -ret: return err; } @@ -4960,9 +4852,11 @@ static int fw_download(int argc, char **argv, struct command *cmd, struct plugin const char *offset = "starting dword offset, default 0"; const char *progress = "display firmware transfer progress"; const char *ignore_ovr = "ignore overwrite errors"; - unsigned int fw_size; - struct nvme_dev *dev; - int err, fw_fd = -1; + + _cleanup_nvme_dev_ struct nvme_dev *dev = NULL; + _cleanup_file_ int fw_fd = -1; + unsigned int fw_size, pos; + int err; struct stat sb; void *fw_buf; bool huge; @@ -4984,45 +4878,41 @@ static int fw_download(int argc, char **argv, struct command *cmd, struct plugin .ignore_ovr = false, }; - OPT_ARGS(opts) = { - OPT_FILE("fw", 'f', &cfg.fw, fw), - OPT_UINT("xfer", 'x', &cfg.xfer, xfer), - OPT_UINT("offset", 'o', &cfg.offset, offset), - OPT_FLAG("progress", 'p', &cfg.progress, progress), - OPT_FLAG("ignore-ovr", 'i', &cfg.ignore_ovr, ignore_ovr), - OPT_END() - }; + NVME_ARGS(opts, cfg, + OPT_FILE("fw", 'f', &cfg.fw, fw), + OPT_UINT("xfer", 'x', &cfg.xfer, xfer), + OPT_UINT("offset", 'o', &cfg.offset, offset), + OPT_FLAG("progress", 'p', &cfg.progress, progress), + OPT_FLAG("ignore-ovr", 'i', &cfg.ignore_ovr, ignore_ovr)); err = parse_and_open(&dev, argc, argv, desc, opts); if (err) - goto ret; + return err; fw_fd = open(cfg.fw, O_RDONLY); cfg.offset <<= 2; if (fw_fd < 0) { nvme_show_error("Failed to open firmware file %s: %s", cfg.fw, strerror(errno)); - err = -EINVAL; - goto close_dev; + return -EINVAL; } err = fstat(fw_fd, &sb); if (err < 0) { nvme_show_perror("fstat"); - goto close_fw_fd; + return err; } fw_size = sb.st_size; if ((fw_size & 0x3) || (fw_size == 0)) { nvme_show_error("Invalid size:%d for f/w image", fw_size); - err = -EINVAL; - goto close_fw_fd; + return -EINVAL; } if (cfg.xfer == 0) { err = nvme_cli_identify_ctrl(dev, &ctrl); if (err) { nvme_show_error("identify-ctrl: %s", nvme_strerror(errno)); - goto close_fw_fd; + return err; } if (ctrl.fwug == 0 || ctrl.fwug == 0xff) cfg.xfer = 4096; @@ -5031,15 +4921,9 @@ static int fw_download(int argc, char **argv, struct command *cmd, struct plugin } else if (cfg.xfer % 4096) cfg.xfer = 4096; - if (cfg.xfer < HUGE_MIN) - fw_buf = __nvme_alloc(fw_size, &huge); - else - fw_buf = nvme_alloc(fw_size, &huge); - - if (!fw_buf) { - err = -ENOMEM; - goto close_fw_fd; - } + fw_buf = nvme_alloc_huge(fw_size, &huge); + if (!fw_buf) + return -ENOMEM; if (read(fw_fd, fw_buf, fw_size) != ((ssize_t)(fw_size))) { err = -errno; @@ -5047,16 +4931,14 @@ static int fw_download(int argc, char **argv, struct command *cmd, struct plugin goto free; } - while (cfg.offset < fw_size) { - cfg.xfer = min(cfg.xfer, fw_size); + for (pos = 0; pos < fw_size; pos += cfg.xfer) { + cfg.xfer = min(cfg.xfer, fw_size - pos); - err = fw_download_single(dev, fw_buf + cfg.offset, fw_size, - cfg.offset, cfg.xfer, cfg.progress, - cfg.ignore_ovr); + err = fw_download_single(dev, fw_buf + pos, fw_size, + cfg.offset + pos, cfg.xfer, + cfg.progress, cfg.ignore_ovr); if (err) break; - - cfg.offset += cfg.xfer; } if (!err) { @@ -5067,12 +4949,8 @@ static int fw_download(int argc, char **argv, struct command *cmd, struct plugin } free: - nvme_free(fw_buf, huge); -close_fw_fd: - close(fw_fd); -close_dev: - dev_close(dev); -ret: + nvme_free_huge(fw_buf, huge); + return err; } @@ -5092,14 +4970,18 @@ static char *nvme_fw_status_reset_type(__u16 status) static bool fw_commit_support_mud(struct nvme_dev *dev) { - struct nvme_id_ctrl ctrl; + _cleanup_free_ struct nvme_id_ctrl *ctrl = NULL; int err; - err = nvme_cli_identify_ctrl(dev, &ctrl); + ctrl = nvme_alloc(sizeof(*ctrl)); + if (!ctrl) + return false; + + err = nvme_cli_identify_ctrl(dev, ctrl); if (err) nvme_show_error("identify-ctrl: %s", nvme_strerror(errno)); - else if (ctrl.frmw >> 5 & 0x1) + else if (ctrl->frmw >> 5 & 0x1) return true; return false; @@ -5131,7 +5013,8 @@ static int fw_commit(int argc, char **argv, struct command *cmd, struct plugin * const char *slot = "[0-7]: firmware slot for commit action"; const char *action = "[0-7]: commit action"; const char *bpid = "[0,1]: boot partition identifier, if applicable (default: 0)"; - struct nvme_dev *dev; + + _cleanup_nvme_dev_ struct nvme_dev *dev = NULL; __u32 result; int err; @@ -5147,31 +5030,26 @@ static int fw_commit(int argc, char **argv, struct command *cmd, struct plugin * .bpid = 0, }; - OPT_ARGS(opts) = { - OPT_BYTE("slot", 's', &cfg.slot, slot), - OPT_BYTE("action", 'a', &cfg.action, action), - OPT_BYTE("bpid", 'b', &cfg.bpid, bpid), - OPT_END() - }; + NVME_ARGS(opts, cfg, + OPT_BYTE("slot", 's', &cfg.slot, slot), + OPT_BYTE("action", 'a', &cfg.action, action), + OPT_BYTE("bpid", 'b', &cfg.bpid, bpid)); err = parse_and_open(&dev, argc, argv, desc, opts); if (err) - goto ret; + return err; if (cfg.slot > 7) { nvme_show_error("invalid slot:%d", cfg.slot); - err = -EINVAL; - goto close_dev; + return -EINVAL; } if (cfg.action > 7 || cfg.action == 4 || cfg.action == 5) { nvme_show_error("invalid action:%d", cfg.action); - err = -EINVAL; - goto close_dev; + return -EINVAL; } if (cfg.bpid > 1) { nvme_show_error("invalid boot partition id:%d", cfg.bpid); - err = -EINVAL; - goto close_dev; + return -EINVAL; } struct nvme_fw_commit_args args = { @@ -5218,25 +5096,21 @@ static int fw_commit(int argc, char **argv, struct command *cmd, struct plugin * fw_commit_print_mud(dev, result); } -close_dev: - dev_close(dev); -ret: return err; } static int subsystem_reset(int argc, char **argv, struct command *cmd, struct plugin *plugin) { const char *desc = "Resets the NVMe subsystem\n"; - struct nvme_dev *dev; + + _cleanup_nvme_dev_ struct nvme_dev *dev = NULL; int err; - OPT_ARGS(opts) = { - OPT_END() - }; + NVME_ARGS(opts, cfg); err = parse_and_open(&dev, argc, argv, desc, opts); if (err) - goto ret; + return err; err = nvme_subsystem_reset(dev_fd(dev)); if (err < 0) { @@ -5246,54 +5120,46 @@ static int subsystem_reset(int argc, char **argv, struct command *cmd, struct pl nvme_show_error("Subsystem-reset: %s", nvme_strerror(errno)); } - dev_close(dev); -ret: return err; } static int reset(int argc, char **argv, struct command *cmd, struct plugin *plugin) { const char *desc = "Resets the NVMe controller\n"; - struct nvme_dev *dev; + + _cleanup_nvme_dev_ struct nvme_dev *dev = NULL; int err; - OPT_ARGS(opts) = { - OPT_END() - }; + NVME_ARGS(opts, cfg); err = parse_and_open(&dev, argc, argv, desc, opts); if (err) - goto ret; + return err; err = nvme_ctrl_reset(dev_fd(dev)); if (err < 0) nvme_show_error("Reset: %s", nvme_strerror(errno)); - dev_close(dev); -ret: return err; } static int ns_rescan(int argc, char **argv, struct command *cmd, struct plugin *plugin) { const char *desc = "Rescans the NVMe namespaces\n"; - struct nvme_dev *dev; + + _cleanup_nvme_dev_ struct nvme_dev *dev = NULL; int err; - OPT_ARGS(opts) = { - OPT_END() - }; + NVME_ARGS(opts, cfg); err = parse_and_open(&dev, argc, argv, desc, opts); if (err) - goto ret; + return err; err = nvme_ns_rescan(dev_fd(dev)); if (err < 0) nvme_show_error("Namespace Rescan"); - dev_close(dev); -ret: return err; } @@ -5306,7 +5172,8 @@ static int sanitize_cmd(int argc, char **argv, struct command *cmd, struct plugi const char *ause_desc = "Allow unrestricted sanitize exit."; const char *sanact_desc = "Sanitize action: 1 = Exit failure mode, 2 = Start block erase, 3 = Start overwrite, 4 = Start crypto erase"; const char *ovrpat_desc = "Overwrite pattern."; - struct nvme_dev *dev; + + _cleanup_nvme_dev_ struct nvme_dev *dev = NULL; int err; struct config { @@ -5335,19 +5202,17 @@ static int sanitize_cmd(int argc, char **argv, struct command *cmd, struct plugi VAL_END() }; - OPT_ARGS(opts) = { - OPT_FLAG("no-dealloc", 'd', &cfg.no_dealloc, no_dealloc_desc), - OPT_FLAG("oipbp", 'i', &cfg.oipbp, oipbp_desc), - OPT_BYTE("owpass", 'n', &cfg.owpass, owpass_desc), - OPT_FLAG("ause", 'u', &cfg.ause, ause_desc), - OPT_BYTE("sanact", 'a', &cfg.sanact, sanact_desc, sanact), - OPT_UINT("ovrpat", 'p', &cfg.ovrpat, ovrpat_desc), - OPT_END() - }; + NVME_ARGS(opts, cfg, + OPT_FLAG("no-dealloc", 'd', &cfg.no_dealloc, no_dealloc_desc), + OPT_FLAG("oipbp", 'i', &cfg.oipbp, oipbp_desc), + OPT_BYTE("owpass", 'n', &cfg.owpass, owpass_desc), + OPT_FLAG("ause", 'u', &cfg.ause, ause_desc), + OPT_BYTE("sanact", 'a', &cfg.sanact, sanact_desc, sanact), + OPT_UINT("ovrpat", 'p', &cfg.ovrpat, ovrpat_desc)); err = parse_and_open(&dev, argc, argv, desc, opts); if (err) - goto ret; + return err; switch (cfg.sanact) { case NVME_SANITIZE_SANACT_EXIT_FAILURE: @@ -5357,29 +5222,25 @@ static int sanitize_cmd(int argc, char **argv, struct command *cmd, struct plugi break; default: nvme_show_error("Invalid Sanitize Action"); - err = -EINVAL; - goto close_dev; + return -EINVAL; } if (cfg.sanact == NVME_SANITIZE_SANACT_EXIT_FAILURE) { if (cfg.ause || cfg.no_dealloc) { nvme_show_error("SANACT is Exit Failure Mode"); - err = -EINVAL; - goto close_dev; + return -EINVAL; } } if (cfg.sanact == NVME_SANITIZE_SANACT_START_OVERWRITE) { if (cfg.owpass > 15) { nvme_show_error("OWPASS out of range [0-15]"); - err = -EINVAL; - goto close_dev; + return -EINVAL; } } else { if (cfg.owpass || cfg.oipbp || cfg.ovrpat) { nvme_show_error("SANACT is not Overwrite"); - err = -EINVAL; - goto close_dev; + return -EINVAL; } } @@ -5399,9 +5260,6 @@ static int sanitize_cmd(int argc, char **argv, struct command *cmd, struct plugi else if (err > 0) nvme_show_status(err); -close_dev: - dev_close(dev); -ret: return err; } @@ -5503,38 +5361,34 @@ static int show_registers(int argc, char **argv, struct command *cmd, struct plu "in binary or human-readable format"; const char *human_readable = "show info in readable format in case of output_format == normal"; + + _cleanup_nvme_dev_ struct nvme_dev *dev = NULL; enum nvme_print_flags flags; - struct nvme_dev *dev; bool fabrics = false; nvme_root_t r; void *bar; int err; struct config { - char *output_format; bool human_readable; }; struct config cfg = { - .output_format = "normal", .human_readable = false, }; - OPT_ARGS(opts) = { - OPT_FMT("output-format", 'o', &cfg.output_format, output_format), - OPT_FLAG("human-readable", 'H', &cfg.human_readable, human_readable), - OPT_END() - }; + NVME_ARGS(opts, cfg, + OPT_FLAG("human-readable", 'H', &cfg.human_readable, human_readable)); err = parse_and_open(&dev, argc, argv, desc, opts); if (err) - goto ret; + return err; r = nvme_scan(NULL); - err = flags = validate_output_format(cfg.output_format); + err = flags = validate_output_format(output_format_val); if (err < 0) { nvme_show_error("Invalid output format"); - goto close_dev; + goto free_tree; } if (cfg.human_readable) @@ -5544,7 +5398,7 @@ static int show_registers(int argc, char **argv, struct command *cmd, struct plu if (!bar) { err = nvme_get_properties(dev_fd(dev), &bar); if (err) - goto close_dev; + goto free_tree; fabrics = true; } @@ -5553,10 +5407,8 @@ static int show_registers(int argc, char **argv, struct command *cmd, struct plu free(bar); else munmap(bar, getpagesize()); -close_dev: - dev_close(dev); +free_tree: nvme_free_tree(r); -ret: return err; } @@ -5567,7 +5419,8 @@ static int get_property(int argc, char **argv, struct command *cmd, struct plugi "CAP=0x0, VS=0x8, CC=0x14, CSTS=0x1c, NSSR=0x20"; const char *offset = "offset of the requested property"; const char *human_readable = "show property in readable format"; - struct nvme_dev *dev; + + _cleanup_nvme_dev_ struct nvme_dev *dev = NULL; __u64 value; int err; @@ -5581,20 +5434,17 @@ static int get_property(int argc, char **argv, struct command *cmd, struct plugi .human_readable = false, }; - OPT_ARGS(opts) = { - OPT_UINT("offset", 'o', &cfg.offset, offset), - OPT_FLAG("human-readable", 'H', &cfg.human_readable, human_readable), - OPT_END() - }; + NVME_ARGS(opts, cfg, + OPT_UINT("offset", 'o', &cfg.offset, offset), + OPT_FLAG("human-readable", 'H', &cfg.human_readable, human_readable)); err = parse_and_open(&dev, argc, argv, desc, opts); if (err) - goto ret; + return err; if (cfg.offset == -1) { nvme_show_error("offset required param"); - err = -EINVAL; - goto close_dev; + return -EINVAL; } struct nvme_get_property_args args = { @@ -5612,9 +5462,6 @@ static int get_property(int argc, char **argv, struct command *cmd, struct plugi else if (err > 0) nvme_show_status(err); -close_dev: - dev_close(dev); -ret: return err; } @@ -5624,7 +5471,8 @@ static int set_property(int argc, char **argv, struct command *cmd, struct plugi "Writes and shows the defined NVMe controller property for NVMe over Fabric"; const char *offset = "the offset of the property"; const char *value = "the value of the property to be set"; - struct nvme_dev *dev; + + _cleanup_nvme_dev_ struct nvme_dev *dev = NULL; int err; struct config { @@ -5637,25 +5485,21 @@ static int set_property(int argc, char **argv, struct command *cmd, struct plugi .value = -1, }; - OPT_ARGS(opts) = { - OPT_UINT("offset", 'o', &cfg.offset, offset), - OPT_UINT("value", 'v', &cfg.value, value), - OPT_END() - }; + NVME_ARGS(opts, cfg, + OPT_UINT("offset", 'o', &cfg.offset, offset), + OPT_UINT("value", 'v', &cfg.value, value)); err = parse_and_open(&dev, argc, argv, desc, opts); if (err) - goto ret; + return err; if (cfg.offset == -1) { nvme_show_error("offset required param"); - err = -EINVAL; - goto close_dev; + return -EINVAL; } if (cfg.value == -1) { nvme_show_error("value required param"); - err = -EINVAL; - goto close_dev; + return -EINVAL; } struct nvme_set_property_args args = { @@ -5675,9 +5519,6 @@ static int set_property(int argc, char **argv, struct command *cmd, struct plugi else if (err > 0) nvme_show_status(err); -close_dev: - dev_close(dev); -ret: return err; } @@ -5695,9 +5536,10 @@ static int format_cmd(int argc, char **argv, struct command *cmd, struct plugin const char *reset = "Automatically reset the controller after successful format"; const char *bs = "target block size"; const char *force = "The \"I know what I'm doing\" flag, skip confirmation before sending command"; - struct nvme_id_ns ns; - struct nvme_id_ctrl ctrl; - struct nvme_dev *dev; + + _cleanup_free_ struct nvme_id_ctrl *ctrl = NULL; + _cleanup_free_ struct nvme_id_ns *ns = NULL; + _cleanup_nvme_dev_ struct nvme_dev *dev = NULL; __u8 prev_lbaf = 0; int block_size; int err, i; @@ -5728,23 +5570,21 @@ static int format_cmd(int argc, char **argv, struct command *cmd, struct plugin .bs = 0, }; - OPT_ARGS(opts) = { - OPT_UINT("namespace-id", 'n', &cfg.namespace_id, namespace_id_desired), - OPT_UINT("timeout", 't', &cfg.timeout, timeout), - OPT_BYTE("lbaf", 'l', &cfg.lbaf, lbaf), - OPT_BYTE("ses", 's', &cfg.ses, ses), - OPT_BYTE("pi", 'i', &cfg.pi, pi), - OPT_BYTE("pil", 'p', &cfg.pil, pil), - OPT_BYTE("ms", 'm', &cfg.ms, ms), - OPT_FLAG("reset", 'r', &cfg.reset, reset), - OPT_FLAG("force", 0, &cfg.force, force), - OPT_SUFFIX("block-size", 'b', &cfg.bs, bs), - OPT_END() - }; + NVME_ARGS(opts, cfg, + OPT_UINT("namespace-id", 'n', &cfg.namespace_id, namespace_id_desired), + OPT_UINT("timeout", 't', &cfg.timeout, timeout), + OPT_BYTE("lbaf", 'l', &cfg.lbaf, lbaf), + OPT_BYTE("ses", 's', &cfg.ses, ses), + OPT_BYTE("pi", 'i', &cfg.pi, pi), + OPT_BYTE("pil", 'p', &cfg.pil, pil), + OPT_BYTE("ms", 'm', &cfg.ms, ms), + OPT_FLAG("reset", 'r', &cfg.reset, reset), + OPT_FLAG("force", 0, &cfg.force, force), + OPT_SUFFIX("block-size", 'b', &cfg.bs, bs)); err = argconfig_parse(argc, argv, desc, opts); if (err) - goto ret; + return err; err = open_exclusive(&dev, argc, argv, cfg.force); if (err) { @@ -5756,32 +5596,34 @@ static int format_cmd(int argc, char **argv, struct command *cmd, struct plugin } else { argconfig_print_help(desc, opts); } - goto ret; + return err; } if (cfg.lbaf != 0xff && cfg.bs != 0) { nvme_show_error( "Invalid specification of both LBAF and Block Size, please specify only one"); - err = -EINVAL; - goto close_dev; + return -EINVAL; } if (cfg.bs) { if ((cfg.bs & (~cfg.bs + 1)) != cfg.bs) { nvme_show_error( "Invalid value for block size (%"PRIu64"), must be a power of two", (uint64_t) cfg.bs); - err = -EINVAL; - goto close_dev; + return -EINVAL; } } - err = nvme_cli_identify_ctrl(dev, &ctrl); + ctrl = nvme_alloc(sizeof(*ctrl)); + if (!ctrl) + return -ENOMEM; + + err = nvme_cli_identify_ctrl(dev, ctrl); if (err) { nvme_show_error("identify-ctrl: %s", nvme_strerror(errno)); - goto close_dev; + return -errno; } - if ((ctrl.fna & 1) == 1) { + if ((ctrl->fna & 1) == 1) { /* * FNA bit 0 set to 1: all namespaces ... shall be configured with the same * attributes and a format (excluding secure erase) of any namespace results in a @@ -5792,7 +5634,7 @@ static int format_cmd(int argc, char **argv, struct command *cmd, struct plugin err = nvme_get_nsid(dev_fd(dev), &cfg.namespace_id); if (err < 0) { nvme_show_error("get-namespace-id: %s", nvme_strerror(errno)); - goto close_dev; + return -errno; } } @@ -5800,12 +5642,15 @@ static int format_cmd(int argc, char **argv, struct command *cmd, struct plugin nvme_show_error( "Invalid namespace ID, specify a namespace to format or use\n" "'-n 0xffffffff' to format all namespaces on this controller."); - err = -EINVAL; - goto close_dev; + return -EINVAL; } if (cfg.namespace_id != NVME_NSID_ALL) { - err = nvme_cli_identify_ns(dev, cfg.namespace_id, &ns); + ns = nvme_alloc(sizeof(*ns)); + if (!ns) + return -ENOMEM; + + err = nvme_cli_identify_ns(dev, cfg.namespace_id, ns); if (err) { if (err < 0) { nvme_show_error("identify-namespace: %s", nvme_strerror(errno)); @@ -5813,13 +5658,13 @@ static int format_cmd(int argc, char **argv, struct command *cmd, struct plugin fprintf(stderr, "identify failed\n"); nvme_show_status(err); } - goto close_dev; + return err; } - nvme_id_ns_flbas_to_lbaf_inuse(ns.flbas, &prev_lbaf); + nvme_id_ns_flbas_to_lbaf_inuse(ns->flbas, &prev_lbaf); if (cfg.bs) { - for (i = 0; i <= ns.nlbaf; ++i) { - if ((1ULL << ns.lbaf[i].ds) == cfg.bs && ns.lbaf[i].ms == 0) { + for (i = 0; i <= ns->nlbaf; ++i) { + if ((1ULL << ns->lbaf[i].ds) == cfg.bs && ns->lbaf[i].ms == 0) { cfg.lbaf = i; break; } @@ -5830,8 +5675,7 @@ static int format_cmd(int argc, char **argv, struct command *cmd, struct plugin (uint64_t)cfg.bs); fprintf(stderr, "Please correct block size, or specify LBAF directly\n"); - err = -EINVAL; - goto close_dev; + return -EINVAL; } } else if (cfg.lbaf == 0xff) { cfg.lbaf = prev_lbaf; @@ -5844,28 +5688,23 @@ static int format_cmd(int argc, char **argv, struct command *cmd, struct plugin /* ses & pi checks set to 7 for forward-compatibility */ if (cfg.ses > 7) { nvme_show_error("invalid secure erase settings:%d", cfg.ses); - err = -EINVAL; - goto close_dev; + return -EINVAL; } if (cfg.lbaf > 63) { nvme_show_error("invalid lbaf:%d", cfg.lbaf); - err = -EINVAL; - goto close_dev; + return -EINVAL; } if (cfg.pi > 7) { nvme_show_error("invalid pi:%d", cfg.pi); - err = -EINVAL; - goto close_dev; + return -EINVAL; } if (cfg.pil > 1) { nvme_show_error("invalid pil:%d", cfg.pil); - err = -EINVAL; - goto close_dev; + return -EINVAL; } if (cfg.ms > 1) { nvme_show_error("invalid ms:%d", cfg.ms); - err = -EINVAL; - goto close_dev; + return -EINVAL; } if (!cfg.force) { @@ -5904,11 +5743,10 @@ static int format_cmd(int argc, char **argv, struct command *cmd, struct plugin if (is_chardev(dev)) { if (ioctl(dev_fd(dev), NVME_IOCTL_RESCAN) < 0) { nvme_show_error("failed to rescan namespaces"); - err = -errno; - goto close_dev; + return -errno; } } else if (cfg.namespace_id != NVME_NSID_ALL) { - block_size = 1 << ns.lbaf[cfg.lbaf].ds; + block_size = 1 << ns->lbaf[cfg.lbaf].ds; /* * If block size has been changed by the format @@ -5920,14 +5758,12 @@ static int format_cmd(int argc, char **argv, struct command *cmd, struct plugin if (ioctl(dev_fd(dev), BLKBSZSET, &block_size) < 0) { nvme_show_error("failed to set block size to %d", block_size); - err = -errno; - goto close_dev; + return -errno; } if (ioctl(dev_fd(dev), BLKRRPART) < 0) { nvme_show_error("failed to re-read partition table"); - err = -errno; - goto close_dev; + return -errno; } } } @@ -5935,9 +5771,6 @@ static int format_cmd(int argc, char **argv, struct command *cmd, struct plugin nvme_ctrl_reset(dev_fd(dev)); } -close_dev: - dev_close(dev); -ret: return err; } @@ -5960,11 +5793,12 @@ static int set_feature(int argc, char **argv, struct command *cmd, struct plugin const char *value = "new value of feature (required)"; const char *cdw12 = "feature cdw12, if used"; const char *save = "specifies that the controller shall save the attribute"; - struct nvme_dev *dev; + + _cleanup_nvme_dev_ struct nvme_dev *dev = NULL; + _cleanup_free_ void *buf = NULL; + _cleanup_file_ int ffd = -1; int err; __u32 result; - void *buf = NULL; - int ffd = STDIN_FILENO; struct config { __u32 namespace_id; @@ -5987,28 +5821,26 @@ static int set_feature(int argc, char **argv, struct command *cmd, struct plugin .save = false, }; - OPT_ARGS(opts) = { - OPT_UINT("namespace-id", 'n', &cfg.namespace_id, namespace_desired), - OPT_BYTE("feature-id", 'f', &cfg.feature_id, feature_id), - OPT_SUFFIX("value", 'v', &cfg.value, value), - OPT_UINT("cdw12", 'c', &cfg.cdw12, cdw12), - OPT_BYTE("uuid-index", 'U', &cfg.uuid_index, uuid_index_specify), - OPT_UINT("data-len", 'l', &cfg.data_len, buf_len), - OPT_FILE("data", 'd', &cfg.file, data), - OPT_FLAG("save", 's', &cfg.save, save), - OPT_END() - }; + NVME_ARGS(opts, cfg, + OPT_UINT("namespace-id", 'n', &cfg.namespace_id, namespace_desired), + OPT_BYTE("feature-id", 'f', &cfg.feature_id, feature_id), + OPT_SUFFIX("value", 'v', &cfg.value, value), + OPT_UINT("cdw12", 'c', &cfg.cdw12, cdw12), + OPT_BYTE("uuid-index", 'U', &cfg.uuid_index, uuid_index_specify), + OPT_UINT("data-len", 'l', &cfg.data_len, buf_len), + OPT_FILE("data", 'd', &cfg.file, data), + OPT_FLAG("save", 's', &cfg.save, save)); err = parse_and_open(&dev, argc, argv, desc, opts); if (err) - goto ret; + return err; if (!argconfig_parse_seen(opts, "namespace-id")) { err = nvme_get_nsid(dev_fd(dev), &cfg.namespace_id); if (err < 0) { if (errno != ENOTTY) { nvme_show_error("get-namespace-id: %s", nvme_strerror(errno)); - goto close_dev; + return -errno; } cfg.namespace_id = NVME_NSID_ALL; } @@ -6016,14 +5848,12 @@ static int set_feature(int argc, char **argv, struct command *cmd, struct plugin if (!cfg.feature_id) { nvme_show_error("feature-id required param"); - err = -EINVAL; - goto close_dev; + return -EINVAL; } if (cfg.uuid_index > 127) { nvme_show_error("invalid uuid index param: %u", cfg.uuid_index); - err = -1; - goto close_dev; + return -1; } if (!cfg.data_len) @@ -6032,12 +5862,9 @@ static int set_feature(int argc, char **argv, struct command *cmd, struct plugin &cfg.data_len); if (cfg.data_len) { - if (posix_memalign(&buf, getpagesize(), cfg.data_len)) { - nvme_show_error("can not allocate feature payload"); - err = -ENOMEM; - goto close_dev; - } - memset(buf, 0, cfg.data_len); + buf = nvme_alloc(cfg.data_len); + if (!buf) + return -ENOMEM; } if (buf) { @@ -6055,17 +5882,15 @@ static int set_feature(int argc, char **argv, struct command *cmd, struct plugin if (ffd <= 0) { nvme_show_error("Failed to open file %s: %s", cfg.file, strerror(errno)); - err = -EINVAL; - goto free; + return -EINVAL; } } err = read(ffd, (void *)buf, cfg.data_len); if (err < 0) { - err = -errno; nvme_show_error("failed to read data buffer from input file: %s", strerror(errno)); - goto close_ffd; + return -errno; } } } @@ -6106,14 +5931,6 @@ static int set_feature(int argc, char **argv, struct command *cmd, struct plugin nvme_show_status(err); } -close_ffd: - if (ffd != STDIN_FILENO) - close(ffd); -free: - free(buf); -close_dev: - dev_close(dev); -ret: return err; } @@ -6126,10 +5943,12 @@ static int sec_send(int argc, char **argv, struct command *cmd, struct plugin *p "associates Security Sends (security-send) and Security Receives (security-recv)."; const char *file = "transfer payload"; const char *tl = "transfer length (cf. SPC-4)"; - int err, sec_fd = STDIN_FILENO; - struct nvme_dev *dev; - void *sec_buf; + + _cleanup_nvme_dev_ struct nvme_dev *dev = NULL; + _cleanup_free_ void *sec_buf = NULL; + _cleanup_file_ int sec_fd = -1; unsigned int sec_size; + int err; struct config { __u32 namespace_id; @@ -6149,24 +5968,21 @@ static int sec_send(int argc, char **argv, struct command *cmd, struct plugin *p .tl = 0, }; - OPT_ARGS(opts) = { - OPT_UINT("namespace-id", 'n', &cfg.namespace_id, namespace_desired), - OPT_FILE("file", 'f', &cfg.file, file), - OPT_BYTE("nssf", 'N', &cfg.nssf, nssf), - OPT_BYTE("secp", 'p', &cfg.secp, secp), - OPT_SHRT("spsp", 's', &cfg.spsp, spsp), - OPT_UINT("tl", 't', &cfg.tl, tl), - OPT_END() - }; + NVME_ARGS(opts, cfg, + OPT_UINT("namespace-id", 'n', &cfg.namespace_id, namespace_desired), + OPT_FILE("file", 'f', &cfg.file, file), + OPT_BYTE("nssf", 'N', &cfg.nssf, nssf), + OPT_BYTE("secp", 'p', &cfg.secp, secp), + OPT_SHRT("spsp", 's', &cfg.spsp, spsp), + OPT_UINT("tl", 't', &cfg.tl, tl)); err = parse_and_open(&dev, argc, argv, desc, opts); if (err) - goto ret; + return err; if (cfg.tl == 0) { nvme_show_error("--tl unspecified or zero"); - err = -EINVAL; - goto close_dev; + return -EINVAL; } if ((cfg.tl & 3) != 0) nvme_show_error( @@ -6179,33 +5995,27 @@ static int sec_send(int argc, char **argv, struct command *cmd, struct plugin *p sec_fd = open(cfg.file, O_RDONLY); if (sec_fd < 0) { nvme_show_error("Failed to open %s: %s", cfg.file, strerror(errno)); - err = -EINVAL; - goto close_dev; + return -EINVAL; } err = fstat(sec_fd, &sb); if (err < 0) { nvme_show_perror("fstat"); - goto close_sec_fd; + return err; } sec_size = cfg.tl > sb.st_size ? cfg.tl : sb.st_size; } - if (posix_memalign(&sec_buf, getpagesize(), cfg.tl)) { - nvme_show_error("No memory for security size:%d", cfg.tl); - err = -ENOMEM; - goto close_sec_fd; - } - - memset(sec_buf, 0, cfg.tl); // ensure zero fill if buf_size > sec_size + sec_buf = nvme_alloc(cfg.tl); + if (!sec_buf) + return -ENOMEM; err = read(sec_fd, sec_buf, sec_size); if (err < 0) { - err = -errno; nvme_show_error("Failed to read data from security file %s with %s", cfg.file, strerror(errno)); - goto free; + return -errno; } struct nvme_security_send_args args = { @@ -6231,13 +6041,6 @@ static int sec_send(int argc, char **argv, struct command *cmd, struct plugin *p else printf("NVME Security Send Command Success\n"); -free: - free(sec_buf); -close_sec_fd: - close(sec_fd); -close_dev: - dev_close(dev); -ret: return err; } @@ -6247,10 +6050,11 @@ static int dir_send(int argc, char **argv, struct command *cmd, struct plugin *p const char *endir = "directive enable"; const char *ttype = "target directive type to be enabled/disabled"; const char *input = "write/send file (default stdin)"; - struct nvme_dev *dev; + + _cleanup_nvme_dev_ struct nvme_dev *dev = NULL; + _cleanup_free_ void *buf = NULL; __u32 result; __u32 dw12 = 0; - void *buf = NULL; int ffd = STDIN_FILENO; int err; @@ -6280,23 +6084,21 @@ static int dir_send(int argc, char **argv, struct command *cmd, struct plugin *p .file = "", }; - OPT_ARGS(opts) = { - OPT_UINT("namespace-id", 'n', &cfg.namespace_id, namespace_id_desired), - OPT_UINT("data-len", 'l', &cfg.data_len, buf_len), - OPT_BYTE("dir-type", 'D', &cfg.dtype, dtype), - OPT_BYTE("target-dir", 'T', &cfg.ttype, ttype), - OPT_SHRT("dir-spec", 'S', &cfg.dspec, dspec_w_dtype), - OPT_BYTE("dir-oper", 'O', &cfg.doper, doper), - OPT_SHRT("endir", 'e', &cfg.endir, endir), - OPT_FLAG("human-readable", 'H', &cfg.human_readable, human_readable_directive), - OPT_FLAG("raw-binary", 'b', &cfg.raw_binary, raw_directive), - OPT_FILE("input-file", 'i', &cfg.file, input), - OPT_END() - }; + NVME_ARGS(opts, cfg, + OPT_UINT("namespace-id", 'n', &cfg.namespace_id, namespace_id_desired), + OPT_UINT("data-len", 'l', &cfg.data_len, buf_len), + OPT_BYTE("dir-type", 'D', &cfg.dtype, dtype), + OPT_BYTE("target-dir", 'T', &cfg.ttype, ttype), + OPT_SHRT("dir-spec", 'S', &cfg.dspec, dspec_w_dtype), + OPT_BYTE("dir-oper", 'O', &cfg.doper, doper), + OPT_SHRT("endir", 'e', &cfg.endir, endir), + OPT_FLAG("human-readable", 'H', &cfg.human_readable, human_readable_directive), + OPT_FLAG("raw-binary", 'b', &cfg.raw_binary, raw_directive), + OPT_FILE("input-file", 'i', &cfg.file, input)); err = parse_and_open(&dev, argc, argv, desc, opts); if (err) - goto ret; + return err; switch (cfg.dtype) { case NVME_DIRECTIVE_DTYPE_IDENTIFY: @@ -6304,15 +6106,13 @@ static int dir_send(int argc, char **argv, struct command *cmd, struct plugin *p case NVME_DIRECTIVE_SEND_IDENTIFY_DOPER_ENDIR: if (!cfg.ttype) { nvme_show_error("target-dir required param\n"); - err = -EINVAL; - goto close_dev; + return -EINVAL; } dw12 = cfg.ttype << 8 | cfg.endir; break; default: nvme_show_error("invalid directive operations for Identify Directives"); - err = -EINVAL; - goto close_dev; + return -EINVAL; } break; case NVME_DIRECTIVE_DTYPE_STREAMS: @@ -6322,22 +6122,18 @@ static int dir_send(int argc, char **argv, struct command *cmd, struct plugin *p break; default: nvme_show_error("invalid directive operations for Streams Directives"); - err = -EINVAL; - goto close_dev; + return -EINVAL; } break; default: nvme_show_error("invalid directive type"); - err = -EINVAL; - goto close_dev; + return -EINVAL; } if (cfg.data_len) { - if (posix_memalign(&buf, getpagesize(), cfg.data_len)) { - err = -ENOMEM; - goto close_dev; - } - memset(buf, 0, cfg.data_len); + buf = nvme_alloc(cfg.data_len); + if (!buf) + return -ENOMEM; } if (buf) { @@ -6346,16 +6142,14 @@ static int dir_send(int argc, char **argv, struct command *cmd, struct plugin *p if (ffd <= 0) { nvme_show_error("Failed to open file %s: %s", cfg.file, strerror(errno)); - err = -EINVAL; - goto free; + return -EINVAL; } } err = read(ffd, (void *)buf, cfg.data_len); if (err < 0) { - err = -errno; nvme_show_error("failed to read data buffer from input file %s", strerror(errno)); - goto close_ffd; + return -errno; } } @@ -6375,7 +6169,7 @@ static int dir_send(int argc, char **argv, struct command *cmd, struct plugin *p err = nvme_directive_send(&args); if (err < 0) { nvme_show_error("dir-send: %s", nvme_strerror(errno)); - goto close_ffd; + return err; } if (!err) { printf("dir-send: type %#x, operation %#x, spec_val %#x, nsid %#x, result %#x\n", @@ -6390,13 +6184,6 @@ static int dir_send(int argc, char **argv, struct command *cmd, struct plugin *p nvme_show_status(err); } -close_ffd: - close(ffd); -free: - free(buf); -close_dev: - dev_close(dev); -ret: return err; } @@ -6404,7 +6191,8 @@ static int write_uncor(int argc, char **argv, struct command *cmd, struct plugin { const char *desc = "The Write Uncorrectable command is used to set a range of logical blocks to invalid."; - struct nvme_dev *dev; + + _cleanup_nvme_dev_ struct nvme_dev *dev = NULL; int err; struct config { @@ -6423,31 +6211,28 @@ static int write_uncor(int argc, char **argv, struct command *cmd, struct plugin .dspec = 0, }; - OPT_ARGS(opts) = { - OPT_UINT("namespace-id", 'n', &cfg.namespace_id, namespace_desired), - OPT_SUFFIX("start-block", 's', &cfg.start_block, start_block), - OPT_SHRT("block-count", 'c', &cfg.block_count, block_count), - OPT_BYTE("dir-type", 'T', &cfg.dtype, dtype), - OPT_SHRT("dir-spec", 'S', &cfg.dspec, dspec_w_dtype), - OPT_END() - }; + NVME_ARGS(opts, cfg, + OPT_UINT("namespace-id", 'n', &cfg.namespace_id, namespace_desired), + OPT_SUFFIX("start-block", 's', &cfg.start_block, start_block), + OPT_SHRT("block-count", 'c', &cfg.block_count, block_count), + OPT_BYTE("dir-type", 'T', &cfg.dtype, dtype), + OPT_SHRT("dir-spec", 'S', &cfg.dspec, dspec_w_dtype)); err = parse_and_open(&dev, argc, argv, desc, opts); if (err) - goto ret; + return err; if (!cfg.namespace_id) { err = nvme_get_nsid(dev_fd(dev), &cfg.namespace_id); if (err < 0) { nvme_show_error("get-namespace-id: %s", nvme_strerror(errno)); - goto close_dev; + return err; } } if (cfg.dtype > 0xf) { nvme_show_error("Invalid directive type, %x", cfg.dtype); - err = -EINVAL; - goto close_dev; + return -EINVAL; } struct nvme_io_args args = { @@ -6469,9 +6254,6 @@ static int write_uncor(int argc, char **argv, struct command *cmd, struct plugin else printf("NVME Write Uncorrectable Success\n"); -close_dev: - dev_close(dev); -ret: return err; } @@ -6511,11 +6293,11 @@ static int invalid_tags(__u64 storage_tag, __u64 ref_tag, __u8 sts, __u8 pif) static int write_zeroes(int argc, char **argv, struct command *cmd, struct plugin *plugin) { - __u16 control = 0; + _cleanup_free_ struct nvme_nvm_id_ns *nvm_ns = NULL; + _cleanup_free_ struct nvme_id_ns *ns = NULL; + _cleanup_nvme_dev_ struct nvme_dev *dev = NULL; __u8 lba_index, sts = 0, pif = 0; - struct nvme_id_ns ns; - struct nvme_dev *dev; - struct nvme_nvm_id_ns nvm_ns; + __u16 control = 0; int err; const char *desc = @@ -6560,37 +6342,32 @@ static int write_zeroes(int argc, char **argv, struct command *cmd, struct plugi .dspec = 0, }; - OPT_ARGS(opts) = { - OPT_UINT("namespace-id", 'n', &cfg.namespace_id, namespace_desired), - OPT_SUFFIX("start-block", 's', &cfg.start_block, start_block), - OPT_SHRT("block-count", 'c', &cfg.block_count, block_count), - OPT_BYTE("dir-type", 'T', &cfg.dtype, dtype), - OPT_FLAG("deac", 'd', &cfg.deac, deac), - OPT_FLAG("limited-retry", 'l', &cfg.limited_retry, limited_retry), - OPT_FLAG("force-unit-access", 'f', &cfg.force_unit_access, force_unit_access), - OPT_BYTE("prinfo", 'p', &cfg.prinfo, prinfo), - OPT_SUFFIX("ref-tag", 'r', &cfg.ref_tag, ref_tag), - OPT_SHRT("app-tag-mask", 'm', &cfg.app_tag_mask, app_tag_mask), - OPT_SHRT("app-tag", 'a', &cfg.app_tag, app_tag), - OPT_SUFFIX("storage-tag", 'S', &cfg.storage_tag, storage_tag), - OPT_FLAG("storage-tag-check", 'C', &cfg.storage_tag_check, storage_tag_check), - OPT_SHRT("dir-spec", 'D', &cfg.dspec, dspec_w_dtype), - OPT_END() - }; + NVME_ARGS(opts, cfg, + OPT_UINT("namespace-id", 'n', &cfg.namespace_id, namespace_desired), + OPT_SUFFIX("start-block", 's', &cfg.start_block, start_block), + OPT_SHRT("block-count", 'c', &cfg.block_count, block_count), + OPT_BYTE("dir-type", 'T', &cfg.dtype, dtype), + OPT_FLAG("deac", 'd', &cfg.deac, deac), + OPT_FLAG("limited-retry", 'l', &cfg.limited_retry, limited_retry), + OPT_FLAG("force-unit-access", 'f', &cfg.force_unit_access, force_unit_access), + OPT_BYTE("prinfo", 'p', &cfg.prinfo, prinfo), + OPT_SUFFIX("ref-tag", 'r', &cfg.ref_tag, ref_tag), + OPT_SHRT("app-tag-mask", 'm', &cfg.app_tag_mask, app_tag_mask), + OPT_SHRT("app-tag", 'a', &cfg.app_tag, app_tag), + OPT_SUFFIX("storage-tag", 'S', &cfg.storage_tag, storage_tag), + OPT_FLAG("storage-tag-check", 'C', &cfg.storage_tag_check, storage_tag_check), + OPT_SHRT("dir-spec", 'D', &cfg.dspec, dspec_w_dtype)); err = parse_and_open(&dev, argc, argv, desc, opts); if (err) - goto ret; + return err; - if (cfg.prinfo > 0xf) { - err = -EINVAL; - goto close_dev; - } + if (cfg.prinfo > 0xf) + return -EINVAL; if (cfg.dtype > 0xf) { nvme_show_error("Invalid directive type, %x", cfg.dtype); - err = -EINVAL; - goto close_dev; + return -EINVAL; } control |= (cfg.prinfo << 10); @@ -6607,31 +6384,37 @@ static int write_zeroes(int argc, char **argv, struct command *cmd, struct plugi err = nvme_get_nsid(dev_fd(dev), &cfg.namespace_id); if (err < 0) { nvme_show_error("get-namespace-id: %s", nvme_strerror(errno)); - goto close_dev; + return err; } } - err = nvme_cli_identify_ns(dev, cfg.namespace_id, &ns); + ns = nvme_alloc(sizeof(*ns)); + if (!ns) + return -ENOMEM; + + err = nvme_cli_identify_ns(dev, cfg.namespace_id, ns); if (err < 0) { nvme_show_error("identify namespace: %s", nvme_strerror(errno)); - goto close_dev; + return err; } else if (err) { nvme_show_status(err); - goto close_dev; + return err; } - err = nvme_identify_ns_csi(dev_fd(dev), cfg.namespace_id, 0, NVME_CSI_NVM, &nvm_ns); - if (!err) { - nvme_id_ns_flbas_to_lbaf_inuse(ns.flbas, &lba_index); - sts = nvm_ns.elbaf[lba_index] & NVME_NVM_ELBAF_STS_MASK; - pif = (nvm_ns.elbaf[lba_index] & NVME_NVM_ELBAF_PIF_MASK) >> 7; - } + nvm_ns = nvme_alloc(sizeof(*nvm_ns)); + if (!nvm_ns) + return -ENOMEM; - if (invalid_tags(cfg.storage_tag, cfg.ref_tag, sts, pif)) { - err = -EINVAL; - goto close_dev; + err = nvme_identify_ns_csi(dev_fd(dev), cfg.namespace_id, 0, NVME_CSI_NVM, nvm_ns); + if (!err) { + nvme_id_ns_flbas_to_lbaf_inuse(ns->flbas, &lba_index); + sts = nvm_ns->elbaf[lba_index] & NVME_NVM_ELBAF_STS_MASK; + pif = (nvm_ns->elbaf[lba_index] & NVME_NVM_ELBAF_PIF_MASK) >> 7; } + if (invalid_tags(cfg.storage_tag, cfg.ref_tag, sts, pif)) + return -EINVAL; + struct nvme_io_args args = { .args_size = sizeof(args), .fd = dev_fd(dev), @@ -6657,9 +6440,6 @@ static int write_zeroes(int argc, char **argv, struct command *cmd, struct plugi else printf("NVME Write Zeroes Success\n"); -close_dev: - dev_close(dev); -ret: return err; } @@ -6677,12 +6457,12 @@ static int dsm(int argc, char **argv, struct command *cmd, struct plugin *plugin const char *idr = "Attribute Integral Dataset for Read"; const char *cdw11 = "All the command DWORD 11 attributes. Use instead of specifying individual attributes"; + _cleanup_nvme_dev_ struct nvme_dev *dev = NULL; + _cleanup_free_ struct nvme_dsm_range *dsm = NULL; uint16_t nr, nc, nb, ns; __u32 ctx_attrs[256] = {0,}; __u32 nlbs[256] = {0,}; __u64 slbas[256] = {0,}; - struct nvme_dsm_range dsm[256]; - struct nvme_dev *dev; int err; struct config { @@ -6707,42 +6487,43 @@ static int dsm(int argc, char **argv, struct command *cmd, struct plugin *plugin .cdw11 = 0, }; - OPT_ARGS(opts) = { - OPT_UINT("namespace-id", 'n', &cfg.namespace_id, namespace_id_desired), - OPT_LIST("ctx-attrs", 'a', &cfg.ctx_attrs, context_attrs), - OPT_LIST("blocks", 'b', &cfg.blocks, blocks), - OPT_LIST("slbs", 's', &cfg.slbas, starting_blocks), - OPT_FLAG("ad", 'd', &cfg.ad, ad), - OPT_FLAG("idw", 'w', &cfg.idw, idw), - OPT_FLAG("idr", 'r', &cfg.idr, idr), - OPT_UINT("cdw11", 'c', &cfg.cdw11, cdw11), - OPT_END() - }; + NVME_ARGS(opts, cfg, + OPT_UINT("namespace-id", 'n', &cfg.namespace_id, namespace_id_desired), + OPT_LIST("ctx-attrs", 'a', &cfg.ctx_attrs, context_attrs), + OPT_LIST("blocks", 'b', &cfg.blocks, blocks), + OPT_LIST("slbs", 's', &cfg.slbas, starting_blocks), + OPT_FLAG("ad", 'd', &cfg.ad, ad), + OPT_FLAG("idw", 'w', &cfg.idw, idw), + OPT_FLAG("idr", 'r', &cfg.idr, idr), + OPT_UINT("cdw11", 'c', &cfg.cdw11, cdw11)); err = parse_and_open(&dev, argc, argv, desc, opts); if (err) - goto ret; + return err; - nc = argconfig_parse_comma_sep_array(cfg.ctx_attrs, (int *)ctx_attrs, ARRAY_SIZE(ctx_attrs)); - nb = argconfig_parse_comma_sep_array(cfg.blocks, (int *)nlbs, ARRAY_SIZE(nlbs)); - ns = argconfig_parse_comma_sep_array_long(cfg.slbas, (unsigned long long *)slbas, ARRAY_SIZE(slbas)); + nc = argconfig_parse_comma_sep_array_u32(cfg.ctx_attrs, ctx_attrs, ARRAY_SIZE(ctx_attrs)); + nb = argconfig_parse_comma_sep_array_u32(cfg.blocks, nlbs, ARRAY_SIZE(nlbs)); + ns = argconfig_parse_comma_sep_array_u64(cfg.slbas, slbas, ARRAY_SIZE(slbas)); nr = max(nc, max(nb, ns)); if (!nr || nr > 256) { nvme_show_error("No range definition provided"); - err = -EINVAL; - goto close_dev; + return -EINVAL; } if (!cfg.namespace_id) { err = nvme_get_nsid(dev_fd(dev), &cfg.namespace_id); if (err < 0) { nvme_show_error("get-namespace-id: %s", nvme_strerror(errno)); - goto close_dev; + return err; } } if (!cfg.cdw11) cfg.cdw11 = (cfg.ad << 2) | (cfg.idw << 1) | (cfg.idr << 0); + dsm = nvme_alloc(sizeof(*dsm) * 256); + if (!dsm) + return -ENOMEM; + nvme_init_dsm_range(dsm, ctx_attrs, nlbs, slbas, nr); struct nvme_dsm_args args = { .args_size = sizeof(args), @@ -6762,9 +6543,6 @@ static int dsm(int argc, char **argv, struct command *cmd, struct plugin *plugin else printf("NVMe DSM: success\n"); -close_dev: - dev_close(dev); -ret: return err; } @@ -6790,10 +6568,10 @@ static int copy_cmd(int argc, char **argv, struct command *cmd, struct plugin *p const char *d_dspec = "directive specific (write part)"; const char *d_format = "source range entry format"; + _cleanup_nvme_dev_ struct nvme_dev *dev = NULL; uint16_t nr, nb, ns, nrts, natms, nats; __u16 nlbs[128] = { 0 }; - unsigned long long slbas[128] = {0,}; - struct nvme_dev *dev; + __u64 slbas[128] = { 0 }; int err; union { @@ -6807,7 +6585,7 @@ static int copy_cmd(int argc, char **argv, struct command *cmd, struct plugin *p union { struct nvme_copy_range f0[128]; struct nvme_copy_range_f1 f1[101]; - } copy; + } *copy; struct config { __u32 namespace_id; @@ -6849,77 +6627,74 @@ static int copy_cmd(int argc, char **argv, struct command *cmd, struct plugin *p .format = 0, }; - OPT_ARGS(opts) = { - OPT_UINT("namespace-id", 'n', &cfg.namespace_id, namespace_id_desired), - OPT_SUFFIX("sdlba", 'd', &cfg.sdlba, d_sdlba), - OPT_LIST("slbs", 's', &cfg.slbas, d_slbas), - OPT_LIST("blocks", 'b', &cfg.nlbs, d_nlbs), - OPT_FLAG("limited-retry", 'l', &cfg.lr, d_lr), - OPT_FLAG("force-unit-access", 'f', &cfg.fua, d_fua), - OPT_BYTE("prinfow", 'p', &cfg.prinfow, d_prinfow), - OPT_BYTE("prinfor", 'P', &cfg.prinfor, d_prinfor), - OPT_SUFFIX("ref-tag", 'r', &cfg.ilbrt, d_ilbrt), - OPT_LIST("expected-ref-tags", 'R', &cfg.eilbrts, d_eilbrts), - OPT_SHRT("app-tag", 'a', &cfg.lbat, d_lbat), - OPT_LIST("expected-app-tags", 'A', &cfg.elbats, d_elbats), - OPT_SHRT("app-tag-mask", 'm', &cfg.lbatm, d_lbatm), - OPT_LIST("expected-app-tag-masks", 'M', &cfg.elbatms, d_elbatms), - OPT_BYTE("dir-type", 'T', &cfg.dtype, d_dtype), - OPT_SHRT("dir-spec", 'S', &cfg.dspec, d_dspec), - OPT_BYTE("format", 'F', &cfg.format, d_format), - OPT_END() - }; + NVME_ARGS(opts, cfg, + OPT_UINT("namespace-id", 'n', &cfg.namespace_id, namespace_id_desired), + OPT_SUFFIX("sdlba", 'd', &cfg.sdlba, d_sdlba), + OPT_LIST("slbs", 's', &cfg.slbas, d_slbas), + OPT_LIST("blocks", 'b', &cfg.nlbs, d_nlbs), + OPT_FLAG("limited-retry", 'l', &cfg.lr, d_lr), + OPT_FLAG("force-unit-access", 'f', &cfg.fua, d_fua), + OPT_BYTE("prinfow", 'p', &cfg.prinfow, d_prinfow), + OPT_BYTE("prinfor", 'P', &cfg.prinfor, d_prinfor), + OPT_SUFFIX("ref-tag", 'r', &cfg.ilbrt, d_ilbrt), + OPT_LIST("expected-ref-tags", 'R', &cfg.eilbrts, d_eilbrts), + OPT_SHRT("app-tag", 'a', &cfg.lbat, d_lbat), + OPT_LIST("expected-app-tags", 'A', &cfg.elbats, d_elbats), + OPT_SHRT("app-tag-mask", 'm', &cfg.lbatm, d_lbatm), + OPT_LIST("expected-app-tag-masks", 'M', &cfg.elbatms, d_elbatms), + OPT_BYTE("dir-type", 'T', &cfg.dtype, d_dtype), + OPT_SHRT("dir-spec", 'S', &cfg.dspec, d_dspec), + OPT_BYTE("format", 'F', &cfg.format, d_format)); err = parse_and_open(&dev, argc, argv, desc, opts); if (err) - goto ret; + return err; - nb = argconfig_parse_comma_sep_array_short(cfg.nlbs, nlbs, ARRAY_SIZE(nlbs)); - ns = argconfig_parse_comma_sep_array_long(cfg.slbas, slbas, ARRAY_SIZE(slbas)); + nb = argconfig_parse_comma_sep_array_u16(cfg.nlbs, nlbs, ARRAY_SIZE(nlbs)); + ns = argconfig_parse_comma_sep_array_u64(cfg.slbas, slbas, ARRAY_SIZE(slbas)); if (cfg.format == 0) { - nrts = argconfig_parse_comma_sep_array(cfg.eilbrts, (int *)eilbrts.f0, - ARRAY_SIZE(eilbrts.f0)); + nrts = argconfig_parse_comma_sep_array_u32(cfg.eilbrts, eilbrts.f0, + ARRAY_SIZE(eilbrts.f0)); } else if (cfg.format == 1) { - nrts = argconfig_parse_comma_sep_array_long(cfg.eilbrts, - (unsigned long long *)eilbrts.f1, - ARRAY_SIZE(eilbrts.f1)); + nrts = argconfig_parse_comma_sep_array_u64(cfg.eilbrts, eilbrts.f1, + ARRAY_SIZE(eilbrts.f1)); } else { nvme_show_error("invalid format"); - err = -EINVAL; - goto close_dev; + return -EINVAL; } - natms = argconfig_parse_comma_sep_array(cfg.elbatms, (int *)elbatms, ARRAY_SIZE(elbatms)); - nats = argconfig_parse_comma_sep_array(cfg.elbats, (int *)elbats, ARRAY_SIZE(elbats)); + natms = argconfig_parse_comma_sep_array_u32(cfg.elbatms, elbatms, ARRAY_SIZE(elbatms)); + nats = argconfig_parse_comma_sep_array_u32(cfg.elbats, elbats, ARRAY_SIZE(elbats)); nr = max(nb, max(ns, max(nrts, max(natms, nats)))); if (!nr || nr > 128 || (cfg.format == 1 && nr > 101)) { nvme_show_error("invalid range"); - err = -EINVAL; - goto close_dev; + return -EINVAL; } if (!cfg.namespace_id) { err = nvme_get_nsid(dev_fd(dev), &cfg.namespace_id); if (err < 0) { nvme_show_error("get-namespace-id: %s", nvme_strerror(errno)); - goto close_dev; + return err; } } + copy = nvme_alloc(sizeof(*copy)); + if (!copy) + return -ENOMEM; + if (cfg.format == 0) - nvme_init_copy_range(copy.f0, nlbs, (__u64 *)slbas, - eilbrts.f0, elbatms, elbats, nr); + nvme_init_copy_range(copy->f0, nlbs, slbas, eilbrts.f0, elbatms, elbats, nr); else if (cfg.format == 1) - nvme_init_copy_range_f1(copy.f1, nlbs, (__u64 *)slbas, - eilbrts.f1, elbatms, elbats, nr); + nvme_init_copy_range_f1(copy->f1, nlbs, slbas, eilbrts.f1, elbatms, elbats, nr); struct nvme_copy_args args = { .args_size = sizeof(args), .fd = dev_fd(dev), .nsid = cfg.namespace_id, - .copy = copy.f0, + .copy = copy->f0, .sdlba = cfg.sdlba, .nr = nr, .prinfor = cfg.prinfor, @@ -6943,9 +6718,6 @@ static int copy_cmd(int argc, char **argv, struct command *cmd, struct plugin *p else printf("NVMe Copy: success\n"); -close_dev: - dev_close(dev); -ret: return err; } @@ -6956,7 +6728,8 @@ static int flush_cmd(int argc, char **argv, struct command *cmd, struct plugin * "finished before the flush was submitted. Additional data may also be\n" "flushed by the controller, from any namespace, depending on controller and\n" "associated namespace status."; - struct nvme_dev *dev; + + _cleanup_nvme_dev_ struct nvme_dev *dev = NULL; int err; struct config { @@ -6967,20 +6740,18 @@ static int flush_cmd(int argc, char **argv, struct command *cmd, struct plugin * .namespace_id = 0, }; - OPT_ARGS(opts) = { - OPT_UINT("namespace-id", 'n', &cfg.namespace_id, namespace_id_desired), - OPT_END() - }; + NVME_ARGS(opts, cfg, + OPT_UINT("namespace-id", 'n', &cfg.namespace_id, namespace_id_desired)); err = parse_and_open(&dev, argc, argv, desc, opts); if (err) - goto ret; + return err; if (!cfg.namespace_id) { err = nvme_get_nsid(dev_fd(dev), &cfg.namespace_id); if (err < 0) { nvme_show_error("get-namespace-id: %s", nvme_strerror(errno)); - goto close_dev; + return err; } } @@ -6991,9 +6762,7 @@ static int flush_cmd(int argc, char **argv, struct command *cmd, struct plugin * nvme_show_status(err); else printf("NVMe Flush: success\n"); -close_dev: - dev_close(dev); -ret: + return err; } @@ -7006,7 +6775,8 @@ static int resv_acquire(int argc, char **argv, struct command *cmd, struct plugi "status Reservation Conflict if the given namespace is already reserved."; const char *prkey = "pre-empt reservation key"; const char *racqa = "reservation acquire action"; - struct nvme_dev *dev; + + _cleanup_nvme_dev_ struct nvme_dev *dev = NULL; int err; struct config { @@ -7027,31 +6797,28 @@ static int resv_acquire(int argc, char **argv, struct command *cmd, struct plugi .iekey = false, }; - OPT_ARGS(opts) = { - OPT_UINT("namespace-id", 'n', &cfg.namespace_id, namespace_id_desired), - OPT_SUFFIX("crkey", 'c', &cfg.crkey, crkey), - OPT_SUFFIX("prkey", 'p', &cfg.prkey, prkey), - OPT_BYTE("rtype", 't', &cfg.rtype, rtype), - OPT_BYTE("racqa", 'a', &cfg.racqa, racqa), - OPT_FLAG("iekey", 'i', &cfg.iekey, iekey), - OPT_END() - }; + NVME_ARGS(opts, cfg, + OPT_UINT("namespace-id", 'n', &cfg.namespace_id, namespace_id_desired), + OPT_SUFFIX("crkey", 'c', &cfg.crkey, crkey), + OPT_SUFFIX("prkey", 'p', &cfg.prkey, prkey), + OPT_BYTE("rtype", 't', &cfg.rtype, rtype), + OPT_BYTE("racqa", 'a', &cfg.racqa, racqa), + OPT_FLAG("iekey", 'i', &cfg.iekey, iekey)); err = parse_and_open(&dev, argc, argv, desc, opts); if (err) - goto ret; + return err; if (!cfg.namespace_id) { err = nvme_get_nsid(dev_fd(dev), &cfg.namespace_id); if (err < 0) { nvme_show_error("get-namespace-id: %s", nvme_strerror(errno)); - goto close_dev; + return err; } } if (cfg.racqa > 7) { nvme_show_error("invalid racqa:%d", cfg.racqa); - err = -EINVAL; - goto close_dev; + return -EINVAL; } struct nvme_resv_acquire_args args = { @@ -7074,9 +6841,6 @@ static int resv_acquire(int argc, char **argv, struct command *cmd, struct plugi else printf("NVME Reservation Acquire success\n"); -close_dev: - dev_close(dev); -ret: return err; } @@ -7088,7 +6852,8 @@ static int resv_register(int argc, char **argv, struct command *cmd, struct plug const char *nrkey = "new reservation key"; const char *rrega = "reservation registration action"; const char *cptpl = "change persistence through power loss setting"; - struct nvme_dev *dev; + + _cleanup_nvme_dev_ struct nvme_dev *dev = NULL; int err; struct config { @@ -7108,37 +6873,33 @@ static int resv_register(int argc, char **argv, struct command *cmd, struct plug .cptpl = false, }; - OPT_ARGS(opts) = { - OPT_UINT("namespace-id", 'n', &cfg.namespace_id, namespace_id_desired), - OPT_SUFFIX("crkey", 'c', &cfg.crkey, crkey), - OPT_SUFFIX("nrkey", 'k', &cfg.nrkey, nrkey), - OPT_BYTE("rrega", 'r', &cfg.rrega, rrega), - OPT_BYTE("cptpl", 'p', &cfg.cptpl, cptpl), - OPT_FLAG("iekey", 'i', &cfg.iekey, iekey), - OPT_END() - }; + NVME_ARGS(opts, cfg, + OPT_UINT("namespace-id", 'n', &cfg.namespace_id, namespace_id_desired), + OPT_SUFFIX("crkey", 'c', &cfg.crkey, crkey), + OPT_SUFFIX("nrkey", 'k', &cfg.nrkey, nrkey), + OPT_BYTE("rrega", 'r', &cfg.rrega, rrega), + OPT_BYTE("cptpl", 'p', &cfg.cptpl, cptpl), + OPT_FLAG("iekey", 'i', &cfg.iekey, iekey)); err = parse_and_open(&dev, argc, argv, desc, opts); if (err) - goto ret; + return err; if (!cfg.namespace_id) { err = nvme_get_nsid(dev_fd(dev), &cfg.namespace_id); if (err < 0) { nvme_show_error("get-namespace-id: %s", nvme_strerror(errno)); - goto close_dev; + return err; } } if (cfg.cptpl > 3) { nvme_show_error("invalid cptpl:%d", cfg.cptpl); - err = -EINVAL; - goto close_dev; + return -EINVAL; } if (cfg.rrega > 7) { nvme_show_error("invalid rrega:%d", cfg.rrega); - err = -EINVAL; - goto close_dev; + return -EINVAL; } struct nvme_resv_register_args args = { @@ -7161,9 +6922,6 @@ static int resv_register(int argc, char **argv, struct command *cmd, struct plug else printf("NVME Reservation success\n"); -close_dev: - dev_close(dev); -ret: return err; } @@ -7178,7 +6936,8 @@ static int resv_release(int argc, char **argv, struct command *cmd, struct plugi "Exclusive Access, all registrants on the namespace except\n" "the issuing controller are notified."; const char *rrela = "reservation release action"; - struct nvme_dev *dev; + + _cleanup_nvme_dev_ struct nvme_dev *dev = NULL; int err; struct config { @@ -7197,30 +6956,27 @@ static int resv_release(int argc, char **argv, struct command *cmd, struct plugi .iekey = 0, }; - OPT_ARGS(opts) = { - OPT_UINT("namespace-id", 'n', &cfg.namespace_id, namespace_desired), - OPT_SUFFIX("crkey", 'c', &cfg.crkey, crkey), - OPT_BYTE("rtype", 't', &cfg.rtype, rtype), - OPT_BYTE("rrela", 'a', &cfg.rrela, rrela), - OPT_FLAG("iekey", 'i', &cfg.iekey, iekey), - OPT_END() - }; + NVME_ARGS(opts, cfg, + OPT_UINT("namespace-id", 'n', &cfg.namespace_id, namespace_desired), + OPT_SUFFIX("crkey", 'c', &cfg.crkey, crkey), + OPT_BYTE("rtype", 't', &cfg.rtype, rtype), + OPT_BYTE("rrela", 'a', &cfg.rrela, rrela), + OPT_FLAG("iekey", 'i', &cfg.iekey, iekey)); err = parse_and_open(&dev, argc, argv, desc, opts); if (err) - goto ret; + return err; if (!cfg.namespace_id) { err = nvme_get_nsid(dev_fd(dev), &cfg.namespace_id); if (err < 0) { nvme_show_error("get-namespace-id: %s", nvme_strerror(errno)); - goto close_dev; + return err; } } if (cfg.rrela > 7) { nvme_show_error("invalid rrela:%d", cfg.rrela); - err = -EINVAL; - goto close_dev; + return -EINVAL; } struct nvme_resv_release_args args = { @@ -7242,9 +6998,6 @@ static int resv_release(int argc, char **argv, struct command *cmd, struct plugi else printf("NVME Reservation Release success\n"); -close_dev: - dev_close(dev); -ret: return err; } @@ -7257,16 +7010,15 @@ static int resv_report(int argc, char **argv, struct command *cmd, struct plugin const char *numd = "number of dwords to transfer"; const char *eds = "request extended data structure"; - struct nvme_resv_status *status; + _cleanup_free_ struct nvme_resv_status *status = NULL; + _cleanup_nvme_dev_ struct nvme_dev *dev = NULL; enum nvme_print_flags flags; - struct nvme_dev *dev; int err, size; struct config { __u32 namespace_id; __u32 numd; __u8 eds; - char *output_format; bool raw_binary; }; @@ -7274,27 +7026,23 @@ static int resv_report(int argc, char **argv, struct command *cmd, struct plugin .namespace_id = 0, .numd = 0, .eds = false, - .output_format = "normal", .raw_binary = false, }; - OPT_ARGS(opts) = { - OPT_UINT("namespace-id", 'n', &cfg.namespace_id, namespace_id_desired), - OPT_UINT("numd", 'd', &cfg.numd, numd), - OPT_FLAG("eds", 'e', &cfg.eds, eds), - OPT_FMT("output-format", 'o', &cfg.output_format, output_format), - OPT_FLAG("raw-binary", 'b', &cfg.raw_binary, raw_dump), - OPT_END() - }; + NVME_ARGS(opts, cfg, + OPT_UINT("namespace-id", 'n', &cfg.namespace_id, namespace_id_desired), + OPT_UINT("numd", 'd', &cfg.numd, numd), + OPT_FLAG("eds", 'e', &cfg.eds, eds), + OPT_FLAG("raw-binary", 'b', &cfg.raw_binary, raw_dump)); err = parse_and_open(&dev, argc, argv, desc, opts); if (err) - goto ret; + return err; - err = flags = validate_output_format(cfg.output_format); + err = flags = validate_output_format(output_format_val); if (err < 0) { nvme_show_error("Invalid output format"); - goto close_dev; + return err; } if (cfg.raw_binary) @@ -7304,7 +7052,7 @@ static int resv_report(int argc, char **argv, struct command *cmd, struct plugin err = nvme_get_nsid(dev_fd(dev), &cfg.namespace_id); if (err < 0) { nvme_show_error("get-namespace-id: %s", nvme_strerror(errno)); - goto close_dev; + return err; } } @@ -7315,12 +7063,9 @@ static int resv_report(int argc, char **argv, struct command *cmd, struct plugin size = (cfg.numd + 1) << 2; - if (posix_memalign((void **)&status, getpagesize(), size)) { - nvme_show_error("No memory for resv report:%d", size); - err = -ENOMEM; - goto close_dev; - } - memset(status, 0, size); + status = nvme_alloc(size); + if (!status) + return -ENOMEM; struct nvme_resv_report_args args = { .args_size = sizeof(args), @@ -7339,10 +7084,7 @@ static int resv_report(int argc, char **argv, struct command *cmd, struct plugin nvme_show_status(err); else nvme_show_error("reservation report: %s", nvme_strerror(errno)); - free(status); -close_dev: - dev_close(dev); -ret: + return err; } @@ -7367,10 +7109,10 @@ static int submit_io(int opcode, char *command, const char *desc, int argc, char int logical_block_size = 0; unsigned long long buffer_size = 0, mbuffer_size = 0; bool huge; - struct nvme_id_ns ns; - struct nvme_nvm_id_ns nvm_ns; + _cleanup_nvme_dev_ struct nvme_dev *dev = NULL; + _cleanup_free_ struct nvme_nvm_id_ns *nvm_ns = NULL; + _cleanup_free_ struct nvme_id_ns *ns = NULL; __u8 lba_index, ms = 0, sts = 0, pif = 0; - struct nvme_dev *dev; const char *start_block_addr = "64-bit addr of first block to access"; const char *data_size = "size of data in bytes"; @@ -7436,40 +7178,38 @@ static int submit_io(int opcode, char *command, const char *desc, int argc, char .force = false, }; - OPT_ARGS(opts) = { - OPT_UINT("namespace-id", 'n', &cfg.namespace_id, namespace_id_desired), - OPT_SUFFIX("start-block", 's', &cfg.start_block, start_block_addr), - OPT_SHRT("block-count", 'c', &cfg.block_count, block_count), - OPT_SUFFIX("data-size", 'z', &cfg.data_size, data_size), - OPT_SUFFIX("metadata-size", 'y', &cfg.metadata_size, metadata_size), - OPT_SUFFIX("ref-tag", 'r', &cfg.ref_tag, ref_tag), - OPT_FILE("data", 'd', &cfg.data, data), - OPT_FILE("metadata", 'M', &cfg.metadata, metadata), - OPT_BYTE("prinfo", 'p', &cfg.prinfo, prinfo), - OPT_SHRT("app-tag-mask", 'm', &cfg.app_tag_mask, app_tag_mask), - OPT_SHRT("app-tag", 'a', &cfg.app_tag, app_tag), - OPT_SUFFIX("storage-tag", 'g', &cfg.storage_tag, storage_tag), - OPT_FLAG("limited-retry", 'l', &cfg.limited_retry, limited_retry_num), - OPT_FLAG("force-unit-access", 'f', &cfg.force_unit_access, force_unit_access), - OPT_FLAG("storage-tag-check", 'C', &cfg.storage_tag_check, storage_tag_check), - OPT_BYTE("dir-type", 'T', &cfg.dtype, dtype_for_write), - OPT_SHRT("dir-spec", 'S', &cfg.dspec, dspec), - OPT_BYTE("dsm", 'D', &cfg.dsmgmt, dsm), - OPT_FLAG("show-command", 'v', &cfg.show, show), - OPT_FLAG("dry-run", 'w', &cfg.dry_run, dry), - OPT_FLAG("latency", 't', &cfg.latency, latency), - OPT_FLAG("force", 0, &cfg.force, force), - OPT_END() - }; + NVME_ARGS(opts, cfg, + OPT_UINT("namespace-id", 'n', &cfg.namespace_id, namespace_id_desired), + OPT_SUFFIX("start-block", 's', &cfg.start_block, start_block_addr), + OPT_SHRT("block-count", 'c', &cfg.block_count, block_count), + OPT_SUFFIX("data-size", 'z', &cfg.data_size, data_size), + OPT_SUFFIX("metadata-size", 'y', &cfg.metadata_size, metadata_size), + OPT_SUFFIX("ref-tag", 'r', &cfg.ref_tag, ref_tag), + OPT_FILE("data", 'd', &cfg.data, data), + OPT_FILE("metadata", 'M', &cfg.metadata, metadata), + OPT_BYTE("prinfo", 'p', &cfg.prinfo, prinfo), + OPT_SHRT("app-tag-mask", 'm', &cfg.app_tag_mask, app_tag_mask), + OPT_SHRT("app-tag", 'a', &cfg.app_tag, app_tag), + OPT_SUFFIX("storage-tag", 'g', &cfg.storage_tag, storage_tag), + OPT_FLAG("limited-retry", 'l', &cfg.limited_retry, limited_retry_num), + OPT_FLAG("force-unit-access", 'f', &cfg.force_unit_access, force_unit_access), + OPT_FLAG("storage-tag-check", 'C', &cfg.storage_tag_check, storage_tag_check), + OPT_BYTE("dir-type", 'T', &cfg.dtype, dtype_for_write), + OPT_SHRT("dir-spec", 'S', &cfg.dspec, dspec), + OPT_BYTE("dsm", 'D', &cfg.dsmgmt, dsm), + OPT_FLAG("show-command", 'v', &cfg.show, show), + OPT_FLAG("dry-run", 'w', &cfg.dry_run, dry), + OPT_FLAG("latency", 't', &cfg.latency, latency), + OPT_FLAG("force", 0, &cfg.force, force)); if (opcode != nvme_cmd_write) { err = parse_and_open(&dev, argc, argv, desc, opts); if (err) - goto ret; + return err; } else { err = argconfig_parse(argc, argv, desc, opts); if (err) - goto ret; + return err; err = open_exclusive(&dev, argc, argv, cfg.force); if (err) { if (errno == EBUSY) { @@ -7481,7 +7221,7 @@ static int submit_io(int opcode, char *command, const char *desc, int argc, char } else { argconfig_print_help(desc, opts); } - goto ret; + return err; } } @@ -7489,15 +7229,13 @@ static int submit_io(int opcode, char *command, const char *desc, int argc, char err = nvme_get_nsid(dev_fd(dev), &cfg.namespace_id); if (err < 0) { nvme_show_error("get-namespace-id: %s", nvme_strerror(errno)); - goto close_dev; + return err; } } dfd = mfd = opcode & 1 ? STDIN_FILENO : STDOUT_FILENO; - if (cfg.prinfo > 0xf) { - err = -EINVAL; - goto close_dev; - } + if (cfg.prinfo > 0xf) + return err; dsmgmt = cfg.dsmgmt; control |= (cfg.prinfo << 10); @@ -7510,8 +7248,7 @@ static int submit_io(int opcode, char *command, const char *desc, int argc, char if (cfg.dtype) { if (cfg.dtype > 0xf) { nvme_show_error("Invalid directive type, %x", cfg.dtype); - err = -EINVAL; - goto close_dev; + return -EINVAL; } control |= cfg.dtype << 4; dsmgmt |= ((__u32)cfg.dspec) << 16; @@ -7521,8 +7258,7 @@ static int submit_io(int opcode, char *command, const char *desc, int argc, char dfd = open(cfg.data, flags, mode); if (dfd < 0) { nvme_show_perror(cfg.data); - err = -EINVAL; - goto close_dev; + return -EINVAL; } } @@ -7544,6 +7280,26 @@ static int submit_io(int opcode, char *command, const char *desc, int argc, char if (nvme_get_logical_block_size(dev_fd(dev), cfg.namespace_id, &logical_block_size) < 0) goto close_mfd; + ns = nvme_alloc(sizeof(*ns)); + if (!ns) { + err = -ENOMEM; + goto close_mfd; + } + + err = nvme_cli_identify_ns(dev, cfg.namespace_id, ns); + if (err > 0) { + nvme_show_status(err); + goto close_mfd; + } else if (err < 0) { + nvme_show_error("identify namespace: %s", nvme_strerror(errno)); + goto close_mfd; + } + + nvme_id_ns_flbas_to_lbaf_inuse(ns->flbas, &lba_index); + ms = ns->lbaf[lba_index].ms; + if (ns->flbas & NVME_NS_FLBAS_META_EXT) + logical_block_size += ms; + buffer_size = ((long long)cfg.block_count + 1) * logical_block_size; if (cfg.data_size < buffer_size) nvme_show_error("Rounding data size to fit block count (%lld bytes)", buffer_size); @@ -7556,29 +7312,23 @@ static int submit_io(int opcode, char *command, const char *desc, int argc, char /* Update the data size based on the required block count */ buffer_size = (nblocks + 1) * logical_block_size; - buffer = nvme_alloc(buffer_size, &huge); + buffer = nvme_alloc_huge(buffer_size, &huge); if (!buffer) { err = -ENOMEM; goto close_mfd; } - if (cfg.metadata_size) { - err = nvme_cli_identify_ns(dev, cfg.namespace_id, &ns); - if (err > 0) { - nvme_show_status(err); - goto free_buffer; - } else if (err < 0) { - nvme_show_error("identify namespace: %s", nvme_strerror(errno)); - goto free_buffer; - } - - nvme_id_ns_flbas_to_lbaf_inuse(ns.flbas, &lba_index); - ms = ns.lbaf[lba_index].ms; + nvm_ns = nvme_alloc(sizeof(*nvm_ns)); + if (!nvm_ns) { + err = -ENOMEM; + goto close_mfd; + } - err = nvme_identify_ns_csi(dev_fd(dev), 1, 0, NVME_CSI_NVM, &nvm_ns); + if (cfg.metadata_size) { + err = nvme_identify_ns_csi(dev_fd(dev), 1, 0, NVME_CSI_NVM, nvm_ns); if (!err) { - sts = nvm_ns.elbaf[lba_index] & NVME_NVM_ELBAF_STS_MASK; - pif = (nvm_ns.elbaf[lba_index] & NVME_NVM_ELBAF_PIF_MASK) >> 7; + sts = nvm_ns->elbaf[lba_index] & NVME_NVM_ELBAF_STS_MASK; + pif = (nvm_ns->elbaf[lba_index] & NVME_NVM_ELBAF_PIF_MASK) >> 7; } mbuffer_size = ((unsigned long long)cfg.block_count + 1) * ms; @@ -7683,22 +7433,20 @@ static int submit_io(int opcode, char *command, const char *desc, int argc, char strerror(errno)); err = -EINVAL; } else { - printf("%s: Success\n", command); + fprintf(stderr, "%s: Success\n", command); } } free_mbuffer: free(mbuffer); free_buffer: - nvme_free(buffer, huge); + nvme_free_huge(buffer, huge); close_mfd: if (strlen(cfg.metadata)) close(mfd); close_dfd: close(dfd); -close_dev: - dev_close(dev); -ret: + return err; } @@ -7731,9 +7479,9 @@ static int verify_cmd(int argc, char **argv, struct command *cmd, struct plugin { __u16 control = 0; __u8 lba_index, sts = 0, pif = 0; - struct nvme_id_ns ns; - struct nvme_nvm_id_ns nvm_ns; - struct nvme_dev *dev; + _cleanup_free_ struct nvme_nvm_id_ns *nvm_ns = NULL; + _cleanup_free_ struct nvme_id_ns *ns = NULL; + _cleanup_nvme_dev_ struct nvme_dev *dev = NULL; int err; const char *desc = "Verify specified logical blocks on the given device."; @@ -7770,29 +7518,25 @@ static int verify_cmd(int argc, char **argv, struct command *cmd, struct plugin .storage_tag_check = false, }; - OPT_ARGS(opts) = { - OPT_UINT("namespace-id", 'n', &cfg.namespace_id, namespace_desired), - OPT_SUFFIX("start-block", 's', &cfg.start_block, start_block), - OPT_SHRT("block-count", 'c', &cfg.block_count, block_count), - OPT_FLAG("limited-retry", 'l', &cfg.limited_retry, limited_retry), - OPT_FLAG("force-unit-access", 'f', &cfg.force_unit_access, force_unit_access_verify), - OPT_BYTE("prinfo", 'p', &cfg.prinfo, prinfo), - OPT_SUFFIX("ref-tag", 'r', &cfg.ref_tag, ref_tag), - OPT_SHRT("app-tag", 'a', &cfg.app_tag, app_tag), - OPT_SHRT("app-tag-mask", 'm', &cfg.app_tag_mask, app_tag_mask), - OPT_SUFFIX("storage-tag", 'S', &cfg.storage_tag, storage_tag), - OPT_FLAG("storage-tag-check", 'C', &cfg.storage_tag_check, storage_tag_check), - OPT_END() - }; + NVME_ARGS(opts, cfg, + OPT_UINT("namespace-id", 'n', &cfg.namespace_id, namespace_desired), + OPT_SUFFIX("start-block", 's', &cfg.start_block, start_block), + OPT_SHRT("block-count", 'c', &cfg.block_count, block_count), + OPT_FLAG("limited-retry", 'l', &cfg.limited_retry, limited_retry), + OPT_FLAG("force-unit-access", 'f', &cfg.force_unit_access, force_unit_access_verify), + OPT_BYTE("prinfo", 'p', &cfg.prinfo, prinfo), + OPT_SUFFIX("ref-tag", 'r', &cfg.ref_tag, ref_tag), + OPT_SHRT("app-tag", 'a', &cfg.app_tag, app_tag), + OPT_SHRT("app-tag-mask", 'm', &cfg.app_tag_mask, app_tag_mask), + OPT_SUFFIX("storage-tag", 'S', &cfg.storage_tag, storage_tag), + OPT_FLAG("storage-tag-check", 'C', &cfg.storage_tag_check, storage_tag_check)); err = parse_and_open(&dev, argc, argv, desc, opts); if (err) - goto ret; + return err; - if (cfg.prinfo > 0xf) { - err = EINVAL; - goto close_dev; - } + if (cfg.prinfo > 0xf) + return -EINVAL; control |= (cfg.prinfo << 10); if (cfg.limited_retry) @@ -7806,31 +7550,37 @@ static int verify_cmd(int argc, char **argv, struct command *cmd, struct plugin err = nvme_get_nsid(dev_fd(dev), &cfg.namespace_id); if (err < 0) { nvme_show_error("get-namespace-id: %s", nvme_strerror(errno)); - goto close_dev; + return err; } } - err = nvme_cli_identify_ns(dev, cfg.namespace_id, &ns); + ns = nvme_alloc(sizeof(*ns)); + if (!ns) + return -ENOMEM; + + err = nvme_cli_identify_ns(dev, cfg.namespace_id, ns); if (err < 0) { nvme_show_error("identify namespace: %s", nvme_strerror(errno)); - goto close_dev; + return err; } else if (err) { nvme_show_status(err); - goto close_dev; + return err; } + nvm_ns = nvme_alloc(sizeof(*nvm_ns)); + if (!nvm_ns) + return -ENOMEM; + err = nvme_identify_ns_csi(dev_fd(dev), cfg.namespace_id, 0, - NVME_CSI_NVM, &nvm_ns); + NVME_CSI_NVM, nvm_ns); if (!err) { - nvme_id_ns_flbas_to_lbaf_inuse(ns.flbas, &lba_index); - sts = nvm_ns.elbaf[lba_index] & NVME_NVM_ELBAF_STS_MASK; - pif = (nvm_ns.elbaf[lba_index] & NVME_NVM_ELBAF_PIF_MASK) >> 7; + nvme_id_ns_flbas_to_lbaf_inuse(ns->flbas, &lba_index); + sts = nvm_ns->elbaf[lba_index] & NVME_NVM_ELBAF_STS_MASK; + pif = (nvm_ns->elbaf[lba_index] & NVME_NVM_ELBAF_PIF_MASK) >> 7; } - if (invalid_tags(cfg.storage_tag, cfg.ref_tag, sts, pif)) { - err = -EINVAL; - goto close_dev; - } + if (invalid_tags(cfg.storage_tag, cfg.ref_tag, sts, pif)) + return -EINVAL; struct nvme_io_args args = { .args_size = sizeof(args), @@ -7856,9 +7606,6 @@ static int verify_cmd(int argc, char **argv, struct command *cmd, struct plugin else printf("NVME Verify Success\n"); -close_dev: - dev_close(dev); -ret: return err; } @@ -7872,8 +7619,9 @@ static int sec_recv(int argc, char **argv, struct command *cmd, struct plugin *p "the same security protocol."; const char *size = "size of buffer (prints to stdout on success)"; const char *al = "allocation length (cf. SPC-4)"; - struct nvme_dev *dev; - void *sec_buf = NULL; + + _cleanup_nvme_dev_ struct nvme_dev *dev = NULL; + _cleanup_free_ void *sec_buf = NULL; int err; struct config { @@ -7896,27 +7644,23 @@ static int sec_recv(int argc, char **argv, struct command *cmd, struct plugin *p .raw_binary = false, }; - OPT_ARGS(opts) = { - OPT_UINT("namespace-id", 'n', &cfg.namespace_id, namespace_desired), - OPT_UINT("size", 'x', &cfg.size, size), - OPT_BYTE("nssf", 'N', &cfg.nssf, nssf), - OPT_BYTE("secp", 'p', &cfg.secp, secp), - OPT_SHRT("spsp", 's', &cfg.spsp, spsp), - OPT_UINT("al", 't', &cfg.al, al), - OPT_FLAG("raw-binary", 'b', &cfg.raw_binary, raw_dump), - OPT_END() - }; + NVME_ARGS(opts, cfg, + OPT_UINT("namespace-id", 'n', &cfg.namespace_id, namespace_desired), + OPT_UINT("size", 'x', &cfg.size, size), + OPT_BYTE("nssf", 'N', &cfg.nssf, nssf), + OPT_BYTE("secp", 'p', &cfg.secp, secp), + OPT_SHRT("spsp", 's', &cfg.spsp, spsp), + OPT_UINT("al", 't', &cfg.al, al), + OPT_FLAG("raw-binary", 'b', &cfg.raw_binary, raw_dump)); err = parse_and_open(&dev, argc, argv, desc, opts); if (err) - goto ret; + return err; if (cfg.size) { - if (posix_memalign(&sec_buf, getpagesize(), cfg.size)) { - nvme_show_error("No memory for security size:%d", cfg.size); - err = -ENOMEM; - goto close_dev; - } + sec_buf = nvme_alloc(sizeof(*sec_buf)); + if (!sec_buf) + return -ENOMEM; } struct nvme_security_receive_args args = { @@ -7946,11 +7690,6 @@ static int sec_recv(int argc, char **argv, struct command *cmd, struct plugin *p d_raw((unsigned char *)sec_buf, cfg.size); } - free(sec_buf); - -close_dev: - dev_close(dev); -ret: return err; } @@ -7967,10 +7706,10 @@ static int get_lba_status(int argc, char **argv, struct command *cmd, const char *rl = "Range Length(RL) specifies the length of the range of contiguous LBAs beginning at SLBA"; + _cleanup_nvme_dev_ struct nvme_dev *dev = NULL; + _cleanup_free_ void *buf = NULL; enum nvme_print_flags flags; unsigned long buf_len; - struct nvme_dev *dev; - void *buf; int err; struct config { @@ -7980,7 +7719,6 @@ static int get_lba_status(int argc, char **argv, struct command *cmd, __u8 atype; __u16 rl; __u32 timeout; - char *output_format; }; struct config cfg = { @@ -7990,42 +7728,35 @@ static int get_lba_status(int argc, char **argv, struct command *cmd, .atype = 0, .rl = 0, .timeout = 0, - .output_format = "normal", }; - OPT_ARGS(opts) = { - OPT_UINT("namespace-id", 'n', &cfg.namespace_id, namespace_desired), - OPT_SUFFIX("start-lba", 's', &cfg.slba, slba), - OPT_UINT("max-dw", 'm', &cfg.mndw, mndw), - OPT_BYTE("action", 'a', &cfg.atype, atype), - OPT_SHRT("range-len", 'l', &cfg.rl, rl), - OPT_UINT("timeout", 't', &cfg.timeout, timeout), - OPT_FMT("output-format", 'o', &cfg.output_format, output_format), - OPT_END() - }; + NVME_ARGS(opts, cfg, + OPT_UINT("namespace-id", 'n', &cfg.namespace_id, namespace_desired), + OPT_SUFFIX("start-lba", 's', &cfg.slba, slba), + OPT_UINT("max-dw", 'm', &cfg.mndw, mndw), + OPT_BYTE("action", 'a', &cfg.atype, atype), + OPT_SHRT("range-len", 'l', &cfg.rl, rl), + OPT_UINT("timeout", 't', &cfg.timeout, timeout)); err = parse_and_open(&dev, argc, argv, desc, opts); if (err) - goto err; + return err; - err = flags = validate_output_format(cfg.output_format); + err = flags = validate_output_format(output_format_val); if (err < 0) { nvme_show_error("Invalid output format"); - goto close_dev; + return err; } if (!cfg.atype) { nvme_show_error("action type (--action) has to be given"); - err = -EINVAL; - goto close_dev; + return -EINVAL; } buf_len = (cfg.mndw + 1) * 4; - buf = calloc(1, buf_len); - if (!buf) { - err = -ENOMEM; - goto close_dev; - } + buf = nvme_alloc(buf_len); + if (!buf) + return -ENOMEM; struct nvme_get_lba_status_args args = { .args_size = sizeof(args), @@ -8046,10 +7777,7 @@ static int get_lba_status(int argc, char **argv, struct command *cmd, nvme_show_status(err); else nvme_show_error("get lba status: %s", nvme_strerror(errno)); - free(buf); -close_dev: - dev_close(dev); -err: + return err; } @@ -8066,7 +7794,7 @@ static int capacity_mgmt(int argc, char **argv, struct command *cmd, struct plug const char *cap_upper = "Most significant 32 bits of the capacity in bytes of the Endurance Group or NVM Set to be created"; - struct nvme_dev *dev; + _cleanup_nvme_dev_ struct nvme_dev *dev = NULL; int err = -1; __u32 result; @@ -8084,22 +7812,19 @@ static int capacity_mgmt(int argc, char **argv, struct command *cmd, struct plug .dw12 = 0, }; - OPT_ARGS(opts) = { - OPT_BYTE("operation", 'o', &cfg.operation, operation), - OPT_SHRT("element-id", 'i', &cfg.element_id, element_id), - OPT_UINT("cap-lower", 'l', &cfg.dw11, cap_lower), - OPT_UINT("cap-upper", 'u', &cfg.dw12, cap_upper), - OPT_END() - }; + NVME_ARGS(opts, cfg, + OPT_BYTE("operation", 'o', &cfg.operation, operation), + OPT_SHRT("element-id", 'i', &cfg.element_id, element_id), + OPT_UINT("cap-lower", 'l', &cfg.dw11, cap_lower), + OPT_UINT("cap-upper", 'u', &cfg.dw12, cap_upper)); err = parse_and_open(&dev, argc, argv, desc, opts); if (err) - goto ret; + return err; if (cfg.operation > 0xf) { nvme_show_error("invalid operation field: %u", cfg.operation); - err = -1; - goto close_dev; + return -1; } struct nvme_capacity_mgmt_args args = { @@ -8125,9 +7850,6 @@ static int capacity_mgmt(int argc, char **argv, struct command *cmd, struct plug nvme_show_error("capacity management: %s", nvme_strerror(errno)); } -close_dev: - dev_close(dev); -ret: return err; } @@ -8137,10 +7859,10 @@ static int dir_receive(int argc, char **argv, struct command *cmd, struct plugin const char *nsr = "namespace stream requested"; enum nvme_print_flags flags = NORMAL; - struct nvme_dev *dev; + _cleanup_nvme_dev_ struct nvme_dev *dev = NULL; + _cleanup_free_ void *buf = NULL; __u32 result; __u32 dw12 = 0; - void *buf = NULL; int err; struct config { @@ -8165,21 +7887,19 @@ static int dir_receive(int argc, char **argv, struct command *cmd, struct plugin .human_readable = false, }; - OPT_ARGS(opts) = { - OPT_UINT("namespace-id", 'n', &cfg.namespace_id, namespace_id_desired), - OPT_UINT("data-len", 'l', &cfg.data_len, buf_len), - OPT_FLAG("raw-binary", 'b', &cfg.raw_binary, raw_directive), - OPT_BYTE("dir-type", 'D', &cfg.dtype, dtype), - OPT_SHRT("dir-spec", 'S', &cfg.dspec, dspec_w_dtype), - OPT_BYTE("dir-oper", 'O', &cfg.doper, doper), - OPT_SHRT("req-resource", 'r', &cfg.nsr, nsr), - OPT_FLAG("human-readable", 'H', &cfg.human_readable, human_readable_directive), - OPT_END() - }; + NVME_ARGS(opts, cfg, + OPT_UINT("namespace-id", 'n', &cfg.namespace_id, namespace_id_desired), + OPT_UINT("data-len", 'l', &cfg.data_len, buf_len), + OPT_FLAG("raw-binary", 'b', &cfg.raw_binary, raw_directive), + OPT_BYTE("dir-type", 'D', &cfg.dtype, dtype), + OPT_SHRT("dir-spec", 'S', &cfg.dspec, dspec_w_dtype), + OPT_BYTE("dir-oper", 'O', &cfg.doper, doper), + OPT_SHRT("req-resource", 'r', &cfg.nsr, nsr), + OPT_FLAG("human-readable", 'H', &cfg.human_readable, human_readable_directive)); err = parse_and_open(&dev, argc, argv, desc, opts); if (err) - goto ret; + return err; if (cfg.human_readable) flags |= VERBOSE; @@ -8195,8 +7915,7 @@ static int dir_receive(int argc, char **argv, struct command *cmd, struct plugin break; default: nvme_show_error("invalid directive operations for Identify Directives"); - err = -EINVAL; - goto close_dev; + return -EINVAL; } break; case NVME_DIRECTIVE_DTYPE_STREAMS: @@ -8214,22 +7933,18 @@ static int dir_receive(int argc, char **argv, struct command *cmd, struct plugin break; default: nvme_show_error("invalid directive operations for Streams Directives"); - err = -EINVAL; - goto close_dev; + return -EINVAL; } break; default: nvme_show_error("invalid directive type"); - err = -EINVAL; - goto close_dev; + return -EINVAL; } if (cfg.data_len) { - if (posix_memalign(&buf, getpagesize(), cfg.data_len)) { - err = -ENOMEM; - goto close_dev; - } - memset(buf, 0, cfg.data_len); + buf = nvme_alloc(cfg.data_len); + if (!buf) + return -ENOMEM; } struct nvme_directive_recv_args args = { @@ -8254,10 +7969,6 @@ static int dir_receive(int argc, char **argv, struct command *cmd, struct plugin else if (err < 0) nvme_show_error("dir-receive: %s", nvme_strerror(errno)); - free(buf); -close_dev: - dev_close(dev); -ret: return err; } @@ -8289,7 +8000,7 @@ static int lockdown_cmd(int argc, char **argv, struct command *cmd, struct plugi "List that is used by the command.If this field is cleared to 0h,\n" "then no UUID index is specified"; - struct nvme_dev *dev; + _cleanup_nvme_dev_ struct nvme_dev *dev = NULL; int err = -1; struct config { @@ -8308,39 +8019,33 @@ static int lockdown_cmd(int argc, char **argv, struct command *cmd, struct plugi .uuid = 0, }; - OPT_ARGS(opts) = { - OPT_BYTE("ofi", 'o', &cfg.ofi, ofi_desc), - OPT_BYTE("ifc", 'f', &cfg.ifc, ifc_desc), - OPT_BYTE("prhbt", 'p', &cfg.prhbt, prhbt_desc), - OPT_BYTE("scp", 's', &cfg.scp, scp_desc), - OPT_BYTE("uuid", 'U', &cfg.uuid, uuid_desc), - OPT_END() - }; + NVME_ARGS(opts, cfg, + OPT_BYTE("ofi", 'o', &cfg.ofi, ofi_desc), + OPT_BYTE("ifc", 'f', &cfg.ifc, ifc_desc), + OPT_BYTE("prhbt", 'p', &cfg.prhbt, prhbt_desc), + OPT_BYTE("scp", 's', &cfg.scp, scp_desc), + OPT_BYTE("uuid", 'U', &cfg.uuid, uuid_desc)); err = parse_and_open(&dev, argc, argv, desc, opts); if (err) - goto ret; + return err; /* check for input argument limit */ if (cfg.ifc > 3) { nvme_show_error("invalid interface settings:%d", cfg.ifc); - err = -1; - goto close_dev; + return -1; } if (cfg.prhbt > 1) { nvme_show_error("invalid prohibit settings:%d", cfg.prhbt); - err = -1; - goto close_dev; + return -1; } if (cfg.scp > 15) { nvme_show_error("invalid scope settings:%d", cfg.scp); - err = -1; - goto close_dev; + return -1; } if (cfg.uuid > 127) { nvme_show_error("invalid UUID index settings:%d", cfg.uuid); - err = -1; - goto close_dev; + return -1; } struct nvme_lockdown_args args = { @@ -8362,9 +8067,6 @@ static int lockdown_cmd(int argc, char **argv, struct command *cmd, struct plugi else printf("Lockdown Command is Successful\n"); -close_dev: - dev_close(dev); -ret: return err; } @@ -8416,11 +8118,11 @@ static int passthru(int argc, char **argv, bool admin, const char *wr = "set dataflow direction to send"; const char *prefill = "prefill buffers with known byte-value, default 0"; + _cleanup_nvme_dev_ struct nvme_dev *dev = NULL; int flags; int mode = 0644; void *data = NULL, *mdata = NULL; int err = 0, dfd, mfd; - struct nvme_dev *dev; __u32 result; bool huge = false; const char *cmd_name = NULL; @@ -8453,37 +8155,35 @@ static int passthru(int argc, char **argv, bool admin, .latency = false, }; - OPT_ARGS(opts) = { - OPT_BYTE("opcode", 'o', &cfg.opcode, opcode), - OPT_BYTE("flags", 'f', &cfg.flags, cflags), - OPT_BYTE("prefill", 'p', &cfg.prefill, prefill), - OPT_SHRT("rsvd", 'R', &cfg.rsvd, rsvd), - OPT_UINT("namespace-id", 'n', &cfg.namespace_id, namespace_desired), - OPT_UINT("data-len", 'l', &cfg.data_len, data_len), - OPT_UINT("metadata-len", 'm', &cfg.metadata_len, metadata_len), - OPT_UINT("timeout", 't', &cfg.timeout, timeout), - OPT_UINT("cdw2", '2', &cfg.cdw2, cdw2), - OPT_UINT("cdw3", '3', &cfg.cdw3, cdw3), - OPT_UINT("cdw10", '4', &cfg.cdw10, cdw10), - OPT_UINT("cdw11", '5', &cfg.cdw11, cdw11), - OPT_UINT("cdw12", '6', &cfg.cdw12, cdw12), - OPT_UINT("cdw13", '7', &cfg.cdw13, cdw13), - OPT_UINT("cdw14", '8', &cfg.cdw14, cdw14), - OPT_UINT("cdw15", '9', &cfg.cdw15, cdw15), - OPT_FILE("input-file", 'i', &cfg.input_file, input), - OPT_FILE("metadata", 'M', &cfg.metadata, metadata), - OPT_FLAG("raw-binary", 'b', &cfg.raw_binary, raw_dump), - OPT_FLAG("show-command", 's', &cfg.show_command, show), - OPT_FLAG("dry-run", 'd', &cfg.dry_run, dry), - OPT_FLAG("read", 'r', &cfg.read, re), - OPT_FLAG("write", 'w', &cfg.write, wr), - OPT_FLAG("latency", 'T', &cfg.latency, latency), - OPT_END() - }; + NVME_ARGS(opts, cfg, + OPT_BYTE("opcode", 'o', &cfg.opcode, opcode), + OPT_BYTE("flags", 'f', &cfg.flags, cflags), + OPT_BYTE("prefill", 'p', &cfg.prefill, prefill), + OPT_SHRT("rsvd", 'R', &cfg.rsvd, rsvd), + OPT_UINT("namespace-id", 'n', &cfg.namespace_id, namespace_desired), + OPT_UINT("data-len", 'l', &cfg.data_len, data_len), + OPT_UINT("metadata-len", 'm', &cfg.metadata_len, metadata_len), + OPT_UINT("timeout", 't', &cfg.timeout, timeout), + OPT_UINT("cdw2", '2', &cfg.cdw2, cdw2), + OPT_UINT("cdw3", '3', &cfg.cdw3, cdw3), + OPT_UINT("cdw10", '4', &cfg.cdw10, cdw10), + OPT_UINT("cdw11", '5', &cfg.cdw11, cdw11), + OPT_UINT("cdw12", '6', &cfg.cdw12, cdw12), + OPT_UINT("cdw13", '7', &cfg.cdw13, cdw13), + OPT_UINT("cdw14", '8', &cfg.cdw14, cdw14), + OPT_UINT("cdw15", '9', &cfg.cdw15, cdw15), + OPT_FILE("input-file", 'i', &cfg.input_file, input), + OPT_FILE("metadata", 'M', &cfg.metadata, metadata), + OPT_FLAG("raw-binary", 'b', &cfg.raw_binary, raw_dump), + OPT_FLAG("show-command", 's', &cfg.show_command, show), + OPT_FLAG("dry-run", 'd', &cfg.dry_run, dry), + OPT_FLAG("read", 'r', &cfg.read, re), + OPT_FLAG("write", 'w', &cfg.write, wr), + OPT_FLAG("latency", 'T', &cfg.latency, latency)); err = parse_and_open(&dev, argc, argv, desc, opts); if (err) - goto ret; + return err; if (cfg.opcode & 0x01) cfg.write = true; @@ -8505,8 +8205,7 @@ static int passthru(int argc, char **argv, bool admin, dfd = open(cfg.input_file, flags, mode); if (dfd < 0) { nvme_show_perror(cfg.input_file); - err = -EINVAL; - goto close_dev; + return -EINVAL; } } @@ -8538,7 +8237,7 @@ static int passthru(int argc, char **argv, bool admin, } if (cfg.data_len) { - data = nvme_alloc(cfg.data_len, &huge); + data = nvme_alloc_huge(cfg.data_len, &huge); if (!data) { err = -ENOMEM; goto free_metadata; @@ -8615,24 +8314,22 @@ static int passthru(int argc, char **argv, bool admin, } else if (err) { nvme_show_status(err); } else { - printf("%s Command %s is Success and result: 0x%08x\n", admin ? "Admin" : "IO", - strcmp(cmd_name, "Unknown") ? cmd_name : "Vendor Specific", result); + fprintf(stderr, "%s Command %s is Success and result: 0x%08x\n", admin ? "Admin" : "IO", + strcmp(cmd_name, "Unknown") ? cmd_name : "Vendor Specific", result); if (cfg.read) passthru_print_read_output(cfg, data, dfd, mdata, mfd, err); } free_metadata: free(mdata); free_data: - nvme_free(data, huge); + nvme_free_huge(data, huge); close_dfd: if (strlen(cfg.input_file)) close(dfd); close_mfd: if (cfg.metadata && strlen(cfg.metadata)) close(mfd); -close_dev: - dev_close(dev); -ret: + return err; } @@ -8718,13 +8415,11 @@ static int gen_dhchap_key(int argc, char **argv, struct command *command, struct .hmac = 0, }; - OPT_ARGS(opts) = { - OPT_STR("secret", 's', &cfg.secret, secret), - OPT_UINT("key-length", 'l', &cfg.key_len, key_len), - OPT_STR("nqn", 'n', &cfg.nqn, nqn), - OPT_UINT("hmac", 'm', &cfg.hmac, hmac), - OPT_END() - }; + NVME_ARGS(opts, cfg, + OPT_STR("secret", 's', &cfg.secret, secret), + OPT_UINT("key-length", 'l', &cfg.key_len, key_len), + OPT_STR("nqn", 'n', &cfg.nqn, nqn), + OPT_UINT("hmac", 'm', &cfg.hmac, hmac)); err = argconfig_parse(argc, argv, desc, opts); if (err) @@ -8837,10 +8532,8 @@ static int check_dhchap_key(int argc, char **argv, struct command *command, stru .key = NULL, }; - OPT_ARGS(opts) = { - OPT_STR("key", 'k', &cfg.key, key), - OPT_END() - }; + NVME_ARGS(opts, cfg, + OPT_STR("key", 'k', &cfg.key, key)); err = argconfig_parse(argc, argv, desc, opts); if (err) @@ -8948,16 +8641,14 @@ static int gen_tls_key(int argc, char **argv, struct command *command, struct pl .insert = false, }; - OPT_ARGS(opts) = { - OPT_STR("keyring", 'k', &cfg.keyring, keyring), - OPT_STR("keytype", 't', &cfg.keytype, keytype), - OPT_STR("hostnqn", 'n', &cfg.hostnqn, hostnqn), - OPT_STR("subsysnqn", 'c', &cfg.subsysnqn, subsysnqn), - OPT_STR("secret", 's', &cfg.secret, secret), - OPT_UINT("hmac", 'm', &cfg.hmac, hmac), - OPT_FLAG("insert", 'i', &cfg.insert, insert), - OPT_END() - }; + NVME_ARGS(opts, cfg, + OPT_STR("keyring", 'k', &cfg.keyring, keyring), + OPT_STR("keytype", 't', &cfg.keytype, keytype), + OPT_STR("hostnqn", 'n', &cfg.hostnqn, hostnqn), + OPT_STR("subsysnqn", 'c', &cfg.subsysnqn, subsysnqn), + OPT_STR("secret", 's', &cfg.secret, secret), + OPT_UINT("hmac", 'm', &cfg.hmac, hmac), + OPT_FLAG("insert", 'i', &cfg.insert, insert)); err = argconfig_parse(argc, argv, desc, opts); if (err) @@ -9069,14 +8760,12 @@ static int check_tls_key(int argc, char **argv, struct command *command, struct .keydata = NULL, }; - OPT_ARGS(opts) = { - OPT_STR("keyring", 'k', &cfg.keyring, keyring), - OPT_STR("keytype", 't', &cfg.keytype, keytype), - OPT_STR("hostnqn", 'n', &cfg.hostnqn, hostnqn), - OPT_STR("subsysnqn", 'c', &cfg.subsysnqn, subsysnqn), - OPT_STR("keydata", 'd', &cfg.keydata, keydata), - OPT_END() - }; + NVME_ARGS(opts, cfg, + OPT_STR("keyring", 'k', &cfg.keyring, keyring), + OPT_STR("keytype", 't', &cfg.keytype, keytype), + OPT_STR("hostnqn", 'n', &cfg.hostnqn, hostnqn), + OPT_STR("subsysnqn", 'c', &cfg.subsysnqn, subsysnqn), + OPT_STR("keydata", 'd', &cfg.keydata, keydata)); err = argconfig_parse(argc, argv, desc, opts); if (err) @@ -9160,35 +8849,27 @@ static int show_topology_cmd(int argc, char **argv, struct command *command, str int err; struct config { - char *output_format; - int verbose; char *ranking; }; struct config cfg = { - .output_format = "normal", - .verbose = 0, .ranking = "namespace", }; - OPT_ARGS(opts) = { - OPT_FMT("output-format", 'o', &cfg.output_format, output_format), - OPT_INCR("verbose", 'v', &cfg.verbose, verbose), - OPT_FMT("ranking", 'r', &cfg.ranking, ranking), - OPT_END() - }; + NVME_ARGS(opts, cfg, + OPT_FMT("ranking", 'r', &cfg.ranking, ranking)); err = argconfig_parse(argc, argv, desc, opts); if (err) return err; - err = flags = validate_output_format(cfg.output_format); + err = flags = validate_output_format(output_format_val); if (err < 0) { nvme_show_error("Invalid output format"); return err; } - if (cfg.verbose) + if (argconfig_parse_seen(opts, "verbose")) flags |= VERBOSE; if (!strcmp(cfg.ranking, "namespace")) { @@ -9200,7 +8881,7 @@ static int show_topology_cmd(int argc, char **argv, struct command *command, str return -EINVAL; } - r = nvme_create_root(stderr, map_log_level(cfg.verbose, false)); + r = nvme_create_root(stderr, map_log_level(!!(flags & VERBOSE), false)); if (!r) { nvme_show_error("Failed to create topology root: %s", nvme_strerror(errno)); return -errno; @@ -9285,7 +8966,7 @@ static int nvme_mi(int argc, char **argv, __u8 admin_opcode, const char *desc) bool send = admin_opcode == nvme_admin_nvme_mi_send ? true : false; int fd = send ? STDIN_FILENO : STDOUT_FILENO; int flags = send ? O_RDONLY : O_WRONLY | O_CREAT; - struct nvme_dev *dev; + _cleanup_nvme_dev_ struct nvme_dev *dev = NULL; __u32 result; bool huge = false; @@ -9309,32 +8990,29 @@ static int nvme_mi(int argc, char **argv, __u8 admin_opcode, const char *desc) .input_file = "", }; - OPT_ARGS(opts) = { - OPT_BYTE("opcode", 'o', &cfg.opcode, opcode), - OPT_UINT("namespace-id", 'n', &cfg.namespace_id, namespace_desired), - OPT_UINT("data-len", 'l', &cfg.data_len, data_len), - OPT_UINT("nmimt", 'm', &cfg.nmimt, nmimt), - OPT_UINT("nmd0", '0', &cfg.nmd0, nmd0), - OPT_UINT("nmd1", '1', &cfg.nmd1, nmd1), - OPT_FILE("input-file", 'i', &cfg.input_file, input), - OPT_END() - }; + NVME_ARGS(opts, cfg, + OPT_BYTE("opcode", 'o', &cfg.opcode, opcode), + OPT_UINT("namespace-id", 'n', &cfg.namespace_id, namespace_desired), + OPT_UINT("data-len", 'l', &cfg.data_len, data_len), + OPT_UINT("nmimt", 'm', &cfg.nmimt, nmimt), + OPT_UINT("nmd0", '0', &cfg.nmd0, nmd0), + OPT_UINT("nmd1", '1', &cfg.nmd1, nmd1), + OPT_FILE("input-file", 'i', &cfg.input_file, input)); err = parse_and_open(&dev, argc, argv, desc, opts); if (err) - goto ret; + return err; if (strlen(cfg.input_file)) { fd = open(cfg.input_file, flags, mode); if (fd < 0) { nvme_show_perror(cfg.input_file); - err = -EINVAL; - goto close_dev; + return -EINVAL; } } if (cfg.data_len) { - data = nvme_alloc(cfg.data_len, &huge); + data = nvme_alloc_huge(cfg.data_len, &huge); if (!data) { err = -ENOMEM; goto close_fd; @@ -9370,13 +9048,11 @@ static int nvme_mi(int argc, char **argv, __u8 admin_opcode, const char *desc) } free_data: - nvme_free(data, huge); + nvme_free_huge(data, huge); close_fd: if (strlen(cfg.input_file)) close(fd); -close_dev: - dev_close(dev); -ret: + return err; } diff --git a/nvme.h b/nvme.h index d859983..10a08f8 100644 --- a/nvme.h +++ b/nvme.h @@ -29,6 +29,7 @@ #include "plugin.h" #include "util/json.h" #include "util/argconfig.h" +#include "util/cleanup.h" enum nvme_print_flags { NORMAL = 0, @@ -100,15 +101,24 @@ int parse_and_open(struct nvme_dev **dev, int argc, char **argv, const char *des void dev_close(struct nvme_dev *dev); +static inline void cleanup_nvme_dev(struct nvme_dev **dev) +{ + if (*dev) + dev_close(*dev); +} +#define _cleanup_nvme_dev_ __cleanup__(cleanup_nvme_dev) + extern const char *output_format; enum nvme_print_flags validate_output_format(const char *format); +bool nvme_is_output_format_json(void); int __id_ctrl(int argc, char **argv, struct command *cmd, struct plugin *plugin, void (*vs)(uint8_t *vs, struct json_object *root)); extern int current_index; -void *nvme_alloc(size_t len, bool *huge); -void nvme_free(void *p, bool huge); +void *nvme_alloc_huge(size_t len, bool *huge); +void nvme_free_huge(void *p, bool huge); + const char *nvme_strerror(int errnum); unsigned long long elapsed_utime(struct timeval start_time, diff --git a/nvmf-autoconnect/udev-rules/71-nvmf-iopolicy-netapp.rules.in b/nvmf-autoconnect/udev-rules/71-nvmf-iopolicy-netapp.rules.in index aefd9d4..299fe22 100644 --- a/nvmf-autoconnect/udev-rules/71-nvmf-iopolicy-netapp.rules.in +++ b/nvmf-autoconnect/udev-rules/71-nvmf-iopolicy-netapp.rules.in @@ -1,3 +1,3 @@ # Enable round-robin for NetApp ONTAP and NetApp E-Series -ACTION=="add", SUBSYSTEM=="nvme-subsystem", ATTR{model}=="NetApp ONTAP Controller", ATTR{iopolicy}="round-robin" -ACTION=="add", SUBSYSTEM=="nvme-subsystem", ATTR{model}=="NetApp E-Series", ATTR{iopolicy}="round-robin" +ACTION=="add", SUBSYSTEM=="nvme-subsystem", ATTR{subsystype}=="nvm", ATTR{model}=="NetApp ONTAP Controller", ATTR{iopolicy}="round-robin" +ACTION=="add", SUBSYSTEM=="nvme-subsystem", ATTR{subsystype}=="nvm", ATTR{model}=="NetApp E-Series", ATTR{iopolicy}="round-robin" diff --git a/plugins/intel/intel-nvme.c b/plugins/intel/intel-nvme.c index e62c85d..3b2f973 100644 --- a/plugins/intel/intel-nvme.c +++ b/plugins/intel/intel-nvme.c @@ -1065,7 +1065,7 @@ static int get_lat_stats_log(int argc, char **argv, struct command *cmd, struct media_version[1] = (data[3] << 8) | data[2]; if (err) - goto close_fd; + goto close_dev; if (media_version[0] == 1000) { __u32 thresholds[OPTANE_V1000_BUCKET_LEN] = {0}; @@ -1088,7 +1088,7 @@ static int get_lat_stats_log(int argc, char **argv, struct command *cmd, struct if (err) { fprintf(stderr, "Quering thresholds failed. "); nvme_show_status(err); - goto close_fd; + goto close_dev; } /* Update bucket thresholds to be printed */ @@ -1124,7 +1124,7 @@ static int get_lat_stats_log(int argc, char **argv, struct command *cmd, struct sizeof(stats)); } -close_fd: +close_dev: dev_close(dev); return err; } diff --git a/plugins/micron/micron-nvme.c b/plugins/micron/micron-nvme.c index d6fb601..195c971 100644 --- a/plugins/micron/micron-nvme.c +++ b/plugins/micron/micron-nvme.c @@ -1799,7 +1799,7 @@ static void GetGenericLogs(int fd, const char *dir) } log_len = le64_to_cpu(pevent_log.tll); - pevent_log_info = nvme_alloc(log_len, &huge); + pevent_log_info = nvme_alloc_huge(log_len, &huge); if (!pevent_log_info) { perror("could not alloc buffer for persistent event log page (ignored)!\n"); return; @@ -1809,7 +1809,7 @@ static void GetGenericLogs(int fd, const char *dir) if (!err) WriteData((__u8 *)pevent_log_info, log_len, dir, "persistent_event_log.bin", "persistent event log"); - nvme_free(pevent_log_info, huge); + nvme_free_huge(pevent_log_info, huge); } static void GetNSIDDInfo(int fd, const char *dir, int nsid) diff --git a/plugins/ocp/ocp-nvme.c b/plugins/ocp/ocp-nvme.c index edf75fc..fdb6b50 100644 --- a/plugins/ocp/ocp-nvme.c +++ b/plugins/ocp/ocp-nvme.c @@ -1863,6 +1863,103 @@ static int ocp_device_capabilities_log(int argc, char **argv, struct command *cm return ret; } +/////////////////////////////////////////////////////////////////////////////// +/////////////////////////////////////////////////////////////////////////////// +/////////////////////////////////////////////////////////////////////////////// +/////////////////////////////////////////////////////////////////////////////// +/// DSSD Power State (Feature Identifier C7h) Set Feature + +static int set_dssd_power_state(struct nvme_dev *dev, const __u32 nsid, + const __u8 fid, __u8 power_state, bool save, + bool uuid) +{ + __u32 result; + int err; + int uuid_index = 0; + + if (uuid) { + /* OCP 2.0 requires UUID index support */ + err = ocp_get_uuid_index(dev, &uuid_index); + if (err || !uuid_index) { + nvme_show_error("ERROR: No OCP UUID index found"); + return err; + } + } + + struct nvme_set_features_args args = { + .args_size = sizeof(args), + .fd = dev_fd(dev), + .fid = fid, + .nsid = nsid, + .cdw11 = power_state, + .cdw12 = 0, + .save = save, + .uuidx = uuid_index, + .cdw15 = 0, + .data_len = 0, + .data = NULL, + .timeout = NVME_DEFAULT_IOCTL_TIMEOUT, + .result = &result, + }; + + err = nvme_set_features(&args); + if (err > 0) { + nvme_show_status(err); + } else if (err < 0) { + nvme_show_perror("Define DSSD Power State"); + fprintf(stderr, "Command failed while parsing.\n"); + } else { + printf("Successfully set DSSD Power State (feature: 0xC7) to below values\n"); + printf("DSSD Power State: 0x%x\n", power_state); + printf("Save bit Value: 0x%x\n", save); + } + + return err; +} + +static int set_dssd_power_state_feature(int argc, char **argv, struct command *cmd, + struct plugin *plugin) +{ + const char *desc = "Define DSSD Power State (Feature Identifier C7h) Set Feature."; + const char *power_state = "DSSD Power State to set in watts"; + const char *save = "Specifies that the controller shall save the attribute"; + const __u32 nsid = 0; + const __u8 fid = 0xC7; + struct nvme_dev *dev; + int err; + + struct config { + __u8 power_state; + bool save; + }; + + struct config cfg = { + .power_state = 0, + .save = false, + }; + + OPT_ARGS(opts) = { + OPT_BYTE("power-state", 'p', &cfg.power_state, power_state), + OPT_FLAG("save", 's', &cfg.save, save), + OPT_FLAG("no-uuid", 'n', NULL, + "Skip UUID index search (UUID index not required for OCP 1.0)"), + OPT_END() + }; + + err = parse_and_open(&dev, argc, argv, desc, opts); + if (err) + return err; + + if (argconfig_parse_seen(opts, "power state")) + err = set_dssd_power_state(dev, nsid, fid, cfg.power_state, + cfg.save, + !argconfig_parse_seen(opts, "no-uuid")); + + dev_close(dev); + + return err; +} + /////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////// diff --git a/plugins/ocp/ocp-nvme.h b/plugins/ocp/ocp-nvme.h index 74dd0ef..dda4ffe 100644 --- a/plugins/ocp/ocp-nvme.h +++ b/plugins/ocp/ocp-nvme.h @@ -22,10 +22,11 @@ PLUGIN(NAME("ocp", "OCP cloud SSD extensions", NVME_VERSION), ENTRY("clear-fw-activate-history", "Clear firmware update history log", clear_fw_update_history) ENTRY("eol-plp-failure-mode", "Define EOL or PLP circuitry failure mode.", eol_plp_failure_mode) ENTRY("clear-pcie-correctable-error-counters", "Clear PCIe correctable error counters", clear_pcie_corectable_error_counters) - ENTRY("vs-fw-activate-history", "Get firmware activation history log", fw_activation_history_log) + ENTRY("fw-activate-history", "Get firmware activation history log", fw_activation_history_log) ENTRY("unsupported-reqs-log", "Get Unsupported Requirements Log Page", ocp_unsupported_requirements_log) ENTRY("error-recovery-log", "Retrieve Error Recovery Log Page", ocp_error_recovery_log) ENTRY("device-capability-log", "Get Device capabilities Requirements Log Page", ocp_device_capabilities_log) + ENTRY("set-dssd-power-state-feature", "Get Device capabilities Requirements Log Page", set_dssd_power_state_feature) ) ); diff --git a/plugins/scaleflux/sfx-nvme.c b/plugins/scaleflux/sfx-nvme.c index 01867c7..e7f3b99 100644 --- a/plugins/scaleflux/sfx-nvme.c +++ b/plugins/scaleflux/sfx-nvme.c @@ -1410,7 +1410,7 @@ static int nvme_dump_evtlog(struct nvme_dev *dev, __u32 namespace_id, __u32 stor if (log_len % 4) log_len = (log_len / 4 + 1) * 4; - pevent_log_info = nvme_alloc(single_len, &huge); + pevent_log_info = nvme_alloc_huge(single_len, &huge); if (!pevent_log_info) { err = -ENOMEM; goto free_pevent; @@ -1453,8 +1453,8 @@ static int nvme_dump_evtlog(struct nvme_dev *dev, __u32 namespace_id, __u32 stor printf("\nDump-evtlog: Success\n"); if (parse) { - nvme_free(pevent_log_info, huge); - pevent_log_info = nvme_alloc(log_len, &huge); + nvme_free_huge(pevent_log_info, huge); + pevent_log_info = nvme_alloc_huge(log_len, &huge); if (!pevent_log_info) { fprintf(stderr, "Failed to alloc enough memory 0x%x to parse evtlog\n", log_len); err = -ENOMEM; @@ -1479,7 +1479,7 @@ static int nvme_dump_evtlog(struct nvme_dev *dev, __u32 namespace_id, __u32 stor close_fd: fclose(fd); free: - nvme_free(pevent_log_info, huge); + nvme_free_huge(pevent_log_info, huge); free_pevent: free(pevent); ret: diff --git a/plugins/solidigm/solidigm-telemetry/nlog.c b/plugins/solidigm/solidigm-telemetry/nlog.c index 43b8918..926772b 100644 --- a/plugins/solidigm/solidigm-telemetry/nlog.c +++ b/plugins/solidigm/solidigm-telemetry/nlog.c @@ -8,15 +8,16 @@ #include "nlog.h" #include "config.h" #include -#include #include +#include "ccan/ilog/ilog.h" + #define LOG_ENTRY_HEADER_SIZE 1 #define LOG_ENTRY_TIMESTAMP_SIZE 2 #define LOG_ENTRY_NUM_ARGS_MAX 8 #define LOG_ENTRY_MAX_SIZE (LOG_ENTRY_HEADER_SIZE + LOG_ENTRY_TIMESTAMP_SIZE + \ LOG_ENTRY_NUM_ARGS_MAX) -#define NUM_ARGS_MASK ((1 << ((int)log2(LOG_ENTRY_NUM_ARGS_MAX)+1)) - 1) +#define NUM_ARGS_MASK ((1 << ((int)STATIC_ILOG_32(LOG_ENTRY_NUM_ARGS_MAX))) - 1) #define MAX_HEADER_MISMATCH_TRACK 10 static int formats_find(struct json_object *formats, uint32_t val, struct json_object **format) diff --git a/plugins/wdc/wdc-nvme.c b/plugins/wdc/wdc-nvme.c index ec3f2b0..ae57e85 100644 --- a/plugins/wdc/wdc-nvme.c +++ b/plugins/wdc/wdc-nvme.c @@ -80,12 +80,13 @@ #define WDC_NVME_SN655_DEV_ID 0x2722 #define WDC_NVME_SN860_DEV_ID 0x2730 #define WDC_NVME_SN660_DEV_ID 0x2704 - -/* This id's are no longer supported, delete ?? */ -#define WDC_NVME_SN550_DEV_ID 0x2708 #define WDC_NVME_SN560_DEV_ID_1 0x2712 #define WDC_NVME_SN560_DEV_ID_2 0x2713 #define WDC_NVME_SN560_DEV_ID_3 0x2714 +#define WDC_NVME_SN861_DEV_ID 0x2750 + +/* This id's are no longer supported, delete ?? */ +#define WDC_NVME_SN550_DEV_ID 0x2708 #define WDC_NVME_SXSLCL_DEV_ID 0x2001 #define WDC_NVME_SN520_DEV_ID 0x5003 @@ -1666,6 +1667,17 @@ static __u64 wdc_get_drive_capabilities(nvme_root_t r, struct nvme_dev *dev) WDC_DRIVE_CAP_CLEAR_PCIE); break; + case WDC_NVME_SN861_DEV_ID: + capabilities |= (WDC_DRIVE_CAP_C0_LOG_PAGE | WDC_DRIVE_CAP_OCP_C1_LOG_PAGE | + WDC_DRIVE_CAP_C3_LOG_PAGE | WDC_DRIVE_CAP_OCP_C4_LOG_PAGE | + WDC_DRIVE_CAP_OCP_C5_LOG_PAGE); + + capabilities |= (WDC_DRIVE_CAP_CAP_DIAG | WDC_DRIVE_CAP_INTERNAL_LOG | + WDC_DRIVE_CAP_DRIVE_STATUS | WDC_DRIVE_CAP_CLEAR_ASSERT | + WDC_DRIVE_CAP_RESIZE | WDC_DRIVE_CAP_FW_ACTIVATE_HISTORY | + WDC_DRVIE_CAP_DISABLE_CTLR_TELE_LOG | + WDC_DRIVE_CAP_REASON_ID); + default: capabilities = 0; } @@ -6514,6 +6526,8 @@ static int wdc_get_c0_log_page(nvme_root_t r, struct nvme_dev *dev, char *format case WDC_NVME_SN560_DEV_ID_3: fallthrough; case WDC_NVME_SN550_DEV_ID: + fallthrough; + case WDC_NVME_SN861_DEV_ID: ret = wdc_get_c0_log_page_sn(r, dev, uuid_index, format, namespace_id, fmt); break; case WDC_NVME_ZN350_DEV_ID: @@ -9437,7 +9451,7 @@ static int wdc_reason_identifier(int argc, char **argv, cfg.log_id != NVME_LOG_LID_TELEMETRY_CTRL) { fprintf(stderr, "ERROR: WDC: Invalid Log ID. It must be 7 (Host) or 8 (Controller)\n"); ret = -1; - goto close_fd; + goto close_dev; } if (cfg.file) { @@ -9448,7 +9462,7 @@ static int wdc_reason_identifier(int argc, char **argv, if (verify_file < 0) { fprintf(stderr, "ERROR: WDC: open: %s\n", strerror(errno)); ret = -1; - goto close_fd; + goto close_dev; } close(verify_file); strncpy(f, cfg.file, PATH_MAX - 1); @@ -9466,12 +9480,12 @@ static int wdc_reason_identifier(int argc, char **argv, if (wdc_get_serial_name(dev, f, PATH_MAX, fileSuffix) == -1) { fprintf(stderr, "ERROR: WDC: failed to generate file name\n"); ret = -1; - goto close_fd; + goto close_dev; } if (strlen(f) > PATH_MAX - 5) { fprintf(stderr, "ERROR: WDC: file name overflow\n"); ret = -1; - goto close_fd; + goto close_dev; } strcat(f, ".bin"); } @@ -9488,7 +9502,7 @@ static int wdc_reason_identifier(int argc, char **argv, nvme_show_status(ret); -close_fd: +close_dev: dev_close(dev); nvme_free_tree(r); return ret; @@ -10372,7 +10386,7 @@ static int wdc_vs_pcie_stats(int argc, char **argv, struct command *command, goto out; } - pcieStatsPtr = nvme_alloc(pcie_stats_size, &huge); + pcieStatsPtr = nvme_alloc_huge(pcie_stats_size, &huge); if (!pcieStatsPtr) { fprintf(stderr, "ERROR: WDC: PCIE Stats alloc: %s\n", strerror(errno)); ret = -1; @@ -10403,7 +10417,7 @@ static int wdc_vs_pcie_stats(int argc, char **argv, struct command *command, } } - nvme_free(pcieStatsPtr, huge); + nvme_free_huge(pcieStatsPtr, huge); out: nvme_free_tree(r); diff --git a/plugins/zns/zns.c b/plugins/zns/zns.c index 5b9d013..7b1b191 100644 --- a/plugins/zns/zns.c +++ b/plugins/zns/zns.c @@ -137,7 +137,7 @@ static int id_ctrl(int argc, char **argv, struct command *cmd, struct plugin *pl err = flags = validate_output_format(cfg.output_format); if (flags < 0) - goto close_fd; + goto close_dev; err = nvme_zns_identify_ctrl(dev_fd(dev), &ctrl); if (!err) @@ -146,7 +146,7 @@ static int id_ctrl(int argc, char **argv, struct command *cmd, struct plugin *pl nvme_show_status(err); else perror("zns identify controller"); -close_fd: +close_dev: dev_close(dev); return err; } @@ -190,7 +190,7 @@ static int id_ns(int argc, char **argv, struct command *cmd, struct plugin *plug flags = validate_output_format(cfg.output_format); if (flags < 0) - goto close_fd; + goto close_dev; if (cfg.vendor_specific) flags |= VS; if (cfg.human_readable) @@ -200,14 +200,14 @@ static int id_ns(int argc, char **argv, struct command *cmd, struct plugin *plug err = nvme_get_nsid(dev_fd(dev), &cfg.namespace_id); if (err < 0) { perror("get-namespace-id"); - goto close_fd; + goto close_dev; } } err = nvme_identify_ns(dev_fd(dev), cfg.namespace_id, &id_ns); if (err) { nvme_show_status(err); - goto close_fd; + goto close_dev; } err = nvme_zns_identify_ns(dev_fd(dev), cfg.namespace_id, &ns); @@ -217,7 +217,7 @@ static int id_ns(int argc, char **argv, struct command *cmd, struct plugin *plug nvme_show_status(err); else perror("zns identify namespace"); -close_fd: +close_dev: dev_close(dev); return err; } @@ -839,8 +839,8 @@ static int report_zones(int argc, char **argv, struct command *cmd, struct plugi unsigned int nr_zones_chunks = 1024, /* 1024 entries * 64 bytes per entry = 64k byte transfer */ nr_zones_retrieved = 0, nr_zones, - offset, log_len; + __u64 offset; int total_nr_zones = 0; struct nvme_zns_id_ns id_zns; struct nvme_id_ns id_ns; @@ -949,7 +949,7 @@ static int report_zones(int argc, char **argv, struct command *cmd, struct plugi log_len = sizeof(struct nvme_zone_report) + ((sizeof(struct nvme_zns_desc) * nr_zones_chunks) + (nr_zones_chunks * zdes)); report_size = log_len; - report = nvme_alloc(report_size, &huge); + report = nvme_alloc_huge(report_size, &huge); if (!report) { perror("alloc"); err = -ENOMEM; @@ -997,7 +997,7 @@ static int report_zones(int argc, char **argv, struct command *cmd, struct plugi ops->zns_finish_zone_list(total_nr_zones, zone_list); } - nvme_free(report, huge); + nvme_free_huge(report, huge); free_buff: free(buff); @@ -1258,13 +1258,13 @@ static int changed_zone_list(int argc, char **argv, struct command *cmd, struct flags = validate_output_format(cfg.output_format); if (flags < 0) - goto close_fd; + goto close_dev; if (!cfg.namespace_id) { err = nvme_get_nsid(dev_fd(dev), &cfg.namespace_id); if (err < 0) { perror("get-namespace-id"); - goto close_fd; + goto close_dev; } } @@ -1277,7 +1277,7 @@ static int changed_zone_list(int argc, char **argv, struct command *cmd, struct else perror("zns changed-zone-list"); -close_fd: +close_dev: dev_close(dev); return err; } diff --git a/scripts/build.sh b/scripts/build.sh index 94c62b8..07b7aa8 100755 --- a/scripts/build.sh +++ b/scripts/build.sh @@ -1,4 +1,5 @@ #!/bin/bash +set -e usage() { echo "Usage: build.sh [-b [release|debug]] " @@ -18,6 +19,8 @@ usage() { echo " fallback download all dependencies" echo " and build them as shared libaries" echo " cross use cross toolchain to build" + echo " coverage build coverage report" + echo " appimage build AppImage target" echo "" echo "configs with muon:" echo " [default] minimal static build" @@ -57,6 +60,8 @@ cd "$(git rev-parse --show-toplevel)" || exit 1 BUILDDIR="$(pwd)/.build-ci" +fn_exists() { declare -F "$1" > /dev/null; } + config_meson_default() { CC="${CC}" "${MESON}" setup \ --werror \ @@ -89,6 +94,26 @@ config_meson_cross() { "${BUILDDIR}" } +config_meson_coverage() { + CC="${CC}" "${MESON}" setup \ + --werror \ + --buildtype="${BUILDTYPE}" \ + --force-fallback-for=libnvme \ + -Dlibnvme:werror=false \ + -Db_coverage=true \ + "${BUILDDIR}" +} + +config_meson_appimage() { + CC="${CC}" "${MESON}" setup \ + --werror \ + --buildtype="${BUILDTYPE}" \ + --force-fallback-for=libnvme \ + --prefix=/usr \ + -Dlibnvme:werror=false \ + "${BUILDDIR}" +} + build_meson() { "${MESON}" compile \ -C "${BUILDDIR}" @@ -99,6 +124,17 @@ test_meson() { -C "${BUILDDIR}" } +test_meson_coverage() { + "${MESON}" test \ + -C "${BUILDDIR}" + ninja -C "${BUILDDIR}" coverage --verbose +} + +install_meson_appimage() { + "${MESON}" install \ + -C "${BUILDDIR}" +} + tools_build_samurai() { mkdir -p "${BUILDDIR}"/build-tools git clone --depth 1 https://github.com/michaelforney/samurai.git \ @@ -173,5 +209,6 @@ if [[ "${BUILDTOOL}" == "muon" ]]; then fi config_"${BUILDTOOL}"_"${CONFIG}" -build_"${BUILDTOOL}" -test_"${BUILDTOOL}" +fn_exists "build_${BUILDTOOL}_${CONFIG}" && "build_${BUILDTOOL}_${CONFIG}" || build_"${BUILDTOOL}" +fn_exists "test_${BUILDTOOL}_${CONFIG}" && "test_${BUILDTOOL}_${CONFIG}" || test_"${BUILDTOOL}" +fn_exists "install_${BUILDTOOL}_${CONFIG}" && "install_${BUILDTOOL}_${CONFIG}" || true; diff --git a/subprojects/libnvme.wrap b/subprojects/libnvme.wrap index 6070d24..0b9475b 100644 --- a/subprojects/libnvme.wrap +++ b/subprojects/libnvme.wrap @@ -1,6 +1,6 @@ [wrap-git] url = https://github.com/linux-nvme/libnvme.git -revision = 4fea83db8328ea788ea8f1001e8ce1cb80ef5fae +revision = 37a803cf77e224f66d86b1e1d9e74a15f55ea600 [provide] libnvme = libnvme_dep diff --git a/unit/test-uint128.c b/unit/test-uint128.c index 6301a38..f8478ef 100644 --- a/unit/test-uint128.c +++ b/unit/test-uint128.c @@ -3,6 +3,7 @@ #include #include #include +#include #include "../util/types.h" @@ -27,25 +28,36 @@ static void check_str(nvme_uint128_t val, const char *exp, const char *res) } struct tostr_test { + const char *locale; nvme_uint128_t val; const char *exp; }; static struct tostr_test tostr_tests[] = { - { U128(0, 0, 0, 0), "0" }, - { U128(0, 0, 0, 1), "1" }, - { U128(0, 0, 0, 10), "10" }, - { U128(4, 3, 2, 1), "316912650112397582603894390785" }, + { NULL, U128(0, 0, 0, 0),"0" }, + { NULL, U128(0, 0, 0, 1), "1" }, + { NULL, U128(0, 0, 0, 10), "10" }, + { NULL, U128(4, 3, 2, 1), "316912650112397582603894390785" }, { + NULL, U128(0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff), "340282366920938463463374607431768211455" }, + { "fr_FR.utf-8", U128(0, 0, 0, 1000), "1\u202f000" }, }; void tostr_test(struct tostr_test *test) { char *str; - str = uint128_t_to_string(test->val); + + if (!setlocale(LC_NUMERIC, test->locale)) + return; + + if (test->locale) + str = uint128_t_to_l10n_string(test->val); + else + str = uint128_t_to_string(test->val); + check_str(test->val, test->exp, str); } diff --git a/util/argconfig.c b/util/argconfig.c index effeea2..5ec3d6f 100644 --- a/util/argconfig.c +++ b/util/argconfig.c @@ -41,6 +41,7 @@ #include #include #include +#include static const char *append_usage_str = ""; @@ -163,6 +164,8 @@ static int argconfig_parse_type(struct argconfig_commandline_options *s, struct char *endptr; int ret = 0; + errno = 0; /* To distinguish success/failure after strtol/stroul call */ + switch (s->config_type) { case CFG_STRING: *((char **)value) = optarg; @@ -177,15 +180,6 @@ static int argconfig_parse_type(struct argconfig_commandline_options *s, struct if (errno || optarg == endptr) ret = argconfig_error("integer", option[index].name, optarg); break; - case CFG_BOOL: { - int tmp = strtol(optarg, &endptr, 0); - - if (errno || tmp < 0 || tmp > 1 || optarg == endptr) - ret = argconfig_error("0 or 1", option[index].name, optarg); - else - *((int *)value) = tmp; - break; - } case CFG_BYTE: ret = argconfig_parse_byte(option[index].name, optarg, (uint8_t *)value); break; @@ -313,23 +307,11 @@ static int argconfig_parse_val(struct argconfig_commandline_options *s, struct o return argconfig_parse_type(s, option, index); } -bool argconfig_output_format_json(bool set) -{ - static bool output_format_json; - - if (set) - output_format_json = true; - - return output_format_json; -} - -static bool argconfig_check_output_format_json(struct argconfig_commandline_options *s) +static bool argconfig_check_human_readable(struct argconfig_commandline_options *s) { for (; s && s->option; s++) { - if (strcmp(s->option, "output-format") || s->config_type != CFG_STRING) - continue; - if (!strcmp(*(char **)s->default_value, "json")) - return true; + if (!strcmp(s->option, "human-readable") && s->config_type == CFG_FLAG) + return s->seen; } return false; @@ -375,14 +357,10 @@ int argconfig_parse(int argc, char *argv[], const char *program_desc, } long_opts[option_index].name = "help"; - long_opts[option_index++].val = 'h'; - - long_opts[option_index].name = "json"; - long_opts[option_index].val = 'j'; + long_opts[option_index].val = 'h'; short_opts[short_index++] = '?'; - short_opts[short_index++] = 'h'; - short_opts[short_index] = 'j'; + short_opts[short_index] = 'h'; optind = 0; while ((c = getopt_long_only(argc, argv, short_opts, long_opts, &option_index)) != -1) { @@ -392,8 +370,6 @@ int argconfig_parse(int argc, char *argv[], const char *program_desc, ret = -EINVAL; break; } - if (c == 'j') - argconfig_output_format_json(true); for (option_index = 0; option_index < options_count; option_index++) { if (c == options[option_index].short_option) break; @@ -416,8 +392,8 @@ int argconfig_parse(int argc, char *argv[], const char *program_desc, break; } - if (argconfig_check_output_format_json(options)) - argconfig_output_format_json(true); + if (!argconfig_check_human_readable(options)) + setlocale(LC_ALL, "C"); out: free(short_opts); @@ -549,6 +525,58 @@ int argconfig_parse_comma_sep_array_long(char *string, unsigned long long *val, } } +#define DEFINE_ARGCONFIG_PARSE_COMMA_SEP_ARRAY_UINT_FUNC(size) \ +int argconfig_parse_comma_sep_array_u##size(char *string, \ + __u##size *val, \ + unsigned int max_length) \ +{ \ + int ret = 0; \ + uintmax_t v; \ + char *tmp; \ + char *p; \ + \ + if (!string || !strlen(string)) \ + return 0; \ + \ + tmp = strtok(string, ","); \ + if (!tmp) \ + return 0; \ + \ + v = strtoumax(tmp, &p, 0); \ + if (*p != 0) \ + return -1; \ + if (v > UINT##size##_MAX) { \ + fprintf(stderr, "%s out of range\n", tmp); \ + return -1; \ + } \ + val[ret] = v; \ + \ + ret++; \ + while (1) { \ + tmp = strtok(NULL, ","); \ + \ + if (tmp == NULL) \ + return ret; \ + \ + if (ret >= max_length) \ + return -1; \ + \ + v = strtoumax(tmp, &p, 0); \ + if (*p != 0) \ + return -1; \ + if (v > UINT##size##_MAX) { \ + fprintf(stderr, "%s out of range\n", tmp); \ + return -1; \ + } \ + val[ret] = v; \ + ret++; \ + } \ +} + +DEFINE_ARGCONFIG_PARSE_COMMA_SEP_ARRAY_UINT_FUNC(16); +DEFINE_ARGCONFIG_PARSE_COMMA_SEP_ARRAY_UINT_FUNC(32); +DEFINE_ARGCONFIG_PARSE_COMMA_SEP_ARRAY_UINT_FUNC(64); + bool argconfig_parse_seen(struct argconfig_commandline_options *s, const char *option) { diff --git a/util/argconfig.h b/util/argconfig.h index eaf8375..2a04a32 100644 --- a/util/argconfig.h +++ b/util/argconfig.h @@ -41,6 +41,8 @@ #include #include +#include + enum argconfig_types { CFG_FLAG, CFG_STRING, @@ -49,7 +51,6 @@ enum argconfig_types { CFG_LONG, CFG_LONG_SUFFIX, CFG_DOUBLE, - CFG_BOOL, CFG_BYTE, CFG_SHORT, CFG_POSITIVE, @@ -174,10 +175,15 @@ int argconfig_parse_comma_sep_array_short(char *string, unsigned short *ret, unsigned int max_length); int argconfig_parse_comma_sep_array_long(char *string, unsigned long long *ret, unsigned int max_length); +int argconfig_parse_comma_sep_array_u16(char *string, __u16 *val, + unsigned int max_length); +int argconfig_parse_comma_sep_array_u32(char *string, __u32 *val, + unsigned int max_length); +int argconfig_parse_comma_sep_array_u64(char *string, __u64 *val, + unsigned int max_length); int argconfig_parse_byte(const char *opt, const char *str, unsigned char *val); void print_word_wrapped(const char *s, int indent, int start, FILE *stream); bool argconfig_parse_seen(struct argconfig_commandline_options *options, const char *option); -bool argconfig_output_format_json(bool set); #endif diff --git a/util/cleanup.h b/util/cleanup.h index 575a25d..9227856 100644 --- a/util/cleanup.h +++ b/util/cleanup.h @@ -2,6 +2,9 @@ #ifndef __CLEANUP_H #define __CLEANUP_H +#include +#include + #define __cleanup__(fn) __attribute__((cleanup(fn))) #define DECLARE_CLEANUP_FUNC(name, type) \ @@ -16,4 +19,17 @@ DECLARE_CLEANUP_FUNC(name, type) \ DECLARE_CLEANUP_FUNC(cleanup_charp, char *); +static inline void freep(void *p) +{ + free(*(void**) p); +} +#define _cleanup_free_ __cleanup__(freep) + +static inline void close_file(int *f) +{ + if (*f >= 0) + close(*f); +} +#define _cleanup_file_ __cleanup__(close_file) + #endif diff --git a/util/json.h b/util/json.h index c362408..54e33e3 100644 --- a/util/json.h +++ b/util/json.h @@ -28,16 +28,18 @@ json_object_object_add(o, k, util_json_object_new_double(v)) #define json_object_add_value_float(o, k, v) \ json_object_object_add(o, k, json_object_new_double(v)) -#define json_object_add_value_string(o, k, v) \ - json_object_object_add(o, k, json_object_new_string(v)) +static inline int json_object_add_value_string(struct json_object *o, const char *k, const char *v) { + return json_object_object_add(o, k, v ? json_object_new_string(v) : NULL); +} #define json_object_add_value_array(o, k, v) \ json_object_object_add(o, k, v) #define json_object_add_value_object(o, k, v) \ json_object_object_add(o, k, v) #define json_array_add_value_object(o, k) \ json_object_array_add(o, k) -#define json_array_add_value_string(o, v) \ - json_object_array_add(o, json_object_new_string(v)) +static inline int json_array_add_value_string(struct json_object *o, const char *v) { + return json_object_array_add(o, v ? json_object_new_string(v) : NULL); +} #define json_print_object(o, u) \ printf("%s", json_object_to_json_string_ext(o, \ JSON_C_TO_STRING_PRETTY | \ diff --git a/util/types.c b/util/types.c index 044391d..376c734 100644 --- a/util/types.c +++ b/util/types.c @@ -67,20 +67,21 @@ static char *__uint128_t_to_string(nvme_uint128_t val, bool l10n) int idx = 60; __u64 div, rem; char *sep = NULL; - int i, len = 0; + int i, len = 0, cl = 0; if (l10n) { sep = localeconv()->thousands_sep; len = strlen(sep); + cl = 1; } /* terminate at the end, and build up from the ones */ str[--idx] = '\0'; do { - if (len && !((sizeof(str) - idx) % (3 + len))) { + if (len && !((sizeof(str) - idx) % (3 + cl))) { for (i = 0; i < len; i++) - str[--idx] = sep[i]; + str[--idx] = sep[len - i - 1]; } rem = val.words[0]; -- cgit v1.2.3