Establishing a current I in an inductor with inductance L requires an input of energy U.
An increasing current i in the inductor causes an emf $\epsilon$ between its terminals, and a corresponding potential difference $V_{ab}$ between the terminals of the source, with point a at higher potential than point b.
The source must be adding energy to the inductor, and the instantaneous power is:
$$\begin{aligned} \frac{dU}{dt} &= P \\ \frac{dU}{dt} &= iV_{ab} \\ \frac{dU}{dt} &= iL\frac{di}{dt} \\ dU &= Li \, di \end{aligned}$$
Note:
$$\begin{aligned} V_{ab} – L\frac{di}{dt} &= 0 \\ V_{ab} &= L \frac{di}{dt} \end{aligned}$$
The total energy supplied to the inductor while current increases from zero to a final value I is:
$$\begin{aligned} U &= L \int\limits_{0}^{I} i \, di \\ &= \frac{1}{2} L i^{2} \end{aligned}$$
When the current decreases from I to zero, the inductor acts as a source that supplies a total amount of energy:
$$U = \frac{1}{2} LI^{2}$$
to the external circuit.
An inductor carrying a current I has energy U stored in it. This energy is actually stored in the magnetic field within the coil, just as the energy of a capacitor is stored in the electric field between its plates.
Consider the ideal toroidal solenoid:
$$\begin{aligned} B &= \frac{\mu_{0} Ni}{2 \pi r} \\ L &= \frac{\mu_{0}N^{2} A}{2 \pi r} \end{aligned}$$
The energy stored in the toroidal solenoid when the current is i is:
$$\begin{aligned} U &= \frac{1}{2} L i^{2} \\ &= \frac{1}{2} \frac{\mu_{0} N^{2}i^{2} A}{2 \pi r} \\ &= \frac{1}{2 \mu_{0}} \left( \frac{\mu_{0} N i}{2 \pi r} \right)^{2} 2 \pi rA \\ &= \frac{B^{2}}{2 \mu_{0}} \times 2 \pi rA \end{aligned}$$
The magnetic field and thus this energy are localized in the volume $V = 2 \pi rA$ enclosed by the windings. The magnetic field energy density:
$$\begin{aligned} u &= \frac{U}{2 \pi rA} \\ &= \frac{B^{2}}{2 \mu_{0}} \end{aligned}$$
When the material inside the toroid is no vacuum but a material with (constant) magnetic permeability $\mu = K_{m} \mu_{0}$,
$$\mu = \frac{B^{2}}{2 \mu}$$