2 * Open connection for network block device
4 * Copyright 1997,1998 Pavel Machek, distribute under GPL
5 * <pavel@atrey.karlin.mff.cuni.cz>
7 * Version 1.0 - 64bit issues should be fixed, now
8 * Version 1.1 - added bs (blocksize) option (Alexey Guzeev, aga@permonline.ru)
9 * Version 1.2 - I added new option '-d' to send the disconnect request
10 * Version 2.0 - Version synchronised with server
11 * Version 2.1 - Check for disconnection before INIT_PASSWD is received
12 * to make errormsg a bit more helpful in case the server can't
13 * open the exported file.
19 #include <sys/ioctl.h>
20 #include <sys/socket.h>
21 #include <sys/types.h>
23 #include <netinet/tcp.h>
24 #include <netinet/in.h> /* sockaddr_in, htons, in_addr */
25 #include <netdb.h> /* hostent, gethostby*, getservby* */
30 #include <sys/mount.h>
33 #error I need GCC to work
36 #include <linux/ioctl.h>
37 #define MY_NAME "nbd_client"
40 int opennet(char *name, int port) {
42 struct sockaddr_in xaddrin;
43 int xaddrinlen = sizeof(xaddrin);
44 struct hostent *hostn;
46 hostn = gethostbyname(name);
48 err("Gethostname failed: %h\n");
50 if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
51 err("Socket failed: %m");
53 xaddrin.sin_family = AF_INET;
54 xaddrin.sin_port = htons(port);
55 xaddrin.sin_addr.s_addr = *((int *) hostn->h_addr);
56 if ((connect(sock, (struct sockaddr *) &xaddrin, xaddrinlen) < 0))
63 void negotiate(int sock, u64 *rsize64, u32 *flags) {
65 char buf[256] = "\0\0\0\0\0\0\0\0\0";
67 printf("Negotiation: ");
68 if (read(sock, buf, 8) < 0)
71 err("Server closed connection");
72 if (strcmp(buf, INIT_PASSWD))
73 err("INIT_PASSWD bad");
75 if (read(sock, &magic, sizeof(magic)) < 0)
77 magic = ntohll(magic);
78 if (magic != cliserv_magic)
79 err("Not enough cliserv_magic");
82 if (read(sock, &size64, sizeof(size64)) < 0)
83 err("Failed/3: %m\n");
84 size64 = ntohll(size64);
86 #ifdef NBD_SET_SIZE_BLOCKS
87 if ((size64>>10) > (~0UL >> 1)) {
88 printf("size = %luMB", (unsigned long)(size64>>20));
89 err("Exported device is too big for me. Get 64-bit machine :-(\n");
91 printf("size = %luKB", (unsigned long)(size64>>10));
93 if (size64 > (~0UL >> 1)) {
94 printf("size = %luKB", (unsigned long)(size64>>10));
95 err("Exported device is too big. Get 64-bit machine or newer kernel :-(\n");
97 printf("size = %lu", (unsigned long)(size64));
100 if (read(sock, flags, sizeof(*flags)) < 0)
101 err("Failed/4: %m\n");
102 *flags = ntohl(*flags);
104 if (read(sock, &buf, 124) < 0)
105 err("Failed/5: %m\n");
111 void setsizes(int nbd, u64 size64, int blocksize, u32 flags) {
113 int read_only = (flags & NBD_FLAG_READ_ONLY) ? 1 : 0;
115 #ifdef NBD_SET_SIZE_BLOCKS
116 if (size64/blocksize > (~0UL >> 1))
117 err("Device too large.\n");
120 if (ioctl(nbd, NBD_SET_BLKSIZE, (unsigned long)blocksize) < 0)
121 err("Ioctl/1.1a failed: %m\n");
122 size = (unsigned long)(size64/blocksize);
123 if ((er = ioctl(nbd, NBD_SET_SIZE_BLOCKS, size)) < 0)
124 err("Ioctl/1.1b failed: %m\n");
125 fprintf(stderr, "bs=%d, sz=%lu\n", blocksize, size);
128 if (size64 > (~0UL >> 1)) {
129 err("Device too large.\n");
131 size = (unsigned long)size64;
132 if (ioctl(nbd, NBD_SET_SIZE, size) < 0)
133 err("Ioctl NBD_SET_SIZE failed: %m\n");
137 ioctl(nbd, NBD_CLEAR_SOCK);
139 if (ioctl(nbd, BLKROSET, (unsigned long) &read_only) < 0)
140 err("Unable to set read-only attribute for device");
143 void finish_sock(int sock, int nbd, int swap) {
144 if (ioctl(nbd, NBD_SET_SOCK, sock) < 0)
145 err("Ioctl NBD_SET_SOCK failed: %m\n");
149 err("You have to compile me on machine with swapping patch enabled in order to use it later.");
152 if (setsockopt(sock, SOL_SOCKET, SO_SWAPPING, &one, sizeof(int)) < 0)
153 err("Could not enable swapping: %m");
157 int main(int argc, char *argv[]) {
160 char *hostname, *nbddev;
170 fprintf(stderr, "nbd-client version %s\n", PACKAGE_VERSION);
171 fprintf(stderr, "Usage: nbd-client [bs=blocksize] host port nbd_device [-swap] [-persist]\n");
172 fprintf(stderr, "Or : nbd-client -d nbd_device\n");
173 fprintf(stderr, "Default value for blocksize is 1024 (recommended for ethernet)\n");
174 fprintf(stderr, "Allowed values for blocksize are 512,1024,2048,4096\n"); /* will be checked in kernel :) */
175 fprintf(stderr, "Note, that kernel 2.4.2 and older ones do not work correctly with\n");
176 fprintf(stderr, "blocksizes other than 1024 without patches\n");
180 ++argv; --argc; /* skip programname */
182 if (strcmp(argv[0], "-d")==0) {
183 nbd = open(argv[1], O_RDWR);
185 err("Can not open NBD: %m");
186 printf("Disconnecting: que, ");
187 if (ioctl(nbd, NBD_CLEAR_QUE)< 0)
188 err("Ioctl failed: %m\n");
189 printf("disconnect, ");
190 #ifdef NBD_DISCONNECT
191 if (ioctl(nbd, NBD_DISCONNECT)<0)
192 err("Ioctl failed: %m\n");
195 fprintf(stderr, "Can't disconnect: I was not compiled with disconnect support!\n" );
198 if (ioctl(nbd, NBD_CLEAR_SOCK)<0)
199 err("Ioctl failed: %m\n");
204 if (strncmp(argv[0], "bs=", 3)==0) {
205 blocksize=atoi(argv[0]+3);
206 ++argv; --argc; /* skip blocksize */
209 if (argc==0) goto errmsg;
211 ++argv; --argc; /* skip hostname */
213 if (argc==0) goto errmsg;
214 port = atoi(argv[0]);
215 ++argv; --argc; /* skip port */
217 if (argc==0) goto errmsg;
218 sock = opennet(hostname, port);
220 nbd = open(nbddev, O_RDWR);
222 err("Can not open NBD: %m");
223 ++argv; --argc; /* skip device */
225 if (argc>2) goto errmsg;
227 if(strncmp(argv[0], "-swap", 5)==0) {
233 if(strncmp(argv[0], "-persist", 8)==0) {
238 argv=NULL; argc=0; /* don't use it later suddenly */
240 negotiate(sock, &size64, &flags);
241 setsizes(nbd, size64, blocksize, flags);
242 finish_sock(sock, nbd, swap);
253 if (ioctl(nbd, NBD_DO_IT) < 0) {
254 fprintf(stderr, "Kernel call returned: %m");
256 /* The user probably did 'nbd-client -d' on us.
264 fprintf(stderr, " Reconnecting\n");
265 close(sock); close(nbd);
266 sock = opennet(hostname, port);
267 nbd = open(nbddev, O_RDWR);
268 negotiate(sock, &new_size, &new_flags);
269 if (size64 != new_size) {
270 err("Size of the device changed. Bye");
272 setsizes(nbd, size64, blocksize,
275 finish_sock(sock,nbd,swap);
279 /* We're on 2.4. It's not clearly defined what exactly
280 * happened at this point. Probably best to quit, now
282 fprintf(stderr, "Kernel call returned.");
286 printf("Closing: que, ");
287 ioctl(nbd, NBD_CLEAR_QUE);
289 ioctl(nbd, NBD_CLEAR_SOCK);