]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libblkid: do size correction of optical discs also by last written sector
authorPali Rohár <pali.rohar@gmail.com>
Thu, 5 Nov 2020 18:18:26 +0000 (19:18 +0100)
committerPali Rohár <pali.rohar@gmail.com>
Tue, 10 Nov 2020 22:21:14 +0000 (23:21 +0100)
Everything after last written sector is undefined.

Signed-off-by: Pali Rohár <pali.rohar@gmail.com>
libblkid/src/probe.c

index ab88efc7249c04fdda7e1e196b6da58e06009bd8..0128b38a9c641799886c8a6b02630802351f667d 100644 (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