Blame SOURCES/libvirt-storage-Modify-storageBackendWipeLocal-to-allow-zero-from-end-of-device.patch

6d3351
From 73114abb7437db2e4625a64377005ddaf8f17840 Mon Sep 17 00:00:00 2001
6d3351
Message-Id: <73114abb7437db2e4625a64377005ddaf8f17840@dist-git>
6d3351
From: John Ferlan <jferlan@redhat.com>
6d3351
Date: Wed, 26 Apr 2017 08:41:10 -0400
6d3351
Subject: [PATCH] storage: Modify storageBackendWipeLocal to allow zero from
6d3351
 end of device
6d3351
6d3351
https://bugzilla.redhat.com/show_bug.cgi?id=1439132
6d3351
6d3351
Add bool 'zero_end' and logic that would allow a caller to wipe specific
6d3351
portions of a target device either from the beginning (the default) or
6d3351
from the end when zero_end is true.
6d3351
6d3351
This will allow for this code to wipe out partition table information
6d3351
from a device.
6d3351
6d3351
(cherry picked from commit 859a2d162ac6dd141539819cfc6157724d12bde6)
6d3351
Signed-off-by: John Ferlan <jferlan@redhat.com>
6d3351
Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
6d3351
---
6d3351
 src/storage/storage_util.c | 40 +++++++++++++++++++++++++++-------------
6d3351
 1 file changed, 27 insertions(+), 13 deletions(-)
6d3351
6d3351
diff --git a/src/storage/storage_util.c b/src/storage/storage_util.c
6d3351
index adec9ab6f..8dbdc770e 100644
6d3351
--- a/src/storage/storage_util.c
6d3351
+++ b/src/storage/storage_util.c
6d3351
@@ -2516,26 +2516,38 @@ static int
6d3351
 storageBackendWipeLocal(const char *path,
6d3351
                         int fd,
6d3351
                         unsigned long long wipe_len,
6d3351
-                        size_t writebuf_length)
6d3351
+                        size_t writebuf_length,
6d3351
+                        bool zero_end)
6d3351
 {
6d3351
     int ret = -1, written = 0;
6d3351
     unsigned long long remaining = 0;
6d3351
+    off_t size;
6d3351
     size_t write_size = 0;
6d3351
     char *writebuf = NULL;
6d3351
 
6d3351
-    VIR_DEBUG("wiping start: 0 len: %llu", wipe_len);
6d3351
-
6d3351
     if (VIR_ALLOC_N(writebuf, writebuf_length) < 0)
6d3351
         goto cleanup;
6d3351
 
6d3351
-    if (lseek(fd, 0, SEEK_SET) < 0) {
6d3351
-        virReportSystemError(errno,
6d3351
-                             _("Failed to seek to the start in volume "
6d3351
-                               "with path '%s'"),
6d3351
-                             path);
6d3351
-        goto cleanup;
6d3351
+    if (!zero_end) {
6d3351
+        if ((size = lseek(fd, 0, SEEK_SET)) < 0) {
6d3351
+            virReportSystemError(errno,
6d3351
+                                 _("Failed to seek to the start in volume "
6d3351
+                                   "with path '%s'"),
6d3351
+                                 path);
6d3351
+            goto cleanup;
6d3351
+        }
6d3351
+    } else {
6d3351
+        if ((size = lseek(fd, -wipe_len, SEEK_END)) < 0) {
6d3351
+            virReportSystemError(errno,
6d3351
+                                 _("Failed to seek to %llu bytes to the end "
6d3351
+                                   "in volume with path '%s'"),
6d3351
+                                 wipe_len, path);
6d3351
+            goto cleanup;
6d3351
+        }
6d3351
     }
6d3351
 
6d3351
+    VIR_DEBUG("wiping start: %zd len: %llu", (ssize_t) size, wipe_len);
6d3351
+
6d3351
     remaining = wipe_len;
6d3351
     while (remaining > 0) {
6d3351
 
6d3351
@@ -2573,7 +2585,8 @@ storageBackendWipeLocal(const char *path,
6d3351
 static int
6d3351
 storageBackendVolWipeLocalFile(const char *path,
6d3351
                                unsigned int algorithm,
6d3351
-                               unsigned long long allocation)
6d3351
+                               unsigned long long allocation,
6d3351
+                               bool zero_end)
6d3351
 {
6d3351
     int ret = -1, fd = -1;
6d3351
     const char *alg_char = NULL;
6d3351
@@ -2648,7 +2661,8 @@ storageBackendVolWipeLocalFile(const char *path,
6d3351
         if (S_ISREG(st.st_mode) && st.st_blocks < (st.st_size / DEV_BSIZE)) {
6d3351
             ret = storageBackendVolZeroSparseFileLocal(path, st.st_size, fd);
6d3351
         } else {
6d3351
-            ret = storageBackendWipeLocal(path, fd, allocation, st.st_blksize);
6d3351
+            ret = storageBackendWipeLocal(path, fd, allocation, st.st_blksize,
6d3351
+                                          zero_end);
6d3351
         }
6d3351
         if (ret < 0)
6d3351
             goto cleanup;
6d3351
@@ -2686,7 +2700,7 @@ storageBackendVolWipePloop(virStorageVolDefPtr vol,
6d3351
         goto cleanup;
6d3351
 
6d3351
     if (storageBackendVolWipeLocalFile(target_path, algorithm,
6d3351
-                                       vol->target.allocation) < 0)
6d3351
+                                       vol->target.allocation, false) < 0)
6d3351
         goto cleanup;
6d3351
 
6d3351
     if (virFileRemove(disk_desc, 0, 0) < 0) {
6d3351
@@ -2735,7 +2749,7 @@ virStorageBackendVolWipeLocal(virConnectPtr conn ATTRIBUTE_UNUSED,
6d3351
         ret = storageBackendVolWipePloop(vol, algorithm);
6d3351
     } else {
6d3351
         ret = storageBackendVolWipeLocalFile(vol->target.path, algorithm,
6d3351
-                                             vol->target.allocation);
6d3351
+                                             vol->target.allocation, false);
6d3351
     }
6d3351
 
6d3351
     return ret;
6d3351
-- 
6d3351
2.12.2
6d3351