Thursday, September 24, 2015

Safely Ejecting Flash Drive from USB Port

Flash Drive USB Port
For years now, many flash drive users have been wondering whether it is safe to eject their devices from a USB Port. The anxiety stems from the possibility that it may harm their data when they just pull it out without going through the 5-second process of safely removing it.

According to Phillip Remaker – Quora, Operating Systems treat disks as objects that can be trusted not to change state suddenly. Whenever the OS is reading or writing files, it has this innate expectation that the files remain accessible and not suddenly disappear in mid-read or mid-write.

If a file is open, a program reading the file expects to be able to return to it and continue reading. Similarly, write commands may be dispatched to a writing subroutine and forgotten by the main program. If a drive disappears between the time the subroutine is called and the data is written to disk, that data is lost forever.

In the olden days, there were formal processes to physically “mount” and “unmount” storage media, and the physical act of mounting a tape or a disk pack triggered some mechanical switch to detect the presence or absence of media. Once the mechanism was engaged, the software could start to use the media (a “soft mount.”). Some media even had mechanical interlock to prevent media from being ejected or removed until the software processes using the media released the lock.

The Macintosh floppy and optical disk provide more modern examples of an interlocked physical and soft mount. One could only eject media through a software command, but that command might fail if some program was holding a file open on the medium. Enter USB connected storage. There is no mechanical interlock in a USB connection to coordinate the hard and soft mount. The user can decide to rip the disk out from under the operating system at any time, and endure all manner of programs freaking out about the sudden loss of media. “Hey! I was using that!”

Symptoms could include: Lost data, corrupted file systems, crashing programs, or hanging computers requiring a reboot. A safe removal executes the “soft unmount” needed to prevent any unexpected Bad Things that may happen if a program loses its access to media.

A safe removal does a few things:
  • It flushes all active writes to disk.
  • It alerts all programs (that know how to be alerted) that the disk is going away, and to take appropriate action.
  • It alerts the user when programs have failed to take action, and still are holding files open.
Users can remove a disk at any time, but they are at the mercy of how well programs using the disk cope with the sudden disappearance of that disk.

In the modern computer, many steps have been taken to defend against the capricious and careless removal of media. For example, Windows even introduced a feature called “Optimize for Quick Removal” that makes sure data is written quickly instead of batched up and written efficiently. It is very hard to get people to change habits. If users are doing exclusively reading (reads) on a media, safe removal is probably not needed. If they are doing writing (writes), they are probably still good if they skip safe removal as long as they have not written anything recently or not doing something silly like indexing that disk.

The bottom line: Safe Removal does a number of important things and is, in fact, the only assuredly safe way to remove a disk. Users probably don’t need it most of the time, but it is a good habit to have since data loss sucks.

No comments:

Post a Comment