@@ -195,7 +195,7 @@ int nvme_host_get_ids(struct nvme_global_ctx *ctx,
195195}
196196
197197int nvme_host_get (struct nvme_global_ctx * ctx , const char * hostnqn ,
198- const char * hostid , nvme_host_t * hp )
198+ const char * hostid , nvme_host_t * host )
199199{
200200 _cleanup_free_ char * hnqn = NULL ;
201201 _cleanup_free_ char * hid = NULL ;
@@ -207,10 +207,12 @@ int nvme_host_get(struct nvme_global_ctx *ctx, const char *hostnqn,
207207 return err ;
208208
209209 h = nvme_lookup_host (ctx , hnqn , hid );
210+ if (!h )
211+ return - ENOMEM ;
210212
211213 nvme_host_set_hostsymname (h , NULL );
212214
213- * hp = h ;
215+ * host = h ;
214216 return 0 ;
215217}
216218
@@ -762,13 +764,37 @@ void nvme_free_host(struct nvme_host *h)
762764 __nvme_free_host (h );
763765}
764766
765- struct nvme_host * nvme_lookup_host (struct nvme_global_ctx * ctx , const char * hostnqn ,
766- const char * hostid )
767+ static int nvme_create_host (struct nvme_global_ctx * ctx , const char * hostnqn ,
768+ const char * hostid , struct nvme_host * * host )
769+ {
770+ struct nvme_host * h ;
771+
772+ h = calloc (1 , sizeof (* h ));
773+ if (!h )
774+ return - ENOMEM ;
775+
776+ h -> hostnqn = strdup (hostnqn );
777+ if (hostid )
778+ h -> hostid = strdup (hostid );
779+ list_head_init (& h -> subsystems );
780+ list_node_init (& h -> entry );
781+ h -> ctx = ctx ;
782+
783+ list_add_tail (& ctx -> hosts , & h -> entry );
784+
785+ * host = h ;
786+
787+ return 0 ;
788+ }
789+
790+ struct nvme_host * nvme_lookup_host (struct nvme_global_ctx * ctx ,
791+ const char * hostnqn , const char * hostid )
767792{
768793 struct nvme_host * h ;
769794
770795 if (!hostnqn )
771796 return NULL ;
797+
772798 nvme_for_each_host (ctx , h ) {
773799 if (strcmp (h -> hostnqn , hostnqn ))
774800 continue ;
@@ -777,16 +803,9 @@ struct nvme_host *nvme_lookup_host(struct nvme_global_ctx *ctx, const char *host
777803 continue ;
778804 return h ;
779805 }
780- h = calloc ( 1 , sizeof ( * h ));
781- if (! h )
806+
807+ if (nvme_create_host ( ctx , hostnqn , hostid , & h ) )
782808 return NULL ;
783- h -> hostnqn = strdup (hostnqn );
784- if (hostid )
785- h -> hostid = strdup (hostid );
786- list_head_init (& h -> subsystems );
787- list_node_init (& h -> entry );
788- h -> ctx = ctx ;
789- list_add_tail (& ctx -> hosts , & h -> entry );
790809
791810 return h ;
792811}
0 commit comments