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. ?

ART
  • 1,131

1 Answers1

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.

dirkt
  • 32,309
  • Thank you for answer dirkt, Do u know code path in kernel which does that ? – ART Feb 18 '17 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). – ctrl-alt-delor Feb 18 '17 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 Feb 18 '17 at 13:30
  • Correct. Even the emergency unmount (Alt+SysRq+U) goes through this path which syncs first. – Gilles 'SO- stop being evil' Feb 18 '17 at 23:45
  • Thank you for reply @richard In my case there are no process writing, I was just worried about writes which were done before, they must be synced to a disk. – ART Feb 20 '17 at 06:30
  • 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). – ctrl-alt-delor Feb 24 '17 at 18:22