16

We have BBB based custom board with kernel 3.12 running on it.

I have doubt regarding umount and & sync.

Lets say a script is umounting a partition, Does it require to run sync command before umount to complete pending writes. ?

1 Answer 1

21

No, you don't need to run sync before umount. umount will complete all pending writes before it actually unmounts the filesystem. It will also refuse to unmount if some process is still using the filesystem, e.g. as current working directory.

Edit: Unmounting is mostly handled in fs/namespace.c. You won't find any explicit call to sync there, but you'll see comments along the line of "mark this mountpoint for unmount, refuse any further operations on it, and if all operations are done, unmount". You can also see explicit in-use checks.

You can easily test yourself that umount really does finish all pending operations: Mount some slow USB stick, copy a large file to it, and directly call umount after cp. It will take several seconds before you see a new prompt, and if you run dstat etc. in another window, you'll see the write operations that are still going on. That's exactly the same behaviour as if you've typed sync.

6
  • Thank you for answer dirkt, Do u know code path in kernel which does that ?
    – ART
    Commented Feb 18, 2017 at 11:47
  • 9
    It could not be any other way. If you had to sync then you would have to block processes from writing first, or else it may not be synced by the time you got to the unmount (a race condition). Commented Feb 18, 2017 at 11:57
  • 2
    umount -l will let the command complete if something is still using the filesystem and wait in the background for the process to finish, then quietly unmount it.
    – Mio Rin
    Commented Feb 18, 2017 at 13:30
  • Correct. Even the emergency unmount (Alt+SysRq+U) goes through this path which syncs first. Commented Feb 18, 2017 at 23:45
  • 3
    My explanation states that a sync would in fact fix what you are worried about. However the writes that come after the sync would cause corruption (if it worked this way), therefore a system that needed a sync before unmount, would be fundamentally broken. As Gnu/Linux is not fundamentally broken, it can not be this way. Therefore you do not need to sync. If you had to do a sync then you would first have to arrange to block all writes to the device (before the sync). Commented Feb 24, 2017 at 18:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .