@@ -2299,19 +2299,24 @@ static int nvme_bytes_to_lba(nvme_ns_t n, off_t offset, size_t count,
22992299 return 0 ;
23002300}
23012301
2302- struct nvme_transport_handle * nvme_ns_get_transport_handle (nvme_ns_t n )
2302+ int nvme_ns_get_transport_handle (nvme_ns_t n ,
2303+ struct nvme_transport_handle * * hdl )
23032304{
2304- if (!n -> hdl ) {
2305- int err ;
2305+ int err ;
23062306
2307- err = nvme_open (n -> ctx , n -> name , & n -> hdl );
2308- if (err )
2309- nvme_msg (n -> ctx , LOG_ERR ,
2310- "Failed to open ns %s, error %d\n" ,
2311- n -> name , err );
2307+ if (n -> hdl )
2308+ goto valid ;
2309+
2310+ err = nvme_open (n -> ctx , n -> name , & n -> hdl );
2311+ if (err ) {
2312+ nvme_msg (n -> ctx , LOG_ERR , "Failed to open ns %s, error %d\n" ,
2313+ n -> name , err );
2314+ return err ;
23122315 }
23132316
2314- return n -> hdl ;
2317+ valid :
2318+ * hdl = n -> hdl ;
2319+ return 0 ;
23152320}
23162321
23172322void nvme_ns_release_transport_handle (nvme_ns_t n )
@@ -2415,28 +2420,43 @@ void nvme_ns_get_uuid(nvme_ns_t n, unsigned char out[NVME_UUID_LEN])
24152420
24162421int nvme_ns_identify (nvme_ns_t n , struct nvme_id_ns * ns )
24172422{
2418- struct nvme_transport_handle * hdl = nvme_ns_get_transport_handle ( n ) ;
2423+ struct nvme_transport_handle * hdl ;
24192424 struct nvme_passthru_cmd cmd ;
2425+ int err ;
2426+
2427+ err = nvme_ns_get_transport_handle (n , & hdl );
2428+ if (err )
2429+ return err ;
24202430
24212431 nvme_init_identify_ns (& cmd , nvme_ns_get_nsid (n ), ns );
24222432 return nvme_submit_admin_passthru (hdl , & cmd );
24232433}
24242434
24252435int nvme_ns_identify_descs (nvme_ns_t n , struct nvme_ns_id_desc * descs )
24262436{
2427- struct nvme_transport_handle * hdl = nvme_ns_get_transport_handle ( n ) ;
2437+ struct nvme_transport_handle * hdl ;
24282438 struct nvme_passthru_cmd cmd ;
2439+ int err ;
2440+
2441+ err = nvme_ns_get_transport_handle (n , & hdl );
2442+ if (err )
2443+ return err ;
24292444
24302445 nvme_init_identify_ns_descs_list (& cmd , nvme_ns_get_nsid (n ), descs );
24312446 return nvme_submit_admin_passthru (hdl , & cmd );
24322447}
24332448
24342449int nvme_ns_verify (nvme_ns_t n , off_t offset , size_t count )
24352450{
2436- struct nvme_transport_handle * hdl = nvme_ns_get_transport_handle ( n ) ;
2451+ struct nvme_transport_handle * hdl ;
24372452 struct nvme_passthru_cmd cmd ;
24382453 __u64 slba ;
24392454 __u16 nlb ;
2455+ int err ;
2456+
2457+ err = nvme_ns_get_transport_handle (n , & hdl );
2458+ if (err )
2459+ return err ;
24402460
24412461 if (nvme_bytes_to_lba (n , offset , count , & slba , & nlb ))
24422462 return -1 ;
@@ -2449,10 +2469,15 @@ int nvme_ns_verify(nvme_ns_t n, off_t offset, size_t count)
24492469
24502470int nvme_ns_write_uncorrectable (nvme_ns_t n , off_t offset , size_t count )
24512471{
2452- struct nvme_transport_handle * hdl = nvme_ns_get_transport_handle ( n ) ;
2472+ struct nvme_transport_handle * hdl ;
24532473 struct nvme_passthru_cmd cmd ;
24542474 __u64 slba ;
24552475 __u16 nlb ;
2476+ int err ;
2477+
2478+ err = nvme_ns_get_transport_handle (n , & hdl );
2479+ if (err )
2480+ return err ;
24562481
24572482 if (nvme_bytes_to_lba (n , offset , count , & slba , & nlb ))
24582483 return -1 ;
@@ -2465,10 +2490,15 @@ int nvme_ns_write_uncorrectable(nvme_ns_t n, off_t offset, size_t count)
24652490
24662491int nvme_ns_write_zeros (nvme_ns_t n , off_t offset , size_t count )
24672492{
2468- struct nvme_transport_handle * hdl = nvme_ns_get_transport_handle ( n ) ;
2493+ struct nvme_transport_handle * hdl ;
24692494 struct nvme_passthru_cmd cmd ;
24702495 __u64 slba ;
24712496 __u16 nlb ;
2497+ int err ;
2498+
2499+ err = nvme_ns_get_transport_handle (n , & hdl );
2500+ if (err )
2501+ return err ;
24722502
24732503 if (nvme_bytes_to_lba (n , offset , count , & slba , & nlb ))
24742504 return -1 ;
@@ -2480,10 +2510,15 @@ int nvme_ns_write_zeros(nvme_ns_t n, off_t offset, size_t count)
24802510
24812511int nvme_ns_write (nvme_ns_t n , void * buf , off_t offset , size_t count )
24822512{
2483- struct nvme_transport_handle * hdl = nvme_ns_get_transport_handle ( n ) ;
2513+ struct nvme_transport_handle * hdl ;
24842514 struct nvme_passthru_cmd cmd ;
24852515 __u64 slba ;
24862516 __u16 nlb ;
2517+ int err ;
2518+
2519+ err = nvme_ns_get_transport_handle (n , & hdl );
2520+ if (err )
2521+ return err ;
24872522
24882523 if (nvme_bytes_to_lba (n , offset , count , & slba , & nlb ))
24892524 return -1 ;
@@ -2496,10 +2531,15 @@ int nvme_ns_write(nvme_ns_t n, void *buf, off_t offset, size_t count)
24962531
24972532int nvme_ns_read (nvme_ns_t n , void * buf , off_t offset , size_t count )
24982533{
2499- struct nvme_transport_handle * hdl = nvme_ns_get_transport_handle ( n ) ;
2534+ struct nvme_transport_handle * hdl ;
25002535 struct nvme_passthru_cmd cmd ;
25012536 __u64 slba ;
25022537 __u16 nlb ;
2538+ int err ;
2539+
2540+ err = nvme_ns_get_transport_handle (n , & hdl );
2541+ if (err )
2542+ return err ;
25032543
25042544 if (nvme_bytes_to_lba (n , offset , count , & slba , & nlb ))
25052545 return -1 ;
@@ -2512,10 +2552,15 @@ int nvme_ns_read(nvme_ns_t n, void *buf, off_t offset, size_t count)
25122552
25132553int nvme_ns_compare (nvme_ns_t n , void * buf , off_t offset , size_t count )
25142554{
2515- struct nvme_transport_handle * hdl = nvme_ns_get_transport_handle ( n ) ;
2555+ struct nvme_transport_handle * hdl ;
25162556 struct nvme_passthru_cmd cmd ;
25172557 __u64 slba ;
25182558 __u16 nlb ;
2559+ int err ;
2560+
2561+ err = nvme_ns_get_transport_handle (n , & hdl );
2562+ if (err )
2563+ return err ;
25192564
25202565 if (nvme_bytes_to_lba (n , offset , count , & slba , & nlb ))
25212566 return -1 ;
@@ -2528,8 +2573,14 @@ int nvme_ns_compare(nvme_ns_t n, void *buf, off_t offset, size_t count)
25282573
25292574int nvme_ns_flush (nvme_ns_t n )
25302575{
2531- return nvme_flush (nvme_ns_get_transport_handle (n ),
2532- nvme_ns_get_nsid (n ));
2576+ struct nvme_transport_handle * hdl ;
2577+ int err ;
2578+
2579+ err = nvme_ns_get_transport_handle (n , & hdl );
2580+ if (err )
2581+ return err ;
2582+
2583+ return nvme_flush (hdl , nvme_ns_get_nsid (n ));
25332584}
25342585
25352586static int nvme_strtou64 (const char * str , void * res )
0 commit comments