CVS has agreed to pay $315 million to settle federal claims over how its pharmacies filled opioid prescriptions.