From: Pali Rohár Date: Thu, 5 Nov 2020 18:18:26 +0000 (+0100) Subject: libblkid: do size correction of optical discs also by last written sector X-Git-Tag: v2.37-rc1~266^2~6 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=bfd4da56283daed2af2d969530628045f57bbf57;p=thirdparty%2Futil-linux.git 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 --- diff --git a/libblkid/src/probe.c b/libblkid/src/probe.c index ab88efc724..0128b38a9c 100644 --- a/libblkid/src/probe.c +++ b/libblkid/src/probe.c @@ -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