Lock when setting scratch or overwrite status.

Fix #394.