libblkid: do size correction of optical discs also by last written sector

Everything after last written sector is undefined.

Signed-off-by: Pali Rohár <pali.rohar@gmail.com>
This commit is contained in:
Pali Rohár 2020-11-05 19:18:26 +01:00
parent 6b88a410ae
commit bfd4da5628
1 changed files with 7 additions and 3 deletions

View File

@ -818,12 +818,16 @@ failed:
* readable by read(2). We have to reduce the probing area to avoid unwanted
* I/O errors in probing functions. It seems that unreadable are always last 2
* or 3 CD blocks (CD block size is 2048 bytes, it means 12 in 512-byte
* sectors).
* sectors). Linux kernel reports (CDROM_LAST_WRITTEN) also location of last
* written block, so we will reduce size based on it too.
*/
static void cdrom_size_correction(blkid_probe pr)
static void cdrom_size_correction(blkid_probe pr, uint64_t last_written)
{
uint64_t n, nsectors = pr->size >> 9;
if (last_written && nsectors > ((last_written+1) << 2))
nsectors = (last_written+1) << 2;
for (n = nsectors - 12; n < nsectors; n++) {
if (!is_sector_readable(pr->fd, n))
goto failed;
@ -978,7 +982,7 @@ int blkid_probe_set_device(blkid_probe pr, int fd,
# endif
if (pr->flags & BLKID_FL_CDROM_DEV) {
cdrom_size_correction(pr);
cdrom_size_correction(pr, last_written);
}
}
#endif