[DRBD-cvs] r1602 - trunk/user

svn at svn.drbd.org svn at svn.drbd.org
Tue Oct 19 13:50:08 CEST 2004


Author: lars
Date: 2004-10-19 13:50:05 +0200 (Tue, 19 Oct 2004)
New Revision: 1602

Added:
   trunk/user/drbd_endian.h
Modified:
   trunk/user/Makefile
   trunk/user/drbdadm_main.c
   trunk/user/drbdmeta.c
Log:
moved all endianness and sizeof(long) compatibility into drbd_endian.h

Modified: trunk/user/Makefile
===================================================================
--- trunk/user/Makefile	2004-10-19 10:13:18 UTC (rev 1601)
+++ trunk/user/Makefile	2004-10-19 11:50:05 UTC (rev 1602)
@@ -17,7 +17,7 @@
 # the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
 #
 
-CFLAGS = -g -c -Wall -I../drbd
+CFLAGS = -g -O2 -c -Wall -I../drbd
 CC = gcc
 
 drbdadm-obj = drbdadm_scanner.o drbdadm_parser.o drbdadm_main.o \

Added: trunk/user/drbd_endian.h
===================================================================
--- trunk/user/drbd_endian.h	2004-10-19 10:13:18 UTC (rev 1601)
+++ trunk/user/drbd_endian.h	2004-10-19 11:50:05 UTC (rev 1602)
@@ -0,0 +1,143 @@
+#ifndef DRBD_ENDIAN_H
+#define DRBD_ENDIAN_H 1
+
+/*
+ * we don't want additional dependencies on other packages,
+ * and we want to avoid to introduce incompatibilities by including kernel
+ * headers from user space.
+ *
+ * we need the u32 and u64 types,
+ * the hamming weight functions,
+ * and the cpu_to_le etc. endianness convert functions.
+ */
+
+#include <stdint.h>
+#include <endian.h>
+
+#ifndef BITS_PER_LONG
+# define BITS_PER_LONG __WORDSIZE
+#endif
+
+#define u64 uint64_t
+#define u32 uint32_t
+#define __u64 uint64_t
+#define __u32 uint32_t
+
+/* linux/byteorder/swab.h */
+
+/* casts are necessary for constants, because we never know how for sure
+ * how U/UL/ULL map to __u16, __u32, __u64. At least not in a portable way.
+ */
+
+/*
+ * __asm__("bswap %0" : "=r" (x) : "0" (x));
+ * oh, well...
+ */
+
+#define __swab32(x) \
+({ \
+	__u32 __x = (x); \
+	((__u32)( \
+		(((__u32)(__x) & (__u32)0x000000ffUL) << 24) | \
+		(((__u32)(__x) & (__u32)0x0000ff00UL) <<  8) | \
+		(((__u32)(__x) & (__u32)0x00ff0000UL) >>  8) | \
+		(((__u32)(__x) & (__u32)0xff000000UL) >> 24) )); \
+})
+
+#define __swab64(x) \
+({ \
+	__u64 __x = (x); \
+	((__u64)( \
+		(__u64)(((__u64)(__x) & (__u64)0x00000000000000ffULL) << 56) | \
+		(__u64)(((__u64)(__x) & (__u64)0x000000000000ff00ULL) << 40) | \
+		(__u64)(((__u64)(__x) & (__u64)0x0000000000ff0000ULL) << 24) | \
+		(__u64)(((__u64)(__x) & (__u64)0x00000000ff000000ULL) <<  8) | \
+	        (__u64)(((__u64)(__x) & (__u64)0x000000ff00000000ULL) >>  8) | \
+		(__u64)(((__u64)(__x) & (__u64)0x0000ff0000000000ULL) >> 24) | \
+		(__u64)(((__u64)(__x) & (__u64)0x00ff000000000000ULL) >> 40) | \
+		(__u64)(((__u64)(__x) & (__u64)0xff00000000000000ULL) >> 56) )); \
+})
+
+/*
+ * no architecture-specific optimization is supplied here.
+ * I still wonder why we should not use <asm/byteorder.h>,
+ * but so be it.
+ */
+
+/*
+ * linux/byteorder/little_endian.h
+ * linux/byteorder/big_endian.h
+ */
+
+#if __BYTE_ORDER == __LITTLE_ENDIAN
+#define cpu_to_le64(x) ((__u64)(x))
+#define le64_to_cpu(x) ((__u64)(x))
+#define cpu_to_le32(x) ((__u32)(x))
+#define le32_to_cpu(x) ((__u32)(x))
+#define cpu_to_be64(x) __swab64((x))
+#define be64_to_cpu(x) __swab64((x))
+#define cpu_to_be32(x) __swab32((x))
+#define be32_to_cpu(x) __swab32((x))
+#elif __BYTE_ORDER == __BIG_ENDIAN
+# define cpu_to_le64(x) __swab64((x))
+# define le64_to_cpu(x) __swab64((x))
+# define cpu_to_le32(x) __swab32((x))
+# define le32_to_cpu(x) __swab32((x))
+# define cpu_to_be64(x) ((__u64)(x))
+# define be64_to_cpu(x) ((__u64)(x))
+# define cpu_to_be32(x) ((__u32)(x))
+# define be32_to_cpu(x) ((__u32)(x))
+#else
+# error "sorry, weird endianness on this box"
+#endif
+
+#if BITS_PER_LONG == 32
+# define LN2_BPL 5
+# define cpu_to_le_long cpu_to_le32
+# define le_long_to_cpu le32_to_cpu
+#elif BITS_PER_LONG == 64
+# define LN2_BPL 6
+# define cpu_to_le_long cpu_to_le64
+# define le_long_to_cpu le64_to_cpu
+#else
+# error "LN2 of BITS_PER_LONG unknown!"
+#endif
+
+/* linux/bitops.h */
+
+/*
+ * hweightN: returns the hamming weight (i.e. the number
+ * of bits set) of a N-bit word
+ */
+
+static inline unsigned int generic_hweight32(unsigned int w)
+{
+        unsigned int res = (w & 0x55555555) + ((w >> 1) & 0x55555555);
+        res = (res & 0x33333333) + ((res >> 2) & 0x33333333);
+        res = (res & 0x0F0F0F0F) + ((res >> 4) & 0x0F0F0F0F);
+        res = (res & 0x00FF00FF) + ((res >> 8) & 0x00FF00FF);
+        return (res & 0x0000FFFF) + ((res >> 16) & 0x0000FFFF);
+}
+
+static inline unsigned long generic_hweight64(__u64 w)
+{
+#if BITS_PER_LONG < 64
+	return generic_hweight32((unsigned int)(w >> 32)) +
+				generic_hweight32((unsigned int)w);
+#else
+	u64 res;
+	res = (w & 0x5555555555555555) + ((w >> 1) & 0x5555555555555555);
+	res = (res & 0x3333333333333333) + ((res >> 2) & 0x3333333333333333);
+	res = (res & 0x0F0F0F0F0F0F0F0F) + ((res >> 4) & 0x0F0F0F0F0F0F0F0F);
+	res = (res & 0x00FF00FF00FF00FF) + ((res >> 8) & 0x00FF00FF00FF00FF);
+	res = (res & 0x0000FFFF0000FFFF) + ((res >> 16) & 0x0000FFFF0000FFFF);
+	return (res & 0x00000000FFFFFFFF) + ((res >> 32) & 0x00000000FFFFFFFF);
+#endif
+}
+
+static inline unsigned long hweight_long(unsigned long w)
+{
+	return sizeof(w) == 4 ? generic_hweight32(w) : generic_hweight64(w);
+}
+
+#endif

Modified: trunk/user/drbdadm_main.c
===================================================================
--- trunk/user/drbdadm_main.c	2004-10-19 10:13:18 UTC (rev 1601)
+++ trunk/user/drbdadm_main.c	2004-10-19 11:50:05 UTC (rev 1602)
@@ -396,6 +396,9 @@
     case SLEEPS_SHORT:     timeout = 5; break;
     case SLEEPS_LONG:      timeout = 60; break;
     case SLEEPS_VERY_LONG: timeout = 600; break;
+    default:
+	fprintf(stderr,"logic bug in %s:%d\n",__FILE__,__LINE__);
+	exit(E_thinko);
     }
     alarm(timeout);
   }

Modified: trunk/user/drbdmeta.c
===================================================================
--- trunk/user/drbdmeta.c	2004-10-19 10:13:18 UTC (rev 1601)
+++ trunk/user/drbdmeta.c	2004-10-19 11:50:05 UTC (rev 1602)
@@ -33,26 +33,17 @@
 #include <sys/ioctl.h>
 
 #include <stdlib.h>
-#include <endian.h>
 #include <stdio.h>
 #include <errno.h>
 #include <string.h>
 #include <unistd.h>
 #include <fcntl.h>
 
-#include <asm/byteorder.h>	/* for the __cpu_to_le64 etc. functions  */
-#include <linux/bitops.h>	/* for the hweight functions  */
-#include <linux/types.h>	/* for the __u32/64 type defs */
-
-#define u64 __u64
-/* because u64 is used in this:
- * #define BLKGETSIZE64 _IOR(0x12,114,sizeof(u64))
- */
-
 #include <linux/fs.h>     /* for BLKGETSIZE64 */
 #include <linux/drbd.h>   /* only use DRBD_MAGIC from here! */
 
 #include "drbdtool_common.h"
+#include "drbd_endian.h"
 
 
 /*
@@ -61,33 +52,15 @@
  * {
  */
 
-#ifndef BITS_PER_LONG
-# define BITS_PER_LONG __WORDSIZE
-#endif
-
 #ifndef ALIGN
 # define ALIGN(x,a) ( ((x) + (a)-1) &~ ((a)-1) )
 #endif
 
-#if BITS_PER_LONG == 32
-# define LN2_BPL 5
-# define cpu_to_le_long __cpu_to_le32
-# define le_long_to_cpu __le32_to_cpu
-
-#elif BITS_PER_LONG == 64
-# define LN2_BPL 6
-# define cpu_to_le_long cpu_to_le64
-# define le_long_to_cpu le64_to_cpu
-
-#else
-# error "LN2 of BITS_PER_LONG unknown!"
-#endif
-
 #define MD_AL_OFFSET_07    8
 #define MD_AL_MAX_SIZE_07  64
 #define MD_BM_OFFSET_07    (MD_AL_OFFSET_07 + MD_AL_MAX_SIZE_07)
 #define DRBD_MD_MAGIC_07   (DRBD_MAGIC+3)
-#define MD_RESERVED_SIZE_07 ( (__u64)128 * (1<<20) )
+#define MD_RESERVED_SIZE_07 ( (u64)128 * (1<<20) )
 #define MD_BM_MAX_SIZE_07  ( MD_RESERVED_SIZE_07 - MD_BM_OFFSET_07*512 )
 
 #define DRBD_MD_MAGIC_08   (DRBD_MAGIC+4)
@@ -118,9 +91,9 @@
 };
 
 struct meta_data {
-	__u32 gc[GEN_CNT_SIZE];   /* v06 */
+	u32 gc[GEN_CNT_SIZE];   /* v06 */
 
-	__u64 la_size;            /* v07  [ units of KB ] */
+	u64 la_size;              /* v07  [ units of KB ] */
 	int bm_size;              /* v07 */
 	unsigned long *bitmap;    /* v07 */
 	int al_size;              /* v07 */
@@ -221,9 +194,9 @@
 	return bits;
 }
 
-__u64 bdev_size(int fd)
+u64 bdev_size(int fd)
 {
-	__u64 size64; /* size in byte. */
+	u64 size64; /* size in byte. */
 	long size;    /* size in sectors. */
 	int err;
 
@@ -236,7 +209,7 @@
 				perror("ioctl(,BLKGETSIZE,) failed");
 				exit(20);
 			}
-			size64 = (typeof(__u64))512 * size;
+			size64 = (typeof(u64))512 * size;
 		} else {
 			perror("ioctl(,BLKGETSIZE64,) failed");
 			exit(20);
@@ -259,18 +232,18 @@
  begin of v07 {
  ******************************************/
 struct __attribute__((packed)) meta_data_on_disk_07 {
-	__u64 la_size;           /* last agreed size. */
-	__u32 gc[GEN_CNT_SIZE];  /* generation counter */
-	__u32 magic;
-	__u32 md_size;
-	__u32 al_offset;         /* offset to this block */
-	__u32 al_nr_extents;     /* important for restoring the AL */
-	__u32 bm_offset;         /* offset to the bitmap, from here */
+	u64 la_size;           /* last agreed size. */
+	u32 gc[GEN_CNT_SIZE];  /* generation counter */
+	u32 magic;
+	u32 md_size;
+	u32 al_offset;         /* offset to this block */
+	u32 al_nr_extents;     /* important for restoring the AL */
+	u32 bm_offset;         /* offset to the bitmap, from here */
 };
 
-__u64 v07_offset(struct format_07* cfg)
+u64 v07_offset(struct format_07* cfg)
 {
-	__u64 offset;
+	u64 offset;
 
 	if(cfg->index == -1) {
 		offset = ( bdev_size(cfg->fd) & ~((1<<12)-1) )
@@ -367,7 +340,7 @@
 	struct format_07* cfg = &config->d.f07;
 	struct meta_data_on_disk_07 buffer;
 	int rr,i,bmw;
-	__u64 offset = v07_offset(cfg);
+	u64 offset = v07_offset(cfg);
 
 	if(lseek64(cfg->fd,offset,SEEK_SET) == -1) {
 		PERROR("lseek() failed");
@@ -380,25 +353,25 @@
 		return 0;
 	}
 
-	if( __be32_to_cpu(buffer.magic) != DRBD_MD_MAGIC_07 ) {
+	if( be32_to_cpu(buffer.magic) != DRBD_MD_MAGIC_07 ) {
 		fprintf(stderr,"Magic number not found\n");
 		return 0;
 	}
 
-	if( __be32_to_cpu(buffer.al_offset) != MD_AL_OFFSET_07 ) {
+	if( be32_to_cpu(buffer.al_offset) != MD_AL_OFFSET_07 ) {
 		fprintf(stderr,"Magic number (al_offset) not found\n");
 		return 0;
 	}
 
-	if( __be32_to_cpu(buffer.bm_offset) != MD_BM_OFFSET_07 ) {
+	if( be32_to_cpu(buffer.bm_offset) != MD_BM_OFFSET_07 ) {
 		fprintf(stderr,"Magic number (bm_offset) not found\n");
 		return 0;
 	}
 
 	for (i = Flags; i < GEN_CNT_SIZE; i++)
-		m->gc[i] = __be32_to_cpu(buffer.gc[i]);
+		m->gc[i] = be32_to_cpu(buffer.gc[i]);
 
-	m->la_size = __be64_to_cpu(buffer.la_size);
+	m->la_size = be64_to_cpu(buffer.la_size);
 
 	if(m->bitmap) {
 		bmw = bm_words(m->la_size);
@@ -427,16 +400,16 @@
 	struct format_07* cfg = &config->d.f07;
 	struct meta_data_on_disk_07 buffer;
 	int rr,i;
-	__u64 offset = v07_offset(cfg);
+	u64 offset = v07_offset(cfg);
 
-	buffer.magic = __cpu_to_be32( DRBD_MD_MAGIC_07 );
-	buffer.al_offset = __cpu_to_be32( MD_AL_OFFSET_07 );
-	buffer.bm_offset = __cpu_to_be32( MD_BM_OFFSET_07 );
+	buffer.magic = cpu_to_be32( DRBD_MD_MAGIC_07 );
+	buffer.al_offset = cpu_to_be32( MD_AL_OFFSET_07 );
+	buffer.bm_offset = cpu_to_be32( MD_BM_OFFSET_07 );
 
 	for (i = Flags; i < GEN_CNT_SIZE; i++)
-		buffer.gc[i] = __cpu_to_be32(m->gc[i]);
+		buffer.gc[i] = cpu_to_be32(m->gc[i]);
 
-	buffer.la_size = __cpu_to_be64(m->la_size);
+	buffer.la_size = cpu_to_be64(m->la_size);
 
 	if(lseek64(cfg->fd,offset,SEEK_SET) == -1) {
 		PERROR("lseek() failed");
@@ -499,25 +472,25 @@
 		return 0;
 	}
 
-	if( __be32_to_cpu(buffer.magic) != DRBD_MD_MAGIC_08 ) {
+	if( be32_to_cpu(buffer.magic) != DRBD_MD_MAGIC_08 ) {
 		fprintf(stderr,"Magic number not found\n");
 		return 0;
 	}
 
-	if( __be32_to_cpu(buffer.al_offset) != MD_AL_OFFSET_07 ) {
+	if( be32_to_cpu(buffer.al_offset) != MD_AL_OFFSET_07 ) {
 		fprintf(stderr,"Magic number (al_offset) not found\n");
 		return 0;
 	}
 
-	if( __be32_to_cpu(buffer.bm_offset) != MD_BM_OFFSET_07 ) {
+	if( be32_to_cpu(buffer.bm_offset) != MD_BM_OFFSET_07 ) {
 		fprintf(stderr,"Magic number (bm_offset) not found\n");
 		return 0;
 	}
 
 	for (i = Flags; i < GEN_CNT_SIZE; i++)
-		m->gc[i] = __be32_to_cpu(buffer.gc[i]);
+		m->gc[i] = be32_to_cpu(buffer.gc[i]);
 
-	m->la_size = __be64_to_cpu(buffer.la_size) / 2 ;
+	m->la_size = be64_to_cpu(buffer.la_size) / 2 ;
 
 	if(m->bitmap) {
 		bmw = bm_words(m->la_size);
@@ -548,14 +521,14 @@
 	int rr,i;
 	__u64 offset = v07_offset(cfg);
 
-	buffer.magic = __cpu_to_be32( DRBD_MD_MAGIC_08 );
-	buffer.al_offset = __cpu_to_be32( MD_AL_OFFSET_07 );
-	buffer.bm_offset = __cpu_to_be32( MD_BM_OFFSET_07 );
+	buffer.magic = cpu_to_be32( DRBD_MD_MAGIC_08 );
+	buffer.al_offset = cpu_to_be32( MD_AL_OFFSET_07 );
+	buffer.bm_offset = cpu_to_be32( MD_BM_OFFSET_07 );
 
 	for (i = Flags; i < GEN_CNT_SIZE; i++)
-		buffer.gc[i] = __cpu_to_be32(m->gc[i]);
+		buffer.gc[i] = cpu_to_be32(m->gc[i]);
 
-	buffer.la_size = __cpu_to_be64(m->la_size * 2);
+	buffer.la_size = cpu_to_be64(m->la_size * 2);
 
 	if(lseek64(cfg->fd,offset,SEEK_SET) == -1) {
 		PERROR("lseek() failed");
@@ -597,8 +570,8 @@
  begin of v06 {
  ******************************************/
 struct __attribute__((packed)) meta_data_on_disk_06 {
-	__u32 gc[GEN_CNT_SIZE];  /* generation counter */
-	__u32 magic;
+	u32 gc[GEN_CNT_SIZE];  /* generation counter */
+	u32 magic;
 };
 
 int v06_parse(struct format * config, char **argv, int argc, int *ai);
@@ -681,13 +654,13 @@
 		return 0;
 	}
 
-	if( __be32_to_cpu(buffer.magic) != DRBD_MD_MAGIC_06 ) {
+	if( be32_to_cpu(buffer.magic) != DRBD_MD_MAGIC_06 ) {
 		fprintf(stderr,"Magic number not found\n");
 		return 0;
 	}
 
 	for (i = Flags; i < GEN_CNT_SIZE; i++)
-		m->gc[i] = __be32_to_cpu(buffer.gc[i]);
+		m->gc[i] = be32_to_cpu(buffer.gc[i]);
 
 	return 1;
 }
@@ -698,10 +671,10 @@
 	struct meta_data_on_disk_06 buffer;
 	int rr,i;
 
-	buffer.magic = __cpu_to_be32( DRBD_MD_MAGIC_06 );
+	buffer.magic = cpu_to_be32( DRBD_MD_MAGIC_06 );
 
 	for (i = Flags; i < GEN_CNT_SIZE; i++)
-		buffer.gc[i] = __cpu_to_be32(m->gc[i]);
+		buffer.gc[i] = cpu_to_be32(m->gc[i]);
 
 	if(lseek64(cfg->fd,0,SEEK_SET) == -1) {
 		PERROR("lseek() failed");
@@ -887,7 +860,7 @@
 int meta_dump_md(struct format * fcfg, char** argv, int argc )
 {
 	struct meta_data* md;
-	__u64 *b;
+	u64 *b;
 	int words;
 	int i;
 
@@ -908,8 +881,8 @@
 	/* if(md->la_size)  TODO. */
 
 	if(md->bitmap) {
-		words = md->bm_size/sizeof(__u64);
-		b = (__u64*) md->bitmap;
+		words = md->bm_size/sizeof(u64);
+		b = (u64*) md->bitmap;
 		printf("bm {");
 		for (i=0;i<words;i++) {
 #if BITS_PER_LONG == 32



More information about the drbd-cvs mailing list