r291: make the build work, again
[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
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, int sdp) {
41         int sock;
42         struct sockaddr_in xaddrin;
43         int xaddrinlen = sizeof(xaddrin);
44         struct hostent *hostn;
45         int af;
46
47         hostn = gethostbyname(name);
48         if (!hostn)
49                 err("Gethostname failed: %h\n");
50
51         af = AF_INET;
52         if(sdp) {
53 #ifdef WITH_SDP
54                 af = AF_INET_SDP;
55 #else
56                 err("Can't do SDP: I was not compiled with SDP support!");
57 #endif
58         }
59         if ((sock = socket(af, SOCK_STREAM, IPPROTO_TCP)) < 0)
60                 err("Socket failed: %m");
61
62         xaddrin.sin_family = af;
63         xaddrin.sin_port = htons(port);
64         xaddrin.sin_addr.s_addr = *((int *) hostn->h_addr);
65         if ((connect(sock, (struct sockaddr *) &xaddrin, xaddrinlen) < 0))
66                 err("Connect: %m");
67
68         setmysockopt(sock);
69         return sock;
70 }
71
72 void negotiate(int sock, u64 *rsize64, u32 *flags) {
73         u64 magic, size64;
74         char buf[256] = "\0\0\0\0\0\0\0\0\0";
75
76         printf("Negotiation: ");
77         if (read(sock, buf, 8) < 0)
78                 err("Failed/1: %m");
79         if (strlen(buf)==0)
80                 err("Server closed connection");
81         if (strcmp(buf, INIT_PASSWD))
82                 err("INIT_PASSWD bad");
83         printf(".");
84         if (read(sock, &magic, sizeof(magic)) < 0)
85                 err("Failed/2: %m");
86         magic = ntohll(magic);
87         if (magic != cliserv_magic)
88                 err("Not enough cliserv_magic");
89         printf(".");
90
91         if (read(sock, &size64, sizeof(size64)) < 0)
92                 err("Failed/3: %m\n");
93         size64 = ntohll(size64);
94
95 #ifdef NBD_SET_SIZE_BLOCKS
96         if ((size64>>10) > (~0UL >> 1)) {
97                 printf("size = %luMB", (unsigned long)(size64>>20));
98                 err("Exported device is too big for me. Get 64-bit machine :-(\n");
99         } else
100                 printf("size = %luKB", (unsigned long)(size64>>10));
101 #else
102         if (size64 > (~0UL >> 1)) {
103                 printf("size = %luKB", (unsigned long)(size64>>10));
104                 err("Exported device is too big. Get 64-bit machine or newer kernel :-(\n");
105         } else
106                 printf("size = %lu", (unsigned long)(size64));
107 #endif
108
109         if (read(sock, flags, sizeof(*flags)) < 0)
110                 err("Failed/4: %m\n");
111         *flags = ntohl(*flags);
112
113         if (read(sock, &buf, 124) < 0)
114                 err("Failed/5: %m\n");
115         printf("\n");
116
117         *rsize64 = size64;
118 }
119
120 void setsizes(int nbd, u64 size64, int blocksize, u32 flags) {
121         unsigned long size;
122         int read_only = (flags & NBD_FLAG_READ_ONLY) ? 1 : 0;
123
124 #ifdef NBD_SET_SIZE_BLOCKS
125         if (size64/blocksize > (~0UL >> 1))
126                 err("Device too large.\n");
127         else {
128                 int er;
129                 if (ioctl(nbd, NBD_SET_BLKSIZE, (unsigned long)blocksize) < 0)
130                         err("Ioctl/1.1a failed: %m\n");
131                 size = (unsigned long)(size64/blocksize);
132                 if ((er = ioctl(nbd, NBD_SET_SIZE_BLOCKS, size)) < 0)
133                         err("Ioctl/1.1b failed: %m\n");
134                 fprintf(stderr, "bs=%d, sz=%lu\n", blocksize, size);
135         }
136 #else
137         if (size64 > (~0UL >> 1)) {
138                 err("Device too large.\n");
139         } else {
140                 size = (unsigned long)size64;
141                 if (ioctl(nbd, NBD_SET_SIZE, size) < 0)
142                         err("Ioctl NBD_SET_SIZE failed: %m\n");
143         }
144 #endif
145
146         ioctl(nbd, NBD_CLEAR_SOCK);
147
148         if (ioctl(nbd, BLKROSET, (unsigned long) &read_only) < 0)
149                 err("Unable to set read-only attribute for device");
150 }
151
152 void set_timeout(int nbd, int timeout) {
153 #ifdef NBD_SET_TIMEOUT
154         if (timeout) {
155                 if (ioctl(nbd, NBD_SET_TIMEOUT, (unsigned long)timeout) < 0)
156                         err("Ioctl NBD_SET_TIMEOUT failed: %m\n");
157                 fprintf(stderr, "timeout=%d\n", timeout);
158         }
159 #endif
160 }
161
162 void finish_sock(int sock, int nbd, int swap) {
163         if (ioctl(nbd, NBD_SET_SOCK, sock) < 0)
164                 err("Ioctl NBD_SET_SOCK failed: %m\n");
165
166 #ifndef SO_SWAPPING
167         if (swap)
168                 err("You have to compile me on machine with swapping patch enabled in order to use it later.");
169 #else
170         if (swap)
171                 if (setsockopt(sock, SOL_SOCKET, SO_SWAPPING, &one, sizeof(int)) < 0)
172                         err("Could not enable swapping: %m");
173 #endif
174 }
175
176 int main(int argc, char *argv[]) {
177         int port, sock, nbd;
178         int blocksize=1024;
179         char *hostname, *nbddev;
180         int swap=0;
181         int cont=0;
182         int timeout=0;
183         int sdp=0;
184         u64 size64;
185         u32 flags;
186
187         logging();
188
189         if (argc < 3) {
190         errmsg:
191                 fprintf(stderr, "nbd-client version %s\n", PACKAGE_VERSION);
192                 fprintf(stderr, "Usage: nbd-client [bs=blocksize] [timeout=sec] host port nbd_device [-swap] [-persist]\n");
193                 fprintf(stderr, "Or   : nbd-client -d nbd_device\n");
194                 fprintf(stderr, "Default value for blocksize is 1024 (recommended for ethernet)\n");
195                 fprintf(stderr, "Allowed values for blocksize are 512,1024,2048,4096\n"); /* will be checked in kernel :) */
196                 fprintf(stderr, "Note, that kernel 2.4.2 and older ones do not work correctly with\n");
197                 fprintf(stderr, "blocksizes other than 1024 without patches\n");
198                 return 1;
199         }
200
201         ++argv; --argc; /* skip programname */
202         
203         if (strcmp(argv[0], "-d")==0) {
204                 nbd = open(argv[1], O_RDWR);
205                 if (nbd < 0)
206                         err("Can not open NBD: %m");
207                 printf("Disconnecting: que, ");
208                 if (ioctl(nbd, NBD_CLEAR_QUE)< 0)
209                         err("Ioctl failed: %m\n");
210                 printf("disconnect, ");
211 #ifdef NBD_DISCONNECT
212                 if (ioctl(nbd, NBD_DISCONNECT)<0)
213                         err("Ioctl failed: %m\n");
214                 printf("sock, ");
215 #else
216                 fprintf(stderr, "Can't disconnect: I was not compiled with disconnect support!\n" );
217                 exit(1);
218 #endif
219                 if (ioctl(nbd, NBD_CLEAR_SOCK)<0)
220                         err("Ioctl failed: %m\n");
221                 printf("done\n");
222                 return 0;
223         }
224         
225         if (strncmp(argv[0], "bs=", 3)==0) {
226                 blocksize=atoi(argv[0]+3);
227                 ++argv; --argc; /* skip blocksize */
228         }
229
230         if (strncmp(argv[0], "timeout=", 8)==0) {
231                 timeout=atoi(argv[0]+8);
232                 ++argv; --argc; /* skip timeout */
233         }
234         
235         if (argc==0) goto errmsg;
236         hostname=argv[0];
237         ++argv; --argc; /* skip hostname */
238
239         if (argc==0) goto errmsg;
240         port = atoi(argv[0]);
241         ++argv; --argc; /* skip port */
242
243         if (argc==0) goto errmsg;
244         nbddev = argv[0];
245         nbd = open(nbddev, O_RDWR);
246         if (nbd < 0)
247           err("Can not open NBD: %m");
248         ++argv; --argc; /* skip device */
249
250         if (argc>3) goto errmsg;
251         if (argc) {
252                 if(strncmp(argv[0], "-swap", 5)==0) {
253                         swap=1;
254                         ++argv;--argc;
255                 }
256         }
257         if (argc) {
258                 if(strncmp(argv[0], "-persist", 8)==0) {
259                         cont=1;
260                         ++argv;--argc;
261                 }
262         }
263         if (argc) {
264                 if(strncmp(argv[0], "-sdp", 4)==0) {
265                         sdp=1;
266                         ++argv;--argc;
267                 }
268         }
269         if(argc) goto errmsg;
270         sock = opennet(hostname, port, sdp);
271         argv=NULL; argc=0; /* don't use it later suddenly */
272
273         negotiate(sock, &size64, &flags);
274         setsizes(nbd, size64, blocksize, flags);
275         set_timeout(nbd, timeout);
276         finish_sock(sock, nbd, swap);
277
278         /* Go daemon */
279         
280         chdir("/");
281 #ifndef NOFORK
282         if (fork())
283                 exit(0);
284 #endif
285
286         do {
287                 if (ioctl(nbd, NBD_DO_IT) < 0) {
288                         fprintf(stderr, "Kernel call returned: %m");
289                         if(errno==EBADR) {
290                                 /* The user probably did 'nbd-client -d' on us.
291                                  * quit */
292                                 cont=0;
293                         } else {
294                                 if(cont) {
295                                         u64 new_size;
296                                         u32 new_flags;
297
298                                         fprintf(stderr, " Reconnecting\n");
299                                         close(sock); close(nbd);
300                                         sock = opennet(hostname, port, sdp);
301                                         nbd = open(nbddev, O_RDWR);
302                                         negotiate(sock, &new_size, &new_flags);
303                                         if (size64 != new_size) {
304                                                 err("Size of the device changed. Bye");
305                                         }
306                                         setsizes(nbd, size64, blocksize,
307                                                                 new_flags);
308
309                                         set_timeout(nbd, timeout);
310                                         finish_sock(sock,nbd,swap);
311                                 }
312                         }
313                 } else {
314                         /* We're on 2.4. It's not clearly defined what exactly
315                          * happened at this point. Probably best to quit, now
316                          */
317                         fprintf(stderr, "Kernel call returned.");
318                         cont=0;
319                 }
320         } while(cont);
321         printf("Closing: que, ");
322         ioctl(nbd, NBD_CLEAR_QUE);
323         printf("sock, ");
324         ioctl(nbd, NBD_CLEAR_SOCK);
325         printf("done\n");
326         return 0;
327 }