-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNvmeDeviceUtils.cpp
More file actions
67 lines (57 loc) · 1.97 KB
/
NvmeDeviceUtils.cpp
File metadata and controls
67 lines (57 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
* Copyright (C) 2025 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "Utils.h"
#include "VoldUtil.h"
#include "WriteBooster.h"
#include <android-base/file.h>
#include <android-base/logging.h>
#include <android-base/parseint.h>
#include <android-base/result.h>
#include <android-base/strings.h>
#include <fcntl.h>
#include <linux/nvme_ioctl.h>
#include <sys/ioctl.h>
#include <unistd.h>
using android::base::ReadFileToString;
using android::base::WriteStringToFile;
namespace android {
namespace vold {
// Define basic NVMe data structure needed for ioctl.
#define NVME_NSID_ALL 0xffffffff
#define NVME_ADMIN_GET_LOG_PAGE 0x2
#define LID_SMART_HEALTH_INFO 0x2
int32_t GetNvmeStorageLifeTime(const std::string& blk_device) {
uint8_t smart_log[512];
struct nvme_admin_cmd cmd = {
.opcode = NVME_ADMIN_GET_LOG_PAGE,
.nsid = NVME_NSID_ALL,
.addr = (uintptr_t)&smart_log,
.data_len = sizeof(smart_log),
.cdw10 = (((sizeof(smart_log) / sizeof(uint32_t)) - 1) << 16) | LID_SMART_HEALTH_INFO,
};
android::base::unique_fd fd(open(blk_device.c_str(), O_RDONLY | O_CLOEXEC));
if (fd < 0) {
PLOG(ERROR) << "Failed to open " << blk_device;
return -1;
}
if (ioctl(fd, NVME_IOCTL_ADMIN_CMD, &cmd)) {
PLOG(ERROR) << "Failed to issue NVME_IOCTL_ADMIN_CMD";
return -1;
}
return smart_log[5];
}
} // namespace vold
} // namespace android