ploop_check(): open image RO, reopen RW later

Most of the time writing to the image file is not required, so
let's optimize for this case and open the file read only. Later,
if an image has dirty flag set, of CHECK_DETAILED is set, reopen
the file in read/write mode.

This also helps with filesystems on which opening the file read/write
is more expensive than opening it read-only.

Reference: PSBM-26499
References: ploop@1036784, ploop@1036927, ploop@1036926, ploop@1002925, ploop@989937
Signed-off-by: Kir Kolyshkin <kir@openvz.org>
fill_hole(): no need to bzero static buf

In C, statically-allocated objects without an explicit initializer
are initialized to zero, so there is no need for explicit bzero().

While at it, mark buf as const to make sure it is kept as is (all
zeroes).

Reference: ploop@1036949
Signed-off-by: Kir Kolyshkin <kir@openvz.org>
check_and_repair_sparse: use fill_hole ret code

Since fill_hole returns SYSEXIT_* values it makes sense to return
those explicitly.

Reference: ploop@1036950
Signed-off-by: Kir Kolyshkin <kir@openvz.org>
fill_hole: use *fd

Replace fd argument with pointer to it.

This is a preparation for the next commit, to make it easier to read.

Reference: ploop@1036951
Signed-off-by: Kir Kolyshkin <kir@openvz.org>
check_and_repair_sparse(): reopen image rw if needed

After the commit r1036784, we can be in the situation when image
is opened as read-only but check_and_repair_sparse() needs to
write to it.

As we don't know beforehand whether we will need to write to an image
file or not, let's fix it in a somewhat kludgy way -- as soon as we
found out we need to write, check opened fd mode and reopen the image
read/write if necessary. Note this is only performed once.

References: #PSBM-20250, #PSBM-26499
Reference: ploop@1036952
Signed-off-by: Kir Kolyshkin <kir@openvz.org>
ploop_check(): don't pass -1 to close

This fixes a complain from Coverity Scan:

> CID 1228664 (#1 of 1): Improper use of negative value (NEGATIVE_RETURNS)
> negative_returns: fd is passed to a parameter that cannot be negative.

While at it, use close_safe() to close the old fd, so we can
detect and report an error.

Reference: ploop@1036954
Signed-off-by: Kir Kolyshkin <kir@openvz.org>