Refactor some code into maybe_apply_dentry()