11// SPDX-License-Identifier: BSD-3-Clause
2- /* Copyright 2020-2022 , Intel Corporation */
2+ /* Copyright 2020-2023 , Intel Corporation */
33
44/*
55 * region_namespace_ndctl.c -- common ndctl functions
1616#include "region_namespace_ndctl.h"
1717#include "region_namespace.h"
1818#include "out.h"
19+ #include "alloc.h"
1920
2021/*
2122 * ndctl_match_devdax -- (internal) returns 0 if the devdax matches
@@ -27,30 +28,43 @@ ndctl_match_devdax(dev_t st_rdev, const char *devname)
2728{
2829 LOG (3 , "st_rdev %lu devname %s" , st_rdev , devname );
2930
31+ int ret = 0 ;
32+ char * path ;
33+ os_stat_t stat ;
34+
3035 if (* devname == '\0' )
3136 return 1 ;
3237
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+ }
3543
3644 if (util_snprintf (path , PATH_MAX , "/dev/%s" , devname ) < 0 ) {
3745 ERR ("!snprintf" );
38- return PMEM2_E_ERRNO ;
46+ ret = PMEM2_E_ERRNO ;
47+ goto end ;
3948 }
4049
4150 if (os_stat (path , & stat )) {
4251 ERR ("!stat %s" , path );
43- return PMEM2_E_ERRNO ;
52+ ret = PMEM2_E_ERRNO ;
53+ goto end ;
4454 }
4555
4656 if (st_rdev != stat .st_rdev ) {
4757 LOG (10 , "skipping not matching device: %s" , path );
48- return 1 ;
58+ ret = 1 ;
59+ goto end ;
4960 }
5061
5162 LOG (4 , "found matching device: %s" , path );
5263
53- return 0 ;
64+ end :
65+ Free (path );
66+
67+ return ret ;
5468}
5569
5670#define BUFF_LENGTH 64
@@ -65,27 +79,37 @@ ndctl_match_fsdax(dev_t st_dev, const char *devname)
6579{
6680 LOG (3 , "st_dev %lu devname %s" , st_dev , devname );
6781
82+ int ret = 0 ;
83+ char * path ;
84+ char dev_id [BUFF_LENGTH ];
85+
6886 if (* devname == '\0' )
6987 return 1 ;
7088
71- char path [PATH_MAX ];
72- char dev_id [BUFF_LENGTH ];
89+ path = Malloc (PATH_MAX * sizeof (char ));
90+ if (path == NULL ) {
91+ ERR ("!Malloc" );
92+ return PMEM2_E_ERRNO ;
93+ }
7394
7495 if (util_snprintf (path , PATH_MAX , "/sys/block/%s/dev" , devname ) < 0 ) {
7596 ERR ("!snprintf" );
76- return PMEM2_E_ERRNO ;
97+ ret = PMEM2_E_ERRNO ;
98+ goto end ;
7799 }
78100
79101 if (util_snprintf (dev_id , BUFF_LENGTH , "%d:%d" ,
80102 major (st_dev ), minor (st_dev )) < 0 ) {
81103 ERR ("!snprintf" );
82- return PMEM2_E_ERRNO ;
104+ ret = PMEM2_E_ERRNO ;
105+ goto end ;
83106 }
84107
85108 int fd = os_open (path , O_RDONLY );
86109 if (fd < 0 ) {
87110 ERR ("!open \"%s\"" , path );
88- return PMEM2_E_ERRNO ;
111+ ret = PMEM2_E_ERRNO ;
112+ goto end ;
89113 }
90114
91115 char buff [BUFF_LENGTH ];
@@ -95,31 +119,38 @@ ndctl_match_fsdax(dev_t st_dev, const char *devname)
95119 int oerrno = errno ; /* save the errno */
96120 os_close (fd );
97121 errno = oerrno ;
98- return PMEM2_E_ERRNO ;
122+ ret = PMEM2_E_ERRNO ;
123+ goto end ;
99124 }
100125
101126 os_close (fd );
102127
103128 if (nread == 0 ) {
104129 ERR ("%s is empty" , path );
105- return PMEM2_E_INVALID_DEV_FORMAT ;
130+ ret = PMEM2_E_INVALID_DEV_FORMAT ;
131+ goto end ;
106132 }
107133
108134 if (buff [nread - 1 ] != '\n' ) {
109135 ERR ("%s doesn't end with new line" , path );
110- return PMEM2_E_INVALID_DEV_FORMAT ;
136+ ret = PMEM2_E_INVALID_DEV_FORMAT ;
137+ goto end ;
111138 }
112139
113140 buff [nread - 1 ] = '\0' ;
114141
115142 if (strcmp (buff , dev_id ) != 0 ) {
116143 LOG (10 , "skipping not matching device: %s" , path );
117- return 1 ;
144+ ret = 1 ;
145+ goto end ;
118146 }
119147
120148 LOG (4 , "found matching device: %s" , path );
121149
122- return 0 ;
150+ end :
151+ Free (path );
152+
153+ return ret ;
123154}
124155
125156/*
0 commit comments