r154: Fix segfault
[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 <asm/page.h>
20 #include <sys/ioctl.h>
21 #include <sys/socket.h>
22 #include <sys/types.h>
23 #include <unistd.h>
24 #include <netinet/tcp.h>
25 #include <netinet/in.h>         /* sockaddr_in, htons, in_addr */
26 #include <netdb.h>              /* hostent, gethostby*, getservby* */
27 #include <stdio.h>
28 #include <fcntl.h>
29 #include <syslog.h>
30 #include <stdlib.h>
31
32 #ifndef __GNUC__
33 #error I need GCC to work
34 #endif
35
36 #include <linux/ioctl.h>
37 #define MY_NAME "nbd_client"
38 #include "cliserv.h"
39
40 int opennet(char *name, int port) {
41         int sock;
42         struct sockaddr_in xaddrin;
43         int xaddrinlen = sizeof(xaddrin);
44         struct hostent *hostn;
45
46         hostn = gethostbyname(name);
47         if (!hostn)
48                 err("Gethostname failed: %h\n");
49
50         if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
51                 err("Socket failed: %m");
52
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))
57                 err("Connect: %m");
58
59         setmysockopt(sock);
60         return sock;
61 }
62
63 u64 negotiate(int sock, int blocksize) {
64         u64 magic, size64;
65         char buf[256] = "\0\0\0\0\0\0\0\0\0";
66
67         printf("Negotiation: ");
68         if (read(sock, buf, 8) < 0)
69                 err("Failed/1: %m");
70         if (strlen(buf)==0)
71                 err("Server closed connection");
72         if (strcmp(buf, INIT_PASSWD))
73                 err("INIT_PASSWD bad");
74         printf(".");
75         if (read(sock, &magic, sizeof(magic)) < 0)
76                 err("Failed/2: %m");
77         magic = ntohll(magic);
78         if (magic != cliserv_magic)
79                 err("Not enough cliserv_magic");
80         printf(".");
81
82         if (read(sock, &size64, sizeof(size64)) < 0)
83                 err("Failed/3: %m\n");
84         size64 = ntohll(size64);
85
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");
90         } else
91                 printf("size = %luKB", (unsigned long)(size64>>10));
92 #else
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");
96         } else
97                 printf("size = %lu", (unsigned long)(size64));
98 #endif
99
100         if (read(sock, &buf, 128) < 0)
101                 err("Failed/4: %m\n");
102         printf("\n");
103
104         return size64;
105 }
106
107 void setsizes(int nbd, u64 size64, int blocksize) {
108         unsigned long size;
109
110 #ifdef NBD_SET_SIZE_BLOCKS
111         if (size64/blocksize > (~0UL >> 1))
112                 err("Device too large.\n");
113         else {
114                 int er;
115                 if (ioctl(nbd, NBD_SET_BLKSIZE, (unsigned long)blocksize) < 0)
116                         err("Ioctl/1.1a failed: %m\n");
117                 size = (unsigned long)(size64/blocksize);
118                 if ((er = ioctl(nbd, NBD_SET_SIZE_BLOCKS, size)) < 0)
119                         err("Ioctl/1.1b failed: %m\n");
120                 fprintf(stderr, "bs=%d, sz=%lu\n", blocksize, size);
121         }
122 #else
123         if (size64 > (~0UL >> 1)) {
124                 err("Device too large.\n");
125         } else {
126                 size = (unsigned long)size64;
127                 if (ioctl(nbd, NBD_SET_SIZE, size) < 0)
128                         err("Ioctl NBD_SET_SIZE failed: %m\n");
129         }
130 #endif
131
132         ioctl(nbd, NBD_CLEAR_SOCK);
133 }
134
135 void finish_sock(int sock, int nbd, int swap) {
136         if (ioctl(nbd, NBD_SET_SOCK, sock) < 0)
137                 err("Ioctl NBD_SET_SOCK failed: %m\n");
138
139 #ifndef SO_SWAPPING
140         if (swap)
141                 err("You have to compile me on machine with swapping patch enabled in order to use it later.");
142 #else
143         if (swap)
144                 if (setsockopt(sock, SOL_SOCKET, SO_SWAPPING, &one, sizeof(int)) < 0)
145                         err("Could not enable swapping: %m");
146 #endif
147 }
148
149 int main(int argc, char *argv[]) {
150         int port, sock, nbd;
151         int blocksize=1024;
152         char *hostname, *nbddev;
153         int swap=0;
154         int cont=0;
155         u64 size64;
156
157         logging();
158
159         if (argc < 3) {
160         errmsg:
161                 fprintf(stderr, "nbd-client version %s\n", PACKAGE_VERSION);
162                 fprintf(stderr, "Usage: nbd-client [bs=blocksize] host port nbd_device [-swap] [-persist]\n");
163                 fprintf(stderr, "Or   : nbd-client -d nbd_device\n");
164                 fprintf(stderr, "Default value for blocksize is 1024 (recommended for ethernet)\n");
165                 fprintf(stderr, "Allowed values for blocksize are 512,1024,2048,4096\n"); /* will be checked in kernel :) */
166                 fprintf(stderr, "Note, that kernel 2.4.2 and older ones do not work correctly with\n");
167                 fprintf(stderr, "blocksizes other than 1024 without patches\n");
168                 return 1;
169         }
170
171         ++argv; --argc; /* skip programname */
172         
173         if (strcmp(argv[0], "-d")==0) {
174                 nbd = open(argv[1], O_RDWR);
175                 if (nbd < 0)
176                         err("Can not open NBD: %m");
177                 printf("Disconnecting: que, ");
178                 if (ioctl(nbd, NBD_CLEAR_QUE)< 0)
179                         err("Ioctl failed: %m\n");
180                 printf("disconnect, ");
181 #ifdef NBD_DISCONNECT
182                 if (ioctl(nbd, NBD_DISCONNECT)<0)
183                         err("Ioctl failed: %m\n");
184                 printf("sock, ");
185 #else
186                 fprintf(stderr, "Can't disconnect: I was not compiled with disconnect support!\n" );
187                 exit(1);
188 #endif
189                 if (ioctl(nbd, NBD_CLEAR_SOCK)<0)
190                         err("Ioctl failed: %m\n");
191                 printf("done\n");
192                 return 0;
193         }
194         
195         if (strncmp(argv[0], "bs=", 3)==0) {
196                 blocksize=atoi(argv[0]+3);
197                 ++argv; --argc; /* skip blocksize */
198         }
199
200         if (argc==0) goto errmsg;
201         hostname=argv[0];
202         ++argv; --argc; /* skip hostname */
203
204         if (argc==0) goto errmsg;
205         port = atoi(argv[0]);
206         ++argv; --argc; /* skip port */
207
208         if (argc==0) goto errmsg;
209         sock = opennet(hostname, port);
210         nbddev = argv[0];
211         nbd = open(nbddev, O_RDWR);
212         if (nbd < 0)
213           err("Can not open NBD: %m");
214         ++argv; --argc; /* skip device */
215
216         if (argc>2) goto errmsg;
217         if (argc!=0) {
218                 if(strncmp(argv[0], "-swap", 5)==0) {
219                         swap=1;
220                         ++argv;--argc;
221                 }
222         }
223         if (argc!=0) {
224                 if(strncmp(argv[0], "-persist", 8)==0) {
225                         cont=1;
226                         ++argv;--argc;
227                 }
228         }
229         argv=NULL; argc=0; /* don't use it later suddenly */
230
231         size64 = negotiate(sock, blocksize);
232         setsizes(nbd, size64, blocksize);
233         finish_sock(sock, nbd, swap);
234
235         /* Go daemon */
236         
237         chdir("/");
238 #ifndef NOFORK
239         if (fork())
240                 exit(0);
241 #endif
242
243         do {
244                 if (ioctl(nbd, NBD_DO_IT) < 0) {
245                         fprintf(stderr, "Kernel call returned: %m");
246                         if(errno==EBADR) {
247                                 /* The user probably did 'nbd-client -d' on us.
248                                  * quit */
249                                 cont=0;
250                         } else {
251                                 if(cont) {
252                                         fprintf(stderr, " Reconnecting\n");
253                                         close(sock); close(nbd);
254                                         sock = opennet(hostname, port);
255                                         nbd = open(nbddev, O_RDWR);
256                                         if(size64!=negotiate(sock,blocksize)) {
257                                                 err("Size of the device changed. Bye");
258                                         }
259                                         setsizes(nbd, size64, blocksize);
260                                         finish_sock(sock,nbd,swap);
261                                 }
262                         }
263                 } else {
264                         /* We're on 2.4. It's not clearly defined what exactly
265                          * happened at this point. Probably best to quit, now
266                          */
267                         fprintf(stderr, "Kernel call returned.");
268                         cont=0;
269                 }
270         } while(cont);
271         printf("Closing: que, ");
272         ioctl(nbd, NBD_CLEAR_QUE);
273         printf("sock, ");
274         ioctl(nbd, NBD_CLEAR_SOCK);
275         printf("done\n");
276         return 0;
277 }