[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

[patch] and merge request: Replace u32 and u64 by standard compliant uint32_t, uint64_t



Here is the merge request:

https://github.com/NetworkBlockDevice/nbd/pull/164

with the description:

---<snip>---
I'd favor to get rid of the non-standard typedefs u32 and u64 and use
C99 standard types uint32_t and uint64_t everywhere. These types are
already used in the project, but mixed wildly with the old types.

During this I also moved some #include directives so each .h includes
only the headers to fulfill its own needs (e.g. to use foreign
declarations for its own declarations). Headers that are only needed for
the implementation are included only in the .c file.

Redundant or unneeded includes are removed at all.
---<snap>---

Please make a review & comment if there is something unclear or could
get improvements. :-)


Lars R.
From 29a90140412ae8761e4ab9e06582c161d5bef7c6 Mon Sep 17 00:00:00 2001
From: roker <roker@pep-project.org>
Date: Wed, 10 Apr 2024 18:18:59 +0200
Subject: [PATCH] use uint32_t & uint64_t instead of u32 & u64, and fix
 #includes

---
 cliserv.c    | 11 ++++++-----
 cliserv.h    | 30 ++++--------------------------
 nbd-client.c | 28 +++++++++++++++-------------
 nbd-server.c | 14 +++++++-------
 nbd-trplay.c |  1 +
 nbd.h        |  2 ++
 nbdsrv.c     |  2 +-
 treefiles.c  |  1 +
 8 files changed, 37 insertions(+), 52 deletions(-)

diff --git a/cliserv.c b/cliserv.c
index 9341e606..3b9d5b8f 100644
--- a/cliserv.c
+++ b/cliserv.c
@@ -1,4 +1,5 @@
 #include <config.h>
+#include <errno.h>
 #include <fcntl.h>
 #include <stdio.h>
 #include <syslog.h>
@@ -9,9 +10,9 @@
 #include <cliserv.h>
 #include <nbd-debug.h>
 
-const u64 cliserv_magic = 0x00420281861253LL;
-const u64 opts_magic = 0x49484156454F5054LL;
-const u64 rep_magic = 0x3e889045565a9LL;
+const uint64_t cliserv_magic = 0x00420281861253LL;
+const uint64_t opts_magic = 0x49484156454F5054LL;
+const uint64_t rep_magic = 0x3e889045565a9LL;
 
 /**
  * Set a socket to blocking or non-blocking
@@ -93,8 +94,8 @@ uint64_t ntohll(uint64_t a) {
 }
 #else
 uint64_t ntohll(uint64_t a) {
-	u32 lo = a & 0xffffffff;
-	u32 hi = a >> 32U;
+	uint32_t lo = a & 0xffffffff;
+	uint32_t hi = a >> 32U;
 	lo = ntohl(lo);
 	hi = ntohl(hi);
 	return ((uint64_t) lo) << 32U | hi;
diff --git a/cliserv.h b/cliserv.h
index b8e79786..1ef01c32 100644
--- a/cliserv.h
+++ b/cliserv.h
@@ -9,36 +9,14 @@
    Send 128 bytes of zeros (reserved for future use)
  */
 
-#include <errno.h>
 #include <string.h>
 #include <netdb.h>
 #include <netinet/tcp.h>
 #include <netinet/in.h>
 #include <stdlib.h>
 
-#if SIZEOF_UNSIGNED_SHORT_INT==4
-typedef unsigned short u32;
-#elif SIZEOF_UNSIGNED_INT==4
-typedef unsigned int u32;
-#elif SIZEOF_UNSIGNED_LONG_INT==4
-typedef unsigned long u32;
-#else
-#error I need at least some 32-bit type
-#endif
-
-#if SIZEOF_UNSIGNED_INT==8
-typedef unsigned int u64;
-#elif SIZEOF_UNSIGNED_LONG_INT==8
-typedef unsigned long u64;
-#elif SIZEOF_UNSIGNED_LONG_LONG_INT==8
-typedef unsigned long long u64;
-#else
-#error I need at least some 64-bit type
-#endif
+#include "config.h"
 
-#define __be32 u32
-#define __be64 u64
-#include "nbd.h"
 
 #ifndef HAVE_FDATASYNC
 #define fdatasync(arg) fsync(arg)
@@ -64,9 +42,9 @@ typedef unsigned long long u64;
 #endif
 #endif
 
-extern const u64 cliserv_magic;
-extern const u64 opts_magic;
-extern const u64 rep_magic;
+extern const uint64_t cliserv_magic;
+extern const uint64_t opts_magic;
+extern const uint64_t rep_magic;
 
 #define INIT_PASSWD "NBDMAGIC"
 
diff --git a/nbd-client.c b/nbd-client.c
index f4e120f8..06cbb693 100644
--- a/nbd-client.c
+++ b/nbd-client.c
@@ -29,6 +29,7 @@
 #include <netinet/tcp.h>
 #include <netinet/in.h>
 #include <netdb.h>
+#include "nbd.h"
 #include "netdb-compat.h"
 #include <inttypes.h>
 #include <stdio.h>
@@ -163,7 +164,7 @@ static struct nl_sock *get_nbd_socket(int *driver_id) {
 }
 
 static void netlink_configure(int index, int *sockfds, int num_connects,
-			      u64 size64, int blocksize, uint16_t flags,
+			      uint64_t size64, int blocksize, uint16_t flags,
 			      int timeout) {
 	struct nl_sock *socket;
 	struct nlattr *sock_attr;
@@ -180,6 +181,7 @@ static void netlink_configure(int index, int *sockfds, int num_connects,
 		    NBD_CMD_CONNECT, 0);
 	if (index >= 0)
 		NLA_PUT_U32(msg, NBD_ATTR_INDEX, index);
+
 	NLA_PUT_U64(msg, NBD_ATTR_SIZE_BYTES, size64);
 	NLA_PUT_U64(msg, NBD_ATTR_BLOCK_SIZE_BYTES, blocksize);
 	NLA_PUT_U64(msg, NBD_ATTR_SERVER_FLAGS, flags);
@@ -236,7 +238,7 @@ static void netlink_disconnect(char *nbddev) {
 }
 #else
 static void netlink_configure(int index, int *sockfds, int num_connects,
-			      u64 size64, int blocksize, uint16_t flags,
+			      uint64_t size64, int blocksize, uint16_t flags,
 			      int timeout)
 {
 }
@@ -531,7 +533,7 @@ void parse_sizes(char *buf, uint64_t *size, uint16_t *flags) {
 	printf("\n");
 }
 
-void send_opt_exportname(int sock, u64 *rsize64, uint16_t *flags, bool can_opt_go, char* name, uint16_t global_flags) {
+void send_opt_exportname(int sock, uint64_t *rsize64, uint16_t *flags, bool can_opt_go, char* name, uint16_t global_flags) {
 	send_request(sock, NBD_OPT_EXPORT_NAME, -1, name);
 	char b[sizeof(*flags) + sizeof(*rsize64)];
 	if(readit(sock, b, sizeof(b)) < 0 && can_opt_go) {
@@ -544,8 +546,8 @@ void send_opt_exportname(int sock, u64 *rsize64, uint16_t *flags, bool can_opt_g
 	}
 }
 
-void negotiate(int *sockp, u64 *rsize64, uint16_t *flags, char* name, uint32_t needed_flags, uint32_t client_flags, uint32_t do_opts, char *certfile, char *keyfile, char *cacertfile, char *tlshostname, bool tls, char *priority, bool can_opt_go) {
-	u64 magic;
+void negotiate(int *sockp, uint64_t *rsize64, uint16_t *flags, char* name, uint32_t needed_flags, uint32_t client_flags, uint32_t do_opts, char *certfile, char *keyfile, char *cacertfile, char *tlshostname, bool tls, char *priority, bool can_opt_go) {
+	uint64_t magic;
 	uint16_t tmp;
 	uint16_t global_flags;
 	char buf[256] = "\0\0\0\0\0\0\0\0\0";
@@ -804,7 +806,7 @@ bool get_from_config(char* cfgname, char** name_ptr, char** dev_ptr, char** host
 	return retval;
 }
 
-void setsizes(int nbd, u64 size64, int blocksize, u32 flags) {
+void setsizes(int nbd, uint64_t size64, int blocksize, uint32_t flags) {
 	unsigned long size;
 	int read_only = (flags & NBD_FLAG_READ_ONLY) ? 1 : 0;
 
@@ -812,14 +814,14 @@ void setsizes(int nbd, u64 size64, int blocksize, u32 flags) {
 		err("Device too large.\n");
 	else {
 		int tmp_blocksize = 4096;
-		if (size64 / (u64)blocksize <= (uint64_t)~0UL)
+		if (size64 / (uint64_t)blocksize <= (uint64_t)~0UL)
 			tmp_blocksize = blocksize;
 		if (ioctl(nbd, NBD_SET_BLKSIZE, tmp_blocksize) < 0) {
 			fprintf(stderr, "Failed to set blocksize %d\n",
 				tmp_blocksize);
 			err("Ioctl/1.1a failed: %m\n");
 		}
-		size = (unsigned long)(size64 / (u64)tmp_blocksize);
+		size = (unsigned long)(size64 / (uint64_t)tmp_blocksize);
 		if (ioctl(nbd, NBD_SET_SIZE_BLOCKS, size) < 0)
 			err("Ioctl/1.1b failed: %m\n");
 		if (tmp_blocksize != blocksize) {
@@ -829,7 +831,7 @@ void setsizes(int nbd, u64 size64, int blocksize, u32 flags) {
 				err("Ioctl/1.1c failed: %m\n");
 			}
 		}
-		fprintf(stderr, "bs=%d, sz=%" PRIu64 " bytes\n", blocksize, (u64)tmp_blocksize * size);
+		fprintf(stderr, "bs=%d, sz=%" PRIu64 " bytes\n", blocksize, (uint64_t)tmp_blocksize * size);
 	}
 
 	ioctl(nbd, NBD_CLEAR_SOCK);
@@ -947,8 +949,8 @@ int main(int argc, char *argv[]) {
 	int timeout=0;
 	int G_GNUC_UNUSED nofork=0; // if -dNOFORK
 	pid_t main_pid;
-	u64 size64 = 0;
-	u64 force_size64 = 0;
+	uint64_t size64 = 0;
+	uint64_t force_size64 = 0;
 	uint16_t flags = 0;
 	bool force_read_only = false;
 	bool preinit = false;
@@ -1062,7 +1064,7 @@ int main(int argc, char *argv[]) {
 			}
 			break;
 		case 'B':
-			force_size64=(u64)strtoull(optarg, NULL, 0);
+			force_size64=(uint64_t)strtoull(optarg, NULL, 0);
 			if(force_size64 == 0) {
 				fprintf(stderr, "E: Invalid size\n");
 				exit(EXIT_FAILURE);
@@ -1341,7 +1343,7 @@ int main(int argc, char *argv[]) {
 				cont=0;
 			} else {
 				if(cont) {
-					u64 new_size;
+					uint64_t new_size;
 					uint16_t new_flags;
 
 					close(sock); close(nbd);
diff --git a/nbd-server.c b/nbd-server.c
index 60fc5a59..ed6745b6 100644
--- a/nbd-server.c
+++ b/nbd-server.c
@@ -1573,7 +1573,7 @@ int expread(READ_CTX *ctx, CLIENT *client) {
 			len : (size_t)DIFFPAGESIZE-offset;
 		if (!(client->server->flags & F_COPYONWRITE))
 			pthread_rwlock_rdlock(&client->export_lock);
-		if (client->difmap[mapcnt]!=(u32)(-1)) { /* the block is already there */
+		if (client->difmap[mapcnt]!=(uint32_t)(-1)) { /* the block is already there */
 			DEBUG("Page %llu is at %lu\n", (unsigned long long)mapcnt,
 			       (unsigned long)(client->difmap[mapcnt]));
 			char *buf = find_read_buf(ctx);
@@ -1642,7 +1642,7 @@ int expwrite(off_t a, char *buf, size_t len, CLIENT *client, int fua) {
 
 		if (!(client->server->flags & F_COPYONWRITE))
 			pthread_rwlock_rdlock(&client->export_lock);
-		if (client->difmap[mapcnt]!=(u32)(-1)) { /* the block is already there */
+		if (client->difmap[mapcnt]!=(uint32_t)(-1)) { /* the block is already there */
 			DEBUG("Page %llu is at %lu\n", (unsigned long long)mapcnt,
 			       (unsigned long)(client->difmap[mapcnt])) ;
 			if (pwrite(client->difffile, buf, wrlen, client->difmap[mapcnt]*DIFFPAGESIZE+offset) != wrlen) goto fail;
@@ -1942,7 +1942,7 @@ int commit_diff(CLIENT* client, bool lock, int fhandle){
 		offset = DIFFPAGESIZE*i;
 		if (lock)
 			pthread_rwlock_wrlock(&client->export_lock);
-		if (client->difmap[i] != (u32)-1){
+		if (client->difmap[i] != (uint32_t)-1){
 			dirtycount += 1;
 			DEBUG("flushing dirty page %d, offset %ld\n", i, offset);
 			if (pread(client->difffile, buf, DIFFPAGESIZE, client->difmap[i]*DIFFPAGESIZE) != DIFFPAGESIZE) {
@@ -1959,7 +1959,7 @@ int commit_diff(CLIENT* client, bool lock, int fhandle){
 				}
 				break;
 			}
-			client->difmap[i] = (u32)-1;
+			client->difmap[i] = (uint32_t)-1;
 		}
 		if (lock)
 			pthread_rwlock_unlock(&client->export_lock);
@@ -2155,17 +2155,17 @@ bool copyonwrite_prepare(CLIENT* client) {
 		err("Could not create diff file (%m)");
 		return false;
 	}
-	if ((client->difmap=calloc(client->exportsize/DIFFPAGESIZE,sizeof(u32)))==NULL) {
+	if ((client->difmap=calloc(client->exportsize/DIFFPAGESIZE,sizeof(uint32_t)))==NULL) {
 		err("Could not allocate memory");
 		return false;
 	}
-	for (i=0;i<client->exportsize/DIFFPAGESIZE;i++) client->difmap[i]=(u32)-1;
+	for (i=0;i<client->exportsize/DIFFPAGESIZE;i++) client->difmap[i]=(uint32_t)-1;
 
 	return true;
 }
 
 void send_export_info(CLIENT* client, SERVER* server, bool maybe_zeroes) {
-	uint64_t size_host = htonll((u64)(client->exportsize));
+	const uint64_t size_host = htonll((uint64_t)(client->exportsize));
 	uint16_t flags = NBD_FLAG_HAS_FLAGS | NBD_FLAG_SEND_WRITE_ZEROES;
 
 	socket_write(client, &size_host, 8);
diff --git a/nbd-trplay.c b/nbd-trplay.c
index deeea517..661c0acc 100644
--- a/nbd-trplay.c
+++ b/nbd-trplay.c
@@ -8,6 +8,7 @@
  * (C) Robert Bosch GmbH, 2021
  */
 
+#include <errno.h>
 #include <stdlib.h>
 #include <limits.h>
 #include <stdio.h>
diff --git a/nbd.h b/nbd.h
index 049e7455..680d04e4 100644
--- a/nbd.h
+++ b/nbd.h
@@ -17,6 +17,8 @@
 
 //#include <linux/types.h>
 
+#include <stdint.h>
+
 #define NBD_SET_SOCK	_IO( 0xab, 0 )
 #define NBD_SET_BLKSIZE	_IO( 0xab, 1 )
 #define NBD_SET_SIZE	_IO( 0xab, 2 )
diff --git a/nbdsrv.c b/nbdsrv.c
index 3c6896c2..6b4182ec 100644
--- a/nbdsrv.c
+++ b/nbdsrv.c
@@ -228,7 +228,7 @@ SERVER* dup_serve(const SERVER *const s) {
 
 uint64_t size_autodetect(int fhandle) {
 	off_t es;
-	u64 bytes __attribute__((unused));
+	uint64_t bytes __attribute__((unused));
 	struct stat stat_buf;
 	int error;
 
diff --git a/treefiles.c b/treefiles.c
index a5552226..38e1ae3a 100644
--- a/treefiles.c
+++ b/treefiles.c
@@ -1,3 +1,4 @@
+#include <errno.h>
 #include <fcntl.h>
 #include <limits.h> // for PATH_MAX
 #include <inttypes.h>

Attachment: OpenPGP_signature.asc
Description: OpenPGP digital signature


Reply to: