STM32 USB: detecting connection and disconnection
You could detect the connection and disconnection from this file:

usbd_core.c 

and the API for it is this

USBD_StatusTypeDef USBD_LL_DevConnected(USBD_HandleTypeDef  *pdev)
USBD_StatusTypeDef USBD_LL_DevDisconnected(USBD_HandleTypeDef  *pdev)


I am not sure about USB Mass Storage Class but in the CDC class, the 2 APIs above detect the USB connection and disconnection, maybe this helps

Things to note:


Connection > when the physical USB cable is plugged into the USB port
Disconnection > when the physical USB cable is unplugged from the USB port


I used STMCubeMX to generate the USB CDC class.