If you legally reside in Spain for five consecutive years while spending the best part of each year in the country, you will qualify for a permanent residence permit. If you keep living in Spain, the permit can be extended infinitely.