From 14308f5a7881104e3acf8bfa20b141edde22965e Mon Sep 17 00:00:00 2001
From: HATAYAMA Daisuke <d.hatayama@jp.fujitsu.com>
Date: Fri, 23 Mar 2012 10:23:04 +0900
Subject: [PATCH 2/2] [PATCH v3 4/4] Add build condition for LZO support.

To enable lzo compression support, build makedumpfile as:

  $ make USELZO=on

To disable, as:

  $ make

In default, lzo compression support is disabled.

Signed-off-by: HATAYAMA Daisuke <d.hatayama@jp.fujitsu.com>
Signed-off-by: Atsushi Kumagai <kumagai-atsushi@mxc.nes.nec.co.jp>
---
 Makefile       |    5 +++++
 README         |    3 +++
 makedumpfile.8 |    1 +
 makedumpfile.c |   24 ++++++++++++++++++++++--
 makedumpfile.h |    2 ++
 print_info.c   |    4 ++++
 6 files changed, 37 insertions(+), 2 deletions(-)

diff --git a/Makefile b/Makefile
index a06b2db..28c881a 100644
--- a/Makefile
+++ b/Makefile
@@ -50,6 +50,11 @@ ifneq ($(LINKTYPE), dynamic)
 LIBS := -static $(LIBS)
 endif
 
+ifeq ($(USELZO), on)
+LIBS := -llzo2 $(LIBS)
+CFLAGS += -DUSELZO
+endif
+
 all: makedumpfile
 
 $(OBJ_PART): $(SRC_PART)
diff --git a/README b/README
index 62ba413..c313a78 100644
--- a/README
+++ b/README
@@ -42,6 +42,9 @@
     where <arch> is the 'uname -m' of the target architecture. 
     The user has to set the environment variable CC to appropriate
     compiler for the target architecture.
+  6.Build with lzo support:
+    # make USELZO=on ; make install
+    The user has to prepare lzo library.
 
 * SUPPORTED KERNELS
   This makedumpfile supports the following kernels.
diff --git a/makedumpfile.8 b/makedumpfile.8
index 42e36b6..7ce04ed 100644
--- a/makedumpfile.8
+++ b/makedumpfile.8
@@ -123,6 +123,7 @@ configuration, you need to use --diskset option.
 .TP
 \fB\-c,\-l\fR
 Compress dump data by each page using zlib for -c option or lzo for -l option.
+(-l option needs USELZO=on when building.)
 .br
 A user cannot specify this option with \-E option, because the ELF format does
 not support compressed data.
diff --git a/makedumpfile.c b/makedumpfile.c
index a7fec52..1342b25 100644
--- a/makedumpfile.c
+++ b/makedumpfile.c
@@ -266,6 +266,7 @@ readpmem_kdump_compressed(unsigned long long paddr, void *bufptr, size_t size)
 			goto error;
 		}
 		memcpy(bufptr, buf2 + page_offset, size);
+#ifdef USELZO
 	} else if (info->flag_lzo_support
 		   && (pd.flags & DUMP_DH_COMPRESSED_LZO)) {
 		retlen = info->page_size;
@@ -277,6 +278,7 @@ readpmem_kdump_compressed(unsigned long long paddr, void *bufptr, size_t size)
 			goto error;
 		}
 		memcpy(bufptr, buf2 + page_offset, size);
+#endif
 	} else
 		memcpy(bufptr, buf + page_offset, size);
 
@@ -2525,8 +2527,16 @@ initial(void)
 	unsigned long size;
 	int debug_info = FALSE;
 
+#ifdef USELZO
 	if (lzo_init() == LZO_E_OK)
 		info->flag_lzo_support = TRUE;
+#else
+	if (info->flag_compress == DUMP_DH_COMPRESSED_LZO) {
+		MSG("'-l' option is disabled, ");
+		MSG("because this binary doesn't support lzo compression.\n");
+		MSG("Try `make USELZO=on` when building.\n");
+	}
+#endif
 
 	if (!is_xen_memory() && info->flag_exclude_xen_dom) {
 		MSG("'-X' option is disable,");
@@ -4695,11 +4705,10 @@ write_kdump_pages(struct cache_data *cd_header, struct cache_data *cd_page)
 	off_t offset_data = 0;
 	struct disk_dump_header *dh = info->dump_header;
 	unsigned char buf[info->page_size], *buf_out = NULL;
-	unsigned long len_buf_out, len_buf_out_zlib, len_buf_out_lzo;
+	unsigned long len_buf_out;
 	struct dump_bitmap bitmap2;
 	struct timeval tv_start;
 	const off_t failed = (off_t)-1;
-	lzo_bytep wrkmem = NULL;
 
 	int ret = FALSE;
 
@@ -4708,6 +4717,10 @@ write_kdump_pages(struct cache_data *cd_header, struct cache_data *cd_page)
 
 	initialize_2nd_bitmap(&bitmap2);
 
+#ifdef USELZO
+	unsigned long len_buf_out_zlib, len_buf_out_lzo;
+	lzo_bytep wrkmem;
+
 	if ((wrkmem = malloc(LZO1X_1_MEM_COMPRESS)) == NULL) {
 		ERRMSG("Can't allocate memory for the working memory. %s\n",
 		       strerror(errno));
@@ -4717,6 +4730,9 @@ write_kdump_pages(struct cache_data *cd_header, struct cache_data *cd_page)
 	len_buf_out_zlib = compressBound(info->page_size);
 	len_buf_out_lzo = info->page_size + info->page_size / 16 + 64 + 3;
 	len_buf_out = MAX(len_buf_out_zlib, len_buf_out_lzo);
+#else
+	len_buf_out = compressBound(info->page_size);
+#endif
 
 	if ((buf_out = malloc(len_buf_out)) == NULL) {
 		ERRMSG("Can't allocate memory for the compression buffer. %s\n",
@@ -4807,6 +4823,7 @@ write_kdump_pages(struct cache_data *cd_header, struct cache_data *cd_page)
 			pd.flags = DUMP_DH_COMPRESSED_ZLIB;
 			pd.size  = size_out;
 			memcpy(buf, buf_out, pd.size);
+#ifdef USELZO
 		} else if (info->flag_lzo_support
 			   && (info->flag_compress & DUMP_DH_COMPRESSED_LZO)
 			   && ((size_out = info->page_size),
@@ -4816,6 +4833,7 @@ write_kdump_pages(struct cache_data *cd_header, struct cache_data *cd_page)
 			pd.flags = DUMP_DH_COMPRESSED_LZO;
 			pd.size  = size_out;
 			memcpy(buf, buf_out, pd.size);
+#endif
 		} else {
 			pd.flags = 0;
 			pd.size  = info->page_size;
@@ -4856,8 +4874,10 @@ write_kdump_pages(struct cache_data *cd_header, struct cache_data *cd_page)
 out:
 	if (buf_out != NULL)
 		free(buf_out);
+#ifdef USELZO
 	if (wrkmem != NULL)
 		free(wrkmem);
+#endif
 
 	return ret;
 }
diff --git a/makedumpfile.h b/makedumpfile.h
index 0a6f8bb..592f122 100644
--- a/makedumpfile.h
+++ b/makedumpfile.h
@@ -31,7 +31,9 @@
 #include <libelf.h>
 #include <byteswap.h>
 #include <getopt.h>
+#ifdef USELZO
 #include <lzo/lzo1x.h>
+#endif
 #include "common.h"
 #include "dwarf_info.h"
 #include "diskdump_mod.h"
diff --git a/print_info.c b/print_info.c
index 580741a..3c9151d 100644
--- a/print_info.c
+++ b/print_info.c
@@ -34,7 +34,11 @@ print_usage(void)
 {
 	MSG("\n");
 	MSG("LZO support:\n");
+#ifdef USELZO
 	MSG("  enabled\n");
+#else
+	MSG("  disabled ('-l' option will be ignored.)\n");
+#endif
 	MSG("\n");
 	MSG("Usage:\n");
 	MSG("  Creating DUMPFILE:\n");
-- 
1.7.9.2

