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