Remove double "it", and redundant information about defaults
[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  * 16/03/2010 - Add IPv6 support.
15  *      Kitt Tientanopajai <kitt@kitty.in.th>
16  *      Neutron Soutmun <neo.neutron@gmail.com>
17  *      Suriya Soutmun <darksolar@gmail.com>
18  */
19
20 #include "config.h"
21 #include "lfs.h"
22
23 #include <sys/ioctl.h>
24 #include <sys/socket.h>
25 #include <sys/types.h>
26 #include <unistd.h>
27 #include <netinet/tcp.h>
28 #include <netinet/in.h>
29 #include <netdb.h>
30 #include <stdio.h>
31 #include <fcntl.h>
32 #include <syslog.h>
33 #include <stdlib.h>
34 #include <sys/mount.h>
35 #include <sys/mman.h>
36 #include <errno.h>
37 #include <getopt.h>
38 #include <stdarg.h>
39
40 #include <linux/ioctl.h>
41 #define MY_NAME "nbd_client"
42 #include "cliserv.h"
43
44 #ifdef WITH_SDP
45 #include <sdp_inet.h>
46 #endif
47
48 int check_conn(char* devname, int do_print) {
49         char buf[256];
50         char* p;
51         int fd;
52         int len;
53
54         if(!strncmp(devname, "/dev/", 5)) {
55                 devname+=5;
56         }
57         if((p=strchr(devname, 'p'))) {
58                 /* We can't do checks on partitions. */
59                 *p='\0';
60         }
61         snprintf(buf, 256, "/sys/block/%s/pid", devname);
62         if((fd=open(buf, O_RDONLY))<0) {
63                 if(errno==ENOENT) {
64                         return 1;
65                 } else {
66                         return 2;
67                 }
68         }
69         len=read(fd, buf, 256);
70         buf[len-1]='\0';
71         if(do_print) printf("%s\n", buf);
72         return 0;
73 }
74
75 int opennet(char *name, char* portstr, int sdp) {
76         int sock;
77         struct addrinfo hints;
78         struct addrinfo *ai = NULL;
79         struct addrinfo *rp = NULL;
80         int e;
81
82         memset(&hints,'\0',sizeof(hints));
83         hints.ai_family = AF_UNSPEC;
84         hints.ai_socktype = SOCK_STREAM;
85         hints.ai_flags = AI_ADDRCONFIG | AI_NUMERICSERV;
86         hints.ai_protocol = IPPROTO_TCP;
87
88         e = getaddrinfo(name, portstr, &hints, &ai);
89
90         if(e != 0) {
91                 fprintf(stderr, "getaddrinfo failed: %s\n", gai_strerror(e));
92                 freeaddrinfo(ai);
93                 exit(EXIT_FAILURE);
94         }
95
96         if(sdp) {
97 #ifdef WITH_SDP
98                 if (ai->ai_family == AF_INET)
99                         ai->ai_family = AF_INET_SDP;
100                 else (ai->ai_family == AF_INET6)
101                         ai->ai_family = AF_INET6_SDP;
102 #else
103                 err("Can't do SDP: I was not compiled with SDP support!");
104 #endif
105         }
106
107         for(rp = ai; rp != NULL; rp = rp->ai_next) {
108                 sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
109
110                 if(sock == -1)
111                         continue;       /* error */
112
113                 if(connect(sock, rp->ai_addr, rp->ai_addrlen) != -1)
114                         break;          /* success */
115         }
116
117         if (rp == NULL)
118                 err("Socket failed: %m");
119
120         setmysockopt(sock);
121
122         freeaddrinfo(ai);
123         return sock;
124 }
125
126 void negotiate(int sock, u64 *rsize64, u32 *flags, char* name) {
127         u64 magic, size64;
128         uint16_t tmp;
129         char buf[256] = "\0\0\0\0\0\0\0\0\0";
130
131         printf("Negotiation: ");
132         if (read(sock, buf, 8) < 0)
133                 err("Failed/1: %m");
134         if (strlen(buf)==0)
135                 err("Server closed connection");
136         if (strcmp(buf, INIT_PASSWD))
137                 err("INIT_PASSWD bad");
138         printf(".");
139         if (read(sock, &magic, sizeof(magic)) < 0)
140                 err("Failed/2: %m");
141         magic = ntohll(magic);
142         if(name) {
143                 uint32_t opt;
144                 uint32_t namesize;
145                 uint32_t reserved = 0;
146
147                 if (magic != opts_magic)
148                         err("Not enough opts_magic");
149                 printf(".");
150                 if(read(sock, &tmp, sizeof(uint16_t)) < 0) {
151                         err("Failed reading flags: %m");
152                 }
153                 *flags = ((u32)ntohs(tmp));
154
155                 /* reserved for future use*/
156                 if (write(sock, &reserved, sizeof(reserved)) < 0)
157                         err("Failed/2.1: %m");
158
159                 /* Write the export name that we're after */
160                 magic = ntohll(opts_magic);
161                 if (write(sock, &magic, sizeof(magic)) < 0)
162                         err("Failed/2.2: %m");
163                 opt = ntohl(NBD_OPT_EXPORT_NAME);
164                 if (write(sock, &opt, sizeof(opt)) < 0)
165                         err("Failed/2.3: %m");
166                 namesize = (u32)strlen(name);
167                 namesize = ntohl(namesize);
168                 if (write(sock, &namesize, sizeof(namesize)) < 0)
169                         err("Failed/2.4: %m");
170                 if (write(sock, name, strlen(name)) < 0)
171                         err("Failed/2.4: %m");
172         } else {
173                 if (magic != cliserv_magic)
174                         err("Not enough cliserv_magic");
175                 printf(".");
176         }
177
178         if (read(sock, &size64, sizeof(size64)) < 0)
179                 err("Failed/3: %m\n");
180         size64 = ntohll(size64);
181
182 #ifdef NBD_SET_SIZE_BLOCKS
183         if ((size64>>12) > (uint64_t)~0UL) {
184                 printf("size = %luMB", (unsigned long)(size64>>20));
185                 err("Exported device is too big for me. Get 64-bit machine :-(\n");
186         } else
187                 printf("size = %luMB", (unsigned long)(size64>>20));
188 #else
189         if (size64 > (~0UL >> 1)) {
190                 printf("size = %luKB", (unsigned long)(size64>>10));
191                 err("Exported device is too big. Get 64-bit machine or newer kernel :-(\n");
192         } else
193                 printf("size = %lu", (unsigned long)(size64));
194 #endif
195
196         if(!name) {
197                 if (read(sock, flags, sizeof(*flags)) < 0)
198                         err("Failed/4: %m\n");
199                 *flags = ntohl(*flags);
200         } else {
201                 if(read(sock, &tmp, sizeof(tmp)) < 0)
202                         err("Failed/4: %m\n");
203                 *flags |= (uint32_t)ntohs(tmp);
204         }
205
206         if (read(sock, &buf, 124) < 0)
207                 err("Failed/5: %m\n");
208         printf("\n");
209
210         *rsize64 = size64;
211 }
212
213 void setsizes(int nbd, u64 size64, int blocksize, u32 flags) {
214         unsigned long size;
215         int read_only = (flags & NBD_FLAG_READ_ONLY) ? 1 : 0;
216
217 #ifdef NBD_SET_SIZE_BLOCKS
218         if (size64>>12 > (uint64_t)~0UL)
219                 err("Device too large.\n");
220         else {
221                 int er;
222                 if (ioctl(nbd, NBD_SET_BLKSIZE, 4096UL) < 0)
223                         err("Ioctl/1.1a failed: %m\n");
224                 size = (unsigned long)(size64>>12);
225                 if ((er = ioctl(nbd, NBD_SET_SIZE_BLOCKS, size)) < 0)
226                         err("Ioctl/1.1b failed: %m\n");
227                 if (ioctl(nbd, NBD_SET_BLKSIZE, (unsigned long)blocksize) < 0)
228                         err("Ioctl/1.1c failed: %m\n");
229                 fprintf(stderr, "bs=%d, sz=%llu bytes\n", blocksize, 4096ULL*size);
230         }
231 #else
232         if (size64 > (~0UL >> 1)) {
233                 err("Device too large.\n");
234         } else {
235                 size = (unsigned long)size64;
236                 if (ioctl(nbd, NBD_SET_SIZE, size) < 0)
237                         err("Ioctl NBD_SET_SIZE failed: %m\n");
238         }
239 #endif
240
241         ioctl(nbd, NBD_CLEAR_SOCK);
242
243         /* ignore error as kernel may not support */
244         ioctl(nbd, NBD_SET_FLAGS, (unsigned long) flags);
245
246         if (ioctl(nbd, BLKROSET, (unsigned long) &read_only) < 0)
247                 err("Unable to set read-only attribute for device");
248 }
249
250 void set_timeout(int nbd, int timeout) {
251         if (timeout) {
252 #ifdef NBD_SET_TIMEOUT
253                 if (ioctl(nbd, NBD_SET_TIMEOUT, (unsigned long)timeout) < 0)
254                         err("Ioctl NBD_SET_TIMEOUT failed: %m\n");
255                 fprintf(stderr, "timeout=%d\n", timeout);
256 #else
257                 err("Ioctl NBD_SET_TIMEOUT cannot be called when compiled on a system that does not support it\n");
258 #endif
259         }
260 }
261
262 void finish_sock(int sock, int nbd, int swap) {
263         if (ioctl(nbd, NBD_SET_SOCK, sock) < 0)
264                 err("Ioctl NBD_SET_SOCK failed: %m\n");
265
266         if (swap)
267                 mlockall(MCL_CURRENT | MCL_FUTURE);
268 }
269
270 void usage(char* errmsg, ...) {
271         if(errmsg) {
272                 char tmp[256];
273                 va_list ap;
274                 va_start(ap, errmsg);
275                 snprintf(tmp, 256, "ERROR: %s\n\n", errmsg);
276                 vfprintf(stderr, errmsg, ap);
277                 va_end(ap);
278         } else {
279                 fprintf(stderr, "nbd-client version %s\n", PACKAGE_VERSION);
280         }
281         fprintf(stderr, "Usage: nbd-client host port nbd_device [-block-size|-b block size] [-timeout|-t timeout] [-swap|-s] [-sdp|-S] [-persist|-p] [-nofork|-n] [-name|-N name]\n");
282         fprintf(stderr, "Or   : nbd-client -d nbd_device\n");
283         fprintf(stderr, "Or   : nbd-client -c nbd_device\n");
284         fprintf(stderr, "Or   : nbd-client -h|--help\n");
285         fprintf(stderr, "Default value for blocksize is 1024 (recommended for ethernet)\n");
286         fprintf(stderr, "Allowed values for blocksize are 512,1024,2048,4096\n"); /* will be checked in kernel :) */
287         fprintf(stderr, "Note, that kernel 2.4.2 and older ones do not work correctly with\n");
288         fprintf(stderr, "blocksizes other than 1024 without patches\n");
289 }
290
291 void disconnect(char* device) {
292         int nbd = open(device, O_RDWR);
293
294         if (nbd < 0)
295                 err("Cannot open NBD: %m\nPlease ensure the 'nbd' module is loaded.");
296         printf("Disconnecting: que, ");
297         if (ioctl(nbd, NBD_CLEAR_QUE)< 0)
298                 err("Ioctl failed: %m\n");
299         printf("disconnect, ");
300 #ifdef NBD_DISCONNECT
301         if (ioctl(nbd, NBD_DISCONNECT)<0)
302                 err("Ioctl failed: %m\n");
303         printf("sock, ");
304 #else
305         fprintf(stderr, "Can't disconnect: I was not compiled with disconnect support!\n" );
306         exit(1);
307 #endif
308         if (ioctl(nbd, NBD_CLEAR_SOCK)<0)
309                 err("Ioctl failed: %m\n");
310         printf("done\n");
311 }
312
313 int main(int argc, char *argv[]) {
314         char* port=NULL;
315         int sock, nbd;
316         int blocksize=1024;
317         char *hostname=NULL;
318         char *nbddev=NULL;
319         int swap=0;
320         int cont=0;
321         int timeout=0;
322         int sdp=0;
323         int G_GNUC_UNUSED nofork=0; // if -dNOFORK
324         u64 size64;
325         u32 flags;
326         int c;
327         int nonspecial=0;
328         char* name=NULL;
329         struct option long_options[] = {
330                 { "block-size", required_argument, NULL, 'b' },
331                 { "check", required_argument, NULL, 'c' },
332                 { "disconnect", required_argument, NULL, 'd' },
333                 { "help", no_argument, NULL, 'h' },
334                 { "name", required_argument, NULL, 'N' },
335                 { "nofork", no_argument, NULL, 'n' },
336                 { "persist", no_argument, NULL, 'p' },
337                 { "sdp", no_argument, NULL, 'S' },
338                 { "swap", no_argument, NULL, 's' },
339                 { "timeout", required_argument, NULL, 't' },
340                 { 0, 0, 0, 0 }, 
341         };
342
343         logging();
344
345         while((c=getopt_long_only(argc, argv, "-b:c:d:hnN:pSst:", long_options, NULL))>=0) {
346                 switch(c) {
347                 case 1:
348                         // non-option argument
349                         if(strchr(optarg, '=')) {
350                                 // old-style 'bs=' or 'timeout='
351                                 // argument
352                                 fprintf(stderr, "WARNING: old-style command-line argument encountered. This is deprecated.\n");
353                                 if(!strncmp(optarg, "bs=", 3)) {
354                                         optarg+=3;
355                                         goto blocksize;
356                                 }
357                                 if(!strncmp(optarg, "timeout=", 8)) {
358                                         optarg+=8;
359                                         goto timeout;
360                                 }
361                                 usage("unknown option %s encountered", optarg);
362                                 exit(EXIT_FAILURE);
363                         }
364                         switch(nonspecial++) {
365                                 case 0:
366                                         // host
367                                         hostname=optarg;
368                                         break;
369                                 case 1:
370                                         // port
371                                         if(!strtol(optarg, NULL, 0)) {
372                                                 // not parseable as a number, assume it's the device and we have a name
373                                                 nbddev = optarg;
374                                                 nonspecial++;
375                                         } else {
376                                                 port = optarg;
377                                                 if(name) {
378                                                         usage("port and name specified at the same time. This is not supported.");
379                                                         exit(EXIT_FAILURE);
380                                                 }
381                                         }
382                                         break;
383                                 case 2:
384                                         // device
385                                         nbddev = optarg;
386                                         break;
387                                 default:
388                                         usage("too many non-option arguments specified");
389                                         exit(EXIT_FAILURE);
390                         }
391                         break;
392                 case 'b':
393                       blocksize:
394                         blocksize=(int)strtol(optarg, NULL, 0);
395                         break;
396                 case 'c':
397                         return check_conn(optarg, 1);
398                 case 'd':
399                         disconnect(optarg);
400                         exit(EXIT_SUCCESS);
401                 case 'h':
402                         usage(NULL);
403                         exit(EXIT_SUCCESS);
404                 case 'n':
405                         nofork=1;
406                         break;
407                 case 'N':
408                         name=optarg;
409                         if(port) {
410                                 usage("port and name specified at the same time. This is not supported.");
411                                 exit(EXIT_FAILURE);
412                         }
413                         port = NBD_DEFAULT_PORT;
414                         break;
415                 case 'p':
416                         cont=1;
417                         break;
418                 case 's':
419                         swap=1;
420                         break;
421                 case 'S':
422                         sdp=1;
423                         break;
424                 case 't':
425                       timeout:
426                         timeout=strtol(optarg, NULL, 0);
427                         break;
428                 default:
429                         fprintf(stderr, "E: option eaten by 42 mice\n");
430                         exit(EXIT_FAILURE);
431                 }
432         }
433
434         if((!port && !name) || !hostname || !nbddev) {
435                 usage("not enough information specified");
436                 exit(EXIT_FAILURE);
437         }
438
439         nbd = open(nbddev, O_RDWR);
440         if (nbd < 0)
441           err("Cannot open NBD: %m\nPlease ensure the 'nbd' module is loaded.");
442         ++argv; --argc; /* skip device */
443
444         sock = opennet(hostname, port, sdp);
445
446         negotiate(sock, &size64, &flags, name);
447         setsizes(nbd, size64, blocksize, flags);
448         set_timeout(nbd, timeout);
449         finish_sock(sock, nbd, swap);
450
451         /* Go daemon */
452         
453 #ifndef NOFORK
454         if(!nofork) {
455                 if (daemon(0,0) < 0)
456                         err("Cannot detach from terminal");
457         }
458 #endif
459         do {
460 #ifndef NOFORK
461                 if (fork()) {
462                         /* Due to a race, the kernel NBD driver cannot
463                          * call for a reread of the partition table
464                          * in the handling of the NBD_DO_IT ioctl().
465                          * Therefore, this is done in the first open()
466                          * of the device. We therefore make sure that
467                          * the device is opened at least once after the
468                          * connection was made. This has to be done in a
469                          * separate process, since the NBD_DO_IT ioctl()
470                          * does not return until the NBD device has
471                          * disconnected.
472                          */
473                         while(check_conn(nbddev, 0)) {
474                                 sleep(1);
475                         }
476                         open(nbddev, O_RDONLY);
477                         exit(0);
478                 }
479 #endif
480
481                 if (ioctl(nbd, NBD_DO_IT) < 0) {
482                         fprintf(stderr, "Kernel call returned: %m");
483                         if(errno==EBADR) {
484                                 /* The user probably did 'nbd-client -d' on us.
485                                  * quit */
486                                 cont=0;
487                         } else {
488                                 if(cont) {
489                                         u64 new_size;
490                                         u32 new_flags;
491
492                                         fprintf(stderr, " Reconnecting\n");
493                                         close(sock); close(nbd);
494                                         sock = opennet(hostname, port, sdp);
495                                         nbd = open(nbddev, O_RDWR);
496                                         negotiate(sock, &new_size, &new_flags, name);
497                                         if (size64 != new_size) {
498                                                 err("Size of the device changed. Bye");
499                                         }
500                                         setsizes(nbd, size64, blocksize,
501                                                                 new_flags);
502
503                                         set_timeout(nbd, timeout);
504                                         finish_sock(sock,nbd,swap);
505                                 }
506                         }
507                 } else {
508                         /* We're on 2.4. It's not clearly defined what exactly
509                          * happened at this point. Probably best to quit, now
510                          */
511                         fprintf(stderr, "Kernel call returned.");
512                         cont=0;
513                 }
514         } while(cont);
515         printf("Closing: que, ");
516         ioctl(nbd, NBD_CLEAR_QUE);
517         printf("sock, ");
518         ioctl(nbd, NBD_CLEAR_SOCK);
519         printf("done\n");
520         return 0;
521 }