Make incorrect use of timeout fail.
[nbd.git] / nbd-client.c
1 /*
2  * Open connection for network block device
3  *
4  * Copyright 1997,1998 Pavel Machek, distribute under GPL
5  *  <pavel@atrey.karlin.mff.cuni.cz>
6  *
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.
14  */
15
16 #include "config.h"
17 #include "lfs.h"
18
19 #include <sys/ioctl.h>
20 #include <sys/socket.h>
21 #include <sys/types.h>
22 #include <unistd.h>
23 #include <netinet/tcp.h>
24 #include <netinet/in.h>         /* sockaddr_in, htons, in_addr */
25 #include <netdb.h>              /* hostent, gethostby*, getservby* */
26 #include <stdio.h>
27 #include <fcntl.h>
28 #include <syslog.h>
29 #include <stdlib.h>
30 #include <sys/mount.h>
31 #include <sys/mman.h>
32 #include <errno.h>
33
34 #ifndef __GNUC__
35 #error I need GCC to work
36 #endif
37
38 #include <linux/ioctl.h>
39 #define MY_NAME "nbd_client"
40 #include "cliserv.h"
41
42 int check_conn(char* devname, int do_print) {
43         char buf[256];
44         char* p;
45         int fd;
46         int len;
47         if(!strncmp(devname, "/dev/", 5)) {
48                 devname+=5;
49         }
50         if((p=strchr(devname, 'p'))) {
51                 /* We can't do checks on partitions. */
52                 *p='\0';
53         }
54         snprintf(buf, 256, "/sys/block/%s/pid", devname);
55         if((fd=open(buf, O_RDONLY))<0) {
56                 if(errno==ENOENT) {
57                         return 1;
58                 } else {
59                         return 2;
60                 }
61         }
62         len=read(fd, buf, 256);
63         buf[len-1]='\0';
64         if(do_print) printf("%s\n", buf);
65         return 0;
66 }
67
68 int opennet(char *name, int port, int sdp) {
69         int sock;
70         struct sockaddr_in xaddrin;
71         int xaddrinlen = sizeof(xaddrin);
72         struct hostent *hostn;
73         int af;
74
75         hostn = gethostbyname(name);
76         if (!hostn)
77                 err("Gethostname failed: %h\n");
78
79         af = AF_INET;
80         if(sdp) {
81 #ifdef WITH_SDP
82                 af = AF_INET_SDP;
83 #else
84                 err("Can't do SDP: I was not compiled with SDP support!");
85 #endif
86         }
87         if ((sock = socket(af, SOCK_STREAM, IPPROTO_TCP)) < 0)
88                 err("Socket failed: %m");
89
90         xaddrin.sin_family = af;
91         xaddrin.sin_port = htons(port);
92         xaddrin.sin_addr.s_addr = *((int *) hostn->h_addr);
93         if ((connect(sock, (struct sockaddr *) &xaddrin, xaddrinlen) < 0))
94                 err("Connect: %m");
95
96         setmysockopt(sock);
97         return sock;
98 }
99
100 void negotiate(int sock, u64 *rsize64, u32 *flags) {
101         u64 magic, size64;
102         char buf[256] = "\0\0\0\0\0\0\0\0\0";
103
104         printf("Negotiation: ");
105         if (read(sock, buf, 8) < 0)
106                 err("Failed/1: %m");
107         if (strlen(buf)==0)
108                 err("Server closed connection");
109         if (strcmp(buf, INIT_PASSWD))
110                 err("INIT_PASSWD bad");
111         printf(".");
112         if (read(sock, &magic, sizeof(magic)) < 0)
113                 err("Failed/2: %m");
114         magic = ntohll(magic);
115         if (magic != cliserv_magic)
116                 err("Not enough cliserv_magic");
117         printf(".");
118
119         if (read(sock, &size64, sizeof(size64)) < 0)
120                 err("Failed/3: %m\n");
121         size64 = ntohll(size64);
122
123 #ifdef NBD_SET_SIZE_BLOCKS
124         if ((size64>>10) > (~0UL >> 1)) {
125                 printf("size = %luMB", (unsigned long)(size64>>20));
126                 err("Exported device is too big for me. Get 64-bit machine :-(\n");
127         } else
128                 printf("size = %luKB", (unsigned long)(size64>>10));
129 #else
130         if (size64 > (~0UL >> 1)) {
131                 printf("size = %luKB", (unsigned long)(size64>>10));
132                 err("Exported device is too big. Get 64-bit machine or newer kernel :-(\n");
133         } else
134                 printf("size = %lu", (unsigned long)(size64));
135 #endif
136
137         if (read(sock, flags, sizeof(*flags)) < 0)
138                 err("Failed/4: %m\n");
139         *flags = ntohl(*flags);
140
141         if (read(sock, &buf, 124) < 0)
142                 err("Failed/5: %m\n");
143         printf("\n");
144
145         *rsize64 = size64;
146 }
147
148 void setsizes(int nbd, u64 size64, int blocksize, u32 flags) {
149         unsigned long size;
150         int read_only = (flags & NBD_FLAG_READ_ONLY) ? 1 : 0;
151
152 #ifdef NBD_SET_SIZE_BLOCKS
153         if (size64/blocksize > (~0UL >> 1))
154                 err("Device too large.\n");
155         else {
156                 int er;
157                 if (ioctl(nbd, NBD_SET_BLKSIZE, (unsigned long)blocksize) < 0)
158                         err("Ioctl/1.1a failed: %m\n");
159                 size = (unsigned long)(size64/blocksize);
160                 if ((er = ioctl(nbd, NBD_SET_SIZE_BLOCKS, size)) < 0)
161                         err("Ioctl/1.1b failed: %m\n");
162                 fprintf(stderr, "bs=%d, sz=%lu\n", blocksize, size);
163         }
164 #else
165         if (size64 > (~0UL >> 1)) {
166                 err("Device too large.\n");
167         } else {
168                 size = (unsigned long)size64;
169                 if (ioctl(nbd, NBD_SET_SIZE, size) < 0)
170                         err("Ioctl NBD_SET_SIZE failed: %m\n");
171         }
172 #endif
173
174         ioctl(nbd, NBD_CLEAR_SOCK);
175
176         if (ioctl(nbd, BLKROSET, (unsigned long) &read_only) < 0)
177                 err("Unable to set read-only attribute for device");
178 }
179
180 void set_timeout(int nbd, int timeout) {
181         if (timeout) {
182 #ifdef NBD_SET_TIMEOUT
183                 if (ioctl(nbd, NBD_SET_TIMEOUT, (unsigned long)timeout) < 0)
184                         err("Ioctl NBD_SET_TIMEOUT failed: %m\n");
185                 fprintf(stderr, "timeout=%d\n", timeout);
186 #else
187                 err("Ioctl NBD_SET_TIMEOUT cannot be called when compiled on a system that does not support it\n");
188 #endif
189         }
190 }
191
192 void finish_sock(int sock, int nbd, int swap) {
193         if (ioctl(nbd, NBD_SET_SOCK, sock) < 0)
194                 err("Ioctl NBD_SET_SOCK failed: %m\n");
195
196 /*
197  * If anyone ever forward-patches this patch, I'll happily re-enable
198  * this code. Until then...
199 #ifndef SO_SWAPPING
200         if (swap)
201                 err("You have to compile me on machine with swapping patch enabled in order to use it later.");
202 #else
203         if (swap)
204                 if (setsockopt(sock, SOL_SOCKET, SO_SWAPPING, &one, sizeof(int)) < 0)
205                         err("Could not enable swapping: %m");
206 #endif
207 */
208         mlockall(MCL_CURRENT | MCL_FUTURE);
209 }
210
211 int main(int argc, char *argv[]) {
212         int port, sock, nbd;
213         int blocksize=1024;
214         char *hostname, *nbddev;
215         int swap=0;
216         int cont=0;
217         int timeout=0;
218         int sdp=0;
219         int nofork=0;
220         u64 size64;
221         u32 flags;
222
223         logging();
224
225         if (argc < 3) {
226         errmsg:
227                 fprintf(stderr, "nbd-client version %s\n", PACKAGE_VERSION);
228                 fprintf(stderr, "Usage: nbd-client [bs=blocksize] [timeout=sec] host port nbd_device [-swap] [-persist] [-nofork]\n");
229                 fprintf(stderr, "Or   : nbd-client -d nbd_device\n");
230                 fprintf(stderr, "Or   : nbd-client -c nbd_device\n");
231                 fprintf(stderr, "Default value for blocksize is 1024 (recommended for ethernet)\n");
232                 fprintf(stderr, "Allowed values for blocksize are 512,1024,2048,4096\n"); /* will be checked in kernel :) */
233                 fprintf(stderr, "Note, that kernel 2.4.2 and older ones do not work correctly with\n");
234                 fprintf(stderr, "blocksizes other than 1024 without patches\n");
235                 return 1;
236         }
237
238         ++argv; --argc; /* skip programname */
239
240         if (strcmp(argv[0], "-d")==0) {
241                 nbd = open(argv[1], O_RDWR);
242                 if (nbd < 0)
243                         err("Cannot open NBD: %m\nPlease ensure the 'nbd' module is loaded.");
244                 printf("Disconnecting: que, ");
245                 if (ioctl(nbd, NBD_CLEAR_QUE)< 0)
246                         err("Ioctl failed: %m\n");
247                 printf("disconnect, ");
248 #ifdef NBD_DISCONNECT
249                 if (ioctl(nbd, NBD_DISCONNECT)<0)
250                         err("Ioctl failed: %m\n");
251                 printf("sock, ");
252 #else
253                 fprintf(stderr, "Can't disconnect: I was not compiled with disconnect support!\n" );
254                 exit(1);
255 #endif
256                 if (ioctl(nbd, NBD_CLEAR_SOCK)<0)
257                         err("Ioctl failed: %m\n");
258                 printf("done\n");
259                 return 0;
260         }
261         if(strcmp(argv[0], "-c")==0) {
262                 return check_conn(argv[1], 1);
263         }
264         
265         if (strncmp(argv[0], "bs=", 3)==0) {
266                 blocksize=atoi(argv[0]+3);
267                 ++argv; --argc; /* skip blocksize */
268         }
269
270         if (strncmp(argv[0], "timeout=", 8)==0) {
271                 timeout=atoi(argv[0]+8);
272                 ++argv; --argc; /* skip timeout */
273         }
274         
275         if (argc==0) goto errmsg;
276         hostname=argv[0];
277         ++argv; --argc; /* skip hostname */
278
279         if (argc==0) goto errmsg;
280         port = atoi(argv[0]);
281         ++argv; --argc; /* skip port */
282
283         if (argc==0) goto errmsg;
284         nbddev = argv[0];
285         nbd = open(nbddev, O_RDWR);
286         if (nbd < 0)
287           err("Cannot open NBD: %m\nPlease ensure the 'nbd' module is loaded.");
288         ++argv; --argc; /* skip device */
289
290         if (argc>3) goto errmsg;
291         if (argc) {
292                 if(strncmp(argv[0], "-swap", 5)==0) {
293                         swap=1;
294                         ++argv;--argc;
295                 }
296         }
297         if (argc) {
298                 if(strncmp(argv[0], "-persist", 8)==0) {
299                         cont=1;
300                         ++argv;--argc;
301                 }
302         }
303         if (argc) {
304                 if(strncmp(argv[0], "-sdp", 4)==0) {
305                         sdp=1;
306                         ++argv;--argc;
307                 }
308         }
309         if (argc) {
310                 if(strncmp(argv[0], "-nofork", 7)==0) {
311                         nofork=1;
312                         ++argv;--argc;
313                 }
314         }
315         if(argc) goto errmsg;
316         sock = opennet(hostname, port, sdp);
317         argv=NULL; argc=0; /* don't use it later suddenly */
318
319         negotiate(sock, &size64, &flags);
320         setsizes(nbd, size64, blocksize, flags);
321         set_timeout(nbd, timeout);
322         finish_sock(sock, nbd, swap);
323
324         /* Go daemon */
325         
326         daemon(0,0);
327         do {
328 #ifndef NOFORK
329                 if (!nofork) {
330                         if (fork()) {
331                                 while(check_conn(nbddev, 0)) {
332                                         sleep(1);
333                                 }
334                                 open(nbddev, O_RDONLY);
335                                 exit(0);
336                         }
337                 }
338 #endif
339
340                 if (ioctl(nbd, NBD_DO_IT) < 0) {
341                         fprintf(stderr, "Kernel call returned: %m");
342                         if(errno==EBADR) {
343                                 /* The user probably did 'nbd-client -d' on us.
344                                  * quit */
345                                 cont=0;
346                         } else {
347                                 if(cont) {
348                                         u64 new_size;
349                                         u32 new_flags;
350
351                                         fprintf(stderr, " Reconnecting\n");
352                                         close(sock); close(nbd);
353                                         sock = opennet(hostname, port, sdp);
354                                         nbd = open(nbddev, O_RDWR);
355                                         negotiate(sock, &new_size, &new_flags);
356                                         if (size64 != new_size) {
357                                                 err("Size of the device changed. Bye");
358                                         }
359                                         setsizes(nbd, size64, blocksize,
360                                                                 new_flags);
361
362                                         set_timeout(nbd, timeout);
363                                         finish_sock(sock,nbd,swap);
364                                 }
365                         }
366                 } else {
367                         /* We're on 2.4. It's not clearly defined what exactly
368                          * happened at this point. Probably best to quit, now
369                          */
370                         fprintf(stderr, "Kernel call returned.");
371                         cont=0;
372                 }
373         } while(cont);
374         printf("Closing: que, ");
375         ioctl(nbd, NBD_CLEAR_QUE);
376         printf("sock, ");
377         ioctl(nbd, NBD_CLEAR_SOCK);
378         printf("done\n");
379         return 0;
380 }