1
1
// SPDX-License-Identifier: BSD-3-Clause
2
- /* Copyright 2020-2022 , Intel Corporation */
2
+ /* Copyright 2020-2023 , Intel Corporation */
3
3
4
4
/*
5
5
* region_namespace_ndctl.c -- common ndctl functions
16
16
#include "region_namespace_ndctl.h"
17
17
#include "region_namespace.h"
18
18
#include "out.h"
19
+ #include "alloc.h"
19
20
20
21
/*
21
22
* ndctl_match_devdax -- (internal) returns 0 if the devdax matches
@@ -27,30 +28,42 @@ ndctl_match_devdax(dev_t st_rdev, const char *devname)
27
28
{
28
29
LOG (3 , "st_rdev %lu devname %s" , st_rdev , devname );
29
30
31
+ int ret = 0 ;
32
+ char * path ;
33
+ os_stat_t stat ;
34
+
30
35
if (* devname == '\0' )
31
36
return 1 ;
32
37
33
- char path [PATH_MAX ];
34
- os_stat_t stat ;
38
+ path = Malloc (PATH_MAX * sizeof (char ));
39
+ if (path == NULL ) {
40
+ ERR ("!Malloc" );
41
+ return PMEM2_E_ERRNO ;
42
+ }
35
43
36
44
if (util_snprintf (path , PATH_MAX , "/dev/%s" , devname ) < 0 ) {
37
45
ERR ("!snprintf" );
38
- return PMEM2_E_ERRNO ;
46
+ ret = PMEM2_E_ERRNO ;
47
+ goto end ;
39
48
}
40
49
41
50
if (os_stat (path , & stat )) {
42
51
ERR ("!stat %s" , path );
43
- return PMEM2_E_ERRNO ;
52
+ ret = PMEM2_E_ERRNO ;
53
+ goto end ;
44
54
}
45
55
46
56
if (st_rdev != stat .st_rdev ) {
47
57
LOG (10 , "skipping not matching device: %s" , path );
48
- return 1 ;
58
+ ret = 1 ;
59
+ goto end ;
49
60
}
50
61
51
62
LOG (4 , "found matching device: %s" , path );
63
+ end :
64
+ Free (path );
52
65
53
- return 0 ;
66
+ return ret ;
54
67
}
55
68
56
69
#define BUFF_LENGTH 64
@@ -65,27 +78,37 @@ ndctl_match_fsdax(dev_t st_dev, const char *devname)
65
78
{
66
79
LOG (3 , "st_dev %lu devname %s" , st_dev , devname );
67
80
81
+ int ret = 0 ;
82
+ char * path ;
83
+ char dev_id [BUFF_LENGTH ];
84
+
68
85
if (* devname == '\0' )
69
86
return 1 ;
70
87
71
- char path [PATH_MAX ];
72
- char dev_id [BUFF_LENGTH ];
88
+ path = Malloc (PATH_MAX * sizeof (char ));
89
+ if (path == NULL ) {
90
+ ERR ("!Malloc" );
91
+ return PMEM2_E_ERRNO ;
92
+ }
73
93
74
94
if (util_snprintf (path , PATH_MAX , "/sys/block/%s/dev" , devname ) < 0 ) {
75
95
ERR ("!snprintf" );
76
- return PMEM2_E_ERRNO ;
96
+ ret = PMEM2_E_ERRNO ;
97
+ goto end ;
77
98
}
78
99
79
100
if (util_snprintf (dev_id , BUFF_LENGTH , "%d:%d" ,
80
101
major (st_dev ), minor (st_dev )) < 0 ) {
81
102
ERR ("!snprintf" );
82
- return PMEM2_E_ERRNO ;
103
+ ret = PMEM2_E_ERRNO ;
104
+ goto end ;
83
105
}
84
106
85
107
int fd = os_open (path , O_RDONLY );
86
108
if (fd < 0 ) {
87
109
ERR ("!open \"%s\"" , path );
88
- return PMEM2_E_ERRNO ;
110
+ ret = PMEM2_E_ERRNO ;
111
+ goto end ;
89
112
}
90
113
91
114
char buff [BUFF_LENGTH ];
@@ -95,31 +118,37 @@ ndctl_match_fsdax(dev_t st_dev, const char *devname)
95
118
int oerrno = errno ; /* save the errno */
96
119
os_close (fd );
97
120
errno = oerrno ;
98
- return PMEM2_E_ERRNO ;
121
+ ret = PMEM2_E_ERRNO ;
122
+ goto end ;
99
123
}
100
124
101
125
os_close (fd );
102
126
103
127
if (nread == 0 ) {
104
128
ERR ("%s is empty" , path );
105
- return PMEM2_E_INVALID_DEV_FORMAT ;
129
+ ret = PMEM2_E_INVALID_DEV_FORMAT ;
130
+ goto end ;
106
131
}
107
132
108
133
if (buff [nread - 1 ] != '\n' ) {
109
134
ERR ("%s doesn't end with new line" , path );
110
- return PMEM2_E_INVALID_DEV_FORMAT ;
135
+ ret = PMEM2_E_INVALID_DEV_FORMAT ;
136
+ goto end ;
111
137
}
112
138
113
139
buff [nread - 1 ] = '\0' ;
114
140
115
141
if (strcmp (buff , dev_id ) != 0 ) {
116
142
LOG (10 , "skipping not matching device: %s" , path );
117
- return 1 ;
143
+ ret = 1 ;
144
+ goto end ;
118
145
}
119
146
120
147
LOG (4 , "found matching device: %s" , path );
148
+ end :
149
+ Free (path );
121
150
122
- return 0 ;
151
+ return ret ;
123
152
}
124
153
125
154
/*
0 commit comments