#include "config.h"
#include "lfs.h"
-#include <asm/page.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <fcntl.h>
#include <syslog.h>
#include <stdlib.h>
+#include <sys/mount.h>
#ifndef __GNUC__
#error I need GCC to work
return sock;
}
-u64 negotiate(int sock, int blocksize) {
+void negotiate(int sock, u64 *rsize64, u32 *flags) {
u64 magic, size64;
char buf[256] = "\0\0\0\0\0\0\0\0\0";
printf("size = %lu", (unsigned long)(size64));
#endif
- if (read(sock, &buf, 128) < 0)
+ if (read(sock, flags, sizeof(*flags)) < 0)
err("Failed/4: %m\n");
+ *flags = ntohl(*flags);
+
+ if (read(sock, &buf, 124) < 0)
+ err("Failed/5: %m\n");
printf("\n");
- return size64;
+ *rsize64 = size64;
}
-void setsizes(int nbd, u64 size64, int blocksize) {
+void setsizes(int nbd, u64 size64, int blocksize, u32 flags) {
unsigned long size;
+ int read_only = (flags & NBD_FLAG_READ_ONLY) ? 1 : 0;
#ifdef NBD_SET_SIZE_BLOCKS
if (size64/blocksize > (~0UL >> 1))
#endif
ioctl(nbd, NBD_CLEAR_SOCK);
+
+ if (ioctl(nbd, BLKROSET, (unsigned long) &read_only) < 0)
+ err("Unable to set read-only attribute for device");
+}
+
+void set_timeout(int nbd, int timeout) {
+#ifdef NBD_SET_TIMEOUT
+ if (timeout) {
+ if (ioctl(nbd, NBD_SET_TIMEOUT, (unsigned long)timeout) < 0)
+ err("Ioctl NBD_SET_TIMEOUT failed: %m\n");
+ fprintf(stderr, "timeout=%d\n", timeout);
+ }
+#endif
}
void finish_sock(int sock, int nbd, int swap) {
char *hostname, *nbddev;
int swap=0;
int cont=0;
+ int timeout=0;
u64 size64;
+ u32 flags;
logging();
if (argc < 3) {
errmsg:
fprintf(stderr, "nbd-client version %s\n", PACKAGE_VERSION);
- fprintf(stderr, "Usage: nbd-client [bs=blocksize] host port nbd_device [-swap] [-persist]\n");
+ fprintf(stderr, "Usage: nbd-client [bs=blocksize] [timeout=sec] host port nbd_device [-swap] [-persist]\n");
fprintf(stderr, "Or : nbd-client -d nbd_device\n");
fprintf(stderr, "Default value for blocksize is 1024 (recommended for ethernet)\n");
fprintf(stderr, "Allowed values for blocksize are 512,1024,2048,4096\n"); /* will be checked in kernel :) */
++argv; --argc; /* skip blocksize */
}
+ if (strncmp(argv[0], "timeout=", 8)==0) {
+ timeout=atoi(argv[0]+8);
+ ++argv; --argc; /* skip timeout */
+ }
+
if (argc==0) goto errmsg;
hostname=argv[0];
++argv; --argc; /* skip hostname */
}
argv=NULL; argc=0; /* don't use it later suddenly */
- size64 = negotiate(sock, blocksize);
- setsizes(nbd, size64, blocksize);
+ negotiate(sock, &size64, &flags);
+ setsizes(nbd, size64, blocksize, flags);
+ set_timeout(nbd, timeout);
finish_sock(sock, nbd, swap);
/* Go daemon */
cont=0;
} else {
if(cont) {
+ u64 new_size;
+ u32 new_flags;
+
fprintf(stderr, " Reconnecting\n");
close(sock); close(nbd);
sock = opennet(hostname, port);
nbd = open(nbddev, O_RDWR);
- if(size64!=negotiate(sock,blocksize)) {
+ negotiate(sock, &new_size, &new_flags);
+ if (size64 != new_size) {
err("Size of the device changed. Bye");
}
- setsizes(nbd, size64, blocksize);
+ setsizes(nbd, size64, blocksize,
+ new_flags);
+
+ set_timeout(nbd, timeout);
finish_sock(sock,nbd,swap);
}
}