Blame SOURCES/0002-fileio-backstore-fix-sparse-file-creation.patch

b8410b
From 3176671662bda79d4b4059b8cc22b4b31a6547e0 Mon Sep 17 00:00:00 2001
b8410b
From: Maurizio Lombardi <mlombard@redhat.com>
b8410b
Date: Fri, 13 Nov 2020 11:44:40 +0100
b8410b
Subject: [PATCH] fileio backstore: fix sparse file creation
b8410b
b8410b
fallocate() can't be used to create sparse files because it
b8410b
actually preallocates all the disk space that will be used by the file
b8410b
backstore, sparse files do not have preallocated disk space
b8410b
by definition.
b8410b
We must therefore use ftruncate().
b8410b
b8410b
We can, on the other hand, use fallocate() to create non-sparse
b8410b
files and fall back to the slower "while() fwrite()" if we
b8410b
are running on Python version < 3.3 where fallocate() is not available
b8410b
b8410b
Fixes 3bd4d8ef7c9b154c53e8b8dd863a570bce7f5c2c
b8410b
b8410b
Signed-off-by: Maurizio Lombardi <mlombard@redhat.com>
b8410b
---
b8410b
 targetcli/ui_backstore.py | 16 ++++++++--------
b8410b
 1 file changed, 8 insertions(+), 8 deletions(-)
b8410b
b8410b
diff --git a/targetcli/ui_backstore.py b/targetcli/ui_backstore.py
b8410b
index 8692f22..9bc0c58 100644
b8410b
--- a/targetcli/ui_backstore.py
b8410b
+++ b/targetcli/ui_backstore.py
b8410b
@@ -423,17 +423,17 @@ class UIFileIOBackstore(UIBackstore):
b8410b
             raise ExecutionError("Could not open %s" % filename)
b8410b
         try:
b8410b
             if sparse:
b8410b
+                os.ftruncate(f.fileno(), size)
b8410b
+            else:
b8410b
+                self.shell.log.info("Writing %d bytes" % size)
b8410b
                 try:
b8410b
+                    # Prior to version 3.3, Python does not provide fallocate
b8410b
                     os.posix_fallocate(f.fileno(), 0, size)
b8410b
                 except AttributeError:
b8410b
-                    # Prior to version 3.3, Python does not provide fallocate
b8410b
-                    os.ftruncate(f.fileno(), size)
b8410b
-            else:
b8410b
-                self.shell.log.info("Writing %d bytes" % size)
b8410b
-                while size > 0:
b8410b
-                    write_size = min(size, 1024)
b8410b
-                    f.write("\0" * write_size)
b8410b
-                    size -= write_size
b8410b
+                    while size > 0:
b8410b
+                        write_size = min(size, 1024)
b8410b
+                        f.write("\0" * write_size)
b8410b
+                        size -= write_size
b8410b
         except (OSError, IOError):
b8410b
             os.remove(filename)
b8410b
             raise ExecutionError("Could not expand file to %d bytes" % size)
b8410b
-- 
b8410b
2.28.0
b8410b