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,42 @@ 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 );
63+ end :
64+ Free (path );
5265
53- return 0 ;
66+ return ret ;
5467}
5568
5669#define BUFF_LENGTH 64
@@ -65,27 +78,37 @@ ndctl_match_fsdax(dev_t st_dev, const char *devname)
6578{
6679 LOG (3 , "st_dev %lu devname %s" , st_dev , devname );
6780
81+ int ret = 0 ;
82+ char * path ;
83+ char dev_id [BUFF_LENGTH ];
84+
6885 if (* devname == '\0' )
6986 return 1 ;
7087
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+ }
7393
7494 if (util_snprintf (path , PATH_MAX , "/sys/block/%s/dev" , devname ) < 0 ) {
7595 ERR ("!snprintf" );
76- return PMEM2_E_ERRNO ;
96+ ret = PMEM2_E_ERRNO ;
97+ goto end ;
7798 }
7899
79100 if (util_snprintf (dev_id , BUFF_LENGTH , "%d:%d" ,
80101 major (st_dev ), minor (st_dev )) < 0 ) {
81102 ERR ("!snprintf" );
82- return PMEM2_E_ERRNO ;
103+ ret = PMEM2_E_ERRNO ;
104+ goto end ;
83105 }
84106
85107 int fd = os_open (path , O_RDONLY );
86108 if (fd < 0 ) {
87109 ERR ("!open \"%s\"" , path );
88- return PMEM2_E_ERRNO ;
110+ ret = PMEM2_E_ERRNO ;
111+ goto end ;
89112 }
90113
91114 char buff [BUFF_LENGTH ];
@@ -95,31 +118,37 @@ ndctl_match_fsdax(dev_t st_dev, const char *devname)
95118 int oerrno = errno ; /* save the errno */
96119 os_close (fd );
97120 errno = oerrno ;
98- return PMEM2_E_ERRNO ;
121+ ret = PMEM2_E_ERRNO ;
122+ goto end ;
99123 }
100124
101125 os_close (fd );
102126
103127 if (nread == 0 ) {
104128 ERR ("%s is empty" , path );
105- return PMEM2_E_INVALID_DEV_FORMAT ;
129+ ret = PMEM2_E_INVALID_DEV_FORMAT ;
130+ goto end ;
106131 }
107132
108133 if (buff [nread - 1 ] != '\n' ) {
109134 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 ;
111137 }
112138
113139 buff [nread - 1 ] = '\0' ;
114140
115141 if (strcmp (buff , dev_id ) != 0 ) {
116142 LOG (10 , "skipping not matching device: %s" , path );
117- return 1 ;
143+ ret = 1 ;
144+ goto end ;
118145 }
119146
120147 LOG (4 , "found matching device: %s" , path );
148+ end :
149+ Free (path );
121150
122- return 0 ;
151+ return ret ;
123152}
124153
125154/*
0 commit comments