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