Pre-allocate loop device as many sizes as necessary

When considering the AOSP/1529838 previously,
it was necessary to search for /dev/block or /sys/block that was already allocated
because many places called the function PreAllocateLoopDevices in apexd.
However, since AOSP/2202018, apexd only requests once after calculating the entire required size.

Therefore, it do not need to search for /sys/block and can check loop device from block 0.
But instead, when EEXIST occurs, it is necessary to decide that loop device
can be reused by distinguishing whether it is already in use or preallocated in the kernel.

BUG: 237955261

Without the change:
[    4.402692] [2:          apexd:  512] apexd: Pre-allocated loop device 71
[    4.404151] [2:          apexd:  512] apexd: Pre-allocated loop device 72
[    4.405380] [2:          apexd:  512] apexd: Pre-allocated loop device 73
[    4.405409] [2:          apexd:  512] apexd: Pre-allocated 42 loopback devices

42 loop devices were pre-allocated again except for the number defined in CONFIG_BLK_DEV_LOOP_MIN_COUNT.

[    1.422162] [6:          apexd:  323] apexd: OnBootstrap done, duration=391

with the change:
[    1.279033] [0:          apexd:  329] apexd: Pre-allocated loop device 38
[    1.279979] [0:          apexd:  329] apexd: Pre-allocated loop device 39
[    1.280950] [0:          apexd:  329] apexd: Pre-allocated loop device 40
[    1.291236] [0:          apexd:  329] apexd: Pre-allocated loop device 41
[    1.291257] [0:          apexd:  329] apexd: Pre-allocated 42 loopback devices

Except for CONFIG_BLK_DEV_LOOP_MIN_COUNT, only the required number was allocated.
There are no unused loop devices left after booting.

[    1.352545] [6:          apexd:  329] apexd: OnBootstrap done, duration=110

Finally,
the OnBootstrap time is reduced by about 300 ms.

Test: Build Pass, manual test

Change-Id: Ib0da5c4c4728975663d613cebaee5e6f5896ca21
Signed-off-by: ByungSoo Kim <bs1770.kim@samsung.com>
1 file changed