In an action for the recovery of real property upon which permanent improvements have been made or general or special taxes or local assessments have been paid by a defendant, or those under whom he or she claims, holding in good faith under color or claim of title adversely to the claim of plaintiff, the value of such improvements and the amount of such taxes or assessments with interest thereon from date of payment must be allowed as a counterclaim to the defendant.