Add validation to ensure tree state cannot be corrupted

This means disallowing trying to move a folder underneath its
own subtree, which would orphan the subtree.
Use reusable utility method for atomic db-level field increments