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

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