This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Safely remove USB flashdrive

The product I'm developing allows video recording directly onto removable USB media. When recording is finished I call the C function "umount" from within the application and supposedly the user can remove the USB flashdrive. But sometimes the file recorded is corrupted or incomplete. I also see this if the user deletes a file from the USB flashdrive then I umount it and it's removed - sometimes it is not completely deleted. I suspect this is due to write behind caching because if we "umount" then wait a while before removing the USB flashdrive all is OK. How can I tell after executing the "umount" function that all files have actually been written/updated to the USB flashdrive and the drive is effectively dead and can be "safely removed"?

(I'm using DM355 dvsdk_1_30_00_40 and mv-4-0-1)

Thanks.

  • So a bit more on this.

    For a few weeks I've had caching switched off during USB stick mounting by specifying the MS_SYNCHRONOUS flag to the mount() function. This has improved the latency from the end of recording/deleting files until the USB stick can be removed without loss of data, but with this flag set the write-behind caching is completely disabled so all video frame writes have to be physically completed before the 'write()' function returns - resulting sometimes in lost frames and very bad jumpy recordings.

    I need therefore to have write-behind caching enabled. Then after the end of recording the LED on the USB stick still blinks for a few seconds showing some (expected) write-behind activity. Then the LED stops blinking. But after a few more seconds it does another few blinks - I think this second set of blinks is perhaps the directory entry being written to the stick?

    It's fine in my system to have the write caching enabled, as long as I can reliably know that the file and directory entry writing is completed and it truly is safe to remove the USB stick - so I can show a 'writing' warning to the user and then an 'OK' message. The user of my system can then pull the device without unmounting it first - I can already spot this happening in the kernel and do some clean up, except where the writes have not yet finished.

    I've tried using fsync() at the end of recording but this blocks for several seconds until the write is flushed and causes an apparent system hang for that time.

    Is there a way that I can tell that all writes to the removable device are completely finished, eg. is there a place in the kernel that I can put a few lines of code to indicate this to the application? Thanks.