Skip to content

use uint32_t & uint64_t instead of u32 & u64, and fix #includes #164

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions cliserv.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <config.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <syslog.h>
Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand Down
30 changes: 4 additions & 26 deletions cliserv.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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"

Expand Down
28 changes: 15 additions & 13 deletions nbd-client.c
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spurious whitespace change?

NLA_PUT_U64(msg, NBD_ATTR_BLOCK_SIZE_BYTES, blocksize);
NLA_PUT_U64(msg, NBD_ATTR_SERVER_FLAGS, flags);
Expand Down Expand Up @@ -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)
{
}
Expand Down Expand Up @@ -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) {
Expand All @@ -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";
Expand Down Expand Up @@ -804,22 +806,22 @@ 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;

if (size64>>12 > (uint64_t)~0UL)
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) {
Expand All @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
14 changes: 7 additions & 7 deletions nbd-server.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions nbd-trplay.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* (C) Robert Bosch GmbH, 2021
*/

#include <errno.h>
#include <stdlib.h>
#include <limits.h>
#include <stdio.h>
Expand Down
2 changes: 2 additions & 0 deletions nbd.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
Expand Down
2 changes: 1 addition & 1 deletion nbdsrv.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
1 change: 1 addition & 0 deletions treefiles.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <errno.h>
#include <fcntl.h>
#include <limits.h> // for PATH_MAX
#include <inttypes.h>
Expand Down